<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-model-item label="问题名称" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="problemName">
              <a-input v-model="model.problemName" placeholder="请输入问题名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="问题名称英语" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="problemNameEnglish">
              <a-input v-model="model.problemNameEnglish" placeholder="请输入问题名称英语"  ></a-input>
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24">
            <a-form-model-item label="问题名称德语" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="problemNameGerman">
              <a-input v-model="model.problemNameGerman" placeholder="请输入问题名称德语"  ></a-input>
            </a-form-model-item>
          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="问题答案" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="problemAnswer">
              <j-editor v-model="model.problemAnswer" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="问题答案英语" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="problemAnswerEnglish">
              <j-editor v-model="model.problemAnswerEnglish" />
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24">
            <a-form-model-item label="问题答案德语" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="problemAnswerGerman">
              <j-editor v-model="model.problemAnswerGerman" />
            </a-form-model-item>
          </a-col>-->
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'

  export default {
    name: 'AaCommonProblemForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
           problemName: [
              { required: true, message: '请输入问题名称!'},
           ],
           problemNameEnglish: [
              { required: true, message: '请输入问题名称英语!'},
           ],
/*            problemNameGerman: [
              { required: true, message: '请输入问题名称德语!'},
           ], */
           problemAnswer: [
              { required: true, message: '请输入问题答案!'},
           ],
           problemAnswerEnglish: [
              { required: true, message: '请输入问题答案英语!'},
           ],
/*            problemAnswerGerman: [
              { required: true, message: '请输入问题答案德语!'},
           ], */
        },
        url: {
          add: "/aa/aaCommonProblem/add",
          edit: "/aa/aaCommonProblem/edit",
          queryById: "/aa/aaCommonProblem/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        getAction(this.url.queryById,{id:record.id}).then(res=>{
          if (res.success) {
            this.model = res.result;
          } else {
            this.model = Object.assign({}, record);
          }
        })
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }

        })
      },
    }
  }
</script>