<!--
 * @Author: zwq
 * @Date: 2021-04-06 19:33:11
 * @LastEditors: zwq
 * @LastEditTime: 2022-03-10 16:54:48
 * @Description:
-->
<template>
  <el-container style="margin:30px">
    <el-aside>
      <side-tree v-if="menuList.length>0" :menu-list="menuList" @getOrganization="getOrganization" />
    </el-aside>
    <el-main style="border:2px solid #E4E4E4;border-radius:10px;margin-left:10px">
      <el-form
        ref="dataForm"
        :model="dataForm"
        :inline="true"
        size="medium"
        label-width="100px"
        label-position="left"
      >
        <el-form-item label-width="60px" :label="$t('userManage.orgName')+'：'">
          <span style="margin: 0 5px;color:#1890FF">{{ dataForm.orgName }}</span>
        </el-form-item>
        <el-form-item :label="$t('userManage.userName')+'：'" prop="name">
          <el-input v-model="dataForm.name" :placeholder="['placeholder.input', $t('userManage.userName')] | i18nFilterForm" clearable :style="{width: '100%'}" />
        </el-form-item>
        <!-- <el-form-item :label="$t('roleManage.roleName')+'：'" prop="roleName">
          <el-select v-model="dataForm.roleName" :placeholder="['placeholder.select', $t('roleManage.roleName')] | i18nFilterForm" clearable :style="{width: '100%'}">
            <el-option
              v-for="(item, index) in roleNameOptions"
              :key="index"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item> -->
        <!-- <el-form-item :label="$t('userManage.sex')" prop="sex">
          <el-select v-model="dataForm.sex" :placeholder="['placeholder.select', $t('userManage.sex')] | i18nFilterForm" clearable :style="{width: '100%'}">
            <el-option
              v-for="(item, index) in sexOptions"
              :key="index"
              :label="item.label"
              :value="item.value"
              :disabled="item.disabled"
            />
          </el-select>
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" @click="getList()">{{ 'btn.search' | i18nFilter }}</el-button>
          <el-button type="primary" @click="showDialog = true">{{ 'btn.add' | i18nFilter }}</el-button>
        </el-form-item>
      </el-form>
      <base-table :table-config="tableProps" :table-data="list" :is-loading="listLoading" :page="dataForm.current" :limit="dataForm.size">
        <method-btn
          slot="handleBtn"
          :method-list="tableBtn"
          :width="trueWidth"
          @clickBtn="handleClick"
        />
      </base-table>
      <pagination v-show="total > dataForm.size" :total="total" :page.sync="dataForm.current" :limit.sync="dataForm.size" @pagination="getList" />

      <add-user-form :visible.sync="showDialog" @refreshDataList="getList" />
      <edit-user-form :visible.sync="showEditDialog" :target-info="{id: curEditId}" @refreshDataList="getList" />
      <assign-Role :visible.sync="AssignRoleDialog" :user-id="userId" @refreshDataList="getList" />
    </el-main>
  </el-container>
</template>

<script>
import sideTree from '../system-manage/sideTree'
import { getOrgList } from '@/api/org'
import AddUserForm from './AddUserForm'
import EditUserForm from './EditUser'
import AssignRole from './AssignRole'
import BaseTable from '@/components/BaseTable'
// edit here
import { getUserList, delUser } from '@/api/user'
import { getRoleList } from '@/api/role'

