<template>
  <div class="dept-menu">
    <el-form :inline="true" :model="dataForm" size="mini" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input 
          v-model="dataForm.name"
          clearable
          placeholder="输入部门名称搜索">
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="dataForm.isUsed"
          clearable
          placeholder="状态">
          <el-option v-for="item in enabledTypeOptions" :key="item.key" :label="item.displayName" :value="item.key" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('sys:dept:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
        <el-button v-if="isAuth('sys:dept:delete')" type="danger" @click="deleteHandle(1)" :disabled="dataListSelections.length <= 0">批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      v-loading="dataListLoading"
      border
      size="mini"
      lazy
      :load="getDeptDatas"
      :default-sort="{prop: 'deptSort', order: 'ascending'}"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
      :data="dataList"
      row-key="deptId"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column
        prop="name"
        header-align="center"
        align="center"
        label="名称">
      </el-table-column>
      <el-table-column
        prop="deptSort"
        header-align="center"
        align="center"
        label="排序">
      </el-table-column>
      <el-table-column
        prop="isUsed"
        header-align="center"
        align="center"
        label="状态">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isUsed"
            :disabled="scope.row.id === 1"
            active-color="#409EFF"
            inactive-color="#F56C6C"
            @change="changeEnabled(scope.row, scope.row.isUsed)"
          />
        </template>
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建日期">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button v-if="isAuth('sys:dept:update')" type="text" size="small" @click="addOrUpdateHandle(scope.row)">修改</el-button>
          <el-button v-if="isAuth('sys:dept:delete')" type="text" size="small" @click="deleteHandle(2, scope.row.deptId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from './dept-add-or-update'
export default {
  data () {
    return {
      dataForm: {
        name: null,
        isUsed: null
      },
      enabledTypeOptions: [
        { key: true, displayName: '启用' },
        { key: false, displayName: '禁用' }
      ],
      dataList: [],
      pageIndex: 0,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate
  },
  activated () {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList () {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/api/dept/vo'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'size': this.pageSize,
          'sort': 'create_time',
          'name': this.dataForm.name,
          'isUsed': this.dataForm.isUsed
        })
      }).then(({data}) => {
        if (data && data.code === 200) {
          this.dataList = data.content
          this.totalPage = data.totalElements
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 表格渲染  :load="getDeptDatas" 点击事件 row-key需要指定唯一的数据id
    getDeptDatas (tree, treeNode, resolve) {
      setTimeout(() => {
        this.$http({
          url: this.$http.adornUrl('/api/dept/vo'),
          method: 'get',
          params: this.$http.adornParams({
            'pid': tree.deptId
          })
        }).then(({data}) => {
          resolve(data.content)
        })
      }, 100)
    },
    // 每页数
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 0
      this.getDataList()
    },
    // 当前页
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle (row) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(row)
      })
    },
    // 删除
    deleteHandle (type, id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.deptId
      })
      this.$confirm(type === 1 ? `确认删除选中的${this.dataListSelections.length}条数据?` : '确定删除吗,如果存在下级节点则一并删除，此操作不能撤销！', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/api/dept'),
          method: 'DELETE',
          data: ids
        }).then(({data}) => {
          if (data && data.code === 200) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch(() => {})
    }
  }
}
</script>