<template>
  <a-row :gutter="10">
    <a-col :md="leftColMd" :sm="24" style="margin-bottom: 20px">
      <a-card :bordered="false">
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper search_box">
          <a-form layout="inline" @keyup.enter.native="searchQuery">
            <a-row :gutter="24">
              <a-col :span="16">
                <a-form-item label="角色名称">
                  <j-input placeholder="请输入角色名称" v-model="queryParam.roleName"></j-input>
                </a-form-item>
              </a-col>
              <a-col :span="4">
                <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
                  <a-button type="primary" @click="searchQuery" slot="enterButton" class="searchBut">搜索</a-button>
                  <a-button @click="searchReset" class="resetBut">重置</a-button>
                </span>
              </a-col>
            </a-row>
          </a-form>
        </div>

        <!-- 操作按钮区域 -->
        <a-row :gutter="15" type="flex" justify="space-between" align="middle">
          <a-col>
            <div class="ant-alert ant-alert-info" style="margin-bottom: 8px">
              <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
              <a style="font-weight: 600; ">{{ selectedRowKeys1.length }}</a
              >项
              <a-button class="clearBut" @click="onClearSelected1">清空</a-button>
            </div>
          </a-col>
          <a-col>
            <div class="table-operator">
              <a-button @click="handleAdd" type="primary" icon="file-add">新建角色</a-button>
              <a-button type="primary" icon="download" @click="handleExportXls('元数据表')">导出</a-button>
              <a-upload
                name="file"
                :showUploadList="false"
                :multiple="false"
                :headers="tokenHeader"
                :action="importExcelUrl"
                @change="handleImportExcel"
              >
                <a-button type="primary" icon="export">导入</a-button>
              </a-upload>
            </div>
          </a-col>
        </a-row>

        <!-- table -->
        <div style="margin-top: 15px">
          <a-table
            style="height: 500px"
            ref="table"
            size="middle"
            bordered
            rowKey="id"
            :columns="columns"
            :dataSource="dataSource"
            :pagination="false"
            :loading="loading"
            :rowSelection="{ selectedRowKeys: selectedRowKeys1, onChange: onSelectChange1, type: 'radio' }"
            @change="handleTableChange"
          >
            <span slot="action" slot-scope="text, record" class="action">
              <a @click="handleOpen(record)">用户</a>
              <a-divider type="vertical" />

              <a-dropdown>
                <a class="ant-dropdown-link"> 更多 <a-icon type="down" /> </a>
                <a-menu slot="overlay" class="overlay">
                  <a-menu-item>
                    <a @click="handlePerssion(record.id)">授权</a>
                  </a-menu-item>
                  <a-menu-item v-has="'role:edit'">
                    <a @click="handleEdit(record)">编辑</a>
                  </a-menu-item>
                  <a-menu-item v-has="'role:delete'">
                    <a-popconfirm
                      title="确定删除吗?"
                      @confirm="() => handleDelete1(record.id)"
                      overlayClassName="popDete"
                    >
                      <a>删除</a>
                    </a-popconfirm>
                  </a-menu-item>
                </a-menu>
              </a-dropdown>
            </span>
          </a-table>
          <div class="myPagination">
            <el-pagination
              ref="elpagination"
              @current-change="changeNum"
              :current-page="ipagination.current"
              background
              :page-size="ipagination.pageSize"
              layout="prev, pager, next,jumper, slot ,total "
              :total="ipagination.total"
            >
              <el-button class="btn-sure" size="mini">确定</el-button>
            </el-pagination>
          </div>
        </div>
        <!-- 右侧的角色权限配置 -->
        <user-role-modal ref="modalUserRole"></user-role-modal>
        <role-modal ref="modalForm" @ok="modalFormOk"></role-modal>
      </a-card>
    </a-col>
    <a-col :md="rightColMd" :sm="24" v-if="this.rightcolval == 1">
      <a-card :bordered="false">
        <div style="text-align: right">
          <a-icon type="close-circle" @click="hideUserList" />
        </div>
        <!-- 查询区域 -->
        <div class="table-page-search-wrapper search_box" v-has="'role:search'">
          <a-form layout="inline">
            <a-row :gutter="24">
              <a-col :span="16">
                <div class="for_item2">
                  <a-form-item label="角色名称">
                    <j-input placeholder="请输入角色名称" v-model="queryParam2.username"></j-input>
                  </a-form-item>
                  <!-- <a-form-item label="角色名称">
                    <a-input-search placeholder="请输入角色名称" size="large" v-model="queryParam2.username"
                      @search="searchQuery2">
                      <a-button slot="enterButton" style="margin-left: 15px" class="searchBut">查询</a-button>
                    </a-input-search>
                  </a-form-item> -->
                </div>
              </a-col>
              <a-col :span="5">
                <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
                  <a-button @click="searchQuery2" class="searchBut">查询</a-button>
                  <a-button @click="searchReset2" class="resetBut">重置</a-button>
                </span>
              </a-col>
            </a-row>
          </a-form>
        </div>
        <!-- 操作按钮区域 -->
        <a-row :gutter="15" type="flex" justify="space-between" align="middle">
          <a-col>
            <div class="ant-alert ant-alert-info" style="margin-bottom: 8px">
              <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
              <a style="font-weight: 600; ">{{ selectedRowKeys2.length }}</a
              >项
              <a-button class="clearBut" @click="onClearSelected2">清空</a-button>
              <a-dropdown v-if="selectedRowKeys2.length > 0" v-has="'role:delete:user'">
                <a-menu slot="overlay">
                  <a-menu-item key="1" @click="batchDel2"><a-icon type="delete" />删除</a-menu-item>
                </a-menu>
                <a-button class="clearBut" style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
              </a-dropdown>
            </div>
          </a-col>
          <a-col>
            <div class="table-operator">
              <a-button
                @click="handleAddUserRole"
                type="primary"
                icon="plus"
                style="margin-top: 16px"
                v-has="'role:add:user'"
                >已有用户</a-button
              >
            </div>
          </a-col>
        </a-row>
        <!-- table区域-begin -->
        <div>
          <a-table
            style="height: 500px"
            ref="table2"
            bordered
            size="middle"
            rowKey="id"
            :columns="columns2"
            :dataSource="dataSource2"
            :pagination="false"
            :loading="loading2"
            :rowSelection="{ selectedRowKeys: selectedRowKeys2, onChange: onSelectChange2 }"
            @change="handleTableChange2"
          >
            <span slot="action" slot-scope="text, record" class="action">
              <!-- <a @click="handleEdit2(record)">编辑</a> -->
              <a-popconfirm
                title="确定删除吗?"
                @confirm="() => handleDelete2(record.id)"
                v-has="'role:delete:user'"
                overlayClassName="popDete"
              >
                <a>删除</a>
              </a-popconfirm>
              <!-- <a-divider type="vertical"/> -->
              <!-- <a-dropdown>
            <a class="ant-dropdown-link">
              更多 <a-icon type="down"/>
            </a>
            <a-menu slot="overlay">
              <a-menu-item>
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete2(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
            </a-menu>
          </a-dropdown> -->
            </span>
          </a-table>
          <div class="myPagination">
            <el-pagination
              ref="elpagination"
              @current-change="changeNum2"
              :current-page="ipagination2.current"
              background
              :page-size="ipagination2.pageSize"
              layout="prev, pager, next,jumper, slot ,total "
              :total="ipagination2.total"
            >
              <el-button class="btn-sure" size="mini">确定</el-button>
            </el-pagination>
          </div>
        </div>
        <!-- 表单区域 -->
        <role-modal ref="modalForm" @ok="modalFormOk"></role-modal>
        <user-modal ref="modalForm2" @ok="modalFormOk2"></user-modal>
        <Select-User-Modal ref="selectUserModal" @selectFinished="selectOK"></Select-User-Modal>
      </a-card>
    </a-col>
  </a-row>
</template>
<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import { deleteAction, postAction, getAction } from '@/api/manage'
import SelectUserModal from './modules/SelectUserModal'
import RoleModal from './modules/RoleModal'
import UserModal from './modules/UserModal'
import { filterObj } from '@/utils/util'
import UserRoleModal from './modules/UserRoleModal'
import moment from 'moment'

export default {
  name: 'RoleUserList',
  mixins: [JeecgListMixin],
  components: {
    UserRoleModal,
    SelectUserModal,
    RoleModal,
    UserModal,
    moment,
  },
  data() {
    return {
      model1: {},
      model2: {},
      currentRoleId: '',
      queryParam1: {},
      queryParam2: {},
      dataSource1: [],
      dataSource2: [],
      ipagination1: {
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '20', '30'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0,
      },
      ipagination2: {
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '20', '30'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0,
      },
      isorter1: {
        column: 'createTime',
        order: 'desc',
      },
      isorter2: {
        column: 'createTime',
        order: 'desc',
      },
      filters1: {},
      filters2: {},
      loading1: false,
      loading2: false,
      selectedRowKeys1: [],
      selectedRowKeys2: [],
      selectionRows1: [],
      selectionRows2: [],
      test: {},
      rightcolval: 0,
      columns: [
        {
          title: '角色编码',
          align: 'center',
          dataIndex: 'roleCode',
          customRender: (text) => {
            console.log(text)
            return text
          },
        },
        {
          title: '角色名称',
          align: 'center',
          dataIndex: 'roleName',
        },
        {
          title: '创建时间',
          dataIndex: 'createTime',
          align: 'center',
          sorter: true,
          customRender: (text) => {
            return moment(text).format('YYYY-MM-DD')
          },
        },
        {
          title: '操作',
          dataIndex: 'action',
          width: 150,
          align: 'center',
          scopedSlots: { customRender: 'action' },
        },
      ],
      columns2: [
        {
          title: '用户账号',
          align: 'center',
          dataIndex: 'username',
          width: 120,
        },
        {
          title: '用户名称',
          align: 'center',
          width: 100,
          dataIndex: 'realname',
        },
        {
          title: '状态',
          align: 'center',
          width: 80,
          dataIndex: 'status_dictText',
        },

        {
          title: '操作',
          dataIndex: 'action',
          scopedSlots: { customRender: 'action' },
          align: 'center',
          width: 120,
        },
      ],

      // 高级查询参数
      superQueryParams2: '',
      // 高级查询拼接条件
      superQueryMatchType2: 'and',
      url: {
        list: '/sys/role/list',
        delete: '/sys/role/delete',
        list2: '/sys/user/userRoleList',
        addUserRole: '/sys/user/addSysUserRole',
        delete2: '/sys/user/deleteUserRole',
        deleteBatch2: '/sys/user/deleteUserRoleBatch',
        exportXlsUrl: 'sys/role/exportXls',
        importExcelUrl: 'sys/role/importExcel',
      },
    }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
    leftColMd() {
      return this.selectedRowKeys1.length === 0 ? 24 : 12
    },
    rightColMd() {
      return this.selectedRowKeys1.length === 0 ? 0 : 12
    },
  },
  methods: {
    onSelectChange2(selectedRowKeys, selectionRows) {
      this.selectedRowKeys2 = selectedRowKeys
      this.selectionRows2 = selectionRows
    },
    onClearSelected2() {
      this.selectedRowKeys2 = []
      this.selectionRows2 = []
    },
    onClearSelected1() {
      this.selectedRowKeys1 = []
      this.selectionRows1 = []
    },
    onSelectChange1(selectedRowKeys, selectionRows) {
      this.rightcolval = 1
      this.selectedRowKeys1 = selectedRowKeys
      this.selectionRows1 = selectionRows
      this.model1 = Object.assign({}, selectionRows[0])
      console.log(this.model1)
      this.currentRoleId = selectedRowKeys[0]
      this.loadData2()
    },
    onClearSelected() {},

    getQueryParams2() {
      //获取查询条件
      let sqp = {}
      if (this.superQueryParams2) {
        sqp['superQueryParams'] = encodeURI(this.superQueryParams2)
        sqp['superQueryMatchType'] = this.superQueryMatchType2
      }
      var param = Object.assign(sqp, this.queryParam2, this.isorter2, this.filters2)
      param.field = this.getQueryField2()
      param.pageNo = this.ipagination2.current
      param.pageSize = this.ipagination2.pageSize
      return filterObj(param)
    },
    getQueryField2() {
      //TODO 字段权限控制
      var str = 'id,'
      this.columns2.forEach(function (value) {
        str += ',' + value.dataIndex
      })
      return str
    },
    handleEdit2: function (record) {
      this.$refs.modalForm2.title = '编辑'
      this.$refs.modalForm2.roleDisabled = true
      this.$refs.modalForm2.edit(record)
    },
    handleAdd2: function () {
      if (this.currentRoleId == '') {
        this.$message.error('请选择一个角色!')
      } else {
        this.$refs.modalForm2.roleDisabled = true
        this.$refs.modalForm2.title = '新增'
        this.$refs.modalForm2.edit({ activitiSync: '1', userIdentity: 1, selectedroles: this.currentRoleId })
      }
    },
    modalFormOk2() {
      // 新增/修改 成功时，重载列表
      this.loadData2()
    },
    loadData2(arg) {
      if (!this.url.list2) {
        this.$message.error('请设置url.list2属性!')
        return
      }
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination2.current = 1
      }
      if (this.currentRoleId === '') return
      let params = this.getQueryParams2() //查询条件
      params.roleId = this.currentRoleId
      this.loading2 = true
      getAction(this.url.list2, params).then((res) => {
        if (res.success) {
          this.dataSource2 = res.result.records
          this.ipagination2.total = res.result.total
        }
        this.loading2 = false
      })
    },
    handleDelete1: function (id) {
      this.handleDelete(id)
      this.dataSource2 = []
      this.currentRoleId = ''
    },
    handleDelete2: function (id) {
      if (!this.url.delete2) {
        this.$message.error('请设置url.delete2属性!')
        return
      }
      var that = this
      deleteAction(that.url.delete2, { roleId: this.currentRoleId, userId: id }).then((res) => {
        if (res.success) {
          that.$message.success(res.message)
          that.loadData2()
        } else {
          that.$message.warning(res.message)
        }
      })
    },
    batchDel2: function () {
      if (!this.url.deleteBatch2) {
        this.$message.error('请设置url.deleteBatch2属性!')
        return
      }
      if (this.selectedRowKeys2.length <= 0) {
        this.$message.warning('请选择一条记录！')
        return
      } else {
        var ids = ''
        for (var a = 0; a < this.selectedRowKeys2.length; a++) {
          ids += this.selectedRowKeys2[a] + ','
        }
        var that = this
        console.log(this.currentDeptId)
        this.$confirm({
          title: '确认删除',
          content: '是否删除选中数据?',
          class: 'delete_qx',
          onOk: function () {
            deleteAction(that.url.deleteBatch2, { roleId: that.currentRoleId, userIds: ids }).then((res) => {
              if (res.success) {
                that.$message.success(res.message)
                that.loadData2()
                that.onClearSelected()
              } else {
                that.$message.warning(res.message)
              }
            })
          },
        })
      }
    },
    selectOK(data) {
      let params = {}
      params.roleId = this.currentRoleId
      params.userIdList = []
      for (var a = 0; a < data.length; a++) {
        params.userIdList.push(data[a])
      }
      console.log(params)
      postAction(this.url.addUserRole, params).then((res) => {
        if (res.success) {
          this.loadData2()
          this.$message.success(res.message)
        } else {
          this.$message.warning(res.message)
        }
      })
    },

    handleAddUserRole() {
      if (this.currentRoleId == '') {
        this.$message.error('请选择一个角色!')
      } else {
        this.$refs.selectUserModal.visible = true
      }
    },
    handleOpen(record) {
      this.rightcolval = 1
      this.selectedRowKeys1 = [record.id]
      this.model1 = Object.assign({}, record)
      this.currentRoleId = record.id
      this.onClearSelected2()
      this.loadData2()
    },
    /*handleEdit: function(record) {
        if (this.currentRoleId == '') {
          this.$message.error('请选择一个角色!')
        } else {
          this.$refs.modalForm.edit(record)
          this.$refs.modalForm.title = '编辑'
        }
      },*/
    searchQuery2() {
      this.loadData2(1)
    },
    searchReset2() {
      this.queryParam2 = {}
      this.loadData2(1)
    },
    handleTableChange2(pagination, filters, sorter) {
      //分页、排序、筛选变化时触发
      //TODO 筛选
      if (Object.keys(sorter).length > 0) {
        this.isorter2.column = sorter.field
        this.isorter2.order = 'ascend' == sorter.order ? 'asc' : 'desc'
      }
      this.ipagination2 = pagination
      this.loadData2()
    },
    hideUserList() {
      //this.rightcolval = 0
      this.selectedRowKeys1 = []
    },
    handlePerssion(roleId) {
      this.$refs.modalUserRole.show(roleId)
    },
    changeNum(val) {
      this.ipagination.current = val
      this.loadData()
    },
    changeNum2(val) {
      this.ipagination2.current = val
      this.loadData2()
    },
  },
}
</script>