import Pagination from '@/components/Pagination'
import MethodBtn from '@/components/BaseTable/subcomponents/MethodBtn'
import dataDict from '@/filters/DataDict'
import i18n from '@/lang'
// edit here
const tableBtn = [{
  type: 'edit',
  btnName: 'btn.edit'
}, {
  type: 'AssignRole',
  btnName: i18n.t('userManage.AssignRole')
}, {
  type: 'delete',
  btnName: 'btn.delete'
}]
// birthday
// orgId
// sex
// code
// remark
const tableProps = [{
  prop: 'name',
  label: i18n.t('userManage.userName'),
  align: 'center'
}, {
  prop: 'email',
  label: i18n.t('userManage.email'),
  align: 'center'
}, {
  prop: 'mobile',
  label: i18n.t('userManage.mobile'),
  align: 'center'
},
// {
//   prop: 'birthday',
//   label: i18n.t('userManage.birthday'),
//   align: 'center'
// },
{
  prop: 'orgName',
  label: i18n.t('userManage.orgName'),
  align: 'center'
},
{
  prop: 'roleName',
  label: i18n.t('roleManage.roleName'),
  align: 'center'
},
// {
//   prop: 'sex',
//   label: i18n.t('userManage.sex'),
//   align: 'center',
//   filter: dataDict('sex')
// },
{
  prop: 'code',
  label: i18n.t('userManage.code'),
  align: 'center'
}, {
  prop: 'enabled',
  label: i18n.t('userManage.status'),
  align: 'center',
  filter: dataDict('enableState')
}, {
  prop: 'remark',
  label: i18n.t('userManage.remark'),
  align: 'center'
}]

const sexOptions = [{
  'label': i18n.t('basicDataFilter.sex.all'),
  'value': ''
}, {
  'label': i18n.t('basicDataFilter.sex.man'),
  'value': 1
}, {
  'label': i18n.t('basicDataFilter.sex.woman'),
  'value': 2
}]

export default {
  components: { sideTree, Pagination, BaseTable, MethodBtn, AddUserForm, EditUserForm, AssignRole },
  data() {
    return {
      menuList: [],
      tableBtn,
      tableProps,
      list: [],
      total: 0,
      trueWidth: 250,
      listLoading: true,
      showDialog: false,
      curEditId: null,
      userId: '',
      showEditDialog: false,
      AssignRoleDialog: false,
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      roleNameOptions: [],
      dataForm: {
        name: '',
        sex: '',
        roleName: '',
        orgId: '',
        orgName: '',
        current: 1,
        size: 10
      },
      sexOptions
    }
  },
  created() {
    this.getOrgList()
    this.getList()
  },
  methods: {
    async getOrgList() {
      // edit here
      const params = {
        current: 1,
        size: 500
      }
      this.roleNameOptions.splice(0, this.roleNameOptions.length)
      const res = await getRoleList(params)
      if (res.code === 0) {
        this.roleNameOptions = res.data
      }
      this.menuList.splice(0, this.menuList.length)
      const res1 = await getOrgList(params)
      if (res1.code === 0) {
        this.menuList = res1.data.records
      }
    },
    getOrganization(data) {
      this.dataForm.orgId = data.id
      this.dataForm.orgName = data.name
      this.getList()
    },
    handleClick(raw) {
      switch (raw.type) {
        case 'delete':
          this.$confirm(i18n.t('deleteMsgBox.content'), i18n.t('deleteMsgBox.hint'), {
            confirmButtonText: i18n.t('btn.confirm'),
            cancelButtonText: i18n.t('btn.cancel'),
            type: 'warning'
          }).then(async() => {
            // 走接口
            const result = await delUser({
              id: raw.data.id
            })
            if (result.code === 0) {
              this.$message({
                type: 'success',
                message: i18n.t('deleteMsgBox.doneMsg')
              })
              this.getList()
            }
          })
          break
        case 'edit':
          this.showEditDialog = true
          this.curEditId = raw.data.id
          break
        case 'AssignRole':
          this.AssignRoleDialog = true
          this.userId = raw.data.id
          break
      }
    },
    async getList() {
      this.listLoading = true
      // edit here
      const res = await getUserList(this.dataForm)
      if (res.code === 0) {
        this.list = res.data.records
        this.list.forEach(item => {
          item.roleName = item.stringList?.toString()
        })
        this.total = res.data.total
        this.listLoading = false
      }
    }
  }
}
</script>
<style scoped>
.el-container >>> .el-aside{
  border:2px solid #E4E4E4;
  border-radius:10px;
  background-color: white;
  min-height:550px;
  width:30%;
  padding-top:20px
}
</style>
