<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="name">
              <a-input v-model="model.name" placeholder="请输入名称"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="名称英语" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.nameEnglish" placeholder="请输入名称"  ></a-input>
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24">
            <a-form-model-item label="名称德语" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="name">
              <a-input v-model="model.nameGerman" placeholder="请输入名称"  ></a-input>
            </a-form-model-item>
          </a-col>-->
<!--          <a-col :span="24">
            <a-form-model-item label="防伪码" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="securityCode">
              <a-input v-model="model.securityCode" placeholder="请输入防伪码"  ></a-input>
            </a-form-model-item>
          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="图片" :labelCol="labelCol" :wrapperCol="wrapperCol" help="图片尺寸：700*700" prop="pic">
              <j-image-upload isMultiple  v-model="model.pic" ></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="视频" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="video">
              <j-upload v-model="model.video" :reNewFile="true" :multiple="false" :beforeUpload="this.beforeUploadHandler" ></j-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="价格" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="price">
              <a-input-number v-model="model.price" placeholder="请输入价格" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="分类" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="typeId">
  	          <j-tree-select
                :choose-child="true"
                ref="treeSelect"
                placeholder="请选择分类"
                v-model="model.typeId"
                dict="aa_commodity_fl,name,id"
                :hasChildField="'has_child'"
                pidValue="0"
                >
              </j-tree-select>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="介绍" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="introduce">
              <a-textarea v-model="model.introduce" rows="4" placeholder="请输入介绍" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="介绍英语" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="introduce">
              <a-textarea v-model="model.introduceEnglish" rows="4" placeholder="请输入介绍" />
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24">
            <a-form-model-item label="介绍德语" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="introduce">
              <a-textarea v-model="model.introduceGerman" rows="4" placeholder="请输入介绍" />
            </a-form-model-item>
          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="介绍图片" :labelCol="labelCol" :wrapperCol="wrapperCol" help="介绍图片尺寸：400*330（推荐png格式）" prop="introducePic">
              <j-image-upload isMultiple  v-model="model.introducePic" ></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="图片集" :labelCol="labelCol" :wrapperCol="wrapperCol" help="图片集尺寸：400*330（推荐png格式）" prop="pics">
              <j-image-upload isMultiple  v-model="model.pics" ></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="型号" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="modelNumber">
              <a-input v-model="model.modelNumber" placeholder="请输入型号"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="文案" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="copyWriter">
              <j-editor v-model="model.copyWriter" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="文案英语" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="copyWriter">
              <j-editor v-model="model.copyWriterEnglish" />
            </a-form-model-item>
          </a-col>
<!--          <a-col :span="24">
            <a-form-model-item label="文案德语" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="copyWriter">
              <j-editor v-model="model.copyWriterGerman" />
            </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'
  import { duplicateCheck } from '@api/api'

  export default {
    name: 'AaCommodityForm',
    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: {
           name: [
              { required: true, message: '请输入名称!'},
           ],
           pic: [
              { required: true, message: '请输入图片!'},
           ],
           price: [
              { required: true, message: '请输入价格!'},
           ],
           introduce: [
              { required: true, message: '请输入介绍!'},
           ],
          // securityCode: [
          //   { required: true, message: '请输入防伪码!'},{validator: this.validateC,}
          // ],
        },
        url: {
          add: "/aa/aaCommodity/add",
          edit: "/aa/aaCommodity/edit",
          queryById: "/aa/aaCommodity/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      validateC(rule, value, callback){
        var params = {
          tableName: 'aa_commodity',
          fieldName: 'security_code',
          fieldVal: value
        };
        duplicateCheck(params).then((res) => {
          if (res.success) {
            callback()
          } else {
            callback("防伪码已存在!")
          }
        })
      },
      add () {
        this.edit(this.modelDefault);
      },
      edit (record) {
        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;
            })
          }

        })
      },
      beforeUploadHandler(file) {
        let fileType = file.type;
        if(fileType.indexOf('video')<0){
          this.$message.warning('请上传视频文件');
          return false;
        }
        return true;
      },
    }
  }
</script>