<template>
  <!-- 基于 Element UI 新增和修改弹窗 -->
  <el-dialog
      :title="!dataForm.id ? '添加' : '修改'"
      :close-on-click-modal="false"
      :visible.sync="visible">
    <!-- 新增和修改表单 -->
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataSubmit()" label-width="80px">
<!--    <el-form-item label="供应商id" prop="custId">-->
<!--      <el-input v-model="dataForm.custId" placeholder="供应商id"></el-input>-->
<!--    </el-form-item>-->
    <el-form-item label="供应商名称" prop="custName">
      <el-input v-model="dataForm.custName" placeholder="供应商名称"></el-input>
    </el-form-item>
    <el-form-item label="商品编号" prop="goodsCode">
      <el-input v-model="dataForm.goodsCode" placeholder="商品编号"></el-input>
    </el-form-item>
    <el-form-item label="采购商品名称" prop="goodsName">
      <el-input v-model="dataForm.goodsName" placeholder="采购商品名称"></el-input>
    </el-form-item>
    <el-form-item label="规格" prop="spec">
      <el-input v-model="dataForm.spec" placeholder="规格"></el-input>
    </el-form-item>
    <el-form-item label="单位" prop="unit">
      <el-input v-model="dataForm.unit" placeholder="单位"></el-input>
    </el-form-item>
    <el-form-item label="商品详情" prop="goodsDesc">
      <el-input v-model="dataForm.goodsDesc" placeholder="商品详情"></el-input>
    </el-form-item>
    <el-form-item label="采购成本价" prop="costPrice">
      <el-input v-model="dataForm.costPrice" placeholder="采购成本价"></el-input>
    </el-form-item>
    <el-form-item label="售价" prop="salePrice">
      <el-input v-model="dataForm.salePrice" placeholder="售价"></el-input>
    </el-form-item>
    <el-form-item label="入库日期" prop="inDate">
      <el-input v-model="dataForm.inDate" placeholder="入库日期"></el-input>
    </el-form-item>
    <el-form-item label="有效日期" prop="expiryDate">
      <el-input v-model="dataForm.expiryDate" placeholder="有效日期"></el-input>
    </el-form-item>
    <el-form-item label="状态" prop="status">
      <el-input v-model="dataForm.status" placeholder="状态"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import * as api from '@/api/cmcustproduct.js'
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          custId: '' ,
          custName: '' ,
          goodsCode: '' ,
          goodsName: '' ,
          spec: '' ,
          unit: '' ,
          goodsDesc: '' ,
          costPrice: '' ,
          salePrice: '' ,
          inDate: '' ,
          expiryDate: '' ,
          status: '' ,
          remark: '' ,
        },
        dataRule: {
          custId: [
            { required: true, message: '供应商id  为必填项', trigger: 'blur' }
          ],
          custName: [
            { required: true, message: '供应商名称  为必填项', trigger: 'blur' }
          ],
          goodsCode: [
            { required: true, message: '商品编号  为必填项', trigger: 'blur' }
          ],
          goodsName: [
            { required: true, message: '采购商品名称  为必填项', trigger: 'blur' }
          ],
          spec: [
            { required: true, message: '规格  为必填项', trigger: 'blur' }
          ],
          unit: [
            { required: true, message: '单位  为必填项', trigger: 'blur' }
          ],
          goodsDesc: [
            { required: true, message: '商品详情  为必填项', trigger: 'blur' }
          ],
          costPrice: [
            { required: true, message: '采购成本价  为必填项', trigger: 'blur' }
          ],
          salePrice: [
            { required: true, message: '售价  为必填项', trigger: 'blur' }
          ],
          inDate: [
            { required: true, message: '入库日期  为必填项', trigger: 'blur' }
          ],
          expiryDate: [
            { required: true, message: '有效日期  为必填项', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '状态  为必填项', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注  为必填项', trigger: 'blur' }
          ],
        }
      }
    },
    methods: {
      init (id) { // 初始化表单验证规则
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(function() {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            api.cmcustproductDetailApi(id).then(function(res) {
              this.dataForm = res;
            }.bind(this))
          }
        }.bind(this))
      },
      // 表单数据提交
      dataSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
              api.cmcustproductUpdateApi(this.dataForm).then(function(res) {
                this.$message.success('保存成功')
                this.visible = false
                this.$emit('refreshDataList')
              }.bind(this));
            } else {
              api.cmcustproductCreateApi(this.dataForm).then(function(res) {
                this.$message.success('新增成功')
                this.visible = false
                this.$emit('refreshDataList')
              }.bind(this));
            }
          }
        })
      }
    }
  }
</script>
