<template>
  <a-spin :spinning="confirmLoading">
    <div class="container">
      <div class="left">
        <j-form-container :disabled="formDisabled">
          <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail" layout="vertical">
            <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="specs">
                  <j-dict-select-tag type="radio" v-model="model.specs" placeholder="请选择规格" dictCode="print_template_specs"
                                     @change="e=>onSpecsChange(e)"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="备注" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="remarks">
                  <a-textarea :rows="4" v-model="model.remarks" placeholder="请输入备注"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="preview">
                  <a-checkbox v-model="model.preview">不显示打印预览窗口，直接打印小票</a-checkbox>
                </a-form-model-item>
              </a-col>

              <a-col :span="24">
                <a-form-model-item label="默认打印机" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="defaultPrint">
                  <a-input v-model="model.defaultPrint" placeholder="请输入默认打印机"  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="打印类型" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="printType">
                  <j-dict-select-tag type="radio" v-model="model.printType" placeholder="请选择打印类型" dictCode="print_template_type"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="是否显示退菜" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="isShowRefund">
                  <j-dict-select-tag type="radio" v-model="model.isShowRefund" placeholder="请选择是否显示退菜" dictCode="print_template_refund"/>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="打印份数" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="printSum">
                  <a-input-number v-model="model.printSum" placeholder="请输入打印份数" style="width: 100%" />
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="打印时内容宽度偏差" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="printContentOffset">
                  <a-input v-model="model.printContentOffset" placeholder="请输入打印时内容宽度偏差"  ></a-input>
                </a-form-model-item>
              </a-col>
              <a-col :span="24">
                <a-form-model-item label="打印时纸张宽度偏差" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="printPaperOffset">
                  <a-input v-model="model.printPaperOffset" placeholder="请输入打印时纸张宽度偏差"  ></a-input>
                </a-form-model-item>
              </a-col>
            </a-row>
          </a-form-model>
        </j-form-container>
      </div>
      <div class="right">
        <a-card class="template" :style="{width:width}">
          <p>Card content</p>
          <p>Card content</p>
          <p>Card content</p>
        </a-card>
      </div>
    </div>

  </a-spin>
</template>

<script>

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

  export default {
    name: 'printTemplateForm',
    components: {
    },
    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        model:{
         },
        labelCol: {
        },
        wrapperCol: {
        },
        confirmLoading: false,
        validatorRules: {
           name: [
              { required: true, message: '请输入打印模板名称!'},
           ],
           specs: [
              { required: true, message: '请输入规格!'},
           ],
          remarks: [
            { required: true, message: '请输入备注!'},
          ],
          printSum: [
            { required: true, message: '请输入打印份数!'},
          ],
          printContentOffset: [
            { required: true, message: '请输入打印时内容宽度偏差!'},
          ],
          printPaperOffset: [
            { required: true, message: '请输入打印时纸张宽度偏差!'},
          ],
        },
        url: {
          add: "/business/busPrintTemplate/add",
          edit: "/business/busPrintTemplate/edit",
          queryById: "/business/busPrintTemplate/queryById"
        },
        width:"94%"
      }
    },
    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.width = record.specs;
        if (record.specs === 'A5'){
          // this.width = '210mm';
        }
        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';
            }
            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;
            })
          }

        })
      },
      onSpecsChange(val){
        console.log('规格切换  ————  '+val)
        this.width = val;
        console.log(this.width)
        if (val === 'A5'){
          // this.width = '210mm';
        }
      }
    }
  }
</script>

<style lang="less" scoped>
  .container{
    display: flex;
    flex-direction: row;
  }
  .left{
    flex: 2;
  }
  .right{
    flex: 3;
  }
  .template{
    margin-left: 10px;
  }
</style>
