<template>
  <div class="box-center">
    <!-- 头 -->
    <div class="app-top">
      <div class="app-titel" style="margin-top: 5px">
        <div class="label" style="color: #32363d; font-weight: 400" v-if="orgType!=3">
          楼盘选择：
        </div>
        <div v-if="orgType!=3">
          <el-select
            v-model="houseId"
            @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 class="label">销讲词：</div>
        <div>
          <el-input v-model="words"></el-input>
        </div>
        <div style="margin-left: 20px">
          <el-button
            style="background: #2671e2; color: #ffffff"
            @click="screening()"
            >筛选</el-button
          >
        </div>
        <div style="margin-left: 20px">
          <el-button
            @click="editorinfo()"
            style="background: #2671e2; color: #ffffff"
            >新增</el-button
          >
        </div>
      </div>
    </div>

    <!-- 表格 -->
    <div class="cen-tab">
      <el-table :data="tableData" stripe height="400" style="width: 100%">
        <el-table-column prop="words" label="敏感词" align="center">
        </el-table-column>
        <el-table-column prop="houseName" label="楼盘名称" align="center">
        </el-table-column>
        <el-table-column prop="createDate" label="创建时间" align="center">
        </el-table-column>

        <el-table-column prop="updateDate" label="修改时间" align="center">
        </el-table-column>
        <!--  scope -->
        <el-table-column label="操作" align="center">
          <template slot-scope="scope" style="">
            <span style="color: #2671e2" @click="bianji(scope.row)">编辑</span>
            <span
              style="color: #2671e2; margin-left: 10px"
              @click="del(scope.row)"
              >删除</span
            >
          </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="currentPage4"
          :page-sizes="[6, 12, 18, 24]"
          :page-size="6"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>

    <el-dialog
      :title="editFlag ? '编辑' : '新增'"
      :visible.sync="dialogVisible"
    >
      <el-form
        :model="ruleForm"
        label-position="labelPosition"
        :rules="ruleser"
        ref="ruleForm"
        label-width="140px"
        style="width: 60%; margin: 0 auto"
      >
        <el-form-item label="禁忌词" prop="words">
          <el-input v-model="ruleForm.words"></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="editor()">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentPage4: 1,
      value: "",
      input: "",
      tableData: [],
      multipleSelection: [],
      dialogVisible: false,
      orgType:localStorage.getItem('orgType'),
      ruleForm: {
        words: "",
        id: "",
      },
      houseId: "",
      houseList: [],
      Page: 1,
      size: 6,
      words: "",
      total: 0,
      ruleser: {
        words: [{ required: true, message: "请填写禁忌词", trigger: "blur" }],
      },
      editFlag: false,
    };
  },
  mounted() {
    this.zkhousePage();
  },
  methods: {
    houseChange() {
      this.Page = 1;
      this.words = "";
      this.taboofindbypagelist();
    },
    zkhousePage() {
      this.$api.api
        .findHouseByUser({
          orgType: localStorage.getItem("orgType"),
        })
        .then((res) => {
          this.houseList = res.data;
          if (localStorage.getItem("orgType") == 3) {
            this.houseId = localStorage.getItem("houseId");
          } else {
            this.houseId = res.data[0].id;
          }
          // this.houseId = res.data[0].id;
          this.taboofindbypagelist();
        });
    },
    taboofindbypagelist() {
      let obj = {
        current: this.Page,
        size: this.size,
        houseId: this.houseId,
        words: this.words,
      };
      this.$api.http.taboofindbypage(obj).then((res) => {
        console.log(res);
        this.tableData = res.data.records;
        this.total = res.data.total;
      });
    },
    screening() {
      this.Page = 1;
      this.taboofindbypagelist();
    },
    //删除
    del(item) {
      this.$confirm("此操作将永久删除, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        distinguishCancelAndClose: true,
        type: "warning",
      })
        .then(() => {
          this.$api.http.taboodelete({ id: item.id }).then((res) => {
            if (res.code == 0) {
              this.$message({
                type: "success",
                message: "删除成功!",
              });
              this.Page = 1;
              this.words = "";
              this.taboofindbypagelist();
            } else {
              this.$message.error(res.message);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    //编辑
    bianji(row) {
      this.ruleForm.id = row.id;
      this.dialogVisible = true;
      this.ruleForm.words = row.words;
      this.editFlag = true;
    },

    editorinfo() {
      this.dialogVisible = true;
      this.editFlag = false;
    },
    editor() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          if (this.editFlag == false) {
            this.$api.http
              .tabooadd({
                houseId: this.houseId,
                words: this.ruleForm.words,
              })
              .then((res) => {
                if (res.code == 0) {
                  this.dialogVisible = false;
                  this.$refs.ruleForm.resetFields();
                  this.Page = 1;
                  this.words = "";
                  this.taboofindbypagelist();
                } else {
                  this.$message.error(res.message);
                }
              });
          } else {
            this.$api.http
              .tabooupdate({
                id: this.ruleForm.id,
                words: this.ruleForm.words,
              })
              .then((res) => {
                if (res.code == 0) {
                  this.dialogVisible = false;
                  this.$refs.ruleForm.resetFields();
                  this.Page = 1;
                  this.words = "";
                  this.taboofindbypagelist();
                } else {
                  this.$message.error(res.message);
                }
              });
          }
        } else {
          return false;
        }
      });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.size = val;
      this.taboofindbypagelist();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.Page = val;
      this.taboofindbypagelist();
    },
  },
};
</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;
      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>
