<template>
  <div id="department">
    <search-box :formList="formList" @search="search" @init="init" />
    <table-list
      :tableSets="tableSet"
      :tableData="tableData"
      :navText="navText"
      @addClick="addClick"
      :loading="loading"
      :rowKey="rowKey"
    >
      <template slot="btn" slot-scope="props">
        <el-button type="text" @click="addClick(props.scope.row, 2)"
          >新增</el-button
        >
        <el-button type="text" @click="addClick(props.scope.row, 0)"
          >修改</el-button
        >
        <el-button type="text" @click="del(props.scope.row)">删除</el-button>
      </template>
    </table-list>
    <!-- <pagination
      :total="total"
      :pageSize="pageSize"
      :currentPage="currentPage"
      @handleSizeChange="handleSizeChange"
      @handleCurrentChange="handleCurrentChange"
    /> -->
    <el-dialog
      :visible.sync="dialogVisible"
      width="480px"
      :show-close="false"
      :before-close="close"
    >
      <div slot="title" class="hearders">
        <div class="leftTitle">
          {{ statusPop === 1 ? "添加" : statusPop === 0 ? "修改" : "添加下级" }}
        </div>
        <div class="rightBoxs">
          <img src="@/assets/images/Close@2x.png" alt="" @click="close" />
        </div>
      </div>
      <div>
        <el-form
          label-position="right"
          label-width="80px"
          :model="listData"
          :rules="rules"
          ref="listData"
        >
          <el-form-item
            v-for="(items, indexs) in listitem"
            :key="indexs"
            :label="items.label"
            :prop="items.prop"
          >
            <el-radio-group
              v-if="items.scope === 'status'"
              v-model="listData[items.prop]"
            >
              <el-radio
                v-for="(item, index) in items.options" 
                :key="index"
                :label="item.value"
                >{{ item.label }}</el-radio
              >
            </el-radio-group>
            <treeselect
              v-else-if="items.scope === 'selectMore'"
              v-model="listData[items.prop]"
              :options="optionsCascader"
              :normalizer="normalizer"
              :show-count="true"
              placeholder="选择上级菜单"
            />
            <el-input-number
              style="width: 100%"
              v-else-if="items.scope === 'numberIndex'"
              v-model="listData[items.prop]"
              controls-position="right"
              :min="0"
              :max="99"
            ></el-input-number>
            <el-input v-else v-model="listData[items.prop]"></el-input>
          </el-form-item>
        </el-form>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="close">取 消</el-button>
        <el-button type="primary" @click="submit('listData')">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

