<template>
  <el-container class="siteform-container">
    <el-header style="height:50px">
      <div class="filter-container">
        <!-- 中心名称 -->
        <span>{{ $t('trials:customSite:form:siteName') }}:</span>
        <el-input v-model="listQuery.SiteName" size="mini" class="mr" clearable />
        <!-- 查询 -->
        <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>
        <span style="margin-left:auto">
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-plus"
            @click="handleCustomSite"
          >
            {{ $t('trials:sitesList:dialogButton:customSite') }}
          </el-button>
          <el-button
            type="primary"
            size="mini"
            :disabled="selectArr.length === 0"
            :loading="assignLoadStatus"
            icon="el-icon-plus"
            @click="handleAssign"
          >
            {{ $t('trials:sitesList:dialogButton:addSite') }}
          </el-button>
        </span>

      </div>
    </el-header>
    <el-main>
      <div class="data-table">
        <el-table
          :data="list"
          stripe
          height="400px"
          class="site-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="SiteName"
            :label="$t('trials:customSite:form:siteName')"
            show-overflow-tooltip
            sortable="custom"
            min-width="120"
          />
          <el-table-column
            prop="AliasName"
            :label="$t('trials:customSite:form:AliasName')"
            show-overflow-tooltip
            sortable="custom"
            min-width="120"
          />
          <el-table-column
            prop="Country"
            :label="$t('trials:customSite:form:country')"
            show-overflow-tooltip
            sortable="custom"
            min-width="80"
          />
          <el-table-column
            prop="City"
            :label="$t('trials:customSite:form:city')"
            show-overflow-tooltip
            min-width="80"
            sortable="custom"
          />
          <el-table-column
            prop="Address"
            :label="$t('trials:customSite:form:address')"
            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-dialog
      v-if="customVisible"
      :visible.sync="customVisible"
      :close-on-click-modal="false"
      :title="$t('trials:customSite:title:custom')"
      width="600px"
      custom-class="base-dialog-wrapper"
      append-to-body
    >
      <CustomSiteForm v-if="customVisible" @close="closeDialog" @getList="handleReset" />
    </el-dialog>
  </el-container>
</template>
<script>
import { getTrialSiteScreeningList, addTrialSites } from '@/api/trials'
import CustomSiteForm from './customSiteForm'
import Pagination from '@/components/Pagination'
const getListQueryDefault = () => {
  return {
    SiteName: '',
    PageIndex: 1,
    PageSize: 20
  }
}
export default {
  components: { Pagination, CustomSiteForm },
  data() {
    return {
      list: [],
      total: 0,
      listQuery: getListQueryDefault(),
      selectArr: [],
      assignLoadStatus: false,
      customVisible: false,
      trialId: ''
    }
  },
  mounted() {
    this.trialId = this.$route.query.trialId
    this.getList()
  },
  methods: {
    getList() {
      const loading = this.$loading({
        target: document.querySelector('.site-table-list'),
        fullscreen: false,
        lock: true
      })
      this.listQuery.TrialId = this.trialId
      getTrialSiteScreeningList(this.listQuery).then(res => {
        loading.close()
        this.list = res.Result.CurrentPageData
        this.total = res.Result.TotalCount
      }).catch(() => { loading.close() })
    },
    handleCustomSite() {
      this.customVisible = true
    },
    closeDialog() {
      this.customVisible = false
    },
    handleAssign() {
      this.$confirm(this.$t('trials:customSite:message:add'), {
        type: 'warning',
        distinguishCancelAndClose: true
      })
        .then(() => {
          const loading = this.$loading({
            target: document.querySelector('.site-table-list'),
            fullscreen: false,
            lock: true
          })
          this.assignLoadStatus = true
          addTrialSites(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) {
      const arr = []
      for (let index = 0; index < val.length; index++) {
        const param = {
          TrialId: this.trialId,
          SiteId: val[index].Id,
          SiteName: val[index].SiteName
        }
        arr.push(param)
      }
      this.selectArr = arr
    },
    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>
.siteform-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>
