<template>
  <el-dialog
    :title="!id ? '新增': display ? '查看' : '出库'"
    width="70%"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <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="recordCode">
            <el-input v-model="dataForm.recordCode" :disabled="true" placeholder="模板出库编码"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8" style="padding-left: 20px">
          <el-form-item label="产品编号" prop="recordNumber">
            <el-input v-model="dataForm.recordNumber" :disabled="true" placeholder="产品编号"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="8" style="padding-left: 20px">
          <el-form-item label="模板名称" prop="templateName">
            <el-input v-model="dataForm.templateName" :disabled="true" placeholder="模板名称"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
      <el-row class="my-row">
        <el-form-item label="备注说明" prop="notes">
            <el-input v-model="dataForm.notes" :disabled="true" placeholder="备注说明"></el-input>
          </el-form-item>
      </el-row>
      <el-row>
        <div class="title"><span style="color: red">*</span> 模板对应的零件</div>
        <el-table
          :data="dataList"
          border
          style="width: 100%;">
          <el-table-column
            label="序号"
            type="index"
            width="50"
            align="center">
          </el-table-column>
          <el-table-column
            prop="materialName"
            header-align="center"
            align="center"
            label="物品名称">
          </el-table-column>
          <el-table-column
            prop="specifications"
            header-align="center"
            align="center"
            label="规格">
          </el-table-column>
          <el-table-column
            prop="planCnt"
            header-align="center"
            align="center"
            label="计划数量">
          </el-table-column>
          <el-table-column
            prop="unitName"
            header-align="center"
            align="center"
            label="单位">
          </el-table-column>
          <el-table-column
            prop="cnt"
            header-align="center"
            align="center"
            label="仓库库存数量">
          </el-table-column>
          <el-table-column
            v-if="display"
            prop="deliveredCnt"
            header-align="center"
            align="center"
            label="已出库">
          </el-table-column>
          <el-table-column
            v-if="!display"
            :key="display"
            prop="planCntUser"
            header-align="center"
            align="center"
            label="请选择数量">
            <template slot-scope="scope">
              <el-input-number v-model="scope.row.planCntUser" :min="1" style="width: 140px;"/>
            </template>
          </el-table-column>
          <el-table-column
            v-if="!display"
            prop="undeliveredCnt"
            header-align="center"
            align="center"
            label="未出库数量">
          </el-table-column>
          <el-table-column
            v-if="display"
            prop="batchName"
            header-align="center"
            align="center"
            label="选择零件的批次和数量">
          </el-table-column>
          <el-table-column
            prop="notes"
            header-align="center"
            align="center"
            label="备注">
          </el-table-column>
        </el-table>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button v-if="!display" @click="visible = false">取消</el-button>
      <el-button v-if="!display" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  import {getDeliveryDetail, templateOut} from '@/api/warehouse'
  export default {
    name: 'template-delivery-detail',
    data () {
      return {
        visible: false,
        display: false,
        dataList: [],
        id: 0,
        dataForm: { },
        dataRule: {
        }
      }
    },
    methods: {
      async init (id, display) {
        this.visible = true
        this.id = id || 0
        this.display = display
        this.dataForm = {}
        this.dataList = []
        if (!id) return
        // 获取详情
        await getDeliveryDetail(this.id).then(({data}) => {
          if (data && data.code === '200') {
            this.dataForm = data.data
            if (data.data.whTemplateItemPlanList) {
              data.data.whTemplateItemPlanList.forEach((item) => {
                this.dataList.push(item)
              })
            }
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            let params = {
              'recordId': this.id,
              'whTemplateItemPlanList': this.dataList
            }
            templateOut(params).then(({data}) => {
              if (data && data.code === '200') {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      },
      validateField (type) {
        this.$refs.dataForm.validateField(type)
      }
    }
  }
</script>

<style scoped>
.my-line{
  border-bottom: 1px solid #c0c4cc;
  margin-bottom: 10px;
}
</style>
