<template>
  <div class="box-center" style="cursor: pointer">
    <!-- 头 -->
    <div class="app-top">
      <div class="app-titel" style="margin-top: 5px">
        <div style="margin-left: 20px" v-if="sys_dept_add">
          <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="name" label="部门名称" align="center">
        </el-table-column>
        <el-table-column prop="peopleCount" label="部门人数" align="center">
        </el-table-column>
        <!--  scope -->
        <el-table-column
          label="操作"
          v-if="sys_dept_edit || sys_dept_del"
          align="center"
        >
          <template slot-scope="scope" style="">
            <span
              v-if="sys_dept_edit"
              style="color: #2671e2"
              @click="bianji(scope.row)"
              >编辑</span
            >
            <span
              v-if="sys_dept_del"
              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="name">
          <el-input v-model="ruleForm.name" maxlength="20"></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>

    <el-dialog title="部门删除" :visible.sync="deptVisible">
      <el-form
        :model="deptForm"
        label-position="labelPosition"
        :rules="ruler"
        ref="deptForm"
        label-width="140px"
        style="width: 60%; margin: 0 auto"
      >
        <el-form-item label="转移部门" prop="newDeptId">
          <el-select
            v-model="deptForm.newDeptId"
            style="width: 80%"
            placeholder="请选择"
            filterable
          >
            <el-option
              v-for="item in deptList"
              :key="item.deptId"
              :label="item.name"
              :value="item.deptId"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="deptVisible = false">取 消</el-button>
        <el-button type="primary" @click="delSure()">保存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      currentPage4: 1,
      value: "",
      input: "",
      tableData: [],
      multipleSelection: [],
      deptList: [],
      dialogVisible: false,
      deptVisible: false,
      orgType: localStorage.getItem("orgType"),
      ruleForm: {
        name: "",
        id: "",
      },
      deptForm: {
        deptId: "",
        newDeptId: "",
      },
      houseId: "",
      houseList: [],
      Page: 1,
      size: 6,
      words: "",
      total: 0,
      ruleser: {
        name: [{ required: true, message: "请选择部门", trigger: "blur" }],
      },
      ruler: {
        newDeptId: [{ required: true, message: "请选择部门", trigger: "blur" }],
      },
      editFlag: false,
    };
  },
  computed: {
    ...mapGetters(["permissions"]),
  },
  created() {
    this.sys_dept_add = this.permissions["sys_dept_add"];
    this.sys_dept_edit = this.permissions["sys_dept_edit"];
    this.sys_dept_del = this.permissions["sys_dept_del"];
  },
  mounted() {
    // this.zkhousePage();
    this.houseId = localStorage.getItem("houseId");
    this.deptFindByPage();
  },
  methods: {
    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.deptFindByPage();
        });
    },
    deptFindByPage() {
      let obj = {
        current: this.Page,
        size: this.size,
        houseId: this.houseId,
        words: this.words,
      };
      this.$api.api.deptFindByPage(obj).then((res) => {
        console.log(res);
        this.tableData = res.data.records;
        this.hasData = res.data.records.map((item) => {
          return item.name;
        });
        console.log(this.hasData);
        this.total = res.data.total;
      });
    },
    screening() {
      this.Page = 1;
      this.deptFindByPage();
    },
    delSure() {
      this.$refs.deptForm.validate((valid) => {
        if (valid) {
          this.$api.api
            .deptDel({
              houseId: this.houseId,
              ...this.deptForm,
            })
            .then((res) => {
              console.log(res);
              this.deptFindByPage();
              this.$message.success("移除成功");
              this.deptVisible = false;
            });
        }
      });
    },
    //删除
    del(row) {
      this.deptForm.deptId = row.deptId;
      this.$api.api
        .deptFindList({
          houseId: this.houseId,
          deptId: row.deptId,
        })
        .then((res) => {
          console.log(res);
          this.deptList = res.data;
          if (this.deptList.length == 0) {
            // this.$message.warning('没有别的部门啦！')
            if (this.tableData[0].peopleCount > 0) {
              this.$message.warning("当前部门下还有用户！");
              return;
            } else {
              this.$api.api
                .deptDel({
                  houseId: this.houseId,
                  deptId: row.deptId,
                  // ...this.deptForm,
                })
                .then((res) => {
                  console.log(res);
                  this.deptFindByPage();
                  this.$message.success("删除成功");
                });
            }
          } else {
            this.deptVisible = true;
          }
        });
    },
    //编辑
    bianji(row) {
      this.ruleForm.deptId = row.deptId;
      this.dialogVisible = true;
      this.ruleForm.name = row.name;
      this.editFlag = true;
    },

    editorinfo() {
      this.ruleForm.name = "";
      this.dialogVisible = true;
      this.editFlag = false;
    },
    editor() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          if (this.hasData.indexOf(this.ruleForm.name) != -1) {
            this.$message.error("该部门已存在");
            return;
          }
          if (this.editFlag == false) {
            console.log(this.hasData);
            // 新增

            this.$api.api
              .deptAdd({
                houseId: this.houseId,
                name: this.ruleForm.name,
              })
              .then((res) => {
                if (res.code == 0) {
                  this.dialogVisible = false;
                  this.$refs.ruleForm.resetFields();
                  this.Page = 1;
                  this.deptFindByPage();
                } else {
                  this.$message.error(res.message);
                }
              });
          } else {
            // 修改
            this.$api.api
              .deptUpdate({
                deptId: this.ruleForm.deptId,
                name: this.ruleForm.name,
              })
              .then((res) => {
                if (res.code == 0) {
                  this.dialogVisible = false;
                  this.$refs.ruleForm.resetFields();
                  this.Page = 1;
                  this.deptFindByPage();
                } else {
                  this.$message.error(res.message);
                }
              });
          }
        } else {
          return false;
        }
      });
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.size = val;
      this.deptFindByPage();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.Page = val;
      this.deptFindByPage();
    },
  },
};
</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>
