<template>
  <el-container class="participant-container">
    <el-header style="height:50px">
      <div class="filter-container">
        <!-- 姓名 -->
        <span>{{ $t('trials:staff:table:name') }}:</span>
        <el-input v-model="listQuery.UserRealName" size="mini" class="mr" clearable />
        <!-- 用户名 -->
        <span>{{ $t('trials:staff:table:uid') }}:</span>
        <el-input v-model="listQuery.UserName" size="mini" class="mr" clearable />
        <!-- 单位 -->
        <span>{{ $t('trials:staff:table:organization') }}:</span>
        <el-input v-model="listQuery.OrganizationName" size="mini" class="mr" clearable />
        <!-- 用户类型 -->
        <span>{{ $t('trials:staff:table:userType') }}:</span>
        <el-select v-model="listQuery.UserTypeEnum" size="mini" clearable class="mr">
          <el-option
            v-for="item of userTypeOptions"
            :key="item.Id"
            :label="item.UserTypeShortName"
            :value="item.UserTypeEnum"
          >
            <span>{{ item.UserType }}</span>
          </el-option>
        </el-select>
        <!-- 查询 -->
        <el-button type="primary" size="mini" icon="el-icon-search" @click="handleSearch">
          {{ $t('common:button:search') }}
        </el-button>
        <!-- 重置 -->
        <el-button size="mini" type="primary" icon="el-icon-refresh-left" @click="handleReset">
          {{ $t('common:button:reset') }}
        </el-button>

        <el-button type="primary" size="mini" style="margin-left:auto" :disabled="selectArr.length === 0" :loading="assignLoadStatus" icon="el-icon-plus" @click="handleAssign">
          {{ $t('common:button:add') }}
        </el-button>
      </div>
    </el-header>
    <el-main>
      <div class="data-table">
        <el-table
          :data="list"
          stripe
          height="400px"
          class="participant-table-list"
          @selection-change="handleSelectChange"
          @sort-change="handleSortByColumn"
        >
          <el-table-column
            type="selection"
            width="50"
            :selectable="handleSelectable"
          />
          <el-table-column type="index" width="50" />
          <!-- 姓名 -->
          <el-table-column
            prop="UserRealName"
            :label="$t('trials:staff:table:name')"
            show-overflow-tooltip
            sortable="custom"
            min-width="100"
          />
          <!-- 用户名 -->
          <el-table-column
            prop="UserName"
            :label="$t('trials:staff:table:uid')"
            show-overflow-tooltip
            sortable="custom"
            min-width="100"
          />
          <!-- 电话 -->
          <el-table-column
            prop="Phone"
            :label="$t('trials:staff:table:phone')"
            show-overflow-tooltip
            sortable="custom"
            min-width="120"
          />
          <!-- 邮箱 -->
          <el-table-column
            prop="EMail"
            :label="$t('trials:staff:table:email')"
            show-overflow-tooltip
            sortable="custom"
            min-width="120"
          />
          <!-- 单位 -->
          <el-table-column
            prop="OrganizationName"
            :label="$t('trials:staff:table:organization')"
            show-overflow-tooltip
            min-width="100"
            sortable="custom"
          />
          <!-- 用户类型 -->
          <el-table-column
            prop="UserType"
            :label="$t('trials:staff:table:userType')"
            show-overflow-tooltip
            sortable="custom"
            min-width="100"
          />

        </el-table>
      </div>
    </el-main>
    <div class="pagination" style="text-align: right;margin-top:5px;">
      <pagination :total="total" :page.sync="listQuery.PageIndex" :limit.sync="listQuery.PageSize" @pagination="getList" />
    </div>
  </el-container>
</template>
<script>
import { getTrialUserScreeningList, addTrialUsers, getUserTypeList } from '@/api/trials'
import Pagination from '@/components/Pagination'
const getListQueryDefault = () => {
  return {
    UserRealName: '',
    UserName: '',
    OrganizationName: '',
    UserTypeEnum: '',
    PageIndex: 1,
    PageSize: 20
  }
}
export default {
  components: { Pagination },
  data() {
    return {
      list: [],
      total: 0,
      listQuery: getListQueryDefault(),
      selectArr: [],
      assignLoadStatus: false,
      isAdmin: JSON.parse(zzSessionStorage.getItem('IsAdmin')),
      userTypeOptions: [],
      trialId: ''
    }
  },
  mounted() {
    this.trialId = this.$route.query.trialId
    this.getUserType()
    this.getList()
  },
  methods: {
    getList() {
      const loading = this.$loading({
        target: document.querySelector('.participant-table-list'),
        fullscreen: false,
        lock: true
      })
      this.listQuery.TrialId = this.trialId
      getTrialUserScreeningList(this.listQuery).then(res => {
        loading.close()
        this.list = res.Result.CurrentPageData
        this.total = res.Result.TotalCount
      }).catch(() => { loading.close() })
    },
    handleAssign() {
      this.$confirm(this.$t('trials:staff:message:addStaff'), {
        type: 'warning',
        distinguishCancelAndClose: true
      })
        .then(() => {
          const loading = this.$loading({
            target: document.querySelector('.participant-table-list'),
            fullscreen: false,
            lock: true
          })
          this.assignLoadStatus = true
          addTrialUsers(this.selectArr)
            .then(res => {
              this.assignLoadStatus = false
              loading.close()
              if (res.IsSuccess) {
                this.$emit('closeDialog')
                this.$message.success(this.$t('common:message:addedSuccessfully'))
              }
            }).catch(() => {
              loading.close()
              this.assignLoadStatus = false
            })
        })
    },
    handleSearch() {
      this.listQuery.PageIndex = 1
      this.getList()
    },
    handleReset() {
      this.listQuery = getListQueryDefault()
      this.getList()
    },
    handleSelectChange(val) {
      this.selectArr = val
    },
    handleSortByColumn(column) {
      if (column.order === 'ascending') {
        this.listQuery.Asc = true
      } else {
        this.listQuery.Asc = false
      }
      this.listQuery.SortField = column.prop
      this.listQuery.PageIndex = 1
      this.getList()
    },
    handleSelectable(row) {
      if (!row.IsSelect) {
        return true
      } else {
        return false
      }
    },
    getUserType() {
      getUserTypeList(2).then(res => {
        this.userTypeOptions = res.Result
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.participant-container{
  height: 100%;
  .el-header{
    .filter-container{
      display: flex;
      align-items: center;
      span{
        font-size:13px;
        margin-right:5px;
      }
      .mr{
        margin-right: 5px;
        width: 120px;
      }
    }
  }
  .el-main{
    padding: 0px;
  }
  .el-footer{
    padding: 0 20px;
  }
}
</style>
