<template>
  <div class="box-center">
    <!-- 头 -->
    <div class="app-top">
      <div class="app-titel" v-if="orgType != 3">
        <div class="label" style="color: #32363d; font-weight: 400">
          项目选择：
        </div>
        <div>
          <el-select
            v-model="searchForm.houseId"
            style="width: 215px"
            @change="houseChange"
            placeholder="请选择"
            filterable
          >
            <el-option
              v-for="item in houseList"
              :key="item.id"
              :label="item.propertyName"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </div>
      </div>
      <div class="app-titel" style="margin-top: 10px">
        <div class="label">正确词：</div>
        <div>
          <el-input
            maxlength="8"
            clearable
            v-model="searchForm.correctWord"
          ></el-input>
        </div>
        <div class="label">错误词：</div>
        <div>
          <el-input
            maxlength="8"
            clearable
            v-model="searchForm.wrongWord"
          ></el-input>
        </div>
        <div style="margin-left: 20px">
          <el-button type="primary" @click="screen">筛选</el-button>
        </div>
        <div style="margin-left: 20px">
          <el-button @click="clascreen">清空筛选条件</el-button>
        </div>
        <div style="margin-left: 20px">
          <el-button @click="addinfo()" type="primary" v-if="tem_ww_add"
            >新增</el-button
          >
        </div>
        <div
          style="margin-left: auto; margin-right: 10px"
          v-if="tem_ww_downLoad"
        >
          <el-button @click="downLoad">导出</el-button>
        </div>
      </div>
    </div>

    <!-- 表格 -->
    <div class="cen-tab">
      <el-table :data="tableData" stripe style="width: 100%">
        <el-table-column type="index" label="序号" width="50">
        </el-table-column>
        <el-table-column prop="correctWord" label="正确词" align="center">
        </el-table-column>
        <el-table-column prop="wrongWord" label="错误词" align="center">
        </el-table-column>

        <el-table-column prop="createTime" label="创建时间" align="center">
        </el-table-column>

        <!-- <el-table-column prop="date" label="备注" align="center">
        </el-table-column> -->
        <!--  scope -->
        <el-table-column label="操作" align="center">
          <template slot-scope="{ row }">
            <el-button v-if="tem_ww_edit" type="text" @click="edit(row)"
              >修改</el-button
            >
            <el-button v-if="tem_ww_del" type="text" @click="del(row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div style="display: flex; justify-content: flex-end; margin-top: 10px">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 30, 50]"
          :page-size="size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>

    <el-dialog
      :title="editFlag ? '修改常错词' : '新增常错词'"
      :visible.sync="dialogVisible"
      @close="reset"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        label-position="labelPosition"
        ref="ruleForm"
        label-width="140px"
        style="width: 60%; margin: 0 auto"
      >
        <el-form-item label="正确词" prop="correctWord">
          <el-input
            v-model="ruleForm.correctWord"
            maxlength="8"
            clearable
          ></el-input>
        </el-form-item>
        <el-form-item label="错误词" prop="wrongWord">
          <el-input
            v-model="ruleForm.wrongWord"
            maxlength="8"
            clearable
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" :loading="loadingFlag" @click="addSure()"
          >保存</el-button
        >
      </div>
    </el-dialog>

    <el-dialog title="删除" :visible.sync="dialogVisible2">
      <div class="delclass">
        <div><el-radio v-model="radiovalue" label="0">直接删除</el-radio></div>
        <div style="margin-top: 10px">
          <el-radio v-model="radiovalue" label="1"
            >删除，同时恢复历史纠错内容</el-radio
          >
        </div>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible2 = false">取 消</el-button>
        <el-button type="primary" @click="delSure()">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
