<template>
  <div>
    <!-- 保存 -->
    <el-button type="primary" size="mini" @click="submit" style="margin-bottom: 5px;">
      {{ $t('common:button:save') }}
    </el-button>
    <el-table
      v-loading="loading"
      :data="exportInfo.QuestionList"
      height="500"
      row-key="Id"
      border
      default-expand-all
      :tree-props="{ children: 'Children', hasChildren: 'hasChildren' }"
    >
      <!-- 问题名称 -->
      <el-table-column 
        prop="QuestionName" 
        :label="$t('trials:readingTracking:config:questionName')"  
        width="300">
      </el-table-column>
      <el-table-column 
        v-for="item in exportInfo.DicList"
        :key="item.Code" 
        :label="$i18n.locale === 'zh' ? item.ValueCN : item.Value"
        
      >
        <template v-slot="scope">
          <el-checkbox
            @change="() => changeState(scope, item.Code)"
            :checked="getCheckState(scope, item.Code)"
            :disabled="getDisabledState(scope.row, item)"
            v-show="!scope.row.IsGroup || !scope.row.HasChildren"
          >
          </el-checkbox>
        </template>
      </el-table-column>
      <!-- CDISC编码 -->
      <el-table-column
        prop="CDISCCode"
        :label="$t('trials:readingTracking:config:code')"   
        width="180" 
      >
       <template v-slot="scope">
          <el-input 
            v-model="scope.row.CDISCCode"
            size="mini"
            :disabled="scope.row.HasChildren || (scope.row.IsTableQuestion && exportInfo.CriterionType !== 0 && exportInfo.CriterionGroup !== 0)"
            v-show="!scope.row.IsGroup || !scope.row.HasChildren"
          >
          </el-input>
        </template>

      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import { getTrialQuestionExportResult, setTrialQuestionExportResult } from '@/api/trials'
export default {
  name: 'configList',
  props: {
    trialReadingCriterionId: {
      type: String,
      required: true
    },
  },
  data() {
    return {
      loading: false,
      exportInfo: {}
    }
  },
  mounted() {
    this.getConfigInfo()
  },
  methods: {
    async getConfigInfo() {
      try {
        this.loading = true
        let res = await getTrialQuestionExportResult(
          {
            trialReadingCriterionId: this.trialReadingCriterionId
          }
        )
        if (res.IsSuccess) {
          this.exportInfo = res.Result
        }
        this.loading = false
      } catch(e) {
        this.loading = false
        console.log(e)
      }
    },
    async submit() {
      this.loading = true
      try {
        let params = this.filterQuestions(this.exportInfo.QuestionList)
        let res = await setTrialQuestionExportResult(params)
        if (res.IsSuccess) {
          this.$message.success(this.$t('common:message:savedSuccessfully'))
        }
        this.loading = false
      } catch(e) {
        this.loading = false
        console.log(e)
      }
    },
    filterQuestions(arr) {
      let params= {
        questionList: [],
        tableQuestionList: []
      }
      for (let i of arr) {
        if (!i.IsGroup && !i.IsTableQuestion) {
          params.questionList.push({
            questionId: i.QuestionId,
            tableQuestionId: i.TableQuestionId,
            exportResult: i.ExportResult,
            CDISCCode: i.CDISCCode
          })
        }
        if (i.TableQuestionId) {
          params.tableQuestionList.push({
            questionId: i.QuestionId,
            tableQuestionId: i.TableQuestionId,
            exportResult: i.ExportResult,
            CDISCCode: i.CDISCCode
          })
        }
        if (i.Children && i.Children.length > 0) {
            const childResults = this.filterQuestions(i.Children)
            params.questionList = params.questionList.concat(childResults.questionList)
            params.tableQuestionList = params.tableQuestionList.concat(childResults.tableQuestionList)
        }
      }
      return params;
    },
    // 获取状态
    getCheckState(item, code) {
      return item.row.ExportResult.indexOf(code) > -1
    },
    // 改变状态
    changeState(item, code) {
     item.row.ExportResult.indexOf(code) > -1 ? item.row.ExportResult.splice(item.row.ExportResult.indexOf(code), 1) : item.row.ExportResult.push(code);
    },
    // 获取禁用状态
    getDisabledState(row, item) {
      // 肿瘤评估标准阅片结果表(1)和阅片结果明细表(2)禁用
      if (this.exportInfo.CriterionGroup === 1 && (item.Code === 1 || item.Code === 2)) {
        return true
      } else if ((this.exportInfo.CriterionGroup === 0 || this.exportInfo.CriterionType === 0 ) && item.Code === 1 && (row.HasChildren || row.IsTableQuestion)) {
        // 自定义或非肿瘤阅片标准阅片结果表(1)不能配置表格问题
        return true
      } else if ( item.Code === 8 && (row.HasChildren || (row.IsTableQuestion && this.exportInfo.CriterionType !== 0 && this.exportInfo.CriterionGroup !== 0) )) {
        // else if (item.Code === 8 && (row.HasChildren || row.IsTableQuestion))
        // CDISC导出不能配置表格问题
        return true
      } else if (item.Code === 2 && row.HasChildren) {
        // 阅片结果明细表
        return true
      } else {
        return false
      }
      
    }
  }
}
</script>