<template>
  <el-container class="crcform-container">
    <el-header>
      <div class="filter-container">
        <span>{{ $t('trials:internalStaff:table:name') }}:</span>
        <el-input v-model="listQuery.UserRealName" size="small" class="mr" clearable />

        <el-button type="primary" size="small" icon="el-icon-search" @click="handleSearch">
          {{ $t('common:button:search') }}
        </el-button>
        <el-button size="small" type="primary" icon="el-icon-refresh-left" @click="handleReset">
          {{ $t('common:button:reset') }}
        </el-button>
        <el-button type="primary" size="small" icon="el-icon-plus" style="margin-left:auto" :disabled="selectArr.length === 0" :loading="assignLoadStatus" @click="handleAssign">
          {{ $t('trials:internalStaff:button:assign') }}
        </el-button>
      </div>
    </el-header>
    <el-main>
      <div class="data-table">
        <el-table
          :data="list"
          stripe
          size="small"
          height="400px"
          class="crc-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:internalStaff:table:name')"
            show-overflow-tooltip
            sortable="custom"
            min-width="120"
          />
          <!-- 用户名 -->
          <el-table-column
            prop="UserName"
            :label="$t('trials:internalStaff:table:uid')"
            min-width="120"
          />
          <!-- 电话 -->
          <el-table-column
            prop="Phone"
            :label="$t('trials:internalStaff:table:phone')"
            show-overflow-tooltip
            sortable="custom"
            min-width="120"
          />
          <!-- 邮箱 -->
          <el-table-column
            prop="EMail"
            :label="$t('trials:internalStaff:table:email')"
            show-overflow-tooltip
            sortable="custom"
            min-width="120"
          />
          <!-- 单位 -->
          <el-table-column
            prop="OrganizationName"
            :label="$t('trials:internalStaff:table:organization')"
            show-overflow-tooltip
            min-width="120"
            sortable="custom"
          />
          <!-- 用户类型 -->
          <el-table-column
            prop="UserType"
            :label="$t('trials:internalStaff:table:userType')"
            show-overflow-tooltip
            sortable="custom"
            min-width="120"
          />
        </el-table>
      </div>
    </el-main>
    <el-footer style="text-align: right;">
      <div class="pagination">
        <pagination :total="total" :page.sync="listQuery.PageIndex" :limit.sync="listQuery.PageSize" @pagination="getList" />
      </div>
    </el-footer>
  </el-container>
</template>
<script>
import { getSiteCRCScreeningList, assignSiteCRC } from '@/api/trials'
import Pagination from '@/components/Pagination'
const getListQueryDefault = () => {
  return {
    UserRealName: '',
    PageIndex: 1,
    PageSize: 20
  }
}
export default {
  components: { Pagination },
  props: {
    siteId: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      list: [],
      total: 0,
      listQuery: getListQueryDefault(),
      selectArr: [],
      assignLoadStatus: false,
      trialId: ''
    }
  },
  mounted() {
    this.trialId = this.$route.query.trialId
    this.getList()
  },
  methods: {
    getList() {
      const loading = this.$loading({
        target: document.querySelector('.crc-table-list'),
        fullscreen: false,
        lock: true
      })
      this.listQuery.TrialId = this.trialId
      this.listQuery.TrialSiteId = this.siteId
      getSiteCRCScreeningList(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:internalStaff:message:assign'), {
        type: 'warning',
        distinguishCancelAndClose: true
      })
        .then(() => {
          const loading = this.$loading({
            target: document.querySelector('.crc-table-list'),
            fullscreen: false,
            lock: true
          })
          this.assignLoadStatus = true
          assignSiteCRC(this.selectArr)
            .then(res => {
              this.assignLoadStatus = false
              loading.close()
              if (res.IsSuccess) {
                this.$emit('closeDialog', true)
              }
            }).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
      }
    }
  }
}
</script>
<style lang="scss" scoped>
.crcform-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>
