<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"
      style="float: left"
    >
      <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">{{title?title:$t('已存在重复项目，是否确认继续创建')}}</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>{{$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>
          <div v-else-if="scope.column.columnname == 'projectname'">
            <span v-for="item in scope.column.data.projectname" :key="item.index">
              <span :style="{color:item.color}">{{item.text}}</span>
            </span>
          </div>
          <div v-else-if="scope.column.columnname == 'address'">
            <span :style="{color:scope.column.data.chars.address && scope.column.data.address.includes(scope.column.data.chars.address)?'red':'#666'}">{{scope.column.data.address || scope.column.data.province?scope.column.data.province+scope.column.data.city+scope.column.data.county+scope.column.data.address:'--'}}</span>
          </div>
          <p
            v-else
          ><span v-if="scope.column.data[scope.column.columnname]">
              <span :style="{color:scope.column.data.chars[scope.column.columnname] && scope.column.data[scope.column.columnname].includes(scope.column.data.chars[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>
      <span slot="footer" class="dialog-footer" v-if="creatShow">
        <el-button @click="dialogTableVisible = false" size="small" 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>
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: 20221208184202,
        content: {
          sa_projectid: "",
          projectname: "",
          address: "",
        },
      },
      fields1: [],
      fields2: [],
      fields3: [],
      siteid: JSON.parse(sessionStorage.getItem("active_account")).siteid,
    };
  },
  methods: {
    async listData(val) {
      this.param.content.sa_projectid = 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.tool.getCheckFontProject(res.data)
          if (this.siteid == "HY") {
            if (val == "审核") {
              this.dialogTableVisible = true;
            } else {
              // this.onCheck();
              this.dialogTableVisible = true;
            }
          } else {
            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() {
      if (this.data.signdate_due !== "") {
        const data = new Date(this.data.signdate_due);
        if (data.getMonth() + 1 < 10) {
          this.data.signdate_due =
            data.getFullYear() + "-0" + (data.getMonth() + 1);
        } else {
          this.data.signdate_due =
            data.getFullYear() + "-" + (data.getMonth() + 1);
        }
      } else {
        this.data.signdate_due = "";
      }
      if (this.data.begdate_due !== "") {
        const data = new Date(this.data.begdate_due);
        if (data.getMonth() + 1 < 10) {
          this.data.begdate_due =
            data.getFullYear() + "-0" + (data.getMonth() + 1);
        } else {
          this.data.begdate_due =
            data.getFullYear() + "-" + (data.getMonth() + 1);
        }
      } else {
        this.data.begdate_due = "";
      }
      if (this.data.enddate_due !== "") {
        const data = new Date(this.data.enddate_due);
        if (data.getMonth() + 1 < 10) {
          this.data.enddate_due =
            data.getFullYear() + "-0" + (data.getMonth() + 1);
        } else {
          this.data.enddate_due =
            data.getFullYear() + "-" + (data.getMonth() + 1);
        }
      } else {
        this.data.enddate_due = "";
      }
      const res = await this.$api.requested({
        id: 20221124154902,
        content: this.data,
      });
      this.tool.showMessage(res, () => {
        if (this.duplicates) {
          this.setTag(res.data.sa_projectid, "疑似重复");
        } 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_project",
          ownerid: id,
          datatag: [data],
        },
      });
      if (res.code == 0){
        this.tool.showMessage(res)
      }else {
        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(val) {
      const res = await this.$api.requested({
        id: "20230410090502",
        content: {},
      });
      if (res.code == 0){
        this.tool.showMessage(res)
      }else {
        this.fields1 = res.data.projectcheckrule.fields1;
        this.fields2 = res.data.projectcheckrule.fields2;
        this.fields3 = res.data.projectcheckrule.fields3;
        this.listData(val);
      }

    },
  },
  created() {
    this.tablecols = this.tool.tabelCol(
      this.$route.name
    ).duplicatesProjectTable.tablecols;
  },
};
</script>

<style scoped>
</style>