<style scoped lang="less">
@import '~@assets/less/common.less';
@import '~@assets/less/shaDow.less';
@import '~@assets/less/tableCommon.less';

// 搜索框
.search_box {
  ::v-deep .ant-input-group .ant-input {
    height: 38px;
    line-height: 38px;
    font-size: 14px;
    border-radius: 2px;
  }

  .searchBut {
    
    border-radius: 0px 2px 2px 0px;
    opacity: 1;
    

    height: 38px;
    line-height: 38px;
    font-size: 14px;
  }

  .resetBut {
    width: 60px;
    height: 38px;
    line-height: 38px;
    background: #dbdbdb;
    border-radius: 2px;
    color: #000;
    opacity: 1;
    padding: 0;
    border: 1px solid #b6b6b6;
  }
}

// ::v-deep
//   .ant-table-thead
//   > tr
//   > th
//   .ant-table-column-sorter
//   .ant-table-column-sorter-inner
//   .ant-table-column-sorter-up.on,
// ::v-deep
//   .ant-table-thead
//   > tr
//   > th
//   .ant-table-column-sorter
//   .ant-table-column-sorter-inner
//   .ant-table-column-sorter-down.on {
  
// }

// 下拉框
::v-deep.ant-select {
  .ant-select-selection--single {
    height: 38px;

    .ant-select-selection__rendered {
      line-height: 38px;
    }
  }

  // .ant-select-dropdown-menu-item-active:not(.ant-select-dropdown-menu-item-disabled) {
    
  // }
}

