<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="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="categoryId">
                        <j-dict-select-tag type="list" v-model="model.categoryId" dictCode="hotel_category,name,id" placeholder="请选择分类" />
                      </a-form-model-item>
                    </a-col>
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="原价" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="originalPrice">-->
<!--              <a-input-number v-model="model.originalPrice" placeholder="请输入原价" style="width: 100%" />-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="商家id" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="shopId">-->
<!--              <j-dict-select-tag type="list" v-model="model.shopId" dictCode="hotel_shop,name,id" placeholder="请选择商家id" />-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
<!--          <a-col :span="24">-->
<!--            <a-form-model-item label="规格" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="skuId">-->
<!--              <j-dict-select-tag type="list" v-model="model.skuId" dictCode="hotel_goods_sku,title,id" placeholder="请选择规格id" />-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->
          <a-col :span="24">
            <a-form-model-item label="商品详情" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="detail">
              <j-image-upload isMultiple  v-model="model.detail" ></j-image-upload>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否上架" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="upFlag">
              <j-dict-select-tag type="radio" v-model="model.upFlag" dictCode="is_delete" placeholder="请选择是否上架 0否 1是" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="已售数量" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="soldNum">
              <a-input-number  v-model="model.soldNum"  placeholder="请输入已售数量" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="排序" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="sort">
              <a-input-number v-model="model.sort" placeholder="请输入排序" style="width: 100%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
            <a-form-model-item label="是否删除" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="delFlag">
              <j-dict-select-tag type="radio" v-model="model.delFlag" dictCode="is_delete" placeholder="请选择是否删除 0否 1是" />
            </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: 'HotelGoodsForm',
    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: {
          soldNum: [
            { required: true, message: '请输入已售数量!'},
          ],
        },
        url: {
          add: "/hotelgoods/hotelGoods/add",
          edit: "/hotelgoods/hotelGoods/edit",
          queryById: "/hotelgoods/hotelGoods/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;
              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>