<template>
  <div class="question-form">
    <el-form ref="ruleForm" :model="ruleForm" size="small" label-width="140px">
      <el-form-item label="问题描述" prop="content" :rules="[{
                    required: true, message: '问题描述不能为空', trigger: 'blur'
                  },{min: 1, max: 200, message: '长度在1 到 200 个字符', trigger: 'blur'}]">
        <el-input maxlength="200" clearable type="textarea" rows="5" v-model="ruleForm.content" size="small" placeholder="请输入问题描述"/>
      </el-form-item>
      <el-form-item label="问题分类" prop="category" :rules="{
                    required: true, message: '问题分类不能为空', trigger: 'change'
                  }">
        <el-select clearable v-model="ruleForm.category" size="small"
                   placeholder="请选择问题分类">
          <el-option v-for="(value,key,index) in categoryList" :key="index" :label="value" :value="parseInt(key)"/>
        </el-select>
      </el-form-item>
      <el-form-item label="问题类型" prop="type" :rules="{
                    required: true, message: '问题类型不能为空', trigger: 'change'
                  }">
        <el-select clearable v-model="ruleForm.type" size="small"
                   placeholder="请选择问题类型">
          <el-option v-for="(value,key,index) in typeList" :key="index" :label="value" :value="parseInt(key)"/>
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="note" :rules="[
      {min: 0, max: 200, message: '最多200 个字符', trigger: 'blur'}]">
        <el-input maxlength="200" clearable type="textarea" rows="5" v-model="ruleForm.note" size="small" placeholder="请输入备注"/>
      </el-form-item>
      <el-form-item label="基础分值" prop="baseScore" :rules="{
                    validator: validateNum, trigger: 'blur'
                  }">
        <el-input clearable v-model="ruleForm.baseScore" size="small" placeholder="0-99"/>
      </el-form-item>
      <el-form-item label="国考基础分值" prop="examBaseScore" :rules="{
                    validator: validateNum, trigger: 'blur'
                  }">
        <el-input clearable v-model="ruleForm.examBaseScore" size="small" placeholder="0-99"/>
      </el-form-item>
      <el-form-item label="每增加问题数量" prop="increaseNum" :rules="{
                    validator: validateNum, trigger: 'blur'
                  }">
        <el-input clearable v-model="ruleForm.increaseNum" size="small" placeholder="0-99"/>
      </el-form-item>
      <el-form-item label="扣分" prop="deduction" :rules="{
                    validator: validateNum, trigger: 'blur'
                  }">
        <el-input clearable v-model="ruleForm.deduction" size="small" placeholder="0-99"/>
      </el-form-item>
      <el-form-item label="显示顺序" prop="sort" :rules="{
                    validator: validateNum, trigger: 'blur'
                  }">
        <el-input clearable v-model="ruleForm.sort" size="small" placeholder="0-99"/>
      </el-form-item>
    </el-form>
    <div style="text-align: center">
      <el-button icon="el-icon-document-add" :loading="addBtnLoading" type="primary" size="medium"
                 @click="submitForm('ruleForm')">提交
      </el-button>
      <el-button icon="el-icon-back" v-if="ruleForm.id" style="margin-left: 15px" size="medium"
                 @click="onBack('ruleForm')">返回
      </el-button>
    </div>
  </div>
</template>

<script>
  import questionApi from '@/api/basicData/question'

  export default {
    name: "questionForm",
    data() {
      var patrn = /^[1-9]+[0-9]*]*$/;
      var validateNum = (rule, value, callback) => {
        if (!value) {
          callback();
        } else {
          if (!patrn.test(value)) {
            callback(new Error('请输入0-99'));
          } else {
            let num = parseInt(value)
            if(num>=0&&num<100){
              callback();
            }else{
              callback(new Error('请输入0-99'));
            }
          }
        }
      };
      return {
        validateNum: validateNum,
        addBtnLoading: false
      }
    },
    props: {
      ruleForm: {
        type: Object,
        default() {
          return {};
        }
      },
      typeList: {
        type: Object,
        default() {
          return {};
        }
      },
      categoryList: {
        type: Object,
        default() {
          return {};
        }
      }
    },
    created() {
    },
    mounted() {
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let method = 'addDriver'
            if (this.ruleForm.id) {
              method = 'editDriver'
            }
            let ruleForm = JSON.parse(JSON.stringify(this.ruleForm))
            this.addBtnLoading = true
            questionApi[method](ruleForm).then(res => {
              this.addBtnLoading = false
              this.$refs[formName].resetFields()
              this.$message({
                type: 'success',
                message: this.ruleForm.id ? '修改成功' : '添加成功'
              })
              if (this.ruleForm.id) {
                this.$emit('submit', 2)
                this.$emit('onBack')
              } else {
                this.$emit('submit', 1)
              }
            }).catch(e => {
              this.addBtnLoading = false
            })
          }
        })
      },
      onBack() {
        this.$refs['ruleForm'].resetFields()
        this.$emit('onBack')
      }
    }
  }
</script>

<style lang="less">
  .question {

  }
</style>
