<template>
  <div class="app-container">
    <el-form :model="queryParams" class="searchIndex" ref="queryForm" :inline="true" v-show="showSearch" label-width="88px">
      <div style="display: inline-block;">
        <el-form-item :class="['treeselectForm treeselectForm2',{borderRed:!validTree}]" label="组织架构" prop="orgIdsname">
          <tree @treeselectFn="treeselectFn" :treeval="queryParams.deptIds" :ismultiple="true" style="width:193px;"></tree>
        </el-form-item>
      </div>
      <el-form-item label="业务员" prop="userId" size="mini">
        <user :userId="queryParams.userId" :orgIds="queryParams.deptIds"  @userIdFn="userIdFn" style="width: 200px;"></user>
      </el-form-item>
      <el-form-item label="门店类型" prop="storeCategoryList" size="mini">
        <el-select v-model="queryParams.storeCategoryList"  multiple collapse-tags  clearable placeholder="请选择门店类型" >
          <el-option
            v-for="item in storeCategoryOptions"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictLabel">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="门店编码" prop="storeCode">
        <el-input
          v-model="queryParams.storeCode"
          placeholder="请输入门店编码"
          clearable
          size="mini"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="门店名称" prop="storeName">
        <el-input
          v-model="queryParams.storeName"
          placeholder="请输入门店名称"
          clearable
          size="mini"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始日期" prop="startTime">
        <el-date-picker
          v-model="queryParams.startTime"
          type="date"
          size="mini"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"
          :picker-options="pickerOptionsStart"
          style="width: 193px;">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束日期" prop="endTime">
        <el-date-picker
          v-model="queryParams.endTime"
          type="date"
          size="mini"
          value-format="yyyy-MM-dd"
          placeholder="选择日期"

          style="width: 193px;">
<!--          :picker-options="pickerOptionsEnd"-->
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sfa:routeDetails:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="buryingPointList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
<!--      <el-table-column label="埋点类型" align="center" prop="buryingPointType"  width="120">-->
<!--        <template slot-scope="scope">-->
<!--          <span>{{cityLevelsFormat(scope.row,scope.column)}}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="公司" align="center"   prop="gsDeptName" :show-overflow-tooltip="true" />
      <el-table-column label="大区" align="center"  width="160" prop="dqDeptName" :show-overflow-tooltip="true"/>
      <el-table-column label="销售部" align="center" prop="deptName" width="160" :show-overflow-tooltip="true"/>
      <el-table-column label="工号" prop="workCode" width="140" :show-overflow-tooltip="true"/>
      <el-table-column label="姓名" prop="nickName" width="140" :show-overflow-tooltip="true" />
      <el-table-column label="门店编码"  prop="storeCode" width="140" :show-overflow-tooltip="true"/>
      <el-table-column label="门店名称"  prop="storeName" width="140":show-overflow-tooltip="true"/>
      <el-table-column label="门店类型" align="center" prop="storeCategory" width="140px" />
      <el-table-column label="计划时间" align="center" prop="genDate" :show-overflow-tooltip="true"  width="160"/>
      <el-table-column label="拜访线路号" align="center" prop="routeNumber" width="160" />
      <!--      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">-->
      <!--        <template slot-scope="scope">-->
      <!--          <el-button-->
      <!--            size="mini"-->
      <!--            type="text"-->
      <!--            icon="el-icon-edit"-->
      <!--            @click="handleUpdate(scope.row)"-->
      <!--            v-hasPermi="['buryingPoint:buryingPoint:edit']"-->
      <!--          >详情</el-button>-->
      <!--&lt;!&ndash;          <el-button&ndash;&gt;-->
      <!--&lt;!&ndash;            size="mini"&ndash;&gt;-->
      <!--&lt;!&ndash;            type="text"&ndash;&gt;-->
      <!--&lt;!&ndash;            icon="el-icon-delete"&ndash;&gt;-->
      <!--&lt;!&ndash;            @click="handleDelete(scope.row)"&ndash;&gt;-->
      <!--&lt;!&ndash;            v-hasPermi="['buryingPoint:buryingPoint:remove']"&ndash;&gt;-->
      <!--&lt;!&ndash;          >删除</el-button>&ndash;&gt;-->
      <!--        </template>-->
      <!--      </el-table-column>-->
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改埋点操作记录对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="埋点值" prop="buryingPointValue">
          <el-input v-model="form.buryingPointValue" placeholder="请输入埋点值" />
        </el-form-item>
        <el-form-item label="埋点名称" prop="buryingPointName">
          <el-input v-model="form.buryingPointName" placeholder="请输入埋点名称" />
        </el-form-item>
        <el-form-item label="埋点位置" prop="buryingPointPosition">
          <el-input v-model="form.buryingPointPosition" placeholder="请输入埋点位置" />
        </el-form-item>
        <el-form-item label="系统模块" prop="systemModel">
          <el-input v-model="form.systemModel" placeholder="请输入系统模块" />
        </el-form-item>
        <el-form-item label="操作人工号" prop="workCode">
          <el-input v-model="form.workCode" placeholder="请输入操作人工号" />
        </el-form-item>
        <el-form-item label="操作人名称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入操作人名称" />
        </el-form-item>
        <el-form-item label="操作人部门名称" prop="deptName">
          <el-input v-model="form.deptName" placeholder="请输入操作人部门名称" />
        </el-form-item>
        <el-form-item label="操作时间" prop="operatingTime">
          <el-date-picker clearable size="small"
                          v-model="form.operatingTime"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="选择操作时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="删除标记" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标记" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBuryingPoint, getBuryingPoint, delBuryingPoint, addBuryingPoint, updateBuryingPoint } from "@/api/sfa/buryingPoint";
