<!-- 核料 -->
<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 prop="materialId">
                <label slot="label"><span style="color:red">*</span> 物品（零件）名称</label>
                <material-component v-model="material" @materialSelected="materialSelected"/>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row class="my-row">
            <el-col :span="8">
              <el-form-item label="物品（零件）规格" prop="specifications">
                <el-input v-model="specifications" placeholder="物品（零件）规格" disabled></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="物品（零件）单位" prop="unitName">
                <el-input v-model="unitName" placeholder="物品（零件）单位" disabled></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="物品（零件）核定数量" prop="inventoryCnt">
                <el-input-number v-model="dataForm.inventoryCnt" :step="1" :min="0" :max="Number(dataForm.cnt)"></el-input-number>
              </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 { updateCheck } from '@/api/production'
  import MaterialComponent from '@/views/modules/common/material-component'

export default {
    name: 'scheduling-check',
    components: {
      MaterialComponent
    },
    data () {
      return {
        id: 0,
        dataForm: {},
        material: {},
        specifications: '',
        unitName: '',
        dataRule: {
          inventoryCnt: [{ required: true, message: '请输入核定数量', trigger: 'change' }]
        }
      }
    },
    methods: {
      onChose () {
        this.$emit('onChose')
      },
      async init (scheduleId, materialId) {
        this.dataForm.id = scheduleId || ''
        this.dataForm.materialId = materialId || ''
      },
      validateField (type) {
        this.$refs.dataForm.validateField(type)
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.materialId === '') {
              this.$message.error('请选择物品！')
              return
            }
            updateCheck(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)
              }
            })
          }
        })
      },
      materialSelected (item) {
        this.unitName = item.unitName
        this.specifications = item.specifications
        this.dataForm.cnt = item.cnt
        this.dataForm.materialId = item.materialId
      }
    }
  }
</script>

<style scoped>

</style>