import searchBox from "@/components/searchBox";
import tableList from "@/components/tableList";
import pagination from "@/components/pagination";
export default {
  components: { Treeselect, searchBox, tableList, pagination },
  data() {
    return {
      loading: false, //当前表单加载是否加载动画
      rowKey: "deptId",
      navText: {
        title: "总数据",
        index: 0,
        ch: "条",
        num: false,
        choice: false,
        addHide: false,
        backFatherBtn: {
          status: false,
          title: "未定义",
        },
      },
      //搜索
      formList: [
        {
          label: "部门名称",
          prop: "deptName",
          placeholder: "请输入部门名称",
        },
        {
          label: "状态",
          prop: "status",
          placeholder: "请选择状态",
          scope: "select",
          options: [
            {
              label: "启用",
              value: 1,
            },
            {
              label: "停用",
              value: 0,
            },
          ],
        },
      ],
      // 表单
      tableSet: [
        {
          label: "部门名称",
          prop: "deptName",
          hidden: true,
        },
        {
          label: "排序",
          prop: "orderNum",
          hidden: true,
        },
        {
          label: "状态",
          prop: "status",
          hidden: true,
          scope: "status",
        },
        {
          label: "创建时间",
          prop: "createTime",
          hidden: true,
        },
      ],
      tableData: [], //表单数据
      total: 0, //一共多少条
      pageSize: 10, //每页多少条数据
      currentPage: 1, //当前页码
      // 弹窗字段
      listitem: [
        {
          label: "上级部门",
          prop: "parentId",
          scope: "selectMore",
        },
        {
          label: "部门名称",
          prop: "deptName",
        },
        {
          label: "显示排序",
          prop: "orderNum",
          scope: "numberIndex",
        },
        {
          label: "负责人",
          prop: "leader",
        },
        {
          label: "联系电话",
          prop: "phone",
        },
        {
          label: "邮箱",
          prop: "email",
        },
        {
          label: "部门状态",
          prop: "status",
          scope: "status",
          options: [
            {
              label: "启用",
              value: "1",
            },
            {
              label: "关闭",
              value: "0",
            },
          ],
        },
      ],
      //   弹窗数据
      listData: {},
      statusPop: -1,
      dialogVisible: false,
      optionsCascader: [], //弹窗分类数据
      //表单验证
      rules: {
        parentId: [
          { required: true, message: "请选择上级部门", trigger: "change" },
        ],
        deptName: [
          { required: true, message: "请输入部门名称", trigger: "blur" },
        ],
        orderNum: [
          { required: true, message: "请输入显示排序", trigger: "blur" },
        ],
        // leader: [{ required: true, message: "请填写负责人", trigger: "blur" }],
        status: [
          { required: true, message: "请选择部门状态", trigger: "change" },
        ],
      },
    };
  },
  mounted() {
    this.search();
  },
  methods: {
    search(v) {
      this.loading = true;
      if (v === undefined) {
        v = {
          statusArray: "0,1",
          pageSize: this.pageSize,
          pageNum: this.currentPage,
        };
      }
      var data = {
        deptName: v.deptName || "",
        statusArray: v.status === undefined ? "0,1" : v.status,
        pageSize: this.pageSize,
        pageNum: this.currentPage,
      };
      this.$api
        .obtainDeptList(data)
        .then((res) => {
          this.initSelect();
          this.tableData = this.handleTree(
            res.data,
            "deptId",
            "parentId",
            "children"
          );
          this.total = res.data.length;
          this.navText.index = res.data.length;
          this.loading = false;
        })
        .catch((err) => {
          this.loading = false;
        });
    },
    initSelect() {
      var data = {
        deptName: "",
        statusArray: "0,1",
        pageSize: this.pageSize,
        pageNum: this.currentPage,
      };
      this.$api
        .obtainDeptList(data)
        .then((res) => {
          var bf = [
            {
              deptId: 0,
              parentId: null,
              deptName: "主分类",
            },
          ];
          bf = bf.concat(res.data);
          this.optionsCascader = this.handleTree(
            bf,
            "deptId",
            "parentId",
            "children"
          );
        })
        .catch((err) => {});
    },
    init() {
      this.search();
    },
    del(v) {
      this.$confirm("此操作将删除该条部门数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          var data = v;
          data.status = -1;
          this.$api.editDept(data).then((res) => {
            if (res.code === 200) {
              this.$message.success("删除成功");
              this.search();
              this.dialogVisible = false;
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    addClick(v, int) {
      if (v === undefined) {
        this.statusPop = 1;
        this.listData = {};
      } else {
        this.statusPop = int;
        if (int === 2) {
          this.listData = {
            parentId: v.deptId,
          };
        } else {
          var data = v.deptId;
          this.$api.obtainDeptId(data).then((res) => {
            this.listData = res.data;
          });
        }
      }
      this.dialogVisible = true;
    },
    submit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.rulesTableSumbit();
        } else {
          return false;
        }
      });
    },
    rulesTableSumbit() {
      var data = this.listData;
      if (this.statusPop === 0) {
        this.$api.editDept(data).then((res) => {
          if (res.code === 200) {
            this.$message.success("修改成功");
            this.search();
            this.dialogVisible = false;
          }
        });
      } else {
        this.$api.addDept(data).then((res) => {
          if (res.code === 200) {
            this.$message.success("新增成功");
            this.search();
            this.dialogVisible = false;
          }
        });
      }
    },
    close() {
      this.$confirm("是否关闭该窗口", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.dialogVisible = false;
        })
        .catch(() => {});
    },
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.deptId,
        label: node.deptName,
        children: node.children,
      };
    },
    handleSizeChange(v) {
      this.pageSize = v;
      this.currentPage = 1;
      this.search();
    },
    handleCurrentChange(v) {
      this.currentPage = v;
      this.search();
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.el-button {
  border-radius: 8px;
}
/deep/.el-dialog {
  border-radius: 8px;
  .el-dialog__header {
    padding: 0;
    .hearders {
      height: 40px;
      display: flex;
      align-items: center;
      justify-content: space-between;
      padding: 0px 18px 0px 20px;
      border-bottom: 1px solid #e2e2e2;
      .leftTitle {
        font-size: 14px;
        font-weight: bold;
        color: #2f4378;
      }
      .rightBoxs {
        display: flex;
        align-items: center;
        img {
          width: 14px;
          height: 14px;
          margin-left: 13px;
          cursor: pointer;
        }
      }
    }
  }
  .el-dialog__footer {
    padding: 0;
    .dialog-footer {
      padding: 0px 40px;
      height: 70px;
      border-top: 1px solid #e2e2e2;
      display: flex;
      align-items: center;
      justify-content: flex-end;
    }
  }
}
.imgBox {
  width: 100%;
  // height: 210px;
  border: 1px solid #e2e2e2;
  border-radius: 8px;
  padding: 8px 8px 3px;
  display: flex;
  flex-direction: column;
  align-items: center;
  .imgLabel {
    flex: 1;
    width: 100%;
    border: 1px dotted #e2e2e2;
    color: #999;
    font-size: 14px;
    cursor: pointer;
    border-radius: 8px;
    .msPhoto {
      display: flex;
      justify-content: center;
      align-items: center;
      max-width: 100%;
      max-height: 270px;
      img {
        max-width: 100%;
        max-height: 270px;
      }
    }
    .imgbbx {
      display: flex;
      flex-direction: column;
      align-items: center;
      justify-content: center;
      width: 100%;
      height: 100%;
      i {
        font-weight: bold;
        margin: 14px 0;
        font-size: 24px;
      }
    }
  }
  p {
    margin: 5px 0px;
  }
}
</style>