::v-deep.table-page-search-wrapper {
  .ant-form-item,
  .table-page-search-submitButtons {
    margin-bottom: 12px;

    & > button:first-child {
      margin-right: 20px;
    }
  }
  .search_box .ant-form-item-children {
    display: flex;
    align-items: center;
  }
}

.search_box .ant-form-inline .ant-form-item > .ant-form-item-label {
  padding-right: 8px;
}

.ant-alert-info {
  background-color: #fff;
  border: none;

  .clearBut {
    height: 38px;
    background: #ffffff;
    border-radius: 2px 2px 2px 2px;
    opacity: 1;
    
    letter-spacing: 0px;
    padding: 0 16px;
    margin-left: 6px;
  }
}

.ant-alert {
  padding: 0;
}

.table-operator {
  .ant-btn {
    // width: 68px;
    height: 38px;
    line-height: 38px;
    
    border-radius: 2px 2px 2px 2px;
    opacity: 1;
    
    font-size: 14px;
    font-family: Source Han Sans CN-Regular, Source Han Sans CN;
    font-weight: 400;
    
    margin: 0 16px 8px 0;
    padding: 0 15px;
  }
  .ant-btn:last-child {
    margin: 0 0 8px 0;
  }
}

// .action {
//   a {
    
//   }
// }

