<template>
  <div class="role">
    <div ref="leftContainer" class="left">
      <el-form :inline="true">
        <el-form-item :label="$t('system:role:title:SearchFilter')">
          <el-input
            v-model="queryParams.SearchFilter"
            clearable
            @keyup.enter.native="getList"
          />
        </el-form-item>
        <el-form-item :label="$t('system:role:title:Group')">
          <el-select v-model="queryParams.GroupId" clearable>
            <el-option
              v-for="item of dict.type.UserTypeGroup"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="getList"
            >{{ $t('common:button:search') }}</el-button
          >
          <el-button
            v-hasPermi="['system:role:add']"
            type="primary"
            icon="el-icon-plus"
            @click="handleAddRole"
            >{{ $t('common:button:add') }}</el-button
          >
        </el-form-item>
      </el-form>
      <el-table
        v-loading="loading"
        v-adaptive="{ bottomOffset: 30 }"
        size="small"
        height="100"
        :data="list"
        class="table"
      >
        <el-table-column type="index" width="50" />
        <el-table-column
          :label="$t('system:role:table:User Type')"
          prop="UserTypeName"
          min-width="220"
          show-overflow-tooltip
        />
        <el-table-column
          :label="$t('system:role:table:Shortname')"
          prop="UserTypeShortName"
          min-width="120"
          show-overflow-tooltip
        />
        <el-table-column
          :label="$t('system:role:table:Group')"
          prop="Note"
          min-width="80"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            {{
              scope.row.UserTypeGroupList.map((v) => {
                return v.GroupName
              }).toString()
            }}
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('system:role:table:PermissionStr')"
          prop="PermissionStr"
          min-width="100"
          show-overflow-tooltip
        />
        <el-table-column
          :label="$t('system:role:table:User Type Enum')"
          prop="UserTypeEnum"
          min-width="100"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            {{ $fd('UserType', scope.row.UserTypeEnum) }}
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('system:role:table:IsEnable')"
          prop="IsEnable"
          min-width="100"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            <el-tag :type="!scope.row.IsEnable ? 'info' : ''">
              {{ $fd('IsEnable', scope.row.IsEnable) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          :label="$t('system:role:table:Description')"
          prop="Description"
          min-width="220"
          show-overflow-tooltip
        />
        <el-table-column :label="$t('common:action:action')" width="280">
          <template slot-scope="scope">
            <el-button
              v-hasPermi="['system:role:edit']"
              type="text"
              @click="handleEditRole(scope.row)"
              >{{ $t('common:button:edit') }}</el-button
            >
            <el-button
              v-hasPermi="['system:role:delete']"
              type="text"
              @click="handleDeleteRole(scope.row)"
              >{{ $t('common:button:delete') }}</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- <div class="right">
      <RoleMenu ref="roleMenu" />
    </div> -->
    <role-form v-if="model_cfg.visible" :data="editRow" @close="closeModal" />
  </div>
</template>
<script>
import { getUserTypeRoleList, deleteUserTypeRole } from '@/api/admin'
// import { getDictionary } from '@/api/dictionary/dictionary'
import RoleForm from './components/RoleForm'
import tableMixins from '@/mixins/table'
import { model_cfg } from './role'
export default {
  name: 'Role',
  components: { RoleForm },
  dicts: ['UserTypeGroup'],
  mixins: [tableMixins],
  data() {
    return {
      model_cfg,
      list: [],
      total: 0,
      loading: false,
      treeData: [],
      defaultProps: {
        label: 'MenuName',
        children: 'Children',
      },
      // 查询参数
      queryParams: {
        GroupId: undefined,
        SearchFilter: undefined,
      },
      dialogVisible: false,
      editRow: {},
      expandedKeys: [],
      treeLoading: false,
      funcListLoading: false,
      funcList: [],
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    getDictionary() {},
    getList() {
      this.loading = true
      getUserTypeRoleList(this.queryParams)
        .then((res) => {
          this.loading = false
          this.list = res.Result
        })
        .catch(() => {
          this.loading = false
        })
    },
    handleAddRole() {
      this.editRow = {}
      this.model_cfg.visible = true
      this.model_cfg.title = this.$t('common:button:add')
    },
    handleEditRole(row) {
      this.editRow = row
      this.model_cfg.visible = true
      this.model_cfg.title = this.$t('common:button:edit')
    },
    // 分配权限，获取菜单树，并勾选已有菜单权限
    handlePermission(row) {
      // this.roleId = row.Id
      // this.$nextTick(function() {
      //   this.$refs['roleMenu'].getMenus(row.Id)
      // })
    },
    handleDeleteRole(row) {
      this.$confirm(this.$t('trials:uploadedDicoms:message:deleteMes'), {
        type: 'warning',
        distinguishCancelAndClose: true,
      }).then(() => {
        deleteUserTypeRole(row.Id).then((res) => {
          if (res.IsSuccess) {
            this.list.splice(
              this.list.findIndex((item) => item.Id === row.Id),
              1
            )
            this.$message.success(this.$t('common:message:deletedSuccessfully'))
          }
        })
      })
    },
    closeModal() {
      this.model_cfg.visible = false
      this.getList()
    },
  },
}
</script>
<style lang="scss">
.role {
  height: 100%;
  box-sizing: border-box;
  display: flex;
  padding: 10px;
  border-radius: 5px;
  .left {
    display: flex;
    flex-direction: column;
    width: 0;
    flex-grow: 4;
    // border-right: 1px solid #ccc;
    .filter-container {
      display: flex;
      align-items: center;
      margin: 5px;
    }
    .data-table {
      flex: 1;
      padding: 5px 0px;
    }
    .pagination-container {
      text-align: right;
    }
  }
  .right {
    width: 0;
    flex-grow: 6;
    overflow-y: auto;
    border-right: 1px solid #ccc;
  }
  .selected-row {
    background-color: cadetblue;
  }
}
</style>
