<template>
  <base-model :config="model_cfg">
    <template slot="dialog-body">
      <el-form
        ref="medicalAuditForm"
        v-loading="loading"
        :model="form"
        :rules="rules"
        label-width="120px"
        size="small"
      >
        <el-form-item label="问题名称" prop="QuestionName">
          <el-input :maxLength="$inputMax" v-model="form.QuestionName" />
        </el-form-item>
        <el-form-item label="类型">
          <el-select v-model="form.Type" @change="((val)=>{typeChange(val, form)})">
            <el-option value="select" label="下拉框" />
            <el-option value="input" label="单行文本框" />
            <el-option value="textarea" label="多行文本框" />
            <el-option value="radio" label="单选框" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.Type !== 'textarea' && form.Type !== 'input'" label="选项: " prop="TypeValue">
          <el-input :maxLength="$inputMax" v-model="form.TypeValue" placeholder="选项请用‘|’分割多个选项" />
        </el-form-item>
        <!-- 任务类型 -->
        <el-form-item
          label="是否通用"
          prop="IsGeneral"
        >
          <el-switch
            v-model="form.IsGeneral">
          </el-switch>
          <span>{{$fd('YesOrNo', form.IsGeneral)}}</span>
        </el-form-item>
        <!-- 任务类型 -->
        <el-form-item
          v-if="!form.IsGeneral"
          label="阅片标准"
          prop="CriterionTypeEnum"
        >
          <el-select v-model="form.CriterionTypeEnum">
            <el-option v-for="item of $d.CriterionType" :key="item.id" :value="item.value" :label="item.label" />
          </el-select>
        </el-form-item>
        <el-form-item
          v-if="form.Type === 'select' || form.Type === 'radio'"
          label="任务类型"
          prop="ReadingCategory"
        >
          <el-select v-model="form.ReadingCategory" @change="((val)=>{readingCategoryChange(val, form)})">
            <el-option v-for="item of $d.ReadingCategory" :key="item.id" :value="item.value" :label="item.label" />
          </el-select>
        </el-form-item>
        <!-- 父问题 -->
        <el-form-item
          label="父问题"
        >
          <el-select
            v-model="form.ParentId"
            clearable
            @change="((val)=>{parentQuestionChange(val, form)})"
          >
            <el-option
              v-for="item of parentOptions"
              :key="item.Id"
              :label="item.QuestionName"
              :value="item.Id"
            />
          </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 label="是否必填">
          <el-radio-group v-model="form.IsRequired">
            <el-radio :label="true">是</el-radio>
            <el-radio :label="false">否</el-radio>
          </el-radio-group>
        </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>
      </el-form>
    </template>
    <template slot="dialog-footer">
      <el-button :disabled="btnLoading" size="small" type="primary" @click="handleCancle">取消</el-button>
      <el-button size="small" type="primary" :loading="btnLoading" @click="handleSave">保存</el-button>
    </template>
  </base-model>
</template>
<script>
import { addOrUpdateReadingMedicineSystemQuestion, getReadingMedicineSystemOtherQuestion } from '@/api/dictionary'
import BaseModel from '@/components/BaseModel'
const formDataDefault = () => {
  return {
    Id: '',
    QuestionName: '',
    IsRequired: true,
    IsEnable: true,
    Type: 'select',
    TypeValue: '',
    ShowOrder: 0,
    ParentId: '',
    ParentTriggerValue: '',
    ReadingCategory: '',
    CriterionTypeEnum: null,
    IsGeneral: true
  }
}
export default {
  name: 'MedicalAuditForm',
  components: { BaseModel },
  data() {
    var validateTypeVal = (rule, value, callback) => {
      if (value === '') {
        callback(new Error(this.$t('common:ruleMessage:specify')))
      } else {
        var arr = value.split('|')
        if (new Set(arr).size !== arr.length) {
          callback(new Error('选项不允许存在相同值'))
        } else {
          callback()
        }
      }
    }
    return {
      btnLoading: false,
      form: formDataDefault(),
      rules: {
        QuestionName: [{ required: true, message: '请注明', trigger: 'blur' },
          { max: 50, message: '最大长度为 50' }],
        TypeValue: [{ required: true, message: '请注明', trigger: 'blur' },
          { validator: validateTypeVal, trigger: 'blur' },
          { max: 200, message: '最大长度为 200' }],
        ParentTriggerValue: [
          { required: true, message: this.$t('common:ruleMessage:select'), trigger: 'blur' }
        ],
        ReadingCategory: [
          { required: true, message: this.$t('common:ruleMessage:select'), trigger: 'blur' }
        ],
      },
      model_cfg: { visible: false, showClose: true, width: '600px', title: '' },
      parentOptions: [],
      parentTriggerValOptions: [],
      loading: false
    }
  },
  mounted() {
    // if (Object.keys(this.data).length && this.data.Id) {
    //   this.form = { ...this.data }
    // }
  },
  methods: {
    async openDialog(title, data) {
      if (!data.Id) {
        this.form = formDataDefault()
      }
      this.model_cfg.visible = true
      this.model_cfg.title = title
      var param = {
        trialId: this.trialId,
        id: data.Id ? data.Id : '',
        readingCategory: data.ReadingCategory ? data.ReadingCategory : ''
      }
      await this.getParentQsOpntions(param)
      if (data.ParentId) {
        var index = this.parentOptions.findIndex(item => {
          return item.Id === data.ParentId
        })
        if (index !== -1) {
          this.parentTriggerValOptions = this.parentOptions[index].TypeValue.split('|')
        }
      }
      if (Object.keys(data).length && data.Id) {
        this.form = { ...data }
      }
    },
    handleSave() {
      this.$refs.medicalAuditForm.validate(valid => {
        if (valid) {
          this.btnLoading = true
          if (this.form.IsGeneral) {
            this.form.CriterionTypeEnum = null
          }
          addOrUpdateReadingMedicineSystemQuestion(this.form).then(res => {
            this.btnLoading = false
            this.$message.success('保存成功！')
            this.model_cfg.visible = false
            this.$emit('getList')
          }).catch(() => {
            this.btnLoading = false
          })
        }
      })
    },
    handleCancle() {
      this.model_cfg.visible = false
    },
    // 获取父问题下拉框选项
    getParentQsOpntions(param) {
      return new Promise((resolve, reject) => {
        this.loading = true
        getReadingMedicineSystemOtherQuestion(param).then(res => {
          this.parentOptions = res.Result
          this.loading = false
          resolve()
        }).catch(() => {
          this.loading = false
          reject()
        })
      })
    },
    readingCategoryChange(val, form) {
      if (val) {
        var param = {
          trialId: this.trialId,
          id: this.form.Id,
          readingCategory: val
        }
        this.parentOptions = []
        this.parentTriggerValOptions = []
        this.getParentQsOpntions(param)
        this.form.ParentId = ''
        this.form.ParentTriggerValue = ''
      }
    },
    // 父问题change事件
    parentQuestionChange(val, form) {
      if (val) {
        var index = this.parentOptions.findIndex(item => {
          return item.Id === val
        })
        if (index !== -1) {
          this.parentTriggerValOptions = this.parentOptions[index].TypeValue.split('|')
          this.form.ReadingCategory = this.parentOptions[index].ReadingCategory
        }
      }
      this.form.ParentTriggerValue = ''
    },
    typeChange(val, data) {
      if (val !== 'select' && val !== 'radio') {
        data.TypeValue = ''
      }
    }
  }
}
</script>
