<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 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" style="width:120px;">
            <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="400" @selection-change="handleSelectChange"
      @select="handleselect" @select-all="handleselectAll">
      <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('YesOrNo', scope.row.IsEnable) }}
        </template>
      </el-table-column>
    </el-table>
    <Pagination style="margin-top: 10px" class="page" :total="total" :page.sync="searchData.PageIndex"
      :limit.sync="searchData.PageSize" @pagination="getList" />
  </box-content>
</template>
<script>
import { getQCQuestionConfigureList } from '@/api/dictionary'
import { batchAddTrialQCQuestionConfigure } from '@/api/trials'
import BoxContent from '@/components/BoxContent'
import Pagination from '@/components/Pagination'
import { mapGetters } from 'vuex'

const searchDataDefault = () => {
  return {
    Asc: true,
    SortField: 'ShowOrder',
    QuestionName: '',
    LanguageType: null,
    Type: '',
    IsEnable: true,
    IsDefeaultViewParent: false,
    PageIndex: 1,
    PageSize: 20
  }
}
export default {
  name: 'QcQuestions',
  components: { BoxContent, Pagination },
  data() {
    return {
      trialId: '',
      searchData: searchDataDefault(),
      list: [],
      total: 0,
      loading: false,
      selectArr: []
    }
  },
  computed: {
    ...mapGetters(['language'])
  },
  created() {
    this.trialId = this.$route.query.trialId;
    this.getList();
  },
  methods: {
    // 获取受试者列表信息
    getList() {
      this.loading = true
      this.searchData.TrialId = this.trialId;
      if (this.$i18n.locale === 'zh') {
        this.searchData.LanguageType = 0
      } else {
        this.searchData.LanguageType = 1
      }
      getQCQuestionConfigureList(this.searchData).then(res => {
        this.loading = false
        this.list = res.Result.CurrentPageData
        this.total = res.Result.TotalCount
      }).catch(() => { this.loading = false })
    },
    handleSelectChange(val) {
      // this.selectArr = val
    },
    handleselectAll(selection) {
      this.selectArr = []
      if (selection.length > 0) {
        this.selectArr = this.list
      }
    },
    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)
      })
      let list = [...selection, ...arr]
      if (selected) {
        list.forEach(item => {
          if (!this.selectArr.find(d => d.Id === item.Id)) {
            this.selectArr.push(item)
          }
        })
      } else {
        list = [row, ...arr]
        list.forEach(item => {
          let index = this.selectArr.indexOf(d => d.Id === item.Id)
          if (!~index) {
            this.selectArr.splice(index, 1)
          }
        })
      }
    },
    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>
