<template>
  <div class="app-container">
    <div class="mymain-container">
      <div class="table">
        <el-table
          :data="dataList"
          row-key="adminWebsitId"
          border
          default-expand-all
          :tree-props="{ children: 'children' }"
        >
          <el-table-column prop="name" label="部门名称"></el-table-column>
          <el-table-column prop="mainNames" label="业务范围" align="center"></el-table-column>

          <el-table-column align="center" label="状态" class-name="status-col" width="80">
            <template slot-scope="scope">
              <el-tag :type="scope.row.status ? 'success' : 'danger'">{{ scope.row.status ? '启用' : '禁用' }}</el-tag>
            </template>
          </el-table-column>
          <el-table-column label="操作" width="268" fixed="right">
            <template slot-scope="scope">
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-plus"
                @click="openMainForm('add', scope.row.adminWebsitId)"
                >添加子部门</el-button
              >
              <el-button
                type="primary"
                size="mini"
                icon="el-icon-edit"
                v-if="scope.row.parentId > 0"
                @click="openMainForm('edit', scope.row.adminWebsitId)"
                >编辑</el-button
              >
              <el-popconfirm
                style="margin-left: 10px"
                title="确定删除吗？"
                v-if="scope.row.parentId > 0"
                @onConfirm="handleDelete(scope.row.adminWebsitId)"
              >
                <el-button slot="reference" size="mini" icon="el-icon-delete">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
      </div>

      <div class="pagination clearfix">
        <div class="fr">
          <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="currentPage"
            :page-sizes="[10, 20, 30, 50]"
            :page-size="10"
            layout="total, sizes, prev, pager, next, jumper"
            :total="listTotal"
          >
          </el-pagination>
        </div>
      </div>
    </div>

    <!-- 添加编辑部门 -->
    <el-dialog
      :title="mainFormType == 'add' ? '添加部门' : '编辑部门'"
      :visible.sync="mainFormVisible"
      :show-close="false"
      width="40%"
      :close-on-click-modal="false"
    >
      <el-form ref="mainForm" :model="mainForm" :rules="mainFormRules" label-position="right" label-width="80px">
        <el-form-item label="上级菜单" prop="pid">
          <el-cascader
            style="width: 100%"
            :options="dataList"
            :props="{ checkStrictly: true, value: 'adminWebsitId', label: 'name' }"
            v-model="mainForm.pid"
            filterable
            clearable
          >
          </el-cascader>
        </el-form-item>
        <el-form-item label="部门名称" prop="name">
          <el-input placeholder="请输入部门名称" v-model="mainForm.name"></el-input>
        </el-form-item>
        <el-form-item label="业务范围" prop="work">
          <el-select v-model="mainForm.work" multiple placeholder="请选择" style="width: 100%">
            <el-option v-for="item in workList" :key="item.dictCode" :label="item.dictValue" :value="item.dictCode">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-radio-group v-model="mainForm.status">
            <el-radio :label="true">启用</el-radio>
            <el-radio :label="false">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelMainForm">取 消</el-button>
        <el-button type="primary" @click="submitMainForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { getDepartmentList, addDepartment, editDepartment, getDepartmentDetail, deleteDepartment } from '@/api/setting'
import { getDictList } from '@/api/common'
import { findElem } from '@/utils/util'

export default {
  data() {
    return {
      dataList: [], // 列表数据
      listLoading: true, // 列表加载loading
      currentPage: 1, // 当前页码
      pageSize: 10, // 每页数量
      listTotal: 0, // 列表总数

      editId: null,
      mainFormType: 'add',
      mainFormVisible: false,
      mainForm: {
        pid: '',
        name: '',
        work: '',
        status: true
      },
      mainFormRules: {
        name: [{ required: true, message: '请填写部门名称', trigger: 'blur' }],
        work: [{ required: true, message: '请选择业务范围', trigger: 'change' }]
      },

      workList: []
    }
  },

  created() {
    this.getList()
  },

  methods: {
    // 查询按钮权限
    checkBtnRole(value) {
      // let btnRole = this.$route.meta.roles;
      // if(!btnRole) {return true}
      // let index = btnRole.indexOf(value);
      // return index >= 0 ? true : false;
      return true
    },

    // 获取部门列表
    getList() {
      getDepartmentList().then(res => {
        this.dataList = res.data
        this.listTotal = res.data.length
      })
    },

    // 更改每页数量
    handleSizeChange(val) {
      this.pageSize = val
      this.currentPage = 1
      this.getList()
    },

    // 更改当前页
    handleCurrentChange(val) {
      this.currentPage = val
      this.getList()
    },

    // 操作 - 删除
    handleDelete(id) {
      deleteDepartment({ id: id }).then(res => {
        this.getList()
        this.$successMsg()
      })
    },

    getDictList() {
      getDictList({ sysDictEnum: 'PRODUCT_TYPE' }).then(res => {
        this.workList = res.data
      })
    },

    // 打开 新增编辑 部门表单
    openMainForm(type, id) {
      this.mainFormType = type
      this.mainFormVisible = true
      this.getDictList()

      if (type == 'add') {
        this.mainForm.pid = id
      } else {
        this.editId = id
        getDepartmentDetail({ id }).then(res => {
          this.mainForm = {
            pid: res.data.parentId,
            name: res.data.name,
            work: res.data.mainIds,
            status: res.data.status
          }
        })
      }
    },

    // 取消 新增编辑 部门表单
    cancelMainForm() {
      this.mainFormVisible = false
      this.$refs.mainForm.resetFields()
      this.mainForm.name = ''
    },

    // 提交 部门表单
    submitMainForm() {
      this.$refs.mainForm.validate(valid => {
        if (valid) {
          let parentId = null
          if (this.mainForm.pid instanceof Array) {
            parentId = this.mainForm.pid[this.mainForm.pid.length - 1]
          } else {
            parentId = this.mainForm.pid
          }
          let params = {
            parentId,
            name: this.mainForm.name,
            mainIds: this.mainForm.work,
            status: this.mainForm.status
          }
          if (this.mainFormType == 'edit') {
            params.adminWebsitId = this.editId
            editDepartment(params).then(res => {
              this.cancelMainForm()
              this.getList()
              this.$successMsg('编辑成功')
            })
          } else {
            addDepartment(params).then(res => {
              this.cancelMainForm()
              this.getList()
              this.$successMsg('添加成功')
            })
          }
        }
      })
    }
  }
}
</script>

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