<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
          id="settingCost-form"
          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 style="margin-bottom:10px;" label="供应商名称">
            {{ supplierName }}
          </a-form-model-item>
          <a-form-model-item style="margin-bottom:10px;" label="原成本价">
            {{ cost ? toThousands(cost) : '--' }}
          </a-form-model-item>
          <a-form-model-item label="变更成本价" prop="modifyCost">
            <a-input-number
              id="settingCost-modifyCost"
              v-model="form.modifyCost"
              :precision="2"
              :min="0"
              :max="99999999"
              placeholder="请输入"
              ref="modifyCost"
              @keyup.enter.native="onSubmit"
              style="width: 100%;" />
          </a-form-model-item>
          <div style="display: flex;justify-content: center;padding: 30px 0;">
            <a-button style="margin-right: 15px;" @click="cansel" id="setting-cost-cancel">
              取消
            </a-button>
            <a-button type="primary" @click="onSubmit" id="setting-cost-submit">
              确定
            </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
    },
    edit: { // 是否编辑
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      spinning: false,
      isShow: this.openModal, //  是否打开弹框
      title: '设置成本价', // 标题
      // 表单label布局设置
      labelCol: { span: 4 },
      wrapperCol: { span: 18 },
      productName: '', // 产品名称
      productCode: '', // 产品编码
      supplierName: '', // 供应商名称
      cost: '', // 成本
      form: {
        supplierSn: '', // 供应商sn
        supplierProductSn: '', // 产品sn
        modifyCost: '' // 最新成本
      },
      // 表单验证规则
      rules: {
        modifyCost: [
          { required: true, message: '请输入成本价', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    // 编辑时设置数据
    setData (row) {
      this.form.supplierSn = row.supplierSn
      this.form.supplierProductSn = row.supplierProductSn
      this.cost = row.cost
      this.productName = row.product.name
      this.productCode = row.product.code
      this.supplierName = row.supplierName
      const _this = this
      _this.$nextTick(() => {
        _this.form.modifyCost = _this.edit ? row.modifyCost : ''
      })
    },
    // 保存
    onSubmit () {
      const _this = this
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const params = JSON.parse(JSON.stringify(_this.form))
          if (params.modifyCost == 0) {
            this.$message.warning('成本价不能是0！')
            return false
          }
          _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.form.modifyCost = ''
      this.form.supplierSn = ''
      this.form.supplierProductSn = ''
      this.$refs.ruleForm.resetFields()
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
      if (newValue) {
        // 默认首项获取焦点
        const _this = this
        setTimeout(() => {
          if (_this.$refs['modifyCost']) {
            _this.$refs['modifyCost'].focus()
          }
        }, 300)
      }
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
      }
    }
  }
}
</script>

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