<template>
  <div>
    <el-button size="mini" type="primary" @click="queryRule">{{
      $t("查看重复")
    }}</el-button>
    <el-dialog
      :title="$t('重复客户')"
      :visible.sync="dialogTableVisible"
      append-to-body
      width="1000px"
      @close="onClose"
    >
      <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>{{ $t(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>{{ $t(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>
            <span v-if="scope.column.data[scope.column.columnname]">
              <span :style="{color:scope.column.data.chars[0][scope.column.columnname] && scope.column.data[scope.column.columnname].includes(scope.column.data.chars[0][scope.column.columnname])?'red':'#666'}">
                {{scope.column.data[scope.column.columnname]}}
              </span>
            </span>
            <span v-else>
              --
            </span>
          </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"
          layout="total,sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "duplicatesCustomer",
  props: ["data"],
  data() {
    return {
      dialogTableVisible: false,
      list: [],
      tablecols: [],
      total: 0,
      currentPage: 0,
      param: {
        id: 20221208172002,
        pageNumber: 1,
        pageSize: 999,
        content: {
          sa_customersid: "",
          enterprisename: "",
          taxno: "",
          address: "",
        },
      },
      fields1: [],
      fields2: [],
      fields3: [],
    };
  },
  methods: {
    async queryData() {
      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] || "";
      });
      this.param.content.sa_customersid = this.data.sa_customersid;

      // this.param.content.enterprisename = this.data.enterprisename
      // this.param.content.taxno = this.data.taxno
      // this.param.content.address = this.data.address
      const res = await this.$api.requested(this.param);

      this.list = res.data;
      this.total = res.total;
      this.currentPage = res.pageNumber;
      this.dialogTableVisible = true;
      if (res.data.length !== 0) {
        this.tagData(this.$route.query.id);
      }
    },
    handleSizeChange(val) {
      // console.log(`每页 ${val} 条`);
      this.param.content.pageSize = val;
      this.queryData();
    },
    handleCurrentChange(val) {
      // console.log(`当前页: ${val}`);
      this.param.content.pageNumber = val;
      this.queryData();
    },
    /*查询规则*/
    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.queryData();
    },
    // 获取标签数据
    async tagData(id) {
      const res = await this.$api.requested({
        content: {
          ownertable: "sa_customers",
          ownerid: this.$route.query.id,
        },
        id: 20220929085401,
      });

      var resTagData = [];
      resTagData = res.data.datatag;
      let flag = 0;
      for (var i = 0; i < res.data.datatag.length; i++) {
        if (res.data.datatag[i] === "疑似重复") {
          flag = 1;
          break;
        }
      }
      if (flag === 1) {
        this.setTag(id, resTagData);
      } else {
        resTagData.push("疑似重复");
        this.setTag(id, resTagData);
      }
    },
    async setTag(id, data) {
      const res = await this.$api.requested({
        id: 20220929090901,
        content: {
          ownertable: "sa_customers",
          ownerid: id,
          datatag: data,
        },
      });
    },
    onClose() {
      this.dialogTableVisible = false;
      this.$emit("onSuccessTag");
    },
  },
  mounted() {},
  created() {
    this.tablecols = this.tool.tabelCol(
      this.$route.name
    ).duplicatesCustomerTable.tablecols;
  },
};
</script>

<style scoped>
/deep/.el-dialog__title {
  line-height: 20px;
  font-size: 16px;
  color: #303133;
}
</style>
