<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="isDeposit">-->
<!--              <a-input v-model="model.isDeposit" placeholder="请输入是否用于押金付款"  ></a-input>-->
<!--            </a-form-model-item>-->
            <a-form-model-item
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol"
                    label="是否用于押金付款"
                    prop="isDeposit">
              <a-switch v-model="model.isDeposit"/>
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
<!--            <a-form-model-item label="是否用于结账付款" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isClosing">-->
<!--              <a-input v-model="model.isClosing" placeholder="请输入是否用于结账付款"  ></a-input>-->
<!--            </a-form-model-item>-->
            <a-form-model-item
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol"
                    label="是否用于结账付款"
                    prop="isClosing">
              <a-switch v-model="model.isClosing"/>
            </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: 20%" />
            </a-form-model-item>
          </a-col>
          <a-col :span="24">
<!--            <a-form-model-item label="状态(0-禁用；1-启用)" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="status">-->
<!--              <a-input-number v-model="model.status" placeholder="请输入状态(0-禁用；1-启用)" style="width: 100%" />-->
<!--            </a-form-model-item>-->
            <a-form-model-item
                    :labelCol="labelCol"
                    :wrapperCol="wrapperCol"
                    label="是否启用"
                    prop="status">
              <a-switch checkedChildren="启用" unCheckedChildren="禁用" v-model="model.status"/>
            </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: 'roomPayTypeForm',
    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: '请输入名称!'},
           ],
          isDeposit: [
            { required: true, message: '请选择!'},
          ],
          isClosing: [
            { required: true, message: '请选择!'},
          ],
          orderBy: [
            { required: true, message: '请输入排序!'},
          ],
          status: [
            { required: true, message: '请选择!'},
          ],
        },
        url: {
          add: "/business/busRoomPayType/add",
          edit: "/business/busRoomPayType/edit",
          queryById: "/business/busRoomPayType/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        return this.disabled
      },
    },
    created () {
       //备份model原始值
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
    },
    methods: {
      add () {
        this.edit(this.modelDefault);
        this.edit({status:true,orderBy:0 ,isDeposit:true,isClosing:true });
        // this.edit({isDeposit:true });
        // this.edit({isClosing:true });
        // this.edit({orderBy:0 });
      },
      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';
              var info = JSON.parse(localStorage.getItem("storeInfo"));
              this.model.hotelId = info.id;
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            var _model = this.model;
            if (_model.status){
              _model.status = 1;
            }else {
              _model.status = 0;
            }
            httpAction(httpurl,_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>