import { exportMethodPost } from "@/util/util";
export default {
  data() {
    return {
      currentPage: 4,
      orgType: localStorage.getItem("orgType"),
      value: "",
      input: "",
      tableData: [],
      multipleSelection: [],
      mistakenList: [], // 常错词数组
      correctList: [], // 正确词数组
      dialogVisible: false,
      loadingFlag: false,
      houseList: [],
      ruleForm: {
        correctWord: "",
        wrongWord: "",
        memo: "",
      },
      searchForm: {
        correctWord: "",
        wrongWord: "",
        houseId: "",
      },
      currentPage: 1,
      editFlag: false,
      size: 10,
      total: 10,
      rules: {
        correctWord: [
          { required: true, message: "请填写正确词", trigger: "change" },
        ],
        wrongWord: [
          { required: true, message: "请填写错误词", trigger: "change" },
        ],
      },
      dialogVisible2: false,
      radiovalue: "0",
      delid: "",
    };
  },
  computed: {
    ...mapGetters(["permissions"]),
  },
  created() {
    this.tem_ww_add = this.permissions["tem_ww_add"];
    this.tem_ww_del = this.permissions["tem_ww_del"];
    this.tem_ww_edit = this.permissions["tem_ww_edit"];
    this.tem_ww_downLoad = this.permissions["tem_ww_downLoad"];
  },
  mounted() {
    // 获取项目列表
    this.zkhousePage();
  },
  methods: {
    downLoad() {
      // this.searchForm
      //  exportMethodPost()
      exportMethodPost(
        "autoSR/zk/correct/indexExport",
        "常错词",
        this.searchForm
      );
    },

    edit(row) {
      this.ruleForm = Object.assign({}, row);
      this.editFlag = true;
      this.dialogVisible = true;
    },
    delSure() {
      axios({
        url:
          `/autoSR/zk/correct/deleteCorrectWord?id=` +
          this.delid +
          "&deleteType=" +
          this.radiovalue,
        method: "GET",
      }).then((res) => {
        if (res.code == 0) {
          this.$message.success("操作成功");
          this.dialogVisible2 = false;
          this.correctFindbypage();
        } else {
          this.$message.warning(res.resMsg);
        }
      });
    },
    del(row) {
      this.delid = row.id;
      this.dialogVisible2 = true;
      // this.$confirm(`是否删除该常错词?`, "提示", {
      //   confirmButtonText: "确定",
      //   cancelButtonText: "取消",
      //   distinguishCancelAndClose: true,
      //   type: "warning",
      // }).then(() => {
      //   axios({
      //     url: `/autoSR/zk/correct/del`,
      //     method: "post",
      //     data: {
      //       id: row.id,
      //     },
      //   }).then((res) => {
      //     if (res.code == 0) {
      //       this.$message.success("删除成功");
      //       this.correctFindbypage();
      //     } else {
      //       this.$message.warning(res.resMsg);
      //     }
      //   });
      // });
    },
    reset() {
      this.$refs.ruleForm.resetFields();
      this.ruleForm = {
        correctWord: "",
        wrongWord: "",
        memo: "",
      };
    },
    addSure() {
      // this.loadingFlag = true;
      this.ruleForm.correctWord = this.ruleForm.correctWord.replace(
        /[^\w\u4e00-\u9fa5]/g,
        ""
      );
      this.ruleForm.wrongWord = this.ruleForm.wrongWord.replace(
        /[^\w\u4e00-\u9fa5]/g,
        ""
      );
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          // 编辑
          if (this.editFlag) {
            console.log("编辑");

            if (this.mistakenList.indexOf(this.ruleForm.correctWord) != -1) {
              this.$message.error("正确词不能与错误词相同");
              return;
            }

            if (this.correctList.indexOf(this.ruleForm.wrongWord) != -1) {
              this.$message.error("错误词不能与正确词相同");
              return;
            }
            this.$api.api.correctUpdate(this.ruleForm).then((res) => {
              console.log(res);
              if (res.code == 0) {
                setTimeout(() => {
                  this.loadingFlag = false;
                  console.log("防重");
                }, 1);
                this.dialogVisible = false;
                this.$message.success("编辑成功");
                this.correctFindbypage();
              }
            });
          } else {
            // 添加

            if (this.correctList.indexOf(this.ruleForm.correctWord) != -1) {
              this.$message.error("正确词已添加过~");
              return;
            }

            if (this.mistakenList.indexOf(this.ruleForm.wrongWord) != -1) {
              this.$message.error("错误词已添加过~");
              return;
            }
            this.$api.api
              .correctAdd({
                houseId: this.searchForm.houseId,
                ...this.ruleForm,
              })
              .then((res) => {
                console.log(res);
                if (res.code == 0) {
                  setTimeout(() => {
                    this.loadingFlag = false;
                    console.log("防重");
                  }, 1);
                  this.dialogVisible = false;
                  this.$message.success("新增常错词成功");
                  this.correctFindbypage();
                }
              });
          }
        }
      });
    },
    screen() {
      this.currentPage = 1;
      this.correctFindbypage();
    },
    clascreen() {
      this.searchForm.wrongWord = "";
      this.searchForm.correctWord = "";
      (this.searchForm.houseId =
        this.orgType == 3
          ? localStorage.getItem("houseId")
          : this.houseList[0].id),
        (this.currentPage = 1);
      this.correctFindbypage();
    },
    houseChange() {
      this.correctFindbypage();
    },
    zkhousePage() {
      this.$api.api
        .findHouseByUser({
          orgType: localStorage.getItem("orgType"),
        })
        .then((res) => {
          // console.log(res)
          this.houseList = res.data;
          if (localStorage.getItem("orgType") == 3) {
            this.searchForm.houseId = localStorage.getItem("houseId");
          } else {
            this.searchForm.houseId = res.data[0].id;
          }
          // this.searchForm.houseId = res.data[0].id;
          // 列表获取
          this.correctFindbypage();
        });
    },
    // 获取常错词列表
    correctFindbypage() {
      let obj = {
        current: this.currentPage,
        size: this.size,
        ...this.searchForm,
      };
      this.$api.api.correctFindbypage(obj).then((res) => {
        console.log(res);
        this.tableData = res.data.records;
        this.mistakenList = res.data.records.map((item) => {
          return item.wrongWord;
        });
        this.correctList = res.data.records.map((item) => {
          return item.correctWord;
        });
        this.total = res.data.total;

        console.log(this.mistakenList, this.correctList);
      });
    },
    addinfo() {
      this.editFlag = false;
      this.dialogVisible = true;
    },
    editor() {
      this.dialogVisible = false;
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.size = val;
      this.correctFindbypage();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.currentPage = val;
      this.correctFindbypage();
    },
  },
};
</script>

