<template>
  <BaseContainer v-loading="loading">
    <!-- 搜索框 -->
    <template slot="search-container">
      <el-form :inline="true">
        <el-form-item label="类型">
          <el-select v-model="searchData.OrganType" clearable style="width:130px;">
            <el-option v-for="item of $d.OrganInfo" :key="item.id" :value="item.value" :label="item.label" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <!-- 查询 -->
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleSearch">
            {{ $t('common:button:search') }}
          </el-button>
          <!-- 重置 -->
          <el-button type="primary" icon="el-icon-refresh-left" size="mini" @click="handleReset">
            {{ $t('common:button:reset') }}
          </el-button>
        </el-form-item>
      </el-form>
      <span style="margin-left:auto;">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          :disabled="selectArr.length === 0"
          @click="handleAdd"
        >
          提交
        </el-button>
      </span>

    </template>

    <template slot="main-container">
      <!-- 受试者列表 -->
      <el-table
        ref="organList"

        v-adaptive="{bottomOffset:60}"
        :data="list"
        stripe
        height="100"
        @selection-change="handleSelectChange"
      >
        <el-table-column type="selection" align="left" width="45" :selectable="handleSelectable" />
        <el-table-column type="index" width="90" />
        <!-- 类型 -->
        <el-table-column
          prop="OrganType"
          label="类型"
          show-overflow-tooltip
        >
          <template slot-scope="scope">
            {{ $fd('OrganInfo',scope.row.OrganType) }}
          </template>
        </el-table-column>
        <!-- 部位 -->
        <el-table-column
          prop="Part"
          label="部位"
          show-overflow-tooltip
        />
        <el-table-column
          prop="TULOC"
          label="TULOC"
          show-overflow-tooltip
        />
        <el-table-column
          prop="TULAT"
          label="TULAT"
          show-overflow-tooltip
        />
        <el-table-column
          prop="Remark"
          label="备注"
          show-overflow-tooltip
        />
      </el-table>

      <!-- 分页组件 -->
      <!-- <pagination class="page" :total="total" :page.sync="searchData.PageIndex" :limit.sync="searchData.PageSize" @pagination="getList" /> -->
    </template>
  </BaseContainer>
</template>
<script>
import { getTrialCheckOrganList, batchAddTrialOrgan } from '@/api/trials'
import BaseContainer from '@/components/BaseContainer'
const searchDataDefault = () => {
  return {
    OrganType: null
  }
}
export default {
  name: 'DefaultOrgansList',
  components: { BaseContainer },
  data() {
    return {
      searchData: searchDataDefault(),
      loading: false,
      list: [],
      total: 0,
      selectArr: []
    }
  },
  watch: {
    list() {
      this.$nextTick(() => {
        this.$refs.organList.doLayout()
      })
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 获取受试者列表
    getList() {
      this.loading = true
      this.searchData.trialId = this.$route.query.trialId
      getTrialCheckOrganList(this.searchData).then(res => {
        this.loading = false
        this.list = res.Result
        // this.list = res.Result.CurrentPageData
        // this.total = res.Result.TotalCount
      }).catch(() => { this.loading = false })
    },
    handleSelectChange(val) {
      var ids = []
      val.map(v => {
        ids.push(v.Id)
      })
      this.selectArr = ids
    },
    handleAdd() {
      this.loading = true
      var param = {
        trialId: this.$route.query.trialId,
        organIds: this.selectArr
      }
      batchAddTrialOrgan(param).then(res => {
        this.loading = false
        this.getList()
        this.$emit('getList')
      }).catch(() => {
        this.loading = false
      })
    },
    // 查询
    handleSearch() {
      // this.searchData.PageIndex = 1
      this.getList()
    },
    // 重置
    handleReset() {
      this.searchData = searchDataDefault()
      this.getList()
    },
    // 排序
    handleSortByColumn(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()
    },
    handleSelectable(row) {
      return !row.IsCheckd
    }
  }
}
</script>
