<template>
  <BaseContainer>
    <template slot="search-container">
      <el-form :inline="true">
        <el-form-item :label="$t('trials:site:table:siteId')">
          <el-input
            v-model="searchData.TrialSiteCode"
            style="width:130px;"
            clearable
          />
        </el-form-item>
        <!-- 中心 -->
        <el-form-item :label="$t('trials:site:table:site')">
          <el-input
            v-model="searchData.SiteName"
            style="width:130px;"
            clearable
          />
        </el-form-item>
        <!-- 参与者 -->
        <el-form-item :label="$t('trials:site:table:staff')">
          <el-input
            v-model="searchData.UserRealName"
            style="width:130px;"
            clearable
          />
        </el-form-item>

        <el-form-item>
          <!-- 查询 -->
          <el-button type="primary" icon="el-icon-search" @click="handleSearch">
            {{ $t('common:button:search') }}
          </el-button>
          <!-- 重置 -->
          <el-button type="primary" icon="el-icon-refresh-left" @click="handleReset">
            {{ $t('common:button:reset') }}
          </el-button>
        </el-form-item>
      </el-form>
    </template>
    <template slot="main-container">
      <el-table
        v-adaptive="{bottomOffset:60}"
        v-loading="loading"
        :data="list"
        stripe
        height="100"
        @sort-change="handleSortChange"
      >
        <el-table-column type="index" width="40" align="left" />
        <!-- 中心编号 -->
        <el-table-column
          prop="TrialSiteCode"
          min-width="100"
          :label="$t('trials:site:table:siteId')"
          show-overflow-tooltip
          sortable="custom"
        />
        <!-- 中心名称 -->
        <el-table-column
          prop="TrialSiteAliasName"
          min-width="100"
          :label="$t('trials:site:table:site')"
          show-overflow-tooltip
          sortable="custom"
        />
        <!-- City -->
        <el-table-column
          prop="City"
          min-width="100"
          :label="$t('trials:site:table:city')"
          show-overflow-tooltip
          sortable="custom"
        />
        <!-- Country -->
        <el-table-column
          prop="Country"
          min-width="100"
          :label="$t('trials:site:table:country')"
          show-overflow-tooltip
          sortable="custom"
        />
        <!-- Subjects -->
        <el-table-column
          prop="SubjectCount"
          min-width="100"
          :label="$t('trials:site:table:subjects')"
          show-overflow-tooltip
          sortable="custom"
        />
        <!-- Visits -->
        <el-table-column
          prop="VisitCount"
          min-width="100"
          :label="$t('trials:site:table:visits')"
          show-overflow-tooltip
          sortable="custom"
        />
        <!-- Staff -->
        <el-table-column
          prop="UserCount"
          min-width="150"
          :label="$t('trials:site:table:staff')"
          show-overflow-tooltip
          sortable="custom"
        >
          <template slot-scope="scope">
            <el-button v-if="scope.row.UserCount > 0" type="text" @click="getStaffList(scope.row)">
              {{ scope.row.UserNameList.length>0 ? scope.row.UserNameList.join(', ') :'' }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <pagination class="page" :total="total" :page.sync="searchData.PageIndex" :limit.sync="searchData.PageSize" @pagination="getList" />
    </template>
    <base-model v-if="crc_model.visible" :config="crc_model">
      <template slot="dialog-body">
        <el-table v-loading="userListLoading" :data="userList" height="400">
          <!-- Name -->
          <el-table-column
            prop="UserRealName"
            :label="$t('trials:site:table:name')"
            width="150"
          />
          <!-- User Type -->
          <el-table-column
            prop="UserType"
            :label="$t('trials:site:table:userType')"
            width="120"
            show-overflow-tooltip
          />
          <!-- Organization -->
          <el-table-column
            prop="OrganizationName"
            :label="$t('trials:site:table:organization')"
            width="150"
          />
          <!-- Phone -->
          <el-table-column
            prop="Phone"
            :label="$t('trials:site:table:phone')"
            show-overflow-tooltip
            sortable="custom"
            width="150"
          />
          <!-- Status -->
          <el-table-column
            prop="IsDeleted"
            :label="$t('trials:site:table:status')"
            show-overflow-tooltip
            sortable
            min-width="100"
          >
            <template slot-scope="scope">
              <el-tag v-if="scope.row.IsDeleted" type="danger">{{ $fd('IsUserExitTrial', scope.row.IsDeleted) }}</el-tag>
              <el-tag v-else>{{ $fd('IsUserExitTrial', scope.row.IsDeleted) }}</el-tag>
            </template>
          </el-table-column>
          <!-- 授权时间 -->
          <el-table-column
            prop="UpdateTime"
            :label="$t('trials:site:table:dataAssigned')"
            show-overflow-tooltip
            sortable="custom"
            min-width="150"
          />
          <!-- 禁用时间 -->
          <el-table-column
            prop="DeletedTime"
            :label="$t('trials:site:table:deletedTime')"
            show-overflow-tooltip
            sortable
            min-width="150"
          />

        </el-table>
      </template>
    </base-model>
  </BaseContainer>
</template>
<script>
import { getSiteCRCList, getTrialSiteCRCList } from '@/api/trials'
import BaseContainer from '@/components/BaseContainer'
import BaseModel from '@/components/BaseModel'
import Pagination from '@/components/Pagination'
const searchDataDefault = () => {
  return {
    UserRealName: '',
    SiteName: '',
    SortField: 'TrialSiteCode',
    Asc: true,
    PageIndex: 1,
    PageSize: 20,
    TrialSiteCode: null
  }
}
export default {
  name: 'SiteList',
  components: { BaseContainer, BaseModel, Pagination },
  data() {
    return {
      searchData: searchDataDefault(),
      list: [],
      total: 0,
      loading: false,
      crc_model: { visible: false, title: this.$t('trials:site:dialogTitle:staff'), width: '60%' },
      userListLoading: '',
      userList: [],
      trialId: ''
    }
  },
  mounted() {
    this.trialId = this.$route.query.trialId

    this.getList()
  },
  methods: {
    getList() {
      this.loading = true
      this.searchData.TrialId = this.trialId
      getSiteCRCList(this.searchData).then(res => {
        this.loading = false
        this.list = res.Result.CurrentPageData
        this.total = res.Result.TotalCount
      }).catch(() => { this.loading = false })
    },
    getStaffList(row) {
      this.currentRow = { ...row }
      this.crc_model.visible = true
      this.userListLoading = true
      getTrialSiteCRCList(this.trialId, this.currentRow.SiteId).then(res => {
        this.userListLoading = false
        this.userList = res.Result
      }).catch(() => { this.userListLoading = false })
    },
    handleSearch() {
      this.searchData.PageIndex = 1
      this.getList()
    },
    handleReset() {
      this.searchData = searchDataDefault()
      this.getList()
    },
    // 排序
    handleSortChange(column) {
      if (column.order === 'ascending') {
        this.searchData.Asc = true
      } else {
        this.searchData.Asc = false
      }
      this.searchData.SortField = column.prop
      this.searchData.PageIndex = 1
      this.getList()
    }
  }
}
</script>
