<template>
    <div class="app-container">
 
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
          <el-form-item label="客户编码" prop="customerCode">
            <el-input
              v-model="queryParams.customerCode"
              placeholder="请输入客户编码"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="客户名称" prop="customerName">
            <el-input
              v-model="queryParams.customerName"
              placeholder="请输入客户名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="手机号" prop="mobile">
            <el-input
              v-model="queryParams.mobile"
              placeholder="请输入客户手机号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="所属部门" prop="deptId">
              <treeselect style="width:205.4px" v-model="queryParams.deptId" :options="deptOptions" :show-count="true" placeholder="请选择所属部门" />
          </el-form-item>
          <el-form-item label="员工姓名" prop="companyUserNickName">
            <el-input
              v-model="queryParams.companyUserNickName"
              placeholder="请输入员工姓名"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="跟进类型" prop="visitType">
            <el-select v-model="queryParams.visitType" placeholder="请选择跟进类型" clearable size="small">
              <el-option
                    v-for="item in typeOptions"
                    :key="item.dictValue"
                    :label="item.dictLabel"
                    :value="item.dictValue"
                  />
            </el-select>
          </el-form-item>
          <el-form-item label="跟进阶段" prop="customerUserStatus">
            <el-select v-model="queryParams.customerUserStatus" placeholder="请选择跟进阶段" clearable size="small">
              <el-option
                    v-for="item in customerUserStatusOptions"
                    :key="item.dictValue"
                    :label="item.dictLabel"
                    :value="item.dictValue"
                  />
            </el-select>
          </el-form-item>
          <el-form-item label="跟进时间" prop="createTime">
            <el-date-picker
            style="width:205.4px"
              clearable size="small"
              v-model="dateRange"
              type="daterange"
              value-format="yyyy-MM-dd"
              start-placeholder="开始日期"
              end-placeholder="结束日期">
            </el-date-picker>
          </el-form-item>
          <el-form-item label="客户创建时间" prop="createTimeRange">
        <el-date-picker
          style="width:205.4px"
          clearable size="small"
          v-model="createTimeRange"
          type="daterange"
          value-format="yyyy-MM-dd"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="领取时间" prop="receiveTimeRange">
        <el-date-picker
          style="width:205.4px"
          clearable size="small"
          v-model="receiveTimeRange"
          type="daterange"
          value-format="yyyy-MM-dd"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>
          <el-form-item>
            <el-button type="cyan" 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"
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['crm:customerVisit:export']"
            >导出</el-button>
          </el-col>
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>
        <el-table  height="500" border v-loading="loading" :data="customerVisitList" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55" align="center" />
          <el-table-column label="ID" align="center" prop="visitId" />
          <el-table-column label="所属公司" align="center" prop="companyName" />
          <el-table-column label="所属员工" align="center" prop="companyUserNickName" />
          <el-table-column label="客户编码" align="center" prop="customerCode" />
          <el-table-column label="客户名称" align="center" prop="customerName" />
          <el-table-column label="跟进类型" width="150" align="center" prop="visitType" >
            <template slot-scope="scope">
                  <el-tag prop="status" v-for="(item, index) in typeOptions"    v-if="scope.row.visitType==item.dictValue">{{item.dictLabel}}</el-tag>
              </template>
          </el-table-column>
          <el-table-column label="跟进阶段" width="150" align="center" prop="customerUserStatus" >
            <template slot-scope="scope">
                  <el-tag prop="status" v-for="(item, index) in customerUserStatusOptions"    v-if="scope.row.customerUserStatus==item.dictValue">{{item.dictLabel}}</el-tag>
              </template>
          </el-table-column>
          <el-table-column label="跟进内容" align="center" prop="content" />
          <el-table-column label="跟进照片" align="center" prop="photos" width="200px">
            <template slot-scope="scope">
              <el-image  v-for="(img) in parsePhoto(scope.row.photos)"
                style="width: 30px; height: 30px;margin-right:5px"
                :src="img" 
                :preview-src-list="parsePhoto(scope.row.photos)">
              </el-image>
            </template>
          </el-table-column>
          <el-table-column label="跟进时间" align="center" prop="createTime" />
          
          <el-table-column label="下次联系时间" align="center" prop="nextTime" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.nextTime, '{y}-{m}-{d}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" fixed="right" width="120px" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">

              <el-button
                size="mini"
                type="text"
                @click="handleShow(scope.row)"
                v-hasPermi="['crm:customer:query']"
              >查看</el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['crm:customerVisit:remove']"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        
        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
  
      <el-drawer
       size="75%"
        :title="show.title" :visible.sync="show.open"
        >
          <customer-details  ref="customerDetails" />
      </el-drawer>
    </div>
  </template>
  
  <script>
  import { listCustomerVisit, getCustomerVisit, delCustomerVisit, addCustomerVisit, updateCustomerVisit, exportCustomerVisit } from "@/api/crm/customerVisit";
  import customerDetails from '../components/customerDetails.vue';
  import { treeselect } from "@/api/company/companyDept";
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  export default {
    name: "CustomerVisit",
    components: { customerDetails,Treeselect },
    data() {
      return {
        customerUserStatusOptions:[],
        dateRange:[],
        defaultProps: {
          children: "children",
          label: "label",
        },
        // 部门树选项
        deptOptions: [],
        // 部门名称
        deptName: undefined,
        typeOptions:[],
        createTimeRange:[],
        receiveTimeRange:[],
        show:{
          title:"客户详情",
          open:false,
        },
        companys:[],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 跟进表格数据
        customerVisitList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          customerId: null,
          visitType: null,
          content: null,
          photos: null,
          nextTime: null,
          lng: null,
          lat: null,
          address: null,
          companyUserId: null,
          companyId: null
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          visitType: [
            { required: true, message: "类型 1 电话 2 微信不能为空", trigger: "change" }
          ],
          companyUserId: [
            { required: true, message: "销售ID不能为空", trigger: "blur" }
          ],
          createTime: [
            { required: true, message: "创建时间不能为空", trigger: "blur" }
          ],
        }
      };
    },
    watch: {
      // 根据名称筛选部门树
      deptName(val) {
        this.$refs.tree.filter(val);
      },
    },
    created() {
      this.getDicts("crm_customer_visit_type").then((response) => {
        this.typeOptions = response.data;
      });
      this.getDicts("crm_customer_user_status").then((response) => {
            this.customerUserStatusOptions = response.data;
        });
      this.getTreeselect();
      this.getList();
    },
    methods: {
      parsePhoto(photos){
        var imgs=[];
        if(photos!=null){
          imgs=photos.split(",");
        }
        return imgs;
      },
      getTreeselect() {
        treeselect().then((response) => {
          this.deptOptions = response.data;
        });
      },
      // 筛选节点
      filterNode(value, data) {
        if (!value) return true;
        return data.label.indexOf(value) !== -1;
      },
      // 节点单击事件
      handleNodeClick(data) {
        this.queryParams.deptId = data.id;
        this.getList();
      },
      handleShow(row){
        var that=this;
        that.show.open=true;
        setTimeout(() => {
            that.$refs.customerDetails.getDetails(row.customerId);
        }, 200);
      },
      /** 查询跟进列表 */
      getList() {
        this.loading = true;
        if(this.createTimeRange!=null&&this.createTimeRange.length==2){
        this.queryParams.createTimeRange=this.createTimeRange[0]+"--"+this.createTimeRange[1]
      }
      else{
        this.queryParams.createTimeRange=null;
      }
      if(this.receiveTimeRange!=null&&this.receiveTimeRange.length==2){
        this.queryParams.receiveTimeRange=this.receiveTimeRange[0]+"--"+this.receiveTimeRange[1]
      }
      else{
        this.queryParams.receiveTimeRange=null;
      }
        listCustomerVisit(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
          this.customerVisitList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          visitId: null,
          customerId: null,
          visitType: null,
          content: null,
          photos: null,
          nextTime: null,
          lng: null,
          lat: null,
          address: null,
          companyUserId: null,
          createTime: null,
          companyId: 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.visitId)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加跟进";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const visitId = row.visitId || this.ids
        getCustomerVisit(visitId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改跟进";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.visitId != null) {
              updateCustomerVisit(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("修改成功");
                  this.open = false;
                  this.getList();
                }
              });
            } else {
              addCustomerVisit(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                }
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const visitIds = row.visitId || this.ids;
        this.$confirm('是否确认删除跟进编号为"' + visitIds + '"的数据项?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
            return delCustomerVisit(visitIds);
          }).then(() => {
            this.getList();
            this.msgSuccess("删除成功");
          }).catch(function() {});
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有跟进数据项?', "警告", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
          }).then(function() {
            return exportCustomerVisit(queryParams);
          }).then(response => {
            this.download(response.msg);
          }).catch(function() {});
      }
    }
  };
  </script>
  