<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="title">
              <a-input v-model="model.title" placeholder="请输入标题"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="投放位置" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="type">
<!--              <a-input-number v-model="model.type" placeholder="请输入投放类型" style="width: 100%" />-->
<!--              <a-select placeholder="请选择投放位置" v-model="model.type">-->
<!--                <a-select-option :value="1">小程序首页</a-select-option>-->
<!--                <a-select-option :value="2">后台登录</a-select-option>-->
<!--                <a-select-option :value="3">商城首页</a-select-option>-->
<!--              </a-select>-->
                <j-dict-select-tag v-model="model.type" placeholder="请选择投放位置" dictCode="advert_site"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
<!--            <a-form-model-item label="广告图片" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="logo">-->
<!--              <a-input v-model="model.logo" placeholder="请输入广告图片"  ></a-input>-->
<!--            </a-form-model-item>-->
<!--            <a-form-item label="图片"  :labelCol="labelCol" :wrapperCol="wrapperCol" prop="logo">-->
<!--              <j-image-upload class="avatar-uploader" text="上传"-->
<!--                              v-decorator="['logo', { rules: [{ required: true, message: '请上传图片！' }] }]"></j-image-upload>-->
<!--            </a-form-item>-->
            <a-form-model-item label="图片"  :labelCol="labelCol" :wrapperCol="wrapperCol" prop="logo">
              <j-image-upload text="上传" v-model="model.logo"></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="链接" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="src">
              <a-input v-model="model.src" placeholder="请输入链接"  ></a-input>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="orderBy">
              <a-input-number v-model="model.orderBy" placeholder="请输入排序" style="width: 100%" />
            </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: 'advertForm',
    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: {
          title: [
            { required: true, message: '请输入广告标题!'},
          ],
           logo: [
              { required: true, message: '请上传图片!'},
           ],
          src: [
              { required: true, message: '请输入链接地址!'},
           ],
          orderBy: [
            { required: true, message: '请输入序号!'},
          ],
        },
        url: {
          add: "/business/busAdvert/add",
          edit: "/business/busAdvert/edit",
          queryById: "/business/busAdvert/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      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;
              var info = JSON.parse(localStorage.getItem("storeInfo"));
              this.model.hotelId = info.id;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
              if(this.model.type === ''){
                  this.model.type = null
              }
            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>
