<template>
  <div>
      <div class="my-title">新增</div>
      <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="auto">
        <el-row class="my-row">
          <el-col :span="8">
            <el-form-item label="名称" prop="materialName">
              <el-input v-model="dataForm.materialName" placeholder="物品（零件）名称" ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="类别" prop="materialTypeId">
              <material-type-component v-model="dataForm.materialTypeId" :type-id.sync="dataForm.materialTypeId"></material-type-component>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="规格" prop="specifications">
              <el-input v-model="dataForm.specifications" placeholder="物品（零件）规格" ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="my-row">
          <el-col :span="8">
            <el-form-item label="数量" prop="cnt">
              <el-input-number v-model="dataForm.cnt" :min="0" placeholder="物品（零件）数量" ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="单价" prop="price">
              <el-input-number v-model="dataForm.price" :precision="2" :step="0.1" :min="0.0" placeholder="物品（零件）单价" ></el-input-number>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="金额" prop="amount">
              <el-input :value="!id?(dataForm.price * dataForm.cnt):dataForm.amount" :disabled="true" placeholder="物品（零件）总金额"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="my-row">
          <el-col :span="8">
            <el-form-item label="单位" prop="unitName">
              <el-input v-model="dataForm.unitName" placeholder="单位" ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="存放仓库" prop="warehouseId">
              <warehouse-select ref="warehouse" v-model:warehouse-id="dataForm.warehouseId" @select="warehouseChanged"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="存放货架" prop="shelveId">
              <shelve-select ref="shelve" v-model="dataForm.shelveId" :warehouse-id.sync="dataForm.warehouseId" :shelve-id.sync="dataForm.shelveId"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="my-row">
          <el-col :span="8">
            <el-form-item label="安全库存" prop="safeQuantity">
              <el-input-number v-model="dataForm.safeQuantity" :min="0" placeholder="安全库存"></el-input-number>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row class="my-row">
          <el-col :span="24">
            <el-form-item label="备注" prop="notes">
              <el-input type="textarea" v-model="dataForm.notes" placeholder="备注" ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="onChose">取消</el-button>
        <el-button type="primary" @click="dataFormSubmit()" v-reClick>确定</el-button>
      </span>
  </div>
</template>

<script>
import WarehouseSelect from './warehouse-select'
import ShelveSelect from './shelve-select'
import UserComponent from '../common/user-component'
import MaterialTypeComponent from '@/views/modules/common/material-type-component.vue'
export default {
  name: 'stock-add',
  components: {MaterialTypeComponent, UserComponent, ShelveSelect, WarehouseSelect},
  data () {
    return {
      visible: false,
      dictType: 'material_type',
      id: 0,
      dataForm: {
        materialId: 0,
        materialCode: '',
        materialName: '',
        materialTypeId: '',
        cateId: '',
        specifications: '',
        unitName: '',
        cnt: '',
        price: '',
        amount: '',
        warehouseId: '',
        shelveId: '',
        shelveName: '',
        notes: ''
      },
      dataRule: {
        materialName: [{ required: true, message: '物品（零件）名称不能为空', trigger: 'blur' }],
        specifications: [{ required: true, message: '规格不能为空', trigger: 'blur' }],
        materialTypeId: [{ required: true, message: '类别不能为空', trigger: 'blur' }],
        cnt: [{ required: true, message: '数量不能为空', trigger: 'blur' }],
        price: [{ required: true, message: '单价不能为空', trigger: 'blur' }],
        unitName: [{ required: true, message: '单位不能为空', trigger: 'blur' }],
        safeQuantity: [{ required: true, message: '安全库存不能为空', trigger: 'blur' }]
      }
    }
  },
  methods: {
    onChose () {
      this.$emit('onChose')
    },
    async init (id) {
      this.visible = true
      this.id = id || 0
      this.dataForm = {}
    },
      // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/biz-service/stock-mg-ctl/materialSave`),
            method: 'post',
            data: this.$http.adornData(this.dataForm)
          }).then(({data}) => {
            if (data && data.code === '200') {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.onChose()
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    },
    validateField (type) {
      this.$refs.dataForm.validateField(type)
    },
    warehouseChanged (item) {
      this.dataForm.warehouseId = item
      this.$refs.shelve.init(item, this.dataForm.shelveId)
    }
  }
}
</script>

<style scoped>{
}
</style>
