<template>
  <a-modal
    centered
    class="edit-product-modal"
    id="edit-product-modal"
    :footer="null"
    :maskClosable="false"
    title="编辑"
    v-model="isShow"
    @cancel="isShow = false"
    :width="700">
    <a-spin :spinning="spinning" tip="Loading...">
      <a-form-model
        id="edit-product-form"
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol"
      >
        <a-form-model-item label="产品编码">
          {{ form.productCode }}
        </a-form-model-item>
        <a-form-model-item label="售价类型" prop="priceType">
          <a-select v-model="form.priceType" id="edit-product-priceType" placeholder="请选择售价类型" allowClear @change="handlePriceType">
            <a-select-option value="TERMINAL_PRICE">
              终端价
            </a-select-option>
            <a-select-option value="CUSTOM_PRICE">
              自定义
            </a-select-option>
          </a-select>
        </a-form-model-item>
        <a-form-model-item label="售价" prop="price">
          <a-input-number
            style="width:100%;"
            id="edit-product-price"
            :min="0.01"
            :max="99999999"
            :precision="2"
            :step="0.1"
            v-model="form.price"
            :disabled="form.priceType==='TERMINAL_PRICE'"
            placeholder="请输入产品售价"/>
        </a-form-model-item>
        <a-form-model-item label="热销产品" prop="hotFlag">
          <v-select
            v-model="form.hotFlag"
            code="FlAG"
            id="edit-product-hotFlag"
            placeholder="请选择是否是热销产品"
            allowClear
            @change="handleHotProduct"
          ></v-select>
        </a-form-model-item>
        <a-form-model-item label="热销产品优先级" prop="hotSort" v-if="form.hotFlag==='1'">
          <a-input-number
            style="width:100%;"
            id="edit-product-money"
            :min="0"
            :max="99999999"
            :precision="0"
            :step="1"
            v-model="form.hotSort"
            placeholder="请输入正整数，数字越大优先级越低"/>
        </a-form-model-item>
      </a-form-model>
      <div class="btn-cont">
        <a-button id="edit-product-modal-back" @click="isShow = false" style="margin-right: 15px;">取消</a-button>
        <a-button type="primary" id="edit-product-modal-save" @click="handleSave">保存</a-button>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { VSelect } from '@/components'
// 接口
import { shopProductDetail, shopProductSaveProduct } from '@/api/shopProduct'
export default {
  name: 'EditProductModal',
  mixins: [commonMixin],
  components: { VSelect },
  props: {
    openModal: {//  弹框显示状态
      type: Boolean,
      default: false
    },
    itemSn: {
      type: String || Number,
      default: ''
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 18 }
      },
      form: {
        priceType: 'TERMINAL_PRICE', // 售价类型
        price: '', // 售价
        hotFlag: undefined, // 是否是热销产品
        hotSort: undefined// 热销产品优先级
      },
      // 必填项校验
      rules: {
        priceType: [{ required: true, message: '请选择售价类型', trigger: 'change' }],
        price: [{ required: true, message: '请输入产品售价', trigger: 'blur' }],
        hotFlag: [{ required: true, message: '请选择是否是热销产品', trigger: 'change' }],
        hotSort: [{ required: true, message: '请输入热销产品优先级', trigger: 'blur' }]
      }
    }
  },
  methods: {
    // 售价类型  change
    handlePriceType (val) {
      if (val === 'TERMINAL_PRICE') {
        this.form.price = this.form && (this.form.terminalPrice || this.form.terminalPrice == 0) ? this.form.terminalPrice : ''
      }
    },
    // 获取编辑详情
    getDetail () {
      this.spinning = true
      shopProductDetail({ shopProductSn: this.itemSn }).then(res => {
        if (res.status == 200 && res.data) {
          res.data.hotSort = res.data.hotFlag == '0' ? undefined : res.data.hotSort
          this.form = res.data
          this.spinning = false
        } else {
          this.form = {}
        }
      })
    },
    // 热销产品切换时清空数据
    handleHotProduct (e) {
      if (e == 0) {
        this.form.hotSort = undefined
      }
    },
    //  保存
    handleSave () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          _this.spinning = true
          _this.form.shopProductSn = _this.itemSn
          shopProductSaveProduct(_this.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
        }
      })
    },
    // 重置
    reSetForm () {
      this.form.priceType = 'TERMINAL_PRICE'
      this.form.price = ''
      this.form.hotFlag = undefined
      this.form.hotSort = undefined
      this.$refs.ruleForm.resetFields()
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.reSetForm()
      }
    }
  }
}
</script>

<style lang="less">
.edit-product-modal {
	.ant-modal-body {
		padding: 40px 40px 24px;
	}
	.btn-cont {
		text-align: center;
		margin: 35px 0 10px;
	}
  .ant-radio + span{
    line-height: 40px;
  }
}
</style>
