<template>
  <a-modal
    centered
    class="productLevelEdit-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="productLevelEdit-form"
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :label-col="formItemLayout.labelCol"
          :wrapper-col="formItemLayout.wrapperCol"
        >
          <a-form-model-item label="产品编码">
            {{ nowData&&nowData.code ? nowData.code : '--' }}
          </a-form-model-item>
          <a-form-model-item label="产品名称">
            {{ nowData&&nowData.name ? nowData.name : '--' }}
          </a-form-model-item>
          <a-form-model-item label="产品级别" prop="level">
            <v-select code="PRODUCT_LEVEL" id="productLevelEdit-level" v-model="form.level" allowClear placeholder="请选择产品级别"></v-select>
          </a-form-model-item>
        </a-form-model>
        <div class="btn-cont">
          <a-button id="productLevelEdit-cancel" @click="isShow = false" style="margin-right: 15px;">取消</a-button>
          <a-button type="primary" id="productLevelEdit-save" @click="handleSave">保存</a-button>
        </div>
      </a-spin>
    </div>
  </a-modal>
</template>

<script>
// 组件
import { VSelect } from '@/components'
// 接口
import { productLevelSave } from '@/api/product'
export default {
  name: 'ProductLevelEditModal',
  components: { VSelect },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    itemId: {// 当前产品级别id
      type: [Number, String],
      default: ''
    },
    nowData: {// 当前产品级别信息
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      // label 标签布局
      formItemLayout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 16 }
      },
      form: {
        level: undefined// 产品级别
      },
      rules: {
        level: [
          { required: true, message: '请选择产品级别', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    // 保存
    handleSave () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const params = _this.nowData
          params.level = _this.form.level
          _this.spinning = true
          productLevelSave(params).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
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
      }
    },
    itemId (newValue, oldValue) {
      if (this.isShow && newValue) {
        this.form.level = this.nowData.level || undefined
      }
    }
  }
}
</script>

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