<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item :label="$t('客户名称')" prop="name">
        <el-input v-model="queryParams.name" :placeholder="$t('请输入客户名称')" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item :label="$t('联系方式')">
        <el-input :placeholder="$t('请输入联系方式')" v-model="queryParams.defaultContactPhone"></el-input>
      </el-form-item>
      <el-form-item :label="$t('客户经理')" prop="customerService">
        <el-select clearable  v-model="queryParams.customerService" :placeholder="$t('请选择客户经理')" clearable size="small">
          <el-option v-for="dict in customerServiceList"
                     :key="dict.id" :label="dict.nickname" :value="dict.id"/>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('客户来源')" prop="source">
        <el-select clearable  v-model="queryParams.source" :placeholder="$t('请选择客户来源')" clearable size="small">
          <el-option v-for="dict in getDictDatas(DICT_TYPE.CUSTOMER_SOURCE)"
                       :key="dict.value" :label="isChinese ? dict.label : dict.labelEn" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('创建时间')">
        <el-date-picker clearable  v-model="dateRangeCreateTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
                        type="datetimerange"  range-separator="-" :start-placeholder="$t('开始日期')" :end-placeholder="$t('结束日期')" />
      </el-form-item>
      <el-form-item  :label="$t('市场获客')" prop="department">
        <dict-selector clearable  :type="DICT_TYPE.ECW_CUSTOMER_RESOURCE_TYPE" v-model="queryParams.marketType" formatter="number"></dict-selector>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">{{$t('搜索')}}</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">{{$t('重置')}}</el-button>
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading" v-hasPermi="['ecw:customer:indirect-export']">{{$t('导出')}}</el-button>
      </el-form-item>
    </el-form>

    <!-- 列表 -->
    <el-table ref="multipleTable" v-loading="loading" :data="list"   @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column :label="$t('客户名称')" align="center" prop="name" >
        <template v-slot="scope">
        <router-link :to="{path: '/customer/indirectInfo',query: {id: scope.row.id}}" class="link-type">
          <span>{{ scope.row.name }}</span>
        </router-link>
        </template>
      </el-table-column>
      <el-table-column :label="$t('联系方式')" prop="defaultContactPhone">
        <template v-slot="{row}">
          +{{row.defaultContactPhone}}
        </template>
      </el-table-column>
       <el-table-column :label="$t('客户来源')" align="center" prop="sourse">
         <template v-slot="{row}">
           <span>{{row.agentCustomerName}}</span>
             <!-- <dict-tag :value="row.resourceType" :type="DICT_TYPE.CUSTOMER_SOURCE" /> -->
           </template>
         </el-table-column>
       <el-table-column :label="$t('资源类型')" align="center" prop="resourceType">
         <template v-slot="{row}">
             <dict-tag :value="row.resourceType" :type="DICT_TYPE.CUSTOMER_ESOURCE_TYPE" />
           </template>
         </el-table-column>
       <el-table-column :label="$t('客户类别')" align="center" prop="sourse">
        <template v-slot="{row}">
           <dict-tag :value="row.type" :type="DICT_TYPE.CUSTOMER_TYPE" />
         </template>
       </el-table-column>
      <el-table-column :label="$t('创建时间')" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('客户经理')" align="center" prop="customerService" :formatter="customerServiceFn">
      </el-table-column>
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-has-permi="['ecw:customer:indirect:perfect']" size="mini" type="text" icon="el-icon-user" @click="delay(scope.row)">{{$t('完善')}}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

  </div>
</template>

<script>
import {updateCustomer, getCustomer,
  getCustomerPage, exportCustomerExcel } from "@/api/ecw/indirectCustomer";
import { getDictDatas, DICT_TYPE } from '@/utils/dict';

import {listServiceUser} from "@/api/system/user";
import {exportCustomerComplaintExcel} from "@/api/ecw/customerComplaint";
import {indirectCustomerExportExcel} from "@/api/ecw/customer";

export default {
  name: "EcwCustomerIndirectcustomer",
  activated() {
    this.getList()
    },
  data() {
    return {
      exportLoading:false,
      getDictDatas,
      DICT_TYPE,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dateRangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        name: null,
        source: null,
        customerService: null,
        marketType:null
      },
      // 网点
      nodeList: [],
      customerId:undefined,
      customerServiceList:[],
    };
  },
  created() {
    this.getList();
    listServiceUser().then(r=>{
      this.customerServiceList = r.data;
    })
  },
  computed:{
    isChinese(){
      return this.$i18n.locale === 'zh_CN'
    },
  },
  methods: {
    // 导出
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime', false);
      // 执行导出
      this.$modal.confirm(this.$t('是否确认导出间接客户数据项?')).then(() => {
        this.exportLoading = true;
        return indirectCustomerExportExcel(params);
      }).then(response => {
        this.$download.excel(response, '间接客户列表.xls');
        this.exportLoading = false;
      }).catch(() => {});
    },
    customerServiceFn(val){
      if(this.customerServiceList.length > 0){
        let index =this.customerServiceList.findIndex(item => item.id === val.customerService);
        return index !== -1  ? this.customerServiceList[index]?.nickname :''
      }else {
        return ''
      }
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 处理查询参数
      let params = {...this.queryParams};
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime', false);
      // 执行查询
      getCustomerPage(params).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRangeCreateTime = [];
      this.resetForm("queryForm");
      this.queryParams = {
        pageNo: 1,
        pageSize: 10,
        name: null,
        source: null,
        customerService: null,
      }
      this.handleQuery();
    },

    delay(row){
      this.$router.push({path:'/customer/editIndirect',query:{id:row.id}})
    },
    handleSelectionChange(val){

    }
  }
};
</script>
