<template>
  <div>
    <el-button size="small" type="primary" @click="isCheck" class="inline-16 normal-btn-width">查 重</el-button>
    <el-dialog title="重复客户" :visible.sync="dialogTableVisible" append-to-body width="1000px">
      <div slot="title" class="dialog-title">
        <span class="title-text">重复客户</span>
        <p style="font-size: 14px;color: red">{{title?title:'已存在重复客户不可继续创建'}}</p>
      </div>
      <tableLayout :layout="tablecols"   :data="list" :opwidth="200" height="calc(100vh - 550px)" :width="true" :custom="true">
        <template v-slot:customcol="scope">
          <div v-if="scope.column.columnname === 'tag'">
            <div v-for="item in scope.column.data.tag_sys" :key="item.index"  style="float: left;margin-left: 5px;margin-bottom: 5px">
              <el-tag  color="#3874F6" size="mini" type="primary" effect="dark">
                <span>{{item}}</span>
              </el-tag>
            </div>
            <div v-for="item in scope.column.data.tag" :key="item.index"  style="float: left;margin-left: 5px;margin-bottom: 5px">
              <el-tag color="#FA8C16" size="mini" type="warning" effect="dark">
                <span>{{item}}</span>
              </el-tag>
            </div>
          </div>
          <div v-else-if="scope.column.columnname === 'leader'">
            <span>{{scope.column.data.leader?scope.column.data.leader.length !== 0 ? scope.column.data.leader[0].name:'--':'--'}}</span>
          </div>
          <p v-else>{{scope.column.data[scope.column.columnname]?scope.column.data[scope.column.columnname]:'--'}}</p>
        </template>
      </tableLayout>
      <div  class="container normal-panel" style="text-align:right">
        <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[20, 50, 100, 200]"
            :page-size="20"
            :total="total">
        </el-pagination>
      </div>
      <span slot="footer" class="dialog-footer" v-if="creatShow">
        <el-button @click="dialogTableVisible = false" type="primary" size="small" class="normal-btn-width">确 定</el-button>
<!--        <el-button :type="buttonTitle === '确定保存'?'warning':'primary'" @click="onCheck" size="small" class="normal-btn-width">{{buttonTitle}}</el-button>-->
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "customerCheck",
  props:["data",'title'],
  data(){
    return {
      dialogTableVisible:false,
      list:[],
      tablecols:[],
      total:0,
      currentPage:0,
      duplicates:false,
      creatShow:false,
      message:'该客户疑似重复，是否确认创建客户?',
      buttonTitle:'确定创建',
      param:{
        "id": 20221208172002,
        "content": {
          "sa_customersid":'',
          "enterprisename":'',
          "taxno":'',
          "address":''
        },
      },
      fields1:[],
      fields2:[],
      fields3:[],
    }
  },
  methods:{
    async listData(){
      this.param.content.sa_customersid = 0
      this.fields1.forEach(item=>{
        this.param.content[item] = this.data[item] || ""
      })
      this.fields2.forEach(item=>{
        this.param.content[item] = this.data[item] || ""
      })
      this.fields3.forEach(item=>{
        this.param.content[item] = this.data[item] || ""
      })
      const res = await this.$api.requested(this.param)
      if (res.code == 0){
        this.tool.showMessage(res)
      }else {
        if (res.total > 0){
          this.list = res.data
          this.total = res.total
          this.currentPage = res.pageNumber
          this.dialogTableVisible = true
          this.$emit('isDuplicate','重复')
        }else {
          if (this.creatShow){
            this.onSubmit()
          }else {
            this.$message({
              message: '无重复数据',
              type: 'success'
            });
            this.$emit('isDuplicate','无重复数据')
          }
        }
      }

    },
    onCheck(){
      if (this.buttonTitle === '确定保存'){
        this.message = '该客户疑似重复，是否确认更新客户?'
      }else {
        this.message = '该客户疑似重复，是否确认创建客户?'
      }
      this.$confirm(this.message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.duplicates = true
        this.onSubmit()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });
      });
    },
    async onSubmit(){
      if (this.data.contactsinfo.telephone){
        this.data.contactsinfo.phonenumber = this.data.contactsinfo.telephone
        this.data.contactsinfo.telephone = ''
      }
      const res = await this.$api.requested({
        "id": 20221216184302,
        "content": this.data
      })
      this.tool.showMessage(res,()=>{
        if (this.duplicates){
          this.setTag(res.data.sa_customersid,'疑似重复')
        }else {
          this.$emit('onSuccess')
         /* this.setTag(res.data.sa_customersid,'')*/
        }
      })
    },
    async setTag(id,data){
      const res = await this.$api.requested({
        "id": 20220929090901,
        "content": {
          "ownertable":"sa_customers",
          "ownerid":id,
          "datatag":[data]
        }
      })
      this.dialogTableVisible = false
      this.duplicates = false
      this.$emit('onSuccess')
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.param.content.pageSize = val
      this.listData()
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.param.content.pageNumber = val
      this.listData()
    },
    /*查询规则*/
    isCheck(){
      this.$emit('onCheck')
    },
    async queryRule(){
      const res = await this.$api.requested({
        "id": "20230410090502",
        "content": {},
      })
      this.fields1 = res.data.custcheckrule.fields1
      this.fields2 = res.data.custcheckrule.fields2
      this.fields3 = res.data.custcheckrule.fields3
      this.listData()
    }
  },
  created() {
    this.tablecols = this.tool.tabelCol(this.$route.name).duplicatesCustomerTable.tablecols
  }
}
</script>

<style scoped>

</style>
