<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.trim="queryParams.name" :placeholder="$t('请输入客户名称')" clearable @keyup.enter.native="handleQuery" onkeyup="this.value=this.value.replace(/(^\s*)|(\s*$)/g,'')"/>
      </el-form-item>
      <el-form-item :label="$t('联系方式')">
        <el-input :placeholder="$t('请输入联系方式')" v-model.trim="queryParams.defaultContactPhone" @keyup.enter.native="handleQuery" onkeyup="this.value=this.value.replace(/(^\s*)|(\s*$)/g,'')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('客户经理')" prop="customerService">
        <el-select clearable  v-model="queryParams.customerService"  @change="handleQuery" :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('请选择客户来源')"  @change="handleQuery" 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"  @change="handleQuery"></dict-selector>
      </el-form-item>
      <el-form-item :label="$t('会员电话')" prop="department">
        <el-col :span="11">
          <area-code-selector style="width: 100%;" v-model="queryParams.memberAreaCode" />
        </el-col>
        <el-col class="line" :span="2"></el-col>
        <el-col :span="11">
          <el-input maxlength="11"
                    width="100%"
                    class="ml-10 w-200"
                    clearable
                    v-model.number="queryParams.memberMobile"
                    :placeholder="$t('请输入会员电话')"/>
        </el-col>
      </el-form-item>
      <el-form-item :label="$t('会员编号')" prop="number">
        <el-input
          v-model.trim="queryParams.memberCode"
          :placeholder="$t('请输入会员编号')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('会员名称')" prop="name">
        <el-input
          v-model.trim="queryParams.memberName"
          :placeholder="$t('请输入会员名称')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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-row :gutter="10" class="mb8">
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <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">
          <span>{{ $l(scope.row,'name') }}</span>
        </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}">
            {{ getDictDatas2(DICT_TYPE.CUSTOMER_TYPE, (row.type||'').split(',')).map(e => isChinese ? e.label : e.labelEn).join(', ')}}
<!--           <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">
          <router-link :to="{path: '/customer/indirectInfo',query: {id: scope.row.id}}" class="link-type">
            <el-button v-has-permi="['ecw:customer:indirect:view']" size="mini" style="margin-right: 10px" type="text" >查看</el-button>
          </router-link>
          <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, getDictDatas2 } from '@/utils/dict';

import {listServiceUser} from "@/api/system/user";
import {exportCustomerComplaintExcel} from "@/api/ecw/customerComplaint";
import {indirectCustomerExportExcel} from "@/api/ecw/customer";
import AreaCodeSelector from "@/components/AreaCodeSelector/index.vue";
import Template from "@/views/cms/template/index.vue";

export default {
  name: "EcwCustomerIndirectcustomer",
  components: {
    Template,
    AreaCodeSelector
  },
  activated() {
    this.getList()
    },
  data() {
    return {
      exportLoading:false,
      getDictDatas,
      getDictDatas2,
      DICT_TYPE,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      // 显示搜索条件
      showSearch: false,
      // 总条数
      total: 0,
      // 客户列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dateRangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        name: null,
        source: null,
        customerService: null,
        marketType:null,
        memberCode: null,
        memberName: null,
        memberMobile: null,
        memberAreaCode: 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();
      }).then(response => {
        this.$message.success(this.$t('已加入导出队列，请稍后在下载日志中下载'))
        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,
        memberCode: null,
        memberName: null,
        memberMobile: null,
        memberAreaCode: null
      }
      this.handleQuery();
    },

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

    }
  }
};
</script>
