<template>
  <a-modal
    centered
    class="editPrice-modal"
    :footer="null"
    :maskClosable="false"
    title="自定义报价"
    v-model="isShow"
    @cancel="isShow = false"
    :width="800">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-form-model
        id="editPrice-form"
        ref="ruleForm"
        :model="form"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol">
        <a-form-model-item label="产品编码" prop="code">
          <div>{{ form.code }}</div>
        </a-form-model-item>
        <a-form-model-item label="产品名称" prop="name">
          <div>{{ form.name }}</div>
        </a-form-model-item>
        <a-form-model-item label="终端价" prop="terminalPrice">
          <div>{{ form.terminalPrice?form.terminalPrice : '--' }}</div>
        </a-form-model-item>
        <a-form-model-item label="自定义终端价" prop="dealerProductPrice.terminalPrice">
          <a-input-number
            :min="0.01"
            :max="999999"
            :precision="2"
            v-model.trim="form.dealerProductPrice.terminalPrice"
            style="width: 90%;margin-right: 5px;"
            placeholder="请输入自定义终端价"
            allowClear /><span>元</span>
        </a-form-model-item>
        <a-form-model-item label="车主价" prop="carOwnersPrice">
          <div>{{ form.carOwnersPrice?form.carOwnersPrice:'--' }}</div>
        </a-form-model-item>
        <a-form-model-item label="自定义车主价" prop="dealerProductPrice.carOwnersPrice">
          <a-input-number
            :min="0.01"
            :max="999999"
            :precision="2"
            v-model.trim="form.dealerProductPrice.carOwnersPrice"
            placeholder="请输入自定义车主价"
            style="width: 90%;margin-right: 5px;"
            allowClear /><span>元</span>
        </a-form-model-item>
      </a-form-model>
      <div class="btn-cont">
        <a-button type="primary" size="large" id="editPrice-btn-submit" @click="handleSubmit">保存</a-button>
        <a-button id="editPrice-btn-back" size="large" @click="isShow = false" style="margin-left: 15px;">取消</a-button>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { productDetail, settingPrice } from '@/api/product.js'
export default {
  name: 'EditPriceModal',
  mixins: [commonMixin],
  props: {
    openModal: {
      //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemId: {
      type: [Number, String],
      default: ''
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      formItemLayout: {
        labelCol: { span: 6 },
        wrapperCol: { span: 16 }
      },
      form: {
        code: '',
        name: '',
        terminalPrice: '', // 终端建议价
        carOwnersPrice: '', //  车主建议价
        dealerProductPrice: {
          terminalPrice: '',
          carOwnersPrice: ''
        }
      }
    }
  },
  methods: {
    // 详情
    getDetail () {
      const _this = this
      productDetail({ sn: this.itemId }).then(res => {
        if (res.status == 200) {
          const objInfo = {}
          objInfo.code = res.data.code
          objInfo.name = res.data.name
          objInfo.terminalPrice = (res.data.terminalPrice || res.data.terminalPrice == 0) ? _this.toThousands(res.data.terminalPrice) : ''
          objInfo.carOwnersPrice = (res.data.carOwnersPrice || res.data.carOwnersPrice == 0) ? _this.toThousands(res.data.carOwnersPrice) : ''
          objInfo.dealerProductPrice = {}
          Object.assign(objInfo.dealerProductPrice, { terminalPrice: res.data.dealerProductPrice && (res.data.dealerProductPrice.terminalPrice || res.data.dealerProductPrice.terminalPrice == 0) ? res.data.dealerProductPrice.terminalPrice : '', carOwnersPrice: res.data.dealerProductPrice && (res.data.dealerProductPrice.carOwnersPrice || res.data.dealerProductPrice.carOwnersPrice == 0) ? res.data.dealerProductPrice.carOwnersPrice : '' })
          _this.form = objInfo
        }
      })
    },
    //  保存
    handleSubmit (e) {
      e.preventDefault()
      const _this = this
      _this.spinning = true
      const ajaxData = {}
      ajaxData.productSn = this.itemId
      ajaxData.dealerProductPrice = {}
      Object.assign(ajaxData.dealerProductPrice, _this.form.dealerProductPrice)
      settingPrice(ajaxData).then(res => {
        if (res.status == 200) {
          _this.$message.success(res.message)
          _this.$emit('ok')
          _this.isShow = false
          _this.spinning = false
        } else {
          _this.spinning = false
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
      } else {
        this.getDetail()
      }
    }
  }
}
</script>

<style lang="less">
.editPrice-modal {
  .ant-modal-body {
    padding: 40px 40px 24px;
  }
  .btn-cont {
    text-align: center;
    margin: 35px 0 10px;
  }
}
</style>
