<template>
  <a-modal
    centered
    class="merchantInfoManagement-modal"
    :footer="null"
    :maskClosable="false"
    :title="title"
    v-model="isShow"
    @cancle="cansel"
    :width="600">
    <div>
      <a-spin :spinning="spinning" tip="Loading...">
        <a-form-model
          ref="ruleForm"
          :model="form"
          :rules="rules"
          :label-col="labelCol"
          :wrapper-col="wrapperCol"
        >
          <a-form-model-item label="产品名称">
            {{ productName }}
          </a-form-model-item>
          <a-form-model-item label="产品编码">
            {{ productCode }}
          </a-form-model-item>
          <a-form-model-item label="成本价" prop="cost">
            <a-input-number
              id="setting-cost"
              v-model="form.cost"
              :precision="2"
              :min="0"
              :max="999999"
              placeholder="请输入"
              style="width: 100%;" />
          </a-form-model-item>
          <div style="display: flex;justify-content: center;padding: 30px 0;">
            <a-button type="primary" @click="onSubmit">
              确定
            </a-button>
            <a-button style="margin-left: 15px;" @click="cansel">
              取消
            </a-button>
          </div>
        </a-form-model></a-spin>
      </a-form-model>
    </div>
  </a-modal>
</template>

<script>
import { VSelect } from '@/components'
import { settingCost } from '@/api/supplier'
export default {
  name: 'SettingCost',
  components: { VSelect },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      spinning: false,
      title: '设置成本价',
      labelCol: { span: 4 },
      wrapperCol: { span: 18 },
      productName: '',
      productCode: '',
      form: {
        supplierSn: '',
        supplierProductSn: '',
        cost: ''
      },
      rules: {
        cost: [
          { required: true, message: '请输入成本价', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    setData (row) {
      this.form.supplierSn = row.supplierSn
      this.form.supplierProductSn = row.supplierProductSn
      this.form.cost = row.cost
      this.productName = row.product.name
      this.productCode = row.product.code
    },
    onSubmit () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const params = JSON.parse(JSON.stringify(_this.form))
          _this.spinning = true
          settingCost(params).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              _this.$emit('ok')
              _this.cansel()
              _this.spinning = false
            } else {
              _this.spinning = false
            }
          })
        } else {
          return false
        }
      })
    },
    cansel () {
      this.$emit('close')
      this.productName = ''
      this.productCode = ''
      this.$refs.ruleForm.resetFields()
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
      }
    }
  }
}
</script>

<style lang="less">
  .merchantInfoManagement-modal{
    .ant-modal-body {
      padding: 40px 40px 24px;
    }
  }
</style>
