<template>
  <a-modal
    centered
    class="settingCost-modal"
    :footer="null"
    :maskClosable="false"
    :title="title"
    v-model="isShow"
    @cancel="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 style="margin-bottom:10px;" label="产品名称">
            <div style="line-height:normal;padding-top:11px;">{{ productName }}</div>
          </a-form-model-item>
          <a-form-model-item style="margin-bottom:10px;" label="产品编码">
            {{ productCode }}
          </a-form-model-item>
          <a-form-model-item label="成本价" prop="cost">
            <a-input-number
              id="settingCost-cost"
              v-model="form.cost"
              :precision="2"
              :min="0"
              :max="99999999"
              placeholder="请输入"
              ref="cost"
              @keyup.enter.native="onSubmit"
              style="width: 100%;" />
          </a-form-model-item>
          <div style="display: flex;justify-content: center;padding: 30px 0;">
            <a-button type="primary" @click="onSubmit" id="setting-cost-submit">
              确定
            </a-button>
            <a-button style="margin-left: 15px;" @click="cansel" id="setting-cost-cancel">
              取消
            </a-button>
          </div>
        </a-form-model></a-spin>
      </a-form-model>
    </div>
  </a-modal>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
// 组件
import { VSelect } from '@/components'
// 接口
import { settingCost } from '@/api/supplier'
export default {
  name: 'SettingCost',
  mixins: [commonMixin],
  components: { VSelect },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      title: '设置成本价', // 弹窗标题
      // 表单label布局
      labelCol: { span: 4 },
      wrapperCol: { span: 18 },
      productName: '', // 产品名称
      productCode: '', // 产品编码
      // 提交参数
      form: {
        supplierSn: '', // 供应商sn
        supplierProductSn: '', // 产品sn
        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
      if (newValue) {
        // 默认首项获取焦点
        const _this = this
        setTimeout(() => {
          if (_this.$refs['cost']) {
            _this.$refs['cost'].focus()
          }
        }, 300)
      }
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
      }
    }
  }
}
</script>

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