<template>
  <div>
    <el-button
      size="small"
      type="primary"
      @click="isCheck"
      class="inline-16 normal-btn-width"
      >{{ $t("查 重") }}</el-button
    >
    <el-dialog
      :title="$t('重复竞争对手')"
      :visible.sync="dialogTableVisible"
      append-to-body
      width="1000px"
    >
      <div slot="title" class="dialog-title">
        <span class="title-text">{{ $t("重复竞争对手") }}</span>
        <p
          v-if="buttonTitle === '确定保存'"
          style="font-size: 14px; color: red"
        >
          {{ $t("已存在重复竞争对手，不可保存") }}
        </p>
        <p v-else style="font-size: 14px; color: red">
          {{ $t("已存在重复竞争对手，不可重复创建") }}
        </p>
      </div>
      <tableLayout
        :layout="tablecols"
        :data="list"
        :opwidth="200"
        height="calc(100vh - 550px)"
        :width="true"
        :custom="true"
      >
        <template v-slot:customcol="scope">
          <p>
            {{
              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"
          layout="total,sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </div>
      <span slot="footer" class="dialog-footer" v-if="creatShow">
        <el-button
          @click="dialogTableVisible = false"
          size="small"
          type="primary"
          class="normal-btn-width"
          >{{ $t("确 定") }}</el-button
        >
<!--        <el-button
          :type="buttonTitle === '确定保存' ? 'warning' : 'primary'"
          @click="onCheck"
          size="small"
          class="normal-btn-width"
          >{{ $t(buttonTitle) }}</el-button
        >-->
      </span>
    </el-dialog>
  </div>
</template>

<script>
import tableLayout from "@/components/table/index5";
export default {
  components: { tableLayout },
  name: "customerCheck",
  props: ["data"],
  data() {
    return {
      dialogTableVisible: false,
      list: [],
      tablecols: [],
      total: 0,
      currentPage: 0,
      duplicates: false,
      creatShow: false,
      message: "该竞争对手疑似重复，是否确认创建竞争对手?",
      buttonTitle: "确定创建",
      param: {
        id: 20230324132602,
        content: {
          sa_competitorid: "",
          enterprisename: "",
        },
      },
      fields1: [],
      fields2: [],
      fields3: [],
    };
  },
  methods: {
    isCheck() {
      this.$emit("onCheck");
    },
    async listData() {
      this.param.content.sa_competitorid = this.data.sa_competitorid || 0;
      this.param.content.enterprisename = this.data.enterprisename;
      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;
        } else {
          if (this.creatShow) {
            this.onSubmit();
          } else {
            this.$message({
              message: this.$t("无重复数据"),
              type: "success",
            });
          }
        }
      }
    },
    onCheck() {
      if (this.buttonTitle === "确定保存") {
        this.message = "该竞争对手疑似重复，是否确认更新竞争对手?";
      } else {
        this.message = "该竞争对手疑似重复，是否确认创建竞争对手?";
      }
      this.$confirm(this.$t(this.message), this.$t("提示"), {
        confirmButtonText: this.$t("确定"),
        cancelButtonText: this.$t("取消"),
        type: "warning",
      })
        .then(() => {
          this.duplicates = true;
          this.onSubmit();
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: this.$t("已取消"),
          });
        });
    },
    async onSubmit() {
      this.data.sys_enterpriseid = 0;
      const res = await this.$api.requested({
        id: 20221018164102,
        content: this.data,
      });
      this.tool.showMessage(res, () => {
        if (this.duplicates) {
          if (this.buttonTitle === "确定保存") {
            this.tagData(res.data.sa_competitorid);
          } else {
            let data = ["疑似重复"];
            this.setTag(res.data.sa_competitorid, data);
          }
        } 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_competitor",
          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();
    },
    // 获取标签数据
    async tagData(id) {
      const res = await this.$api.requested({
        content: {
          ownertable: "sa_competitor",
          ownerid: this.$route.query.id,
        },
        id: 20220929085401,
      });
      var resTagData = [];
      resTagData = res.data.datatag;
      if (this.duplicates) {
        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);
        }
      } else {
        for (var k = 0; k < res.data.datatag.length; k++) {
          if (res.data.datatag[k] === "疑似重复") {
            resTagData.splice(k);
            break;
          }
        }
        this.setTag(id, resTagData);
      }
    },
  },
  created() {
    this.tablecols = this.tool.tabelCol(
      this.$route.name
    ).duplicatesCompetitorTable.tablecols;
  },
};
</script>

<style scoped>
</style>
