<template>
  <div>
    <el-dialog
      :title="!id ? '新增': display ? '详情' : '新增或修改'"
      width="50%"
      :close-on-click-modal="false"
      :visible.sync="visible">
      <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="100px">
        <el-form-item label="物品名称" prop="materialName">
<!--          <material-component v-model="dataForm" :material-id="dataForm.materialId"/>-->
          <el-input v-model="dataForm.materialName" :disabled="display" placeholder="物品名称"></el-input>
        </el-form-item>
        <el-form-item label="型号及规格" prop="specification">
          <el-input v-model="dataForm.specification" :disabled="display" placeholder="型号及规格"></el-input>
        </el-form-item>
        <el-form-item label="采购类别" prop="purchaseType">
          <el-select
            v-model="dataForm.purchaseType"
            :disabled="display"
            remote
            placeholder="请选择">
            <el-option
              v-for="item in optionsType"
              :key="item.code"
              :label="item.value"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="数量" prop="cnt">
          <el-input-number v-model="dataForm.cnt" :disabled="display" :min="1"/>
        </el-form-item>
        <el-form-item label="单位" prop="unitName">
          <el-input v-model="dataForm.unitName" :disabled="display" placeholder="单位"></el-input>
        </el-form-item>
        <el-form-item label="采购期限" prop="deadline">
          <el-date-picker
            v-model="dataForm.deadline"
            value-format="yyyy-MM-dd"
            type="date">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="批次号" prop="batchNumber">
          <el-input v-model="dataForm.batchNumber" :disabled="display" placeholder="批次号"></el-input>
        </el-form-item>
        <el-form-item label="用途" prop="use">
          <el-input v-model="dataForm.use" :disabled="display" placeholder="用途"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="notes">
          <el-input type="textarea" v-model="dataForm.notes" :disabled="display" placeholder="备注"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button v-if="!display" type="primary" @click="dataFormSubmit()" v-reClick>确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import MaterialComponent from '@/views/modules/common/material-component'
  export default {
    name: 'add-material',
    props: {
      optionsType: {
        type: Array,
        default: () => []
      }
    },
    components: {MaterialComponent},
    data () {
      return {
        visible: false,
        display: false,
        dataList: [],
        transferData: {},
        dataForm: {},
        id: 0,
        dataRule: {
          materialName: [{ required: true, message: '物品名称不能为空', trigger: 'blur' }],
          specification: [{ required: true, message: '型号及规格不能为空', trigger: 'blur' }],
          unitName: [{ required: true, message: '单位不能为空', trigger: 'blur' }],
          purchaseType: [{ required: true, message: '请选择采购类别', trigger: 'change' }],
          deadline: [{ required: true, message: '请选择采购期限', trigger: 'change' }]
          // batchNumber: [{ required: true, message: '批次号不能为空', trigger: 'blur' }]
        }
      }
    },
    methods: {
      init (id, display, transferData) {
        this.dataForm = {
          cnt: 1
        }
        this.visible = true
        this.id = id || Math.round(Math.random() * 1000000)
        this.display = display
        if (!id) return
        this.dataForm = transferData
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.dataForm.detailId = this.id
            this.visible = false
            this.$emit('addItem', this.dataForm)
          }
        })
      }
    }
  }
</script>

<style scoped>

</style>