<style scoped="scoped" lang="scss" >
.box-center {
  width: 100%;
  padding: 15px;
  min-width: 1000px;
  padding-bottom: 100px;
}
.delclass {
  width: 400px;
  // border: 1px solid red;
  margin: 0 auto;
}
.cen-tab {
  width: 100%;
  padding: 15px;
  background: #ffffff;
  margin-top: 15px;
}
.tophove {
  color: #ffffff;
  background: #2671e2;
}
.app-top {
  width: 100%;
  background: #ffffff;
  box-shadow: 0px 0px 12px 0px rgba(0, 0, 0, 0.04);
  border-radius: 4px;
  padding-top: 15px;
  padding-bottom: 15px;
  .app-titel {
    width: 100%;
    display: flex;
    align-items: center;
    .label {
      font-size: 14px;
      font-weight: 400;
      color: #32363d;
      line-height: 14px;
      width: 80px;
      margin-left: 15px;
      text-align: right;
    }
    .toptimeqhuan {
      height: 30px;
      background: #ffffff;
      display: flex;
      align-items: center;
    }
    .toptimeqhuan div {
      padding-left: 20px;
      padding-right: 20px;
      text-align: center;
      line-height: 30px;
      font-size: 14px;
      margin-right: 15px;
      border-radius: 4px;
      border: 1px solid #e0e0e0;
    }
  }
}
</style>
