<template>
  <el-form
    ref="clinicalDataForm"
    v-loading="loading"
    :model="form"
    size="small"
    :rules="rules"
    label-width="110px"
  >
    <div class="base-dialog-body">
      <el-form-item label="类型" prop="Type">
        <el-select v-model="form.Type">
          <el-option
            v-for="item of $d.Criterion_Question_Type"
            :key="item.value"
            :value="item.value"
            :label="item.label"
          />
        </el-select>
      </el-form-item>
      <el-form-item
        v-if="form.Type === 'group'"
        label="分组名称"
        prop="GroupName"
      >
        <el-input v-model="form.GroupName" />
      </el-form-item>
      <el-form-item
        v-if="form.Type !== 'group'"
        label="问题名称"
        prop="QuestionName"
      >
        <el-input v-model="form.QuestionName" />
      </el-form-item>

      <el-form-item
        v-if="form.Type !== 'textarea' && form.Type !== 'input' && form.Type !== 'group' && form.Type !== 'summary' && form.Type !== 'upload' && form.Type !== 'number'"
        label="选项"
        prop="TypeValue"
      >
        <el-input v-model="form.TypeValue" placeholder="选项请用‘|’分割多个选项" />
      </el-form-item>

      <!-- 父问题 -->
      <el-form-item v-if="form.Type !== 'group'" label="父问题">
        <el-select v-model="form.ParentId" clearable @change="((val)=>{parentQuestionChange(val, form)})">
          <el-option
            v-for="item of parentOptions"
            :key="item.QuestionId"
            :label="item.QuestionName"
            :value="item.QuestionId"
          />
        </el-select>
      </el-form-item>
      <!-- 父问题触发值 -->
      <el-form-item v-if="form.ParentId" label="父问题触发值" prop="ParentTriggerValue">
        <el-select v-model="form.ParentTriggerValue">
          <el-option
            v-for="item of parentTriggerValOptions"
            :key="item"
            :label="item"
            :value="item"
          />
        </el-select>
      </el-form-item>

      <el-form-item v-if="form.Type !== 'group'" label="问题分组">
        <el-select v-model="form.GroupName" clearable :disabled="isParentExistGroup">
          <el-option
            v-for="group of groupOptions"
            :key="group"
            :label="group"
            :value="group"
          />
        </el-select>
      </el-form-item>

      <el-form-item v-if="form.Type !== 'group'" label="是否必填">
        <el-radio-group v-model="form.IsRequired">
          <el-radio v-for="item of $d.YesOrNo" :label="item.value" :key="item.id">{{ item.label }}</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item v-if="form.Type === 'select' || form.Type === 'radio'" label="是否裁判问题">
        <el-radio-group v-model="form.IsJudgeQuestion">
          <el-radio v-for="item of $d.YesOrNo" :label="item.value" :key="item.id">{{ item.label }}</el-radio>
        </el-radio-group>
      </el-form-item>

      <el-form-item v-if="form.Type !== 'group' && form.Type !== 'summary'" label="注释">
        <el-input v-model="form.Remark" />
      </el-form-item>
      <!-- <el-form-item label="启用状态: ">
        <el-radio-group v-model="form.IsEnable">
          <el-radio :label="true">启用</el-radio>
          <el-radio :label="false">停用</el-radio>
        </el-radio-group>
      </el-form-item> -->

      <el-form-item label="序号" prop="ShowOrder">
        <el-input-number v-model="form.ShowOrder" controls-position="right" :min="0" />
      </el-form-item>
    </div>
    <div class="base-dialog-footer" style="text-align:right;margin-top:10px;">
      <el-form-item>
        <!-- 取消 -->
        <el-button
          :disabled="btnLoading"
          size="small"
          type="primary"
          @click="close"
        >
          {{ $t('common:button:cancel') }}
        </el-button>
        <!-- 保存 -->
        <el-button size="small" type="primary" :loading="btnLoading" @click="save">
          {{ $t('common:button:save') }}
        </el-button>
      </el-form-item>
    </div>
  </el-form>
