<template>
  <div class="app-container">
    <el-form ref="queryForm" :model="queryParams" :inline="true" size="small" label-width="90px">
      <el-form-item label="货单编号" prop="sheetNo">
        <el-input v-model="queryParams.sheetNo" placeholder="请输入货单编号" clearable style="width: 220px" />
      </el-form-item>
      <el-form-item label="货单日期" prop="sheetDate">
        <el-date-picker v-model="queryParams.sheetDate" type="date" value-format="yyyy-MM-dd" placeholder="请选择货单日期" style="width: 220px" />
      </el-form-item>
      <el-form-item label="生产类型" prop="productionType">
        <el-input v-model="queryParams.productionType" placeholder="请输入生产类型" clearable style="width: 220px" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" v-hasPermi="['erp:stock:inbound:add']">新增入库</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete" v-hasPermi="['erp:stock:inbound:remove']">删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" prop="id" width="80" align="center" />
      <el-table-column label="货单日期" prop="sheetDate" min-width="110" align="center" />
      <el-table-column label="货单编号" prop="sheetNo" min-width="130" align="center" />
      <el-table-column label="生产类型" prop="productionType" min-width="110" align="center" />
      <el-table-column label="生产车间编号" prop="productionPlantNo" min-width="130" align="center" />
      <el-table-column label="合计金额" prop="totalAmount" min-width="120" align="center" />
      <el-table-column label="备注" prop="remark" min-width="160" show-overflow-tooltip />
      <el-table-column label="操作" align="center" width="160">
        <template slot-scope="scope">
          <el-button type="text" size="mini" @click="handleView(scope.row)" v-hasPermi="['erp:stock:inbound:query']">查看</el-button>
          <el-button type="text" size="mini" @click="handleDelete(scope.row)" v-hasPermi="['erp:stock:inbound:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-dialog :title="title" :visible.sync="open" width="1300px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" v-if="!readonly" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="8">
            <el-form-item label="货单日期" prop="sheetDate">
              <el-date-picker
                style="width: 100%;"
                v-model="form.sheetDate"
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="请选择货单日期"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="货单编号">
              <el-input v-model="form.sheetNo" placeholder="提交后自动生成" readonly />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="拷贝单号">
              <el-input>
                <template #append>
                  <el-tooltip class="item" effect="dark" content="拷贝单号" placement="top">
                    <el-button type="success" icon="el-icon-search" circle size="mini"></el-button>
                  </el-tooltip>
                </template>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="生产类型" prop="productionType">
              <el-select style="width: 100%;" filterable v-model="form.productionType">
                <el-option v-for="item in dict.type.inventory_production_type" :key="item.value" :value="item.value" :label="item.label"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生产车间编号" prop="productionPlantNo">
              <el-select style="width: 100%;" filterable v-model="form.productionPlantNo" @change="changeProductionPlantNo">
                <el-option v-for="item in productionPlantList" :key="item.productionPlantNo" :value="item.productionPlantNo" :label="item.productionPlantName"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="货单备注">
              <el-input type="textarea" v-model="form.remark" show-word-limit placeholder="请输入货单备注" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-divider content-position="left">货品列表</el-divider>
          <el-form-item label-width="0">
            <el-table
              :data="form.goodsList"
              style="width: 100%;"
              border
              stripe
            >
              <el-table-column label="货品编号" prop="goodsNum" fixed width="200" align="center">
                <template slot-scope="{ row, $index }">
                  <el-form-item
                    :prop="`goods.${$index}.goodsNum`"
                    :rules="rules.goodsNum"
                    class="table-form-item"
                  >
                    <el-input
                      v-model="row.goodsNum"
                      placeholder="请输入货品编号"
                      @change="handleEdit($index, 'goodsNum', row.goodsNum)"
                    >
                      <template #append>
                        <el-tooltip class="item" effect="dark" content="货品速查" placement="top">
                          <el-button type="success" icon="el-icon-search" circle size="mini" @click="openQueryMaterial($index)"></el-button>
                        </el-tooltip>
                      </template>
                    </el-input>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="货品名称" prop="goodsName" fixed width="150" align="center">
                <template slot-scope="{ row, $index }">
                  <el-form-item
                    :prop="`goods.${$index}.goodsName`"
                    :rules="rules.goodsName"
                    class="table-form-item"
                  >
                    <el-input
                      v-model="row.goodsName"
                      placeholder="请输入货品名称"
                      @change="handleEdit($index, 'goodsNum', row.goodsName)"
                    >
                    </el-input>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="规格" prop="goodsSpec" width="120" align="center">
                <template slot-scope="{ row, $index }">
                  <el-form-item
                    :prop="`goods.${$index}.goodsSpec`"
                    :rules="rules.goodsSpec"
                    class="table-form-item"
                  >
                    <el-input
                      v-model="row.goodsSpec"
                      placeholder="请输入规格"
                      @change="handleEdit($index, 'goodsSpec', row.goodsSpec)"
                    >
                    </el-input>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="品类" prop="goodsType" width="150" align="center">
                <template slot-scope="{ row, $index }">
                  <el-form-item
                    :prop="`goods.${$index}.goodsType`"
                    :rules="rules.goodsType"
                    class="table-form-item"
                  >
                    <el-select
                      v-model="row.goodsType"
                      placeholder="请选择品类"
                      @change="handleEdit($index, 'goodsType', row.goodsType)"
                    >
                      <el-option v-for="item in goodsList" :key="item.num" :value="item.num" :label="item.categoryName"></el-option>
                    </el-select>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="级别" prop="goodsLevel" width="150" align="center">
                <template slot-scope="{ row, $index }">
                  <el-form-item
                    :prop="`goods.${$index}.goodsLevel`"
                    :rules="rules.goodsLevel"
                    class="table-form-item"
                  >
                    <el-select
                      v-model="row.goodsLevel"
                      placeholder="请选择级别"
                      @change="handleEdit($index, 'goodsLevel', row.goodsLevel)"
                    >
                      <el-option v-for="item in levelList" :key="item.levelNo" :value="item.levelNo" :label="item.categoryName"></el-option>
                    </el-select>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="仓库" prop="warehouseNo" width="150" align="center">
                <template slot-scope="{ row, $index }">
                  <el-form-item
                    :prop="`goods.${$index}.warehouseNo`"
                    :rules="rules.warehouseNo"
                    class="table-form-item"
                  >
                    <el-select
                      v-model="row.warehouseNo"
                      placeholder="请选择仓库"
                      @change="handleEdit($index, 'warehouseNo', row.warehouseNo)"
                    >
                      <el-option v-for="item in wareList" :key="item.warehouseNo" :value="item.warehouseNo" :label="item.warehouseName"></el-option>
                    </el-select>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="批次号" width="130" align="center">
                <template slot-scope="{ row }">
                  <el-input v-model="row.batchNo" placeholder="请输入批次号" />
                </template>
              </el-table-column>
              <el-table-column label="基本计量数量" prop="baseQty" width="150" align="center">
                <template slot-scope="{ row, $index }">
                  <el-form-item
                    :prop="`goods.${$index}.baseQty`"
                    :rules="rules.baseQty"
                    class="table-form-item"
                  >
                    <decimal-input
                      v-model="row.baseQty"
                      :min="0"
                      :max="999999"
                      :precision="2"
                      placeholder="请输入基本计量数量"
                      @blur="handleEdit($index, 'baseQty', row.baseQty)"
                    />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="基本计量单位" prop="baseUnit" width="120" align="center">
                <template slot-scope="{ row, $index }">
                  <el-form-item
                    :prop="`goods.${$index}.baseUnit`"
                    :rules="rules.baseUnit"
                    class="table-form-item"
                  >
                    <el-input
                      v-model="row.baseUnit"
                      placeholder="请输入基本计量单位"
                      @change="handleEdit($index, 'baseUnit', row.baseUnit)"
                    >
                    </el-input>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="辅助计量数量" prop="assistantQty" width="150" align="center">
                <template slot-scope="{ row, $index }">
                  <el-form-item
                    :prop="`goods.${$index}.assistantQty`"
                    :rules="rules.assistantQty"
                    class="table-form-item"
                  >
                    <decimal-input
                      v-model="row.assistantQty"
                      :min="0"
                      :max="999999"
                      :precision="2"
                      placeholder="请输入辅助计量数量"
                      @blur="handleEdit($index, 'assistantQty', row.assistantQty)"
                    />
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="辅助计量单位" prop="assistantUnit" width="120" align="center">
                <template slot-scope="{ row, $index }">
                  <el-form-item
                    :prop="`goods.${$index}.assistantUnit`"
                    :rules="rules.assistantUnit"
                    class="table-form-item"
                  >
                    <el-input
                      v-model="row.assistantUnit"
                      placeholder="请输入辅助计量单位"
                      @change="handleEdit($index, 'assistantUnit', row.assistantUnit)"
                    >
                    </el-input>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="货品备注" prop="remark" width="120" align="center">
                <template slot-scope="{ row, $index }">
                  <el-form-item
                    :prop="`goods.${$index}.remark`"
                    :rules="rules.remark"
                    class="table-form-item"
                  >
                    <el-input
                      v-model="row.remark"
                      placeholder="请输入货品备注"
                      @change="handleEdit($index, 'remark', row.remark)"
                    >
                    </el-input>
                  </el-form-item>
                </template>
              </el-table-column>
              <el-table-column label="操作" width="100" align="center" fixed="right">
                <template slot-scope="{ $index }">
                  <el-button
                    type="danger"
                    size="small"
                    icon="el-icon-delete"
                    @click="handleDeleteRow($index)"
                  />
                </template>
              </el-table-column>
            </el-table>
            <!-- <el-table :data="form.goodsList" style="width: 100%;" border stripe>
              <el-table-column label="物料编号" prop="goodsNum" fixed width="210" align="center">
                <template slot-scope="{ row }">
                  <el-select
                    v-model="row.goodsNum"
                    filterable
                    placeholder="请选择物料"
                    style="width: 100%"
                    @change="onMaterialChange(row)"
                  >
                    <el-option
                      v-for="item in materialOptions"
                      :key="item.goodsNum"
                      :label="`${item.goodsNum} - ${item.goodsName}`"
                      :value="item.goodsNum"
                    />
                  </el-select>
                </template>
              </el-table-column>
              <el-table-column label="物料名称" prop="goodsName" fixed width="150" align="center" />
              <el-table-column label="规格" prop="goodsSpec" width="120" align="center" />
              <el-table-column label="品类" prop="goodsType" width="120" align="center" />
              <el-table-column label="级别" prop="goodsLevel" width="120" align="center" />
              <el-table-column label="仓库编号" prop="warehouseNo" width="120" align="center" />
              <el-table-column label="仓库名称" prop="warehouseName" width="150" align="center" />
              <el-table-column label="批次号" width="130" align="center">
                <template slot-scope="{ row }">
                  <el-input v-model="row.batchNo" placeholder="请输入批次号" />
                </template>
              </el-table-column>
              <el-table-column label="基本数量" width="150" align="center">
                <template slot-scope="{ row }">
                  <el-input-number v-model="row.baseQty" :precision="4" :min="0" @change="calcTotalAmount" />
                </template>
              </el-table-column>
              <el-table-column label="基本单位" prop="baseUnit" width="120" align="center" />
              <el-table-column label="辅助数量" width="150" align="center">
                <template slot-scope="{ row }">
                  <el-input-number v-model="row.assistantQty" :precision="4" :min="0" />
                </template>
              </el-table-column>
              <el-table-column label="辅助单位" prop="assistantUnit" width="120" align="center" />
              <el-table-column label="金额" width="150" align="center">
                <template slot-scope="{ row }">
                  <el-input-number v-model="row.amount" :precision="4" :min="0" @change="calcTotalAmount" />
                </template>
              </el-table-column>
              <el-table-column label="货品备注" width="150" align="center">
                <template slot-scope="{ row }">
                  <el-input v-model="row.remark" placeholder="请输入货品备注" />
                </template>
              </el-table-column>
              <el-table-column label="操作" width="100" align="center" fixed="right">
                <template slot-scope="{ $index }">
                  <el-button
                    type="danger"
                    size="small"
                    icon="el-icon-delete"
                    @click="removeGoodsRow($index)"
                  />
                </template>
              </el-table-column>
            </el-table> -->
            <div style="margin-top: 10px; text-align: center;">
              <el-button type="primary" icon="el-icon-plus" size="small" @click="addGoodsRow">
                添加货品
              </el-button>
            </div>
          </el-form-item>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="总金额">
              <el-input :value="totalAmountText" readonly />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template v-else>
        <el-descriptions border>
          <el-descriptions-item label="货单日期">{{ form.sheetDate }}</el-descriptions-item>
          <el-descriptions-item label="货单编号">{{ form.sheetNo }}</el-descriptions-item>
          <el-descriptions-item label="生产类型">{{ form.productionType }}</el-descriptions-item>
          <el-descriptions-item label="生产车间编号">{{ form.productionPlantNo }}</el-descriptions-item>
          <el-descriptions-item label="生产车间名称">{{ form.productionPlantName }}</el-descriptions-item>
          <el-descriptions-item label="货单备注">{{ form.remark }}</el-descriptions-item>
        </el-descriptions>
        <el-divider content-position="left">货品列表</el-divider>
        <el-table :data="form.goodsList" style="width: 100%;" border stripe>
          <el-table-column label="物料编号" prop="goodsNum" fixed width="210" align="center" />
          <el-table-column label="物料名称" prop="goodsName" fixed width="150" align="center" />
          <el-table-column label="规格" prop="goodsSpec" width="120" align="center" />
          <el-table-column label="品类" prop="goodsType" width="120" align="center" />
          <el-table-column label="级别" prop="goodsLevel" width="120" align="center" />
          <el-table-column label="仓库编号" prop="warehouseNo" width="120" align="center" />
          <el-table-column label="仓库名称" prop="warehouseName" width="150" align="center" />
          <el-table-column label="批次号" prop="batchNo" width="130" align="center" />
          <el-table-column label="基本数量" prop="baseQty" width="150" align="center" />
          <el-table-column label="基本单位" prop="baseUnit" width="120" align="center" />
          <el-table-column label="辅助数量" prop="assistantQty" width="150" align="center" />
          <el-table-column label="辅助单位" prop="assistantUnit" width="120" align="center" />
          <el-table-column label="金额" prop="amount" width="150" align="center" />
          <el-table-column label="货品备注" prop="remark" width="150" align="center" />
        </el-table>
      </template>
      <div slot="footer" class="dialog-footer" v-if="!readonly">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
      <query-material ref="query-material" @selectList="getSelectList"></query-material>
    </el-dialog>
  </div>
