<!-- 采购 -->
<template>
    <div>
        <div class="my-title">{{subTitle}}</div>
        <!-- 表单 -->
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="auto">
          <div class="form-item" v-for="(item, index) in dataForm.list" :key="item.purchaseDetailId">
            <el-row>
            <el-col :span="6">
              <el-form-item label="物品名称">
                <span>{{item.materialName}}</span>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="规格/型号">
                <span>{{item.specification}}</span>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="数量">
                <span>{{item.cnt}}</span>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="用途">
                <span>{{item.batchNumber}}</span>
              </el-form-item>
            </el-col>
          </el-row>
            <el-row class="my-row">
              <el-col :span="6">
                <el-form-item label="采购数量" :prop="'list.' + index + '.cnt'" :rules="dataRule.cnt">
                  <el-input-number v-model="item.cnt" :step="1" :min="0"></el-input-number>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item label="单位" :prop="'list.' + index + '.unitName'" :rules="dataRule.unitName">
                  <el-input v-model="item.unitName" placeholder="单位单位"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="备注" :prop="'list.' + index + '.notes'" :rules="dataRule.notes">
                  <el-input type="textarea" v-model="item.notes"></el-input>
                </el-form-item>
              </el-col>
            </el-row>
          </div>
        </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 {purchase} from '@/api/production'
import MaterialComponent from '@/views/modules/common/material-component'

export default {
  name: 'scheduling-purchase',
  components: {
    MaterialComponent
  },
  data () {
    return {
      id: 0,
      dataForm: {
        list:[]
      },
      dataRule: {
        cnt: [{ required: true, message: '请输入数量', trigger: 'change' }],
        unitName: [{ required: true, message: '请输入单位', trigger: 'blur' }]
      },
      subTitle:''
    }
  },
  methods: {
    onChose () {
      this.$emit('onChose')
    },
    async init (row) {
      this.dataForm.id = row.id
      if(row instanceof Array){
        this.subTitle = '批量采购'
        this.dataForm.list = row
      } else {
        this.subTitle = '采购'
        this.dataForm.list = [row]
      }
    },
    validateField (type) {
      this.$refs.dataForm.validateField(type)
    },
      // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          purchase(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)
            }
          })
        }
      })
    }
  }
}
</script>

<style scoped>
.form-item {
  margin: 5px 0;
  padding:25px 0 5px;
  background-color: #f1f9f9;
  border-radius: 10px;
}
</style>