</template>
<script>
import { addOrUpdateReadingQuestionTrial, getTrialCriterionOtherQuestion, getTrialGroupNameList } from '@/api/trials'
export default {
  name: 'AddOrUpdateClinicalData',
  props: {
    data: {
      type: Object,
      default() { return {} }
    }
  },

  data() {
    return {
      form: {
        Id: '',
        ReadingQuestionCriterionTrialId: '',
        QuestionName: '',
        Type: 'select',
        TypeValue: '',
        ParentId: '',
        ParentTriggerValue: '',
        ShowOrder: 0,
        IsRequired: true,
        IsJudgeQuestion: false,
        GroupName: '',
        Remark: ''
        // IsEnable: true
      },
      rules: {
        Type: [
          { required: true, message: '请选择', trigger: 'blur' }
        ],
        QuestionName: [{ required: true, message: '请注明', trigger: 'blur' },
          { max: 50, message: '最大长度为 50' }],
        TypeValue: [{ required: true, message: '请注明', trigger: 'blur' },
          { max: 200, message: '最大长度为 200' }],
        ParentTriggerValue: [
          { required: true, message: '请选择', trigger: 'blur' }
        ],
        GroupName: [{ required: true, message: '请注明', trigger: 'blur' },
          { max: 50, message: '最大长度为 50' }]
      },
      loading: false,
      btnLoading: false,
      parentOptions: [],
      parentTriggerValOptions: [],
      groupOptions: [],
      isParentExistGroup: false
    }
  },
  mounted() {
    this.initForm()
  },
  methods: {
    async initForm() {
      await this.getGroupOptions()
      await this.getParentQuestions()
      if (Object.keys(this.data).length > 0) {
        for (const k in this.form) {
          if (this.data.hasOwnProperty(k)) {
            this.form[k] = this.data[k]
          }
        }
        if (this.form.ParentId !== '' && this.form.ParentId !== null && this.form.GroupName !== '') {
          this.isParentExistGroup = true
        }
      }
    },
    save() {
      this.$refs.clinicalDataForm.validate(valid => {
        if (!valid) return
        this.btnLoading = true
        this.form.TrialId = this.$route.query.trialId
        addOrUpdateReadingQuestionTrial(this.form).then(res => {
          this.btnLoading = false
          this.$emit('getList')
          this.$emit('close')
          this.$message.success(this.$t('common:message:savedSuccessfully'))
        })
          .catch(() => {
            this.btnLoading = false
          })
      })
    },
    getGroupOptions() {
      return new Promise((resolve, reject) => {
        this.loading = true
        var param = {
          criterionId: this.data.ReadingQuestionCriterionTrialId
        }
        getTrialGroupNameList(param).then(res => {
          this.groupOptions = res.Result
          this.loading = false
          resolve()
        }).catch(() => {
          this.loading = false
          reject()
        })
      })
    },
    getParentQuestions() {
      return new Promise((resolve, reject) => {
        this.loading = true
        var param = {
          ReadingQuestionCriterionTrialId: this.data.ReadingQuestionCriterionTrialId,
          id: this.data.Id
        }
        getTrialCriterionOtherQuestion(param).then(res => {
          this.parentOptions = res.Result
          this.loading = false
          resolve()
        }).catch(() => {
          this.loading = false
          reject()
        })
      })
    },

    parentQuestionChange(val, form) {
      this.isParentExistGroup = false
      if (val) {
        var index = this.parentOptions.findIndex(item => {
          return item.QuestionId === val
        })
        if (index !== -1) {
          this.parentTriggerValOptions = this.parentOptions[index].TypeValue.split('|')
          if (this.parentOptions[index].GroupName) {
            this.isParentExistGroup = true
            form.GroupName = this.parentOptions[index].GroupName
          }
        }
      } else {
        form.ParentTriggerValue = ''
        form.GroupName = ''
      }
    },
    close() {
      this.$emit('close')
    }

  }
}
</script>
<style lang="scss" scoped>
</style>
