<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <el-button class="filter-item" size="mini" type="success" icon="el-icon-edit" :disabled="crud.selections.length !== 1" @click="systemConfigClick(crud.selections[0])">配置子系统权限</el-button>
      <el-button class="filter-item" size="mini" type="success" icon="el-icon-edit" :disabled="crud.selections.length !== 1" @click="appMenuConfigClick(crud.selections[0])">配置APP菜单权限</el-button>

      <!--配置子系统权限-->
      <el-dialog title="配置子系统权限" :visible.sync="systemConfig">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-descriptions>
            <el-descriptions-item label="姓名">{{ form.name }}</el-descriptions-item>
            <el-descriptions-item label="员工工号">{{ form.userNumber }}</el-descriptions-item>
            <el-descriptions-item label="职务">{{ form.jobs && form.jobs.map(item => item.name).join(',') }}</el-descriptions-item>
            <el-descriptions-item label="所属部门">{{ form.dept.name }}</el-descriptions-item>
          </el-descriptions>
          <div>授权子系统权限:</div>
          <el-checkbox-group v-model="dmUserSystem">
            <el-checkbox v-for="dmSystem in dmSystemList" :key="dmSystem.id" :label="dmSystem.id" name="type" border>{{ dmSystem.systemName }}</el-checkbox>
          </el-checkbox-group>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="cancelSystemConfig">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="submitSystemConfig">确认</el-button>
        </div>
      </el-dialog>

      <!--配置app菜单权限-->
      <el-dialog title="配置app菜单权限" :visible.sync="appMenuConfig">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-descriptions>
            <el-descriptions-item label="姓名">{{ form.name }}</el-descriptions-item>
            <el-descriptions-item label="员工工号">{{ form.userNumber }}</el-descriptions-item>
            <el-descriptions-item label="职务">{{ form.jobs && form.jobs.map(item => item.name).join(',') }}</el-descriptions-item>
            <el-descriptions-item label="所属部门">{{ form.dept.name }}</el-descriptions-item>
          </el-descriptions>
          <div>授权子系统权限:</div>
          <el-checkbox-group v-model="dmUserAppMenu">
            <el-checkbox v-for="dmAppMenu in dmAppMenuList" :key="dmAppMenu.id" :label="dmAppMenu.id" name="type" border>{{ dmAppMenu.name }}</el-checkbox>
          </el-checkbox-group>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="cancelAppMenuConfig">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="submitAppMenuConfig">确认</el-button>
        </div>
      </el-dialog>

      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="name" label="姓名" />
        <el-table-column prop="type" label="人员种类">
          <template slot-scope="scope">
            {{ dict.label.dm_usertype[scope.row.type] }}
          </template>
        </el-table-column>

        <el-table-column prop="userNumber" label="员工工号" />
        <el-table-column prop="jobs" label="职务">
          <template slot-scope="{row, column}">
            {{ row[column.property] && row[column.property].map(item => item.name).join(',') }}
          </template>
        </el-table-column>
        <el-table-column prop="contacts" label="手机" />
        <el-table-column label="人员状态" align="center" prop="enabled">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.status"
              active-color="#409EFF"
              inactive-color="#F56C6C"
              :disabled="true"
            />
          </template>
        </el-table-column>
        <el-table-column prop="updateTime" label="最后更新时间" />
        <el-table-column v-if="checkPer(['admin','dmUser:edit','dmUser:del'])" label="操作" width="150px" align="center">
          <!--          <template slot-scope="scope">-->
          <!--            <el-button-->
          <!--              size="mini"-->
          <!--              type="primary"-->
          <!--              @click="openDetailsConfig(scope.row)"-->
          <!--            >详情</el-button>-->
          <!--          </template>-->
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudDmUser from '@/api/dm/user/dmUser'
import { getDmSystem } from '@/api/dm/system/dmSystem'
import { getDmAppMenu } from '@/api/dm/appMenu/dmAppMenu'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'

const defaultForm = { id: null, name: null, sex: null, contacts: null, email: null, deptId: null, dept: {}, isApprover: null, createBy: null, updateBy: null, createTime: null, updateTime: null, faceId: null, cardId: null, usercode: null, userNumber: null, status: null, type: null, picture: null, remark: null, isVisitorApproval: null, userRootGroupId: null, userRootGroupName: null }
const optShow = {
  add: false,
  edit: false,
  del: false,
  download: true,
  reset: false
}
export default {
  name: 'DmUser',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['dm_usertype'],
  cruds() {
    return CRUD({ title: '123', url: 'api/dmUser', idField: 'id', sort: 'id,desc', crudMethod: { ...crudDmUser }, optShow: optShow })
  },
  data() {
    return {
      permission: {
        add: ['admin', 'dmUser:add'],
        edit: ['admin', 'dmUser:edit'],
        del: ['admin', 'dmUser:del']
      },
      rules: {
      },
      dmSystemList: [],
      systemConfig: false,
      dmUserSystem: [],
      dmAppMenuList: [],
      appMenuConfig: false,
      dmUserAppMenu: []
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      return true
    },
    systemConfigClick(data) {
      this.form = data
      for (let i = 0; i < data.dmSystems.length; i++) {
        this.dmUserSystem.push(data.dmSystems[i].id)
      }
      console.log('this.dmUserSystem', this.dmUserSystem)
      this.systemConfig = true
      this.getDmSystemList()
    },
    // 获取所有子系统
    getDmSystemList() {
      getDmSystem().then(res => {
        this.dmSystemList = res.content
      })
    },
    cancelSystemConfig() {
      this.dmSystemList = []
      this.dmUserSystem = []
      this.systemConfig = false
    },
    submitSystemConfig() {
      console.log(this.dmUserSystem)

      const updateUserSystemData = {}
      updateUserSystemData.userId = this.form.id
      updateUserSystemData.systemIds = this.dmUserSystem
      crudDmUser.updateUserSystem(updateUserSystemData)
      this.dmSystemList = []
      this.dmUserSystem = []
      this.systemConfig = false
      this.crud.refresh()
    },
    appMenuConfigClick(data) {
      this.form = data
      console.log('data.dmAppMenus.length:', data.dmAppMenus.length)
      for (let i = 0; i < data.dmAppMenus.length; i++) {
        this.dmUserAppMenu.push(data.dmAppMenus[i].id)
      }
      console.log('this.dmUserAppMenu', this.dmUserAppMenu)
      this.appMenuConfig = true
      this.getDmAppMenuList()
    },
    getDmAppMenuList() {
      getDmAppMenu().then(res => {
        this.dmAppMenuList = res.content
      })
    },
    cancelAppMenuConfig() {
      this.dmAppMenuList = []
      this.dmUserAppMenu = []
      this.appMenuConfig = false
    },
    submitAppMenuConfig() {
      console.log(this.dmUserAppMenu)

      const updateUserAppMenuData = {}
      updateUserAppMenuData.userId = this.form.id
      updateUserAppMenuData.appMenuIds = this.dmUserAppMenu
      crudDmUser.updateUserAppMenu(updateUserAppMenuData)
      this.dmAppMenuList = []
      this.dmUserAppMenu = []
      this.appMenuConfig = false
      this.crud.refresh()
    }
  }
}
</script>

<style scoped>

</style>
