<template>
  <a-modal
    class="setmealPayMoneyModal"
    title="套餐收款"
    width="60%"
    centered
    :footer="null"
    :maskClosable="false"
    :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 id="setmealPayMoney-custMobile" v-model.trim="form.custMobile" @input="mobileInput" :maxLength="11" placeholder="请输入手机号" />
      </a-form-model-item>
      <!-- 销售合作商 -->
      <a-form-model-item label="销售合作商" prop="dataSourceNo">
        <a-select id="setmealPayMoney-dataSourceNo" v-model="form.dataSourceNo" placeholder="请选择该套餐是由哪个合作商售出">
          <a-select-option v-for="(item, index) in partnerList" :key="index" :value="item.salesChannelNo">{{ item.salesChannelName }}</a-select-option>
        </a-select>
      </a-form-model-item>
      <!-- 客户姓名 -->
      <a-form-model-item label="客户姓名" prop="custName">
        <a-input id="setmealPayMoney-custName" 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 { isMobile } from '@/libs/tools'
import { mapGetters } from 'vuex'
import { bundleFindByMobile, bundleBuy } from '@/api/customerBundle.js'
import { salesChannelList } from '@/api/FinancialManagement'
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()
        }
      }
    }
    return {
      isShow: this.openModal, //  弹框是否展示
      form: {
        custMobile: '',
        custName: '',
        custId: null,
        dataSourceNo: undefined //  销售合作商
      },
      rules: {
        custMobile: [{ required: true, message: '请输入手机号', trigger: 'blur' }, { validator: isMobileValid, trigger: 'blur' }],
        dataSourceNo: [{ required: true, message: '请选择销售合作商', trigger: 'change' }]
      },
      setmealData: null, //  套餐数据
      partnerList: [] //  合作商  下拉数据
    }
  },
  computed: {
    ...mapGetters(['authUserInfo'])
  },
  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.custId = res.data.id
              } else {
                //  未查到客户信息
                this.form.custId = null
              }
            } else {
              this.form.custId = null
            }
          })
        }
      }
    },
    //  确认收款
    onSubmit () {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const params = {
            bundleId: this.setmealData.id,
            custId: this.form.custId,
            custName: this.form.custName,
            custMobile: this.form.custMobile,
            dataSourceNo: this.form.dataSourceNo,
            totalAmount: this.setmealData.price,
            paymentDetail: { payType: 10001 }
          }
          bundleBuy(params).then(res => {
            if (res.status == 200) {
              if (this.$hasPermissions('M_bundel_buyRecord')) {
                this.$router.push({ path: '/SetmealSales/PurchasedSetmeal' })
              } else {
                this.cancel()
              }
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    //  取消收款
    cancel () {
      this.resetForm() //  重置表单数据
      this.isShow = false
    },
    // 重置表单
    resetForm () {
      this.form.custMobile = ''
      this.form.custName = ''
      this.form.custId = null
      this.form.dataSourceNo = undefined
    },
    //  获取渠道商下的合作商信息
    getSalesChannel () {
      salesChannelList().then(res => {
        if (res.status == 200) {
          this.partnerList = res.data
          const salesChannelType = this.authUserInfo.extInfo.channel.salesChannelType
          if (salesChannelType == 'SALLER') { //  合作商
            this.form.dataSourceNo = res.data.length > 0 ? res.data[0].salesChannelNo : undefined
          }
        } else {
          this.partnerList = []
          this.form.dataSourceNo = undefined
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (val) {
      if (!val) {
        this.$emit('close')
      } else {
        this.resetForm() //  重置表单数据
        this.getSalesChannel()
      }
    },
    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>