import tree from "@/views/components/sfa/treeselectParent";
import user from "@/views/components/sfa/user";
import {listRouteD, listRouteDetails} from "@/api/sfa/routeDetails";
import {beforeAdd} from "@/api/sfa/store";
export default {
  name: "BuryingPoint",
  components: { tree,user},
  data() {
    return {
      // 遮罩层
      loading: true,
      pointTypeOptions:[],
      buryingPointType:[],
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 埋点操作记录表格数据
      buryingPointList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      storeCategoryOptions:[],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        deptIds:null,
        startTime:null,
        endTime:null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      validTree:true,
      pickerOptionsStart: {
        disabledDate: time => {
          let da = 1000*60*60*24*90;
          if(this.queryParams.endTime !== '') {
            let value2 = Date.parse(this.queryParams.endTime)
            //  value2 < time.getTime() time.getTime()< value2-da
            return  value2 < time.getTime()|| time.getTime()< value2-da
          } else {
            return false
          }
        }
      },
      pickerOptionsEnd: {
        disabledDate: time => {
          let oneDay = 1000*60*60*24*1;
          let year = 1000*60*60*24*365;
          if(this.queryParams.startTime !== '') {
            let value1 = Date.parse(this.queryParams.startTime)
            // && time.getTime() > value1+year;
            return value1 > time.getTime() || time.getTime() > new Date().getTime();
          } else {
            return false
          }
        }
      },
    };
  },
  created() {
    if(this.$store.state.user.type == '2'){
      this.getDicts("ysl_store_type").then(response => {
        this.storeCategoryOptions = response.data;
      });
    }else{
      this.getDicts("sfa_store_type").then(response => {
        this.storeCategoryOptions = response.data;
      });
    }
    // 类型
    this.getDicts("burying_point_type").then(response => {
      this.pointTypeOptions = response.data;
    });
    this.$set(this.queryParams,"endTime", this.parseTime(new Date(),'{yy}-{mm}-{dd}'));
    this.$set(this.queryParams,"startTime", this.queryMonth());
    this.getList();
  },
  methods: {
    // 门店类型
    storeCategoryFormatter(row,column){
      return this.selectDictLabel(this.storeCategoryOptions, row.storeCategory);
    },
    queryMonth () {
      //先获取当前日期
      let data = new Date()
      let year = data.getFullYear()
      //当前月份需要加一
      let mon = data.getMonth() + 1
      let day = data.getDate()
      //一月的时候年份要减一
      if(mon == 1){
        year--
        mon = 12
      }else if(mon == 3 && day > 28){
        //三月要考虑是否为闰年
        mon--
        if(year%4==0&&year%100!=0||year%400==0){
          day = 29
        }else{
          day = 28
        }
      }else if(mon != 12 || mon != 8 && day == 31){
        //31号的月份要考虑上个月是否有31号
        mon--
        day == 30
      } else {
        mon--
      }
      mon < 10 ? mon = '0'+mon: mon;
      day < 10 ? day = '0'+day: day;

      //最后拼接年月日
      let date = year + "-" + mon + "-" + day
      return date;
    },
    userIdFn(val){
      this.queryParams.userId = val;
    },
    treeselectFn(val){
      this.queryParams.deptIds = val;
      this.$forceUpdate();
    },
    /** 查询埋点操作记录列表 */
    getList() {
      this.loading = true;
      var formData=this.queryParams
      if(formData.deptIds!=null){
        if(formData.deptIds[0]==null){
          formData.deptIds=null
        }
      }
      // if(formData.storeCategorys!=null){
      //   formData.storeCategoryList=formData.storeCategorys
      // }
      listRouteD(formData).then(response => {
        this.buryingPointList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cityLevelsFormat(row, column){
      return this.selectDictLabel(this.pointTypeOptions, row.buryingPointType);
    },
    // 表单重置
    reset() {
      this.form = {
        buryingPointId: null,
        buryingPointType: null,
        buryingPointValue: null,
        buryingPointName: null,
        buryingPointPosition: null,
        systemModel: null,
        userId: null,
        workCode: null,
        nickName: null,
        deptId: null,
        deptName: null,
        operatingTime: null,
        delFlag: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.buryingPointId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加埋点操作记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const buryingPointId = row.buryingPointId || this.ids
      getBuryingPoint(buryingPointId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改埋点操作记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.buryingPointId != null) {
            updateBuryingPoint(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBuryingPoint(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const buryingPointIds = row.buryingPointId || this.ids;
      this.$modal.confirm('是否确认删除埋点操作记录编号为"' + buryingPointIds + '"的数据项？').then(function() {
        return delBuryingPoint(buryingPointIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      var formData=this.queryParams
      if(formData.deptIds!=null){
        if(formData.deptIds[0]==null){
          formData.deptIds=null
        }
      }
      // if(formData.storeCategorys!=null){
      //   formData.storeCategoryList=formData.storeCategorys
      // }
      this.download('sfa/routeDetails/export', {
        ...formData
      }, `buryingPoint_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
