<template>
  <div>
    <el-button size="mini" type="primary" @click="onShow">{{
      $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>
          <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>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "duplicatesProject",
  props: ["data"],
  data() {
    return {
      dialogTableVisible: false,
      tablecols: [],
      list: [],
      total: 0,
      currentPage: 0,
      param: {
        id: 20221208184202,
        pageNumber: 1,
        pageSize: 999,
        content: {
          sa_projectid: "",
          projectname: "",
          address: "",
        },
      },
      fields1: [],
      fields2: [],
      fields3: [],
    };
  },
  methods: {
    onShow() {
      this.queryRule();
      // this.queryData()
    },
    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_projectid = this.data.sa_projectid;
      const res = await this.$api.requested(this.param);
      this.list = res.data;
      this.total = res.total
      this.tool.getCheckFontProject(res.data)
      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.projectcheckrule.fields1;
      this.fields2 = res.data.projectcheckrule.fields2;
      this.fields3 = res.data.projectcheckrule.fields3;
      this.queryData();
    },
    // 获取标签数据
    async tagData(id) {
      const res = await this.$api.requested({
        content: {
          ownertable: "sa_project",
          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_project",
          ownerid: id,
          datatag: data,
        },
      });
    },
    onClose() {
      this.$emit("dialogClose");
    },
  },
  created() {
    this.tablecols = this.tool.tabelCol(
      this.$route.name
    ).duplicatesProjectTable.tablecols;
  },
};
</script>

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