<template>
  <div class="sy-content">
    <el-row>
      <el-col :span="24">
        <!-- 表单查询 -->
        <el-form ref="queryForm" :model="queryParams" :inline="true" size="small">
          <el-form-item label="部门名称" prop="departmentName">
            <el-input v-model="queryParams.departmentName" placeholder="请输入部门名称" clear />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handelSearch">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="handelReset">重置</el-button>
          </el-form-item>
        </el-form>

        <el-button size="mini" icon="el-icon-plus" type="primary" plain @click="handelAdd">新增</el-button>
        <el-button
          type="danger"
          plain
          icon="el-icon-circle-close"
          size="mini"
          :disabled="multiple"
          @click="handelDelAll"
        >批量删除</el-button>

        <!-- 表格数据信息 -->
        <el-table v-loading="loading" :data="deptList" row-key="id" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column key="departmentName" label="部门名称" align="center" prop="departmentName" />
          <el-table-column key="departmentDescribe" label="部门描述" align="center" prop="departmentDescribe" />
          <el-table-column key="departmentNum" label="排序号" align="center" prop="departmentNum" />
          <el-table-column key="updateTime" label="更新时间" align="center" prop="updateTime" />
          <el-table-column key="createTime" label="创建时间" align="center" prop="createTime" />
          <el-table-column label="操作" align="center" width="250" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-edit" @click="handelEdit(scope.row.id)">修改
              </el-button>
              <el-button size="mini" type="text" icon="el-icon-delete" @click="handelDel(scope.row.id)">删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>

        <!-- 分页信息 -->
        <pagination
          v-show="total > 0"
          :total="total"
          :page.sync="queryParams.page"
          :limit.sync="queryParams.limit"
          align="right"
          @pagination="getDeptList"
        />
      </el-col>
    </el-row>

    <!-- 弹出框 -->
    <el-dialog :title="title" :visible.sync="dialogVisible" width="35%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="上级部门" prop="parentId">
              <selectTree
                v-if="options.length"
                :options="options"
                placeholder="请选择上级部门"
                @handelGetValue="handelGetValue"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="部门名称" prop="departmentName">
              <el-input v-model="form.departmentName" clearable placeholder="请输入部门名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="部门描述" prop="departmentDescribe">
              <el-input v-model="form.departmentDescribe" clearable placeholder="请输入部门描述" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="显示排序" prop="departmentNum">
              <el-input-number
                v-model="form.departmentNum"
                placeholder="0"
                controls-position="right"
                :min="1"
                :max="10"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handelSubmit('form')">确 定</el-button>
        <el-button @click="handelCancel('form')">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getDeptList, addDept, getDeptById, editDept, delDept, getAllSonMenuList } from '@/api/system/dept.js'
import selectTree from '@/components/SelectTree'
import XEUtils from 'xe-utils'
export default {
  components: { selectTree },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 查询参数
      queryParams: {
        page: 1,
        limit: 10
      },
      // 总条数
      total: 0,
      // 表格数据
      deptList: [],
      // 弹出框
      dialogVisible: false,
      // 弹出框标题
      title: '',
      // 弹出框表单
      form: {},
      // 菜单类别
      options: [],
      // 表单验证
      rules: {
        departmentName: [
          { required: true, message: '部门单名称不能为空', trigger: 'blur' }
        ],
        departmentDescribe: [
          { required: true, message: '部门描述不能为空', trigger: 'blur' }
        ],
        departmentNum: [
          { required: true, message: '排序不能为空', trigger: ['change', 'blur'] }
        ]
      },
      // 修改菜单id
      id: null,
      // 控制批量删除按钮
      multiple: true,
      // 删除部门id集合
      ids: [],
      name: null
    }
  },
  created() {
    this.handelSearch()
  },
  methods: {
    /** 获取表格数据 */
    getDeptList() {
      getDeptList(this.queryParams).then(res => {
        if (res.code === 200) {
          const data = res.data
          this.deptList = XEUtils.toArrayTree(data.departmentList)
          this.total = data.count
        }
      })
    },
    /** 获取上级部门 */
    getOptionsList() {
      getAllSonMenuList().then(res => {
        const data = res.data
        this.$nextTick(() => {
          this.$delTreeChildrenNode(data)
          this.options = data
        })
      })
    },
    /** 搜索 */
    handelSearch() {
      this.queryParams.page = 1
      this.getDeptList()
    },
    /** 重置按钮操作 */
    handelReset() {
      this.$refs['queryForm'].resetFields()
      this.handelSearch()
    },
    /** 新增按钮 */
    handelAdd() {
      this.getOptionsList()
      this.dialogVisible = true
      this.title = '添加部门'
      this.reset()
    },
    /** 修改按钮 */
    handelEdit(id) {
      this.id = id
      getDeptById({ id }).then(res => {
        this.form = res.data
      })
      this.dialogVisible = true
      this.title = '修改部门'
    },
    /** 表格多选 */
    handleSelectionChange(val) {
      this.ids = val.map(item => item.id)
      this.multiple = !val.length
    },
    /** 删除按钮 */
    handelDel(id) {
      this.$confirm('是否删除该部门?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delDept([id]).then(res => {
          if (res.code === 200) {
            this.$message({
              type: 'success',
              message: '删除成功'
            })
            this.getDeptList()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    /** 批量删除 */
    handelDelAll() {
      this.$confirm('是否批量删除部门?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delDept(this.ids).then(res => {
          if (res.code === 200) {
            this.$message({
              type: 'success',
              message: '删除成功'
            })
            this.getDeptList()
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    /** 选择上级部门给表单赋值 */
    handelGetValue(val) {
      this.form.parentId = val || 0
    },
    /** 表单提交 */
    handelSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.title === '添加部门') {
            addDept(this.form).then(res => {
              if (res.code === 200) {
                this.$message({
                  message: res.data,
                  type: 'success'
                })
                this.getDeptList()
                this.dialogVisible = false
              }
            })
          } else {
            this.form.id = this.id
            editDept(this.form).then(res => {
              if (res.code === 200) {
                this.$message({
                  message: res.data,
                  type: 'success'
                })
                this.getDeptList()
                this.dialogVisible = false
              }
            })
          }
        }
      })
    },
    /** 表单取消 */
    handelCancel() {
      this.dialogVisible = false
      this.reset()
    },
    /** 表单重置 */
    reset() {
      if (this.$refs['form']) {
        this.$refs['form'].resetFields()
      }
    }
  }
}
</script>

<style lang="scss" rel="stylesheet/scss" scoped>

</style>
