<template>
  <el-form
    ref="oncologyForm"
    v-loading="loading"
    :model="form"
    size="small"
    :rules="rules"
    label-width="110px"
  >
    <!-- 评估结果 -->
    <el-form-item :label="$t('trials:readingUnit:oncologyReading:title:oncologyAssessIdsStr')" prop="OncologyAssessIdsStr">
      <el-input
        v-model="form.OncologyAssessIdsStr"
        type="textarea"
        :autosize="{ minRows: 1, maxRows: 4}"
        show-word-limit
        style="width:400px"
        :disabled="form.IsSystemCriterion || isSign || !hasPermi(['trials:trials-panel:setting:reading-unit:edit'])"
      />
      <el-button
        :disabled="form.IsSystemCriterion || isSign || !hasPermi(['trials:trials-panel:setting:reading-unit:edit'])"
        icon="el-icon-plus"
        circle
        @click="handleSetCriterion"
      />
      <!--      <el-checkbox-group :disabled="form.IsSystemCriterion" v-model="form.OncologyAssessIds">-->
      <!--        <el-checkbox v-for="item of $d.OncologyAssessType" :label="item.id">-->
      <!--          {{item.raw.ValueCN}}-->
      <!--        </el-checkbox>-->
      <!--      </el-checkbox-group>-->
    </el-form-item>
    <!-- 评估原因说明 -->
    <el-form-item :label="$t('trials:readingUnit:oncologyReading:title:evaluationReason')" prop="EvaluationReason">
      <el-input
        v-model="form.EvaluationReason"
        type="textarea"
        :autosize="{ minRows: 3, maxRows: 4}"
        maxlength="500"
        show-word-limit
        style="width:400px"
        :disabled="isSign || !hasPermi(['trials:trials-panel:setting:reading-unit:edit'])"
      />
    </el-form-item>
    <!-- 是否展示详情 -->
       <el-form-item
         :label="$t('trials:readingUnit:oncologyReading:title:showDetails')"
         prop="IsShowDetail"
       >
         <el-radio-group
           v-model="form.IsShowDetail"
           :disabled="isSign || !hasPermi(['trials:trials-panel:setting:reading-unit:edit'])"
         >
           <el-radio
             v-for="item of $d.YesOrNo"
             :key="`IsShowDetail${item.value}`"
             :label="item.value"
           >
             {{ item.label }}
           </el-radio>
         </el-radio-group>
       </el-form-item>
    <el-form-item v-if="!isSign && hasPermi(['trials:trials-panel:setting:reading-unit:edit'])">
      <!-- 保存 -->
      <el-button
        size="small"
        type="primary"
        @click="handleSave(true)"
      >
        {{ $t('common:button:save') }}
      </el-button>
    </el-form-item>
    <el-dialog
      v-if="config.visible"
      :visible.sync="config.visible"
      :close-on-click-modal="false"
      :title="config.title"
      width="90%"
      append-to-body
    >
      <div style="text-align: right">
        <el-button
          v-loading="loading"
          size="mini"
          type="primary"
          @click="handleTypeSave"
        >
          {{ $t('common:button:save') }}
        </el-button>
      </div>
      <el-table
        ref="multipleTable"
        v-loading="loading"
        v-adaptive="{bottomOffset:80}"
        height="100"
        :data="$d.OncologyAssessType"
        stripe
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
        />
        <!-- 中文值 -->
        <el-table-column
          prop="raw.ValueCN"
          :label="$t('trials:readingUnit:oncologyReading:title:valueCN')"
          show-overflow-tooltip
        />
        <!-- 英文值 -->
        <el-table-column
          prop="raw.Value"
          :label="$t('trials:readingUnit:oncologyReading:title:valueEN')"
          show-overflow-tooltip
        />
      </el-table>
    </el-dialog>
  </el-form>
</template>
<script>
import { getOncologySet, setOncologySet } from '@/api/trials'
export default {
  name: 'OncologyForm',
  props: {
    trialReadingCriterionId: {
      type: String,
      default() {
        return ''
      }
    }
  },
  data() {
    return {
      form: {
        TrialId: '',
        EvaluationResult: '',
        EvaluationReason: '',
        IsShowDetail: true,
        OncologyAssessIds: [],
        IsSystemCriterion: true,
        OncologyAssessIdsStr: null
      },
      rules: {
        OncologyAssessIdsStr: [{ required: true, message: this.$t('common:ruleMessage:specify'), trigger: ['blur'] }],
        EvaluationReason: [{ required: true, message: this.$t('common:ruleMessage:specify'), trigger: ['blur', 'change'] }],
        IsShowDetail: [{ required: true, message: this.$t('common:ruleMessage:select'), trigger: ['blur', 'change'] }]
      },
      config: { visible: false, title: this.$t('trials:readingUnit:oncologyReading:title:oncologyReading') }, // '肿瘤学阅片'
      loading: false,
      isSign: true,
      selectedList: []
    }
  },
  mounted() {
    this.initForm()
  },
  methods: {
    handleSelectionChange(val) {
      this.selectedList = val
    },
    toggleSelection(rows) {
      console.log(this.$refs.multipleTable)
      if (rows) {
        rows.forEach(row => {
          this.$refs.multipleTable.toggleRowSelection(row)
        })
      } else {
        this.$refs.multipleTable.clearSelection()
      }
    },
    handleSetCriterion() {
      this.config.visible = true
      this.$nextTick(() => {
        var a = this.$d.OncologyAssessType.filter(v => {
          return !!this.form.OncologyAssessIds.find(v1 => {
            return v1 === v.id
          })
        })
        this.toggleSelection(a)
      })
    },
    handleTypeSave() {
      this.form.OncologyAssessIds = this.selectedList.map(v => v.id)
      this.form.OncologyAssessIdsStr = this.form.OncologyAssessIds.map(v => this.$fd('OncologyAssessType', v, 'id')).toString()
      this.config.visible = false
    },
    initForm() {
      this.loading = true
      getOncologySet({ trialId: this.$route.query.trialId, TrialReadingCriterionId: this.trialReadingCriterionId }).then(res => {
        if (Object.keys(res.Result).length > 0) {
          for (const k in res.Result) {
            if (this.form.hasOwnProperty(k)) {
              this.form[k] = res.Result[k]
            }
          }
          this.isSign = res.Result.IsSign
        }
        this.form.OncologyAssessIdsStr = this.form.OncologyAssessIds.map(v => this.$fd('OncologyAssessType', v, 'id')).toString()
        this.loading = false
      }).catch(() => {
        this.loading = false
      })
    },
    handleSave(isPrompt) {
      return new Promise((resolve, reject) => {
        this.$refs['oncologyForm'].validate((valid) => {
          if (!valid) {
            resolve(false)
          } else {
            this.loading = true
            // 保存配置信息
            this.form.TrialId = this.$route.query.trialId
            this.form.TrialReadingCriterionId = this.trialReadingCriterionId
            setOncologySet(this.form).then(res => {
              this.loading = false
              if (res.IsSuccess && isPrompt) {
                this.$message.success(this.$t('common:message:savedSuccessfully'))
              }
              resolve(true)
            }).catch(_ => {
              this.loading = false
              resolve(false)
            })
          }
        })
      })
    }
  }
}
</script>
