<template>
  <div>
    <a-modal
      centered
      :title="title"
      :width="1000"
      :visible="visible"
      @ok="handleOk"
      @cancel="handleCancel"
      cancelText="关闭"
    >
      <!-- 查询区域 -->
      <div class="table-page-search-wrapper search_box">
        <a-form layout="inline" @keyup.enter.native="searchQuery">
          <a-row :gutter="24">
            <a-col :span="9">
              <div class="for_item2">
                <a-form-item label="用户账号">
                  <a-input-search
                    placeholder="请输入用户账号"
                    size="large"
                    v-model="queryParam.username"
                    @search="searchQuery"
                  >
                    <a-button slot="enterButton" class="searchBut">搜索</a-button>
                  </a-input-search>
                  <!-- </a-input-search> -->
                </a-form-item>
              </div>
            </a-col>
            <a-col :span="2">
              <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
                <a-button @click="searchReset" class="resetBut">重置</a-button>
              </span>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- table区域-begin -->
      <div>
        <a-table
          size="small"
          bordered
          rowKey="id"
          :columns="columns1"
          :dataSource="dataSource1"
          :pagination="false"
          :loading="loading"
          :scroll="{ y: 240 }"
          :rowSelection="{
            selectedRowKeys: selectedRowKeys,
            onSelectAll: onSelectAll,
            onSelect: onSelect,
            onChange: onSelectChange,
          }"
          @change="handleTableChange"
        >
        </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>
      <!-- table区域-end -->
    </a-modal>
  </div>
</template>

<script>
import { filterObj } from '@/utils/util'
import { getAction } from '@/api/manage'

export default {
  name: 'SelectUserModal',
  data() {
    return {
      title: '添加已有用户',
      names: [],
      visible: false,
      placement: 'right',
      description: '',
      // 查询条件
      queryParam: {},
      // 表头
      columns1: [
        {
          title: '#',
          dataIndex: '',
          key: 'rowIndex',
          width: 50,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '用户账号',
          align: 'center',
          width: 100,
          dataIndex: 'username',
        },
        {
          title: '用户名称',
          align: 'center',
          width: 100,
          dataIndex: 'realname',
        },
        {
          title: '性别',
          align: 'center',
          width: 100,
          dataIndex: 'sex_dictText',
        },
        {
          title: '电话',
          align: 'center',
          width: 100,
          dataIndex: 'phone',
        },
        {
          title: '部门',
          align: 'center',
          width: 150,
          dataIndex: 'orgCode',
        },
      ],
      columns2: [
        {
          title: '用户账号',
          align: 'center',
          dataIndex: 'username',
        },
        {
          title: '用户名称',
          align: 'center',
          dataIndex: 'realname',
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          width: 100,
          scopedSlots: { customRender: 'action' },
        },
      ],
      //数据集
      dataSource1: [],
      dataSource2: [],
      // 分页参数
      ipagination: {
        current: 1,
        pageSize: 10,
        pageSizeOptions: ['10', '20', '30'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
        showQuickJumper: true,
        showSizeChanger: true,
        total: 0,
      },
      isorter: {
        column: 'createTime',
        order: 'desc',
      },
      loading: false,
      selectedRowKeys: [],
      selectedRows: [],
      url: {
        list: '/sys/user/list',
      },
    }
  },
  created() {
    this.loadData()
  },
  methods: {
    searchQuery() {
      this.loadData(1)
    },
    searchReset() {
      this.queryParam = {}
      this.loadData(1)
    },
    handleCancel() {
      this.visible = false
    },
    handleOk() {
      this.dataSource2 = this.selectedRowKeys
      console.log('data:' + this.dataSource2)
      this.$emit('selectFinished', this.dataSource2)
      this.visible = false
    },
    add() {
      this.visible = true
    },
    loadData(arg) {
      //加载数据 若传入参数1则加载第一页的内容
      if (arg === 1) {
        this.ipagination.current = 1
      }
      var params = this.getQueryParams() //查询条件
      getAction(this.url.list, params).then((res) => {
        if (res.success) {
          this.dataSource1 = res.result.records
          this.ipagination.total = res.result.total
        }
      })
    },
    getQueryParams() {
      var param = Object.assign({}, this.queryParam, this.isorter)
      param.field = this.getQueryField()
      param.pageNo = this.ipagination.current
      param.pageSize = this.ipagination.pageSize
      return filterObj(param)
    },
    getQueryField() {
      //TODO 字段权限控制
    },
    onSelectAll(selected, selectedRows, changeRows) {
      if (selected === true) {
        for (var a = 0; a < changeRows.length; a++) {
          this.dataSource2.push(changeRows[a])
        }
      } else {
        for (var b = 0; b < changeRows.length; b++) {
          this.dataSource2.splice(this.dataSource2.indexOf(changeRows[b]), 1)
        }
      }
      // console.log(selected, selectedRows, changeRows);
    },
    onSelect(record, selected) {
      if (selected === true) {
        this.dataSource2.push(record)
      } else {
        var index = this.dataSource2.indexOf(record)
        //console.log();
        if (index >= 0) {
          this.dataSource2.splice(this.dataSource2.indexOf(record), 1)
        }
      }
    },
    onSelectChange(selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectionRows = selectedRows
    },
    onClearSelected() {
      this.selectedRowKeys = []
      this.selectionRows = []
    },
    handleDelete: function (record) {
      this.dataSource2.splice(this.dataSource2.indexOf(record), 1)
    },
    handleTableChange(pagination, filters, sorter) {
      //分页、排序、筛选变化时触发
      console.log(sorter)
      //TODO 筛选
      if (Object.keys(sorter).length > 0) {
        this.isorter.column = sorter.field
        this.isorter.order = 'ascend' == sorter.order ? 'asc' : 'desc'
      }
      this.ipagination = pagination
      this.loadData()
    },
    changeNum(val) {
      this.ipagination.current = val
      this.loadData()
    },
  },
}
</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-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;
  }
}
.table-page-search-wrapper ::v-deep.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;
  }
}

::v-deep .ant-dropdown-menu-item:hover {
  background: #f2f5f965;
}

.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;
}
/deep/ .ant-modal-footer {
  padding: 10px 16px 45px;
  border-top: 0;
  > div {
    display: flex;
    justify-content: center;
  }
  .ant-btn {
    width: 76px;
    height: 35px;
    line-height: 34px;
    border-radius: 2px 2px 2px 2px;
    opacity: 1;
    
    padding: 0;

 
  }

  .ant-btn-primary {
    
    
    color: #ffffff;

    &:hover {
      color: #fff !important;
    }
  }
}
</style>