<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="999999"
              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="999999"
              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="999999"
              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="999999"
              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="999999"
              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 type="primary" id="productPricingEdit-save" @click="handleSave">保存</a-button>
          <a-button id="productPricingEdit-cancel" @click="isShow = false" style="margin-left: 15px;">取消</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: {
      type: [Number, String],
      default: ''
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      spinning: false,
      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' }
        ]
      },
      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 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>
