<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="公司名" prop="companyId">
          <el-select filterable  style="width: 220px" v-model="queryParams.companyId" placeholder="请选择公司名" clearable size="small">
              <el-option
                v-for="item in companys"
                :key="item.companyId"
                :label="item.companyName"
                :value="item.companyId"
              />
        </el-select>
      </el-form-item>
      <el-form-item label="操作人(平台)" prop="sysUserName">
        <el-input
          v-model="queryParams.sysUserName"
          placeholder="请输入"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作人(公司)" prop="companyUserName">
        <el-input
          v-model="queryParams.companyUserName"
          placeholder="请输入"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作时间" prop="createTime">
        <el-date-picker clearable size="small" style="width: 205.4px"
          v-model="dateRange"
          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-table v-loading="loading" :data="customerAssignList" @selection-change="handleSelectionChange" border>
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="ID" align="center" prop="assignId" />
        <el-table-column label="公司" align="center" prop="companyName" />
        <el-table-column label="分配数量" align="center" prop="customerIds" width="120" >
                <template slot-scope="scope">
                    <el-tag   >{{scope.row.customerIds.split(',').length}}</el-tag>
                </template>
            </el-table-column>
        <el-table-column label="操作时间" align="center" prop="createTime" />
        <el-table-column label="操作人(平台)" align="center" prop="sysUserName" />
        <el-table-column label="操作人(公司)" align="center" prop="companyUserName" />
        <el-table-column label="备注" align="center" prop="remark" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
            <el-button
            size="mini"
            type="text"
            @click="handleShow(scope.row)"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleCancel(scope.row)"
            v-hasPermi="['crm:customerAssign:cancel']"
          >撤销</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-dialog :title="customer.title" :visible.sync="customer.open" width="1000px" append-to-body>
          <el-table border  :data="customers" >
              <el-table-column label="客户名称" align="center" prop="customerName" />
              <el-table-column label="客户手机号" align="center" prop="mobile" />
          </el-table>
      </el-dialog>
    </div>
  </template>

  <script>
  import { listCustomerAssign,   cancelCustomerAssign } from "@/api/crm/customerAssign";
  import { getCompanyList } from "@/api/company/company";
  import { getCustomerListByIds } from "@/api/crm/customer";
  export default {
    name: "CustomerAssign",
    data() {
      return {
        customer:{
            open:false,
            title:"查看客户信息"
        },
        customers:[],
        companys:[],
        dateRange:[],
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 客户分配记录表格数据
        customerAssignList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          companyId: null,
          companyUserId: null,
          customerIds: null,
          sysUserId: null,
          assignType: null,
        },

      };
    },
    created() {
        getCompanyList().then(response => {
            this.companys = response.data;
        });
    },
    methods: {
        handleShow(item){
          this.getCustomerListByIds(item.customerIds)
        },
        getData(){
            this.getList()
        },
        getCustomerListByIds(ids) {
            var data={customerIds:ids};
            getCustomerListByIds(data).then(response => {
              this.customers = response.data;
              this.customer.open=true;
            });
        },
        getData(){
            this.getList()
        },
        /** 查询客户分配记录列表 */
        getList() {
            this.loading = true;
            listCustomerAssign(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
            this.customerAssignList = response.rows;
            this.total = response.total;
            this.loading = false;
            });
        },

        /** 搜索按钮操作 */
        handleQuery() {
            this.queryParams.pageNum = 1;
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        // 多选框选中数据
        handleSelectionChange(selection) {
            this.ids = selection.map(item => item.assignId)
            this.single = selection.length!==1
            this.multiple = !selection.length
        },
        handleCancel(row) {
            this.$confirm('是否确认撤销客户分配记录编号为"' + row.assignId + '"的数据项?', "警告", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning"
            }).then(function() {
                var data={assignId: row.assignId}
                return cancelCustomerAssign(data);
            }).then(() => {
                this.getList();
                this.msgSuccess("操作成功");
            }).catch(function() {});
        },

    }
  };
  </script>
