<template>
  <div class="box-center">
    <!-- 头 -->
    <div class="app-top">
      <div class="app-titel" style="margin-top: 5px">
        <div style="margin-left: 20px">
          <el-button
            @click="addinfo()"
            style="background: #2671e2; color: #ffffff"
            >新增</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="name" label="名称" align="center">
        </el-table-column>
        <el-table-column prop="permission" 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>

    <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="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="描述：" prop="permission">
          <el-input type="textarea" v-model="ruleForm.permission"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addSure()">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      currentPage: 4,
      orgType:localStorage.getItem('orgType'),
      value: "",
      input: "",
      tableData: [],
      multipleSelection: [],
      dialogVisible: false,
      houseList: [],
      ruleForm: {
        name: "",
        permission: "",
      },
      searchForm: {
        correctWord: "",
        wrongWord: "",
        houseId: "",
      },
      currentPage: 1,
      editFlag: false,
      size: 10,
      total: 10,
      rules: {
        name: [
          { required: true, message: "请填写名称", trigger: "change" },
        ],
      },
    };
  },
  computed: {
    ...mapGetters(["permissions"]),
  },
  created() {

  },
  mounted() {
    // 获取楼盘列表
    // this.zkhousePage();
    this.correctFindbypage();
  },
  methods: {
    edit(row) {
      this.ruleForm = Object.assign({}, row);
      this.editFlag = true;
      this.dialogVisible = true;
    },
    del(row) {
      console.log(row, "删除");
      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() {
      // console.log(this.addressOptions)
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          console.log(this.ruleForm);
          // 编辑
          // return;
          if (this.editFlag) {
            console.log("编辑");
            this.$api.api.correctUpdate(this.ruleForm).then((res) => {
              console.log(res);
              if (res.code == 0) {
                this.dialogVisible = false;
                this.$message.success("编辑成功");
                this.correctFindbypage();
              }
            });
          } else {
            // 添加
            console.log("添加");
            // return;
            this.$api.api
              .correctAdd({
                houseId: this.searchForm.houseId,
                ...this.ruleForm,
              })
              .then((res) => {
                console.log(res);
                if (res.code == 0) {
                  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.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() {
      this.$api.api.apiTree().then((res) => {
        console.log(res);
        this.tableData = res.data;
      });
    },
    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: 1200px;
  padding-bottom: 100px;
}
.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;
    }
    .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>
