<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.RealName" 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 />
        <!-- 是否加入 -->
        <template v-if="!isView">
          <span>{{ $t('trials:staff:table:Join') }}:</span>
          <el-select v-model="listQuery.IsAuditRecordUserSelect" placeholder="" size="mini" class="mr" clearable>
            <el-option v-for="item in $d.IsJoin" :key="item.id" :label="item.label" :value="item.value">
            </el-option>
          </el-select>
        </template>
        <!-- 用户类型 -->
        <!-- <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" /> -->
          <el-table-column type="index" width="50" />
          <!-- 姓名 -->
          <el-table-column prop="FullName" :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="UserTypeShortName" :label="$t('trials:staff:table:userType')" show-overflow-tooltip
            sortable="custom" min-width="100" /> -->
          <el-table-column prop="IsAuditRecordUserSelect" :label="$t('trials:staff:table:Join')" show-overflow-tooltip>
            <template slot-scope="scope">
              <span>{{ $fd('IsJoin', scope.row.IsAuditRecordUserSelect) }}</span>
            </template>
          </el-table-column>
          <el-table-column :label="$t('common:action:action')" width="120" align="left" fixed="right">
            <template slot-scope="scope">
              <el-button type="text" @click="changeUser(true, scope.row)" :disabled="scope.row.IsAuditRecordUserSelect">
                {{ $t('trials:trials-workbench:inspectionManagement:button:join') }}
              </el-button>
              <el-button type="text" @click="changeUser(false, scope.row)"
                :disabled="!scope.row.IsAuditRecordUserSelect">
                {{ $t('trials:trials-workbench:inspectionManagement:button:Exit') }}
              </el-button>
            </template>
          </el-table-column>
        </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 { getUserList } from '@/api/admin'
import { addOrDeleteAuditRecordUser } from '@/api/trials'
import Pagination from '@/components/Pagination'
const getListQueryDefault = () => {
  return {
    RealName: '',
    UserName: '',
    OrganizationName: '',
    IsAuditRecordUserSelect: null,
    UserTypeEnum: '16',
    PageIndex: 1,
    PageSize: 20,
  }
}
export default {
  components: { Pagination },
  props: {
    AuditRecordId: {
      type: String,
      default: ''
    },
    isView: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      list: [],
      total: 0,
      listQuery: getListQueryDefault(),
      selectArr: [],
      assignLoadStatus: false,
      // isAdmin: JSON.parse(zzSessionStorage.getItem('IsAdmin')),
      userTypeOptions: [],
      trialId: '',
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    async changeUser(IsAdd = true, row) {
      try {
        let data = {
          AuditRecordId: this.AuditRecordId,
          IsAdd,
          identityUserIdList: [row.Id]
        }
        let res = await addOrDeleteAuditRecordUser(data)
        if (res.IsSuccess) {
          this.getList()
          this.$emit("getList")
        }
      } catch (err) {
        console.log(err)
      }
    },
    getList() {
      const loading = this.$loading({
        target: document.querySelector('.participant-table-list'),
        fullscreen: false,
        lock: true,
      })
      this.listQuery.UserState = 1
      this.listQuery.AuditRecordId = this.AuditRecordId
      if (this.isView) this.listQuery.IsAuditRecordUserSelect = true
      getUserList(this.listQuery)
        .then((res) => {
          loading.close()
          this.list = res.Result.CurrentPageData
          this.total = res.Result.TotalCount
        })
        .catch(() => {
          loading.close()
        })
    },
    handleAssign() {
      this.$emit('add', this.selectArr)
    },
    handleSearch() {
      this.listQuery.PageIndex = 1
      this.getList()
    },
    handleReset() {
      this.listQuery = getListQueryDefault()
      if (this.isView) this.listQuery.IsAuditRecordUserSelect = true
      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()
    },
  },
}
</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>
