<template>
  <a-modal
    class="setmealPayMoneyModal"
    title="套餐收款"
    width="60%"
    :footer="null"
    :destroyOnClose="true"
    @cancel="isShow=false"
    v-model="isShow">
    <!-- 表单 -->
    <a-form-model ref="ruleForm" :model="form" :rules="rules" :label-col="{ span: 4 }" :wrapper-col="{ span: 20 }">
      <a-form-model-item label="手机号" prop="custMobile">
        <a-input v-model.trim="form.custMobile" @input="mobileInput" :maxLength="11" placeholder="支持新增和搜索" />
      </a-form-model-item>
      <a-form-model-item label="车牌号" prop="vehicleNumber">
        <a-input v-model.trim="form.vehicleNumber" :maxLength="8" placeholder="请输入车牌号(最多8个字符)" />
      </a-form-model-item>
      <a-form-model-item label="客户姓名" prop="custName">
        <a-input v-model.trim="form.custName" :maxLength="30" placeholder="请输入客户姓名(最多30个字符)" />
      </a-form-model-item>
      <a-form-model-item label="应收金额" class="item-con">
        <p class="item-main"><span class="item-price" v-if="setmealData">{{ setmealData.price || 0 }}</span>元</p>
      </a-form-model-item>
      <a-form-model-item label="收款方式" class="item-con">
        <p class="item-main">现金</p>
      </a-form-model-item>
      <a-form-model-item label="" class="btn-cont" :label-col="{ span: 0 }" :wrapper-col="{ span: 24 }">
        <a-button type="primary" id="setmealPayMoney-submit" @click="onSubmit">确认收款</a-button>
        <a-button id="setmealPayMoney-cancel" class="setmealPayMoney-cancel" @click="cancel">取消</a-button>
      </a-form-model-item>
      <p class="text-center">重要提示：请确认在线下实际收到款后再点击【确认收款】！</p>
    </a-form-model>
  </a-modal>
</template>

<script>
import { isLicensePlate, isMobile } from '@/libs/tools'
import { bundleFindByMobile, bundleBuy } from '@/api/customerBundle.js'
export default {
  name: 'SetmealPayMoney',
  props: {
    openModal: {
      //  弹框是否展示
      type: Boolean,
      default: false
    },
    setmealInfo: {
      //  套餐信息
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    // 校验手机号
    const isMobileValid = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入手机号'))
      } else {
        if (!isMobile(value)) {
          callback(new Error('请输入正确的手机号'))
        } else {
          callback()
        }
      }
    }
    // 校验车牌号
    const isLicensePlateValid = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入车牌号'))
      } else {
        if (!isLicensePlate(value)) {
          callback(new Error('请输入正确的车牌号'))
        } else {
          callback()
        }
      }
    }
    return {
      isShow: this.openModal, //  弹框是否展示
      form: {
        custMobile: '',
        vehicleNumber: '',
        custName: '',
        custId: null
      },
      rules: {
        custMobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { validator: isMobileValid, trigger: 'blur' }
        ],
        vehicleNumber: [
          { required: true, message: '请输入车牌号', trigger: 'blur' },
          { validator: isLicensePlateValid, trigger: 'blur' }
        ]
      },
      setmealData: null //  套餐数据
    }
  },
  methods: {
    //  手机号输入监听
    mobileInput () {
      if (this.form.custMobile.length == 11) {
        if (isMobile(this.form.custMobile)) { //  校验手机号
          //  根据手机号去查询是否已绑定车牌
          bundleFindByMobile({ mobile: this.form.custMobile }).then(res => {
            if (res.status == 200) {
              if (res.data) { //  查到客户信息
                this.form.vehicleNumber = ''
                this.form.custName = res.data.name
                this.form.custId = res.data.id
              } else { //  未查到客户信息
                this.form.vehicleNumber = ''
                this.form.custName = ''
                this.form.custId = null
              }
            } else {
              this.form.vehicleNumber = ''
              this.form.custName = ''
              this.form.custId = null
            }
          })
        }
      }
    },
    //  确认收款
    onSubmit () {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const params = {
            bundleId: this.setmealData.id,
            orderBundle: {
              bundleId: this.setmealData.id,
              custId: this.form.custId,
              custName: this.form.custName,
              custMobile: this.form.custMobile,
              vehicleNumber: this.form.vehicleNumber,
              totalAmount: this.setmealData.price
            },
            paymentDetailList: [
              { payType: 10001 }
            ]
          }
          bundleBuy(params).then(res => {
            if (res.status == 200) {
              this.$emit('close')
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    //  取消收款
    cancel () {
      this.resetForm() //  重置表单数据
      this.isShow = false
    },
    // 重置表单
    resetForm () {
      this.form.custMobile = ''
      this.form.vehicleNumber = ''
      this.form.custName = ''
      this.form.custId = null
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (val) {
      if (!val) {
        this.$emit('close')
      } else {
        this.resetForm() //  重置表单数据
      }
    },
    setmealInfo (newValue, oldValue) {
      if (newValue && this.isShow) {
        this.setmealData = newValue
      } else {
        this.setmealData = null
      }
    }
  }
}
</script>

<style lang="less">
  .setmealPayMoneyModal{
    .item-con{
      .item-main{
        margin: 0;
        .item-price{
          font-size: 24px;
          color: red;
          margin-right: 5px;
        }
      }
    }
    .btn-cont{
      text-align: center;
      .setmealPayMoney-cancel{
        margin-left: 30px;
      }
    }
    .text-center{
      text-align: center;
    }
  }
</style>