</template>

<script>
import {
  addInbound,
  delInbound,
  getInbound,
  listInbound,
  listWarehouseOptions,
  listInboundMaterialOptions
} from "@/api/erp/stock";
import { getGoodsList } from "@/api/baseInfo/goods";
import { getLevelList } from "@/api/baseInfo/level";
import queryMaterial from "../../../components/queryMaterial.vue";
export default {
  name: "StockInbound",
  components: {queryMaterial},
  dicts: ['inventory_production_type'],
  data() {
    return {
      loading: false,
      showSearch: true,
      total: 0,
      list: [],
      ids: [],
      multiple: true,
      open: false,
      title: "",
      readonly: false,
      materialOptions: [],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sheetNo: undefined,
        sheetDate: undefined,
        productionType: undefined
      },
      form: {},
      rules: {
        sheetDate: [{ required: true, message: "货单日期不能为空", trigger: "change" }],
        productionType: [{ required: true, message: "生产类型不能为空", trigger: "blur" }]
      },
      goodsList: [],
      levelList: [],
      productionPlantList: [
        {
          productionPlantNo: '001',
          productionPlantName: '常规生产车间'
        }
      ]
    };
  },
  computed: {
    totalAmountText() {
      const total = (this.form.goodsList || []).reduce((sum, item) => sum + Number(item.amount || 0), 0);
      return total.toFixed(4);
    }
  },
  created() {
    this.getList();
    this.loadMaterialOptions();
    this.getWarehouse()
    this.getGoods()
    this.getLevel()
  },
  methods: {
     /**生产车间**/
     changeProductionPlantNo(val) {
      let item = getFindByKey(this.productionPlantList, val, 'productionPlantNo')
      if(item) {
        this.form.productionPlantName = item.productionPlantName
      }
    },
    /** 仓库列表 **/
    getWarehouse() {
      listWarehouseOptions({}).then(res => {
        this.wareList = res.data
      })
    },
    /** 品种 **/
    getGoods() {
      getGoodsList({}).then(res => {
        this.goodsList = res.data
      })
    },
    /** 级别 **/
    getLevel() {
      getLevelList({}).then(res => {
        this.levelList = res.data
      })
    },
    getList() {
      this.loading = true;
      listInbound(this.queryParams).then((res) => {
        this.list = res.rows || [];
        this.total = res.total || 0;
        this.loading = false;
      });
    },
    loadMaterialOptions() {
      listInboundMaterialOptions().then((res) => {
        this.materialOptions = res.data || [];
      });
    },
    reset() {
      this.form = {
        id: undefined,
        sheetDate: this.parseTime(new Date(), "{y}-{m}-{d}"),
        sheetNo: "",
        productionType: "",
        productionPlantNo: "",
        productionPlantName: "",
        remark: "",
        goodsList: [this.buildEmptyGoodsRow()]
      };
      this.resetForm("form");
    },
    buildEmptyGoodsRow() {
      return {
        goodsNum: "",
        goodsName: "",
        goodsSpec: "",
        goodsType: "",
        goodsLevel: "",
        warehouseNo: "",
        warehouseName: "",
        batchNo: "",
        baseUnit: "",
        baseQty: 0,
        assistantUnit: "",
        assistantQty: 0,
        amount: 0,
        remark: ""
      };
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    handleEdit(index, field, value) {
      console.log(`编辑第 ${index} 行，字段: ${field}，值: ${value}`);

      // 触发对应字段的校验
      this.$refs.form.validateField(`goods.${index}.${field}`);

      // // 自动计算金额
      // if (field === 'unitPrice' || field === 'baseNum') {
      //   const row = this.form.goods[index];
      //   row.amount = (row.unitPrice || 0) * (row.baseNum || 0);
      // }
      if(field === 'goodsType') {
        let item = getFindByKey(this.goodsList, value, 'num')
        if(item) {
          this.form.goodsList[this.goodsIndex].goodsTypeName = item.categoryName
        }
      }
      // if(field === 'goodsLevel') {
      //   let item = getFindByKey(this.levelList, value, 'levelNo')
      //   if(item) {
      //     this.form.goodsList[this.goodsIndex].levelName = item.levelName
      //   }
      // }
      if(field === 'warehouseNo') {
        let item = getFindByKey(this.wareList, value, 'warehouseNo')
        if(item) {
          this.form.goodsList[this.goodsIndex].warehouseName = item.warehouseName
        }
      }
    },
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.multiple = !selection.length;
    },
    handleAdd() {
      this.readonly = false;
      this.reset();
      this.open = true;
      this.title = "新增生产入库";
    },
    handleView(row) {
      this.readonly = true;
      getInbound(row.id).then((res) => {
        this.form = Object.assign(
          {
            goodsList: []
          },
          res.data || {}
        );
        this.open = true;
        this.title = "查看生产入库";
      });
    },
    addGoodsRow() {
      this.form.goodsList.push(this.buildEmptyGoodsRow());
    },
    removeGoodsRow(index) {
      this.form.goodsList.splice(index, 1);
      if (this.form.goodsList.length === 0) {
        this.form.goodsList.push(this.buildEmptyGoodsRow());
      }
      this.calcTotalAmount();
    },
    onMaterialChange(row) {
      const hit = this.materialOptions.find((item) => item.goodsNum === row.goodsNum);
      if (!hit) {
        return;
      }
      row.goodsName = hit.goodsName || "";
      row.goodsSpec = hit.goodsSpec || "";
      row.goodsType = hit.goodsType || "";
      row.goodsLevel = hit.goodsLevel || "";
      row.warehouseNo = hit.warehouseNo || "";
      row.warehouseName = hit.warehouseName || "";
      row.baseUnit = hit.baseUnit || "";
      row.assistantUnit = hit.assistantUnit || "";
    },
    handleDeleteRow(index) {
      this.$confirm('确定要删除该货品吗？','提示', {
        type: 'warning'
      }).then(() => {
        this.form.goodsList.splice(index, 1)
        this.$message.success('删除成功')
      })
    },
    calcTotalAmount() {
      this.$forceUpdate();
    },
    cancel() {
      this.open = false;
      this.reset();
    },
    validateGoodsList() {
      const goodsList = this.form.goodsList || [];
      if (!goodsList.length) {
        this.$modal.msgError("至少需要一条物料明细");
        return false;
      }
      for (let i = 0; i < goodsList.length; i++) {
        const row = goodsList[i];
        if (!row.goodsNum) {
          this.$modal.msgError(`第${i + 1}行未选择物料`);
          return false;
        }
        if (!row.batchNo) {
          this.$modal.msgError(`第${i + 1}行批次号不能为空`);
          return false;
        }
      }
      return true;
    },
    submitForm() {
      this.$refs.form.validate((valid) => {
        if (!valid || !this.validateGoodsList()) {
          return;
        }
        const payload = Object.assign({}, this.form, {
          goodsList: (this.form.goodsList || []).map((item) => ({
            ...item,
            baseQty: Number(item.baseQty || 0),
            assistantQty: Number(item.assistantQty || 0),
            amount: Number(item.amount || 0)
          }))
        });
        addInbound(payload).then(() => {
          this.$modal.msgSuccess("提交成功");
          this.open = false;
          this.getList();
        });
      });
    },
    handleDelete(row) {
      const ids = row.id || this.ids.join(",");
      this.$modal.confirm("确认删除选中的生产入库单吗？").then(() => delInbound(ids)).then(() => {
        this.$modal.msgSuccess("删除成功");
        this.getList();
      }).catch(() => {});
    },
    // 拿到物料
    getSelectList(list) {
      if(list.length === 0) {
        this.form.goodsList = []
      } else if(list.length === 1) {
        this.form.goodsList.splice(this.goodsIndex, 1, {
          goodsNum: list[0].goodsNum,
          goodsName: list[0].goodsName,
          goodsSpec: list[0].goodsSpec,
          goodsType: list[0].goodsType,
          goodsTypeName: list[0].goodsTypeName,
          goodsLevel: list[0].level,
          warehouseNo: null,
          warehouseName: null,
          batchNo: null,
          baseUnit: list[0].unit,
          baseQty: 0,
          assistantUnit: list[0].assistantUnit,
          assistantQty: 0,
          amount: 0,
          remark: ""
        })
      } else if(list.length > 1) {
        this.form.goodsList.splice(this.goodsIndex, 1, {
          goodsNum: list[0].goodsNum,
          goodsName: list[0].goodsName,
          goodsSpec: list[0].goodsSpec,
          goodsType: list[0].goodsType,
          goodsTypeName: list[0].goodsTypeName,
          goodsLevel: list[0].level,
          warehouseNo: null,
          warehouseName: null,
          batchNo: null,
          baseUnit: list[0].unit,
          baseQty: 0,
          assistantUnit: list[0].assistantUnit,
          assistantQty: 0,
          amount: 0,
          remark: ""
        })
        for(let i = 1; i<list.length; i++) {
          this.form.goodsList.push({
              goodsNum: list[0].goodsNum,
              goodsName: list[0].goodsName,
              goodsSpec: list[0].goodsSpec,
              goodsType: list[0].goodsType,
              goodsTypeName: list[0].goodsTypeName,
              goodsLevel: list[0].level,
              warehouseNo: null,
              warehouseName: null,
              batchNo: null,
              baseUnit: list[0].unit,
              baseQty: 0,
              assistantUnit: list[0].assistantUnit,
              assistantQty: 0,
              amount: 0,
              remark: ""
            })
        }
      }
    },
    // 打开物料速查表
    openQueryMaterial(index) {
      this.goodsIndex = index
      this.$refs['query-material'].open()
    },
  }
};
</script>
