<template>
  <box-content v-loading="loading">
    <!-- 搜索框 -->
    <div class="search">
      <el-form :inline="true" size="mini" class="base-search-form">
        <!-- 审核问题 -->
        <el-form-item :label="$t('trials:qcCfg:table:questionName')">
          <el-input :maxLength="$inputMax" v-model="searchData.QuestionName" clearable style="width:120px;" />
        </el-form-item>
        <!-- 类型 -->
        <el-form-item :label="$t('trials:qcCfg:table:type')">
          <el-select v-model="searchData.Type" clearable style="width:120px;">
            <el-option v-for="item of $d.QcType" :key="item.label" :value="item.value" :label="item.label" />
          </el-select>
        </el-form-item>
        <!-- 级别: -->
        <el-form-item :label="$t('trials:qcCfg:table:level')">
          <el-select v-model="searchData.IsDefeaultViewParent">
            <el-option v-for="item of $d.IsDefeaultViewParentLevel" :key="item.id" :value="item.value" :label="item.label" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">{{ $t('common:button:search') }}</el-button>
          <el-button type="primary" @click="handleReset">{{ $t('common:button:reset') }}</el-button>
        </el-form-item>
      </el-form>
      <span style="margin-left:auto">
        <el-button type="primary" size="mini" :disabled="selectArr.length<=0" @click="handleSubmit">
          {{ $t('common:button:submit') }}
        </el-button>
      </span>
    </div>

    <el-table
      ref="qsList"
      :data="list"
      stripe
      size="small"
      height="600"
      @selection-change="handleSelectChange"
      @select="handleselect"
    >
      <el-table-column type="selection" align="left" width="45" :selectable="handleSelectTable" />
      <!-- 序号 -->
      <el-table-column
        prop="ShowOrder"
        :label="$t('trials:qcCfg:table:order')"
        width="60"
      />
      <!-- 审核问题 -->
      <el-table-column
        prop="QuestionName"
        :label="$t('trials:qcCfg:table:questionName')"
        show-overflow-tooltip
      />
      <!-- 类型 -->
      <el-table-column
        prop="Type"
        :label="$t('trials:qcCfg:table:type')"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          {{ $fd('QcType', scope.row.Type) }}
        </template>
      </el-table-column>
      <!-- 选项 -->
      <el-table-column
        prop="TypeValue"
        :label="$t('trials:qcCfg:table:typeValue')"
        show-overflow-tooltip
        min-width="110"
      />

      <!-- 父问题 -->
      <el-table-column
        prop="ParentShowOrder"
        :label="$t('trials:qcCfg:table:parentQs')"
        show-overflow-tooltip
        width="120"
      />
      <!-- 父问题触发值 -->
      <el-table-column
        prop="ParentTriggerValue"
        :label="$t('trials:qcCfg:table:parentTriggerValue')"
        show-overflow-tooltip
      />
      <!-- 是否必填 -->
      <el-table-column
        prop="IsRequired"
        :label="$t('trials:qcCfg:table:isRequired')"
        min-width="90"
      >
        <template slot-scope="scope">
          {{ $fd('YesOrNo', scope.row.IsRequired) }}
        </template>
      </el-table-column>
      <!-- 启用状态 -->
      <el-table-column
        prop="IsEnable"
        :label="$t('trials:qcCfg:table:isEnable')"
        min-width="120"
      >
        <template slot-scope="scope">
          {{ $fd('IsEnable', scope.row.IsEnable) }}
        </template>
      </el-table-column>
    </el-table>
  </box-content>
</template>
<script>
import { getQCQuestionConfigureList } from '@/api/dictionary'
import { batchAddTrialQCQuestionConfigure } from '@/api/trials'
import BoxContent from '@/components/BoxContent'
const searchDataDefault = () => {
  return {
    Asc: true,
    SortField: 'ShowOrder',
    QuestionName: '',
    Type: '',
    IsEnable: true,
    IsDefeaultViewParent: false
  }
}
export default {
  name: 'QcQuestions',
  components: { BoxContent },
  data() {
    return {
      trialId: '',
      searchData: searchDataDefault(),
      list: [],
      loading: false,
      selectArr: []
    }
  },
  mounted() {
    this.trialId = this.$route.query.trialId
    this.getList()
  },
  methods: {
    // 获取受试者列表信息
    getList() {
      this.loading = true
      this.searchData.TrialId = this.trialId
      getQCQuestionConfigureList(this.searchData).then(res => {
        this.loading = false
        this.list = res.Result
      }).catch(() => { this.loading = false })
    },
    handleSelectChange(val) {
      this.selectArr = val
    },
    handleselect(selection, row) {
      const selected = selection.length && selection.indexOf(row) !== -1
      console.log(selection)

      var arr = this.list.filter(i => i.ParentId === row.Id)
      arr.map(item => {
        this.$refs.qsList.toggleRowSelection(item, selected)
      })
    },
    handleSubmit() {
      this.loading = true
      batchAddTrialQCQuestionConfigure(this.trialId, this.selectArr).then(res => {
        this.loading = false
        if (res.IsSuccess) {
          this.$message.success(this.$t('common:message:savedSuccessfully'))
          this.$emit('getList')
          this.$emit('close')
        }
      }).catch(() => { this.loading = false })
    },
    handleSelectTable(row) {
      return !row.ParentId
    },
    // 查询
    handleSearch() {
      this.getList()
    },
    // 重置
    handleReset() {
      this.searchData = searchDataDefault()
      this.getList()
    }
  }
}
</script>