.myPagination {
  margin-top: 40px;
  display: flex;
  justify-content: end;

  ::v-deep .el-pagination {
    font-weight: 600;

    .btn-sure {
      background: #dbdbdb;
      border-radius: 2px 2px 2px 2px;
      opacity: 1;
      border: 1px solid #b6b6b6;
      padding: 0;
      color: #000;
      margin: 0 20px 0 10px;
      width: 50px;

      // &:hover {
      //   // background: none;
      // }

      // &:focus {
      //   // background: none;
      // }
    }

    .el-select .el-input .el-input__inner {
      color: #000;
      font-size: 14px;
      padding-right: 17px;
      font-weight: 400;
      border-radius: 2px;
    }

    .el-pager li {
      font-size: 16px;
      color: #888888;
      width: 34px;

      &:hover {
        opacity: 0.9;
      }
    }

    .el-pagination span:not([class*='suffix']) {
      font-size: 14px;
      color: #888888;
    }

    .el-pager li:not(.disabled).active {
      color: #fff;
    }
  }

  .ant-pagination {
    color: #888888;
  }

  ::v-deep .ant-select-selection--single {
    height: 28px !important;
  }

  ::v-deep .ant-select-selection__rendered {
    line-height: 28px !important;
  }

  ::v-deep .ant-pagination.mini .ant-pagination-options-quick-jumper input {
    height: 28px;
  }

  ::v-deep .ant-pagination.mini .ant-pagination-item {
    font-size: 16px;
    height: 28px;
    line-height: 28px;
    width: 34px;
  }
}

::v-deep ::-webkit-scrollbar-thumb {
  
  border-radius: 9px 9px 9px 9px;
}
</style>