<template>
  <a-modal
    centered
    class="productPricingEdit-modal"
    :footer="null"
    :maskClosable="false"
    title="编辑价格"
    v-model="isShow"
    @cancel="isShow=false"
    :width="800">
    <!-- 编辑价格 -->
    <div>
      <a-spin :spinning="spinning" tip="Loading...">
        <a-form-model
          id="productPricingEdit-form"
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
        >
          <a-form-model-item label="成本价">
            <div v-if="detailData && detailData.supplierProductList" style="margin-top: 8px;">
              <p v-for="(item, index) in detailData.supplierProductList" :key="index" style="margin: 0;line-height: 1.5em;">{{ ((item.cost || item.cost==0) ? item.cost.toFixed(2) : '--') + ' 元 -- ' + (item.supplierName||'') + '；' }}</p>
            </div>
            <span v-else>--</span>
          </a-form-model-item>
          <a-form-model-item label="省级价" prop="afterProvincePrice">
            <a-input-number
              id="productPricingEdit-afterProvincePrice"
              v-model="form.afterProvincePrice"
              :precision="2"
              :min="0.01"
              :max="99999999"
              placeholder="请输入省级价"
              ref="afterProvincePrice"
              @keydown.enter.native="nextFocus('afterProvincePrice', 'afterCityPrice', $event)"
              style="width: 100%;" />
          </a-form-model-item>
          <a-form-model-item label="市级价" prop="afterCityPrice">
            <a-input-number
              id="productPricingEdit-afterCityPrice"
              v-model="form.afterCityPrice"
              :precision="2"
              :min="0.01"
              :max="99999999"
              placeholder="请输入市级价"
              ref="afterCityPrice"
              @keydown.enter.native="nextFocus('afterCityPrice', 'afterSpecialPrice', $event)"
              style="width: 100%;" />
          </a-form-model-item>
          <a-form-model-item label="特约价" prop="afterSpecialPrice">
            <a-input-number
              id="productPricingEdit-afterSpecialPrice"
              v-model="form.afterSpecialPrice"
              :precision="2"
              :min="0.01"
              :max="99999999"
              placeholder="请输入特约价"
              ref="afterSpecialPrice"
              @keydown.enter.native="nextFocus('afterSpecialPrice', 'afterTerminalPrice', $event)"
              style="width: 100%;" />
          </a-form-model-item>
          <a-form-model-item label="终端价" prop="afterTerminalPrice">
            <a-input-number
              id="productPricingEdit-afterTerminalPrice"
              v-model="form.afterTerminalPrice"
              :precision="2"
              :min="0.01"
              :max="99999999"
              placeholder="请输入终端价"
              ref="afterTerminalPrice"
              @keydown.enter.native="nextFocus('afterTerminalPrice', 'afterCarOwnersPrice', $event)"
              style="width: 100%;" />
          </a-form-model-item>
          <a-form-model-item label="车主价" prop="afterCarOwnersPrice">
            <a-input-number
              id="productPricingEdit-afterCarOwnersPrice"
              v-model="form.afterCarOwnersPrice"
              :precision="2"
              :min="0.01"
              :max="99999999"
              placeholder="请输入车主价"
              ref="afterCarOwnersPrice"
              @keydown.enter.native="nextFocus('afterCarOwnersPrice', 'changeReason', $event, 'vSelect')"
              style="width: 100%;" />
          </a-form-model-item>
          <a-form-model-item label="变更原因" prop="changeReason">
            <v-select
              code="PRODUCT_PRICE_CHANGE_REASON"
              id="productPricingEdit-changeReason"
              v-model="form.changeReason"
              allowClear
              placeholder="请选择变更原因"
              ref="changeReason"
              @keydown.enter.native="nextFocus('changeReason', '', $event)"
            ></v-select>
          </a-form-model-item>
        </a-form-model>
        <div class="btn-cont">
          <a-button id="productPricingEdit-cancel" @click="isShow = false" style="margin-right: 15px;">取消</a-button>
          <a-button type="primary" id="productPricingEdit-save" @click="handleSave">保存</a-button>
        </div>
      </a-spin>
    </div>
  </a-modal>
</template>

<script>
import { VSelect } from '@/components'
// 接口
import { productPricingSnDetail, productPricingSave } from '@/api/product'
export default {
  name: 'ProductPricingEditModal',
  components: { VSelect },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemSn: {// 定价产品sn
      type: [Number, String],
      default: ''
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      formItemLayout: {// 表单布局设置
        labelCol: { span: 4 },
        wrapperCol: { span: 17 }
      },
      form: {
        afterProvincePrice: '', // 省级价
        afterCityPrice: '', // 市级价
        afterSpecialPrice: '', // 特约价
        afterTerminalPrice: '', // 终端价
        afterCarOwnersPrice: '', // 车主价
        changeReason: undefined// 变更原因
      },
      rules: {
        changeReason: [
          { required: true, message: '请选择变更原因', trigger: 'change' }
        ],
        afterProvincePrice: [
          { required: true, message: '请输入省级价', trigger: 'blur' }
        ],
        afterCityPrice: [
          { required: true, message: '请输入市级价', trigger: 'blur' }
        ],
        afterSpecialPrice: [
          { required: true, message: '请输入特约价', trigger: 'blur' }
        ],
        afterTerminalPrice: [
          { required: true, message: '请输入终端价', trigger: 'blur' }
        ],
        afterCarOwnersPrice: [
          { required: true, message: '请输入车主价', trigger: 'blur' }
        ]
      },
      detailData: null //  数据详情
    }
  },
  methods: {
    //  获取详情
    getDetail () {
      productPricingSnDetail({ sn: this.itemSn }).then(res => {
        if (res.status == 200) {
          this.detailData = res.data
          this.form.afterProvincePrice = res.data.afterProvincePrice || ''
          this.form.afterCityPrice = res.data.afterCityPrice || ''
          this.form.afterSpecialPrice = res.data.afterSpecialPrice || ''
          this.form.afterTerminalPrice = res.data.afterTerminalPrice || ''
          this.form.afterCarOwnersPrice = res.data.afterCarOwnersPrice || ''
          this.form.changeReason = res.data.id && res.data.changeReason ? '价格修改' : '首次定价'
        } else {
          this.detailData = null
          this.$refs.ruleForm.resetFields()
        }
      })
    },
    // 保存    定价规则==>省级价<市级价<特约价<终端价<车主价
    handleSave () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const newFormData = _this.form
          if (newFormData.afterCityPrice * 1 < newFormData.afterProvincePrice * 1 || newFormData.afterCityPrice * 1 > newFormData.afterSpecialPrice * 1) {
            _this.$message.error({ content: '价格设置规则：省级价≤市级价≤特约价', duration: 5 })
            return
          }
          const form = JSON.parse(JSON.stringify(_this.form))
          form.id = this.detailData.id || undefined
          form.productSn = _this.itemSn
          _this.spinning = true
          productPricingSave(form).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              setTimeout(() => {
                _this.$emit('ok')
                _this.isShow = false
                _this.spinning = false
              }, 1000)
            } else {
              _this.spinning = false
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    // 回车键快捷定位表单下一项
    nextFocus (nowRef, nextRef, event, isComponent) {
      const _this = this
      if (_this.$refs[nowRef]) {
        _this.$nextTick(() => {
          if (_this.$refs[nextRef]) {
            event.target.blur()
            if (isComponent == 'vSelect') {
              _this.$refs[nextRef].$children[0].focus()
            } else {
              _this.$refs[nextRef].focus()
            }
          }
        })
      }
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.costPrice = ''
        this.$refs.ruleForm.resetFields()
      } else {
        // 默认首项获取焦点
        const _this = this
        setTimeout(() => {
          if (_this.$refs['afterProvincePrice']) {
            _this.$refs['afterProvincePrice'].focus()
          }
        }, 300)
      }
    },
    itemSn (newValue, oldValue) {
      if (this.isShow && newValue) {
        this.getDetail()
      }
    }
  }
}
</script>

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