<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="116px">
      <el-form-item label="类型" prop="materialClassfication">
        <el-select v-model="queryParams.materialClassfication" placeholder="请选择类型" clearable>
          <el-option
            v-for="dict in dict.type.mes_material_classfication"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="物料编码" prop="materialCode">
        <el-input
          v-model="queryParams.materialCode"
          placeholder="请输入物料编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="物料名称" prop="materialName">
        <el-input
          v-model="queryParams.materialName"
          placeholder="请输入物料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料规格" prop="materialSpec">
        <el-input
          v-model="queryParams.materialSpec"
          placeholder="请输入物料规格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料大类" prop="materialCategories">
        <el-select v-model="queryParams.materialCategories" placeholder="请选择物料大类" clearable>
          <el-option
            v-for="dict in dict.type.material_categories"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="常备物料标识" prop="alwaysFlag">
        <el-select v-model="queryParams.alwaysFlag" placeholder="请选择常备物料标识" clearable>
          <el-option
            v-for="dict in dict.type.mes_material_bind_flag"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

<!--      <el-form-item label="自动出库标识" prop="autoOutstockFlag">-->
<!--        <el-select v-model="queryParams.autoOutstockFlag" placeholder="请选择自动出库标识" clearable>-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.mes_material_auto_outstock_flag"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="bom内物料标识" prop="accessoriesFlag">-->
<!--        <el-select v-model="queryParams.accessoriesFlag" placeholder="请选择bom内物料标识" clearable>-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.mes_material_accessories_flag"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="易耗品标识" prop="lowValueConsumableFlag">-->
<!--        <el-select v-model="queryParams.lowValueConsumableFlag" placeholder="请选择易耗品标识" clearable>-->
<!--          <el-option-->
<!--            v-for="dict in dict.type.mes_material_low_value_consumable_flag"-->
<!--            :key="dict.value"-->
<!--            :label="dict.label"-->
<!--            :value="dict.value"-->
<!--          />-->
<!--        </el-select>-->
<!--      </el-form-item>-->
      <el-form-item label="批次标识" prop="batchFlag">
        <el-select v-model="queryParams.batchFlag" placeholder="请选择批次标识" clearable>
          <el-option
            v-for="dict in dict.type.active_flag"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="activeFlag">
        <el-select v-model="queryParams.activeFlag" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.mes_material_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="低于安全库存" prop="isExceeded">
        <el-select v-model="queryParams.isExceeded" placeholder="请选择是否低于安全库存" clearable>
          <el-option
            v-for="dict in dict.type.active_flag"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </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="['mes:materialinfo:add']"-->
      <!--        >新增</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['mes:materialinfo:edit']"
        >修改
        </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="['mes:materialinfo:remove']"-->
      <!--        >删除</el-button>-->
      <!--      </el-col>-->
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['mes:materialinfo:export']"-->
<!--        >导出-->
<!--        </el-button>-->
<!--      </el-col>-->

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="syncMaterialInfos"
          v-hasPermi="['mes:materialinfo:sync']"
        >同步
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-check"
          size="mini"
          :disabled="multiple"
          @click="handleBatchAdd"
          v-hasPermi="['mes:materialinfo:batchEdit']"
        >批量保存
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="materialinfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="类型" align="center" prop="materialClassfication">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mes_material_classfication" :value="scope.row.materialClassfication"/>
        </template>
      </el-table-column>
      <el-table-column label="物料编码" align="center" prop="materialCode"/>
      <el-table-column label="物料名称" align="center" prop="materialName"/>
      <el-table-column label="物料规格" align="center" prop="materialSpec"/>
      <el-table-column label="物料大类" align="center" prop="materialCategories">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.material_categories" :value="scope.row.materialCategories"/>
        </template>
      </el-table-column>
      <!--      <el-table-column label="物料类型" align="center" prop="materialTypeId"/>-->
      <el-table-column label="物料类型" align="center" prop="materialTypeName"/>
      <el-table-column label="单位" align="center" prop="unitName"/>
      <el-table-column label="常备物料" align="center" prop="alwaysFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mes_material_bind_flag" :value="scope.row.alwaysFlag"/>
        </template>
      </el-table-column>
<!--      <el-table-column label="自动出库标识" align="center" prop="autoOutstockFlag">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.mes_material_auto_outstock_flag" :value="scope.row.autoOutstockFlag"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="bom内物料标识" align="center" prop="accessoriesFlag">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.mes_material_accessories_flag" :value="scope.row.accessoriesFlag"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="易耗品标识" align="center" prop="lowValueConsumableFlag">-->
<!--        <template slot-scope="scope">-->
<!--          <dict-tag :options="dict.type.mes_material_low_value_consumable_flag" :value="scope.row.lowValueConsumableFlag"/>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="批次标识" align="center" prop="batchFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mes_material_batch_flag" :value="scope.row.batchFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="批次数量" align="center" prop="batchAmount"/>
<!--      <el-table-column label="安全库存数量" align="center" prop="safeStockAmount" width="120" v-if="checkPermi(['mes:materialinfo:batchEdit'])">
        <template slot-scope="scope">
          <el-input-number
            v-model="scope.row.safeStockAmount"
            :min="0"
            :max="999999999"
            size="mini"
            style="width: 110px"
            @change="handleSafeStockChange(scope.row)"
          />
        </template>
      </el-table-column>-->
      <el-table-column label="安全库存数量" align="center" prop="safeStockAmount" width="120" />
      <el-table-column label="库存数量" align="center" prop="availableAmount" >
        <template slot-scope="scope">
          <span :style="setCellClassName(scope.row)">
          {{scope.row.availableAmount}}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="activeFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.mes_material_status" :value="scope.row.activeFlag"/>
        </template>
      </el-table-column>
      <el-table-column label="旧物料编码" align="center" prop="oldMaterialCode"/>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mes:materialinfo:edit']"
            v-if="scope.row.materialClassfication === MATERIAL_CLASSFICATION.ERP"
          >修改
          </el-button>
          <!--          <el-button-->
          <!--            size="mini"-->
          <!--            type="text"-->
          <!--            icon="el-icon-delete"-->
          <!--            @click="handleDelete(scope.row)"-->
          <!--            v-hasPermi="['mes:materialinfo: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="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="物料编码" prop="materialCode">
          <el-input v-model="form.materialCode" placeholder="请输入物料编码" :disabled="true"/>
        </el-form-item>
        <!--        <el-form-item label="旧物料编码" prop="oldMaterialCode">-->
        <!--          <el-input v-model="form.oldMaterialCode" placeholder="请输入旧物料编码" />-->
        <!--        </el-form-item>-->
        <el-form-item label="物料名称" prop="materialName">
          <el-input v-model="form.materialName" placeholder="请输入物料名称" :disabled="true"/>
        </el-form-item>
        <el-form-item label="物料规格" prop="materialSpec">
          <el-input type="textarea" v-model="form.materialSpec" placeholder="请输入物料名称" :disabled="true"/>
        </el-form-item>

        <el-row>
          <el-col :span="12">
            <el-form-item label="物料大类" prop="materialCategories">
              <el-select v-model="queryParams.materialCategories" placeholder="请选择物料大类" clearable>
                <el-option
                  v-for="dict in dict.type.material_categories"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>

            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="物料类型" prop="materialTypeId">
              <!--          <el-input v-model="form.materialTypeId" placeholder="请输入物料类型"/>-->
              <treeselect v-model="form.materialTypeId" :options="baseMaterialTypeOptions" :normalizer="normalizer"
                          placeholder="请选择物料类型"/>
            </el-form-item>
          </el-col>
        </el-row>



        <el-row>
          <el-col :span="12">
            <el-form-item label="常备物料" placeholder="请选择常备物料标识" prop="alwaysFlag">
              <el-radio-group v-model="form.alwaysFlag">
                <el-radio
                  v-for="dict in dict.type.active_flag"
                  :key="dict.value"
                  :label="dict.value"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
<!--          <el-col :span="12">-->
<!--            <el-form-item label="自动出库" placeholder="请选择自动出库标识" prop="autoOutstockFlag">-->
<!--              <el-radio-group v-model="form.autoOutstockFlag">-->
<!--                <el-radio-->
<!--                  v-for="dict in dict.type.mes_material_auto_outstock_flag"-->
<!--                  :key="dict.value"-->
<!--                  :label="dict.value"-->
<!--                >{{ dict.label }}-->
<!--                </el-radio>-->
<!--              </el-radio-group>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
        </el-row>



<!--        <el-row>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item placeholder="请选择bom内物料标识" label="bom内物料标识" prop="accessoriesFlag">-->
<!--              <el-radio-group v-model="form.accessoriesFlag">-->
<!--                <el-radio-->
<!--                  v-for="dict in dict.type.mes_material_accessories_flag"-->
<!--                  :key="dict.value"-->
<!--                  :label="dict.value"-->
<!--                >{{ dict.label }}-->
<!--                </el-radio>-->
<!--              </el-radio-group>-->

<!--            </el-form-item>-->
<!--          </el-col>-->
<!--          <el-col :span="12">-->
<!--            <el-form-item placeholder="请选择易耗品标识" label="易耗品标识" prop="lowValueConsumableFlag">-->
<!--              <el-radio-group v-model="form.lowValueConsumableFlag">-->
<!--                <el-radio-->
<!--                  v-for="dict in dict.type.mes_material_low_value_consumable_flag"-->
<!--                  :key="dict.value"-->
<!--                  :label="dict.value"-->
<!--                >{{ dict.label }}-->
<!--                </el-radio>-->
<!--              </el-radio-group>-->
<!--            </el-form-item>-->
<!--          </el-col>-->
<!--        </el-row>-->



        <el-row>
          <el-col :span="12">
            <el-form-item label="批次标识" prop="batchFlag">
              <el-radio-group v-model="form.batchFlag">
                <el-radio
                  v-for="dict in dict.type.active_flag"
                  :key="dict.value"
                  :label="dict.value"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="批次数量" prop="batchAmount" v-if="form.batchFlag==='1'">
              <el-input-number v-model="form.batchAmount" placeholder="请输入批次数量" :min="1" :max="10000000000"
                               style="width:180px"/>
            </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="12">
            <el-form-item label="安全库存数量" prop="safeStockAmount">
              <el-input-number v-model="form.safeStockAmount" placeholder="请输入安全库存数量" :min="1" :max="10000000000"
                               style="width:180px"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="状态" prop="activeFlag">
              <el-radio-group v-model="form.activeFlag" :disabled="form.activeFlag !== '1'">
                <el-radio
                  v-for="dict in dict.type.mes_material_status"
                  :key="dict.value"
                  :label="dict.value"
                >{{ dict.label }}
                </el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>






        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="form.remark" placeholder="请输入备注"/>
        </el-form-item>


      </el-form>


      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleBatchSave">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改物料信息对话框 -->
    <el-dialog :title="title" :visible.sync="batchOpen" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="安全库存数量" prop="safeStockAmount">
          <el-input-number v-model="form.safeStockAmount" placeholder="请输入安全库存数量" :min="1" :max="10000000000"
                           style="width:180px"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="handleBatchSave">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listMaterialinfo,
  getMaterialinfo,
  delMaterialinfo,
  addMaterialinfo,
  updateMaterialinfo,
  getMaterialTypes,
  batchUpdateSafeStock
} from "@/api/mes/materialinfo";
import {listBaseMaterialType} from "@//api/mes/baseMaterialType";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import {runJob} from "@/api/monitor/job";
import { checkPermi } from "@/utils/permission";

export default {
  name: "Materialinfo",
  dicts: ['active_flag', 'material_categories', 'mes_material_bind_flag', 'mes_material_batch_flag', 'mes_material_status','mes_material_low_value_consumable_flag','mes_material_classfication','mes_material_auto_outstock_flag','mes_material_accessories_flag'],
  components: {
    Treeselect
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 物料类型信息树选项
      baseMaterialTypeOptions: [],
      // 物料信息表格数据
      materialinfoList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示批量修改安全库存数量弹出层
      batchOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        erpId: null,
        materialCode: null,
        oldMaterialCode: null,
        materialName: null,
        materialCategories: null,
        materialSubclass: null,
        materialTypeId: null,
        batchFlag: null,
        materialUnitId: null,
        materialUnit: null,
        materialMatkl: null,
        materialSpec: null,
        netWeight: null,
        grossWeight: null,
        factoryId: null,
        createOrgId: null,
        useOrgId: null,
        prodlineId: null,
        activeFlag: null,
        deletedFlag: null,
        approveDate: null,
        erpModifyDate: null,
        materialClassfication:null,
        autoOutstockFlag:null,
        accessoriesFlag: null,
        lowValueConsumableFlag: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        // bindFlag: [
        //   {required: true, message: "常备物料不能为空", trigger: "change"}
        // ],
        // batchFlag: [
        //   {required: true, message: "批次标识不能为空", trigger: "change"}
        // ],
        // batchAmount: [
        //   {required: true, message: "批次数量不能为空", trigger: "blur"}
        // ],
        // materialTypeId: [
        //   {required: true, message: "物料类型不能为空", trigger: "blur"}
        // ],
      },
      currentStatus: '',
      MATERIAL_CLASSFICATION:{
        ERP:'1',
        VIRTUAL:'2'
      },
      // 跟踪已修改的安全库存数据
      modifiedSafeStockData: []
    };
  },
  created() {
    this.getList();
    this.checkEditPermission();
  },
  methods: {
    /** 查询物料信息列表 */
    getList() {
      this.loading = true;
      listMaterialinfo(this.queryParams).then(response => {
        this.materialinfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.batchOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        alwaysFlag: null,
        safeStockAmount: null,
        materialId: null,
        erpId: null,
        materialCode: null,
        oldMaterialCode: null,
        materialName: null,
        materialCategories: null,
        materialSubclass: null,
        materialTypeId: null,
        batchFlag: null,
        materialUnitId: null,
        materialUnit: null,
        materialMatkl: null,
        materialSpec: null,
        netWeight: null,
        grossWeight: null,
        factoryId: null,
        createOrgId: null,
        useOrgId: null,
        prodlineId: null,
        activeFlag: null,
        deletedFlag: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        approveDate: null,
        erpModifyDate: null,
        lowValueConsumableFlag:null,
        accessoriesFlag:null,
        autoOutstockFlag:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.materialId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加物料信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.getTreeselect();
      if (row != null) {
        // this.form.parentId = row.matrialTypeId;
      }
      const materialId = row.materialId || this.ids
      getMaterialinfo(materialId).then(response => {
        this.form = response.data;
        this.currentStatus = this.form.activeFlag;
        this.open = true;
        this.title = "修改物料信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.materialId != null) {
            let thisForm = this.form;
            if (this.form.activeFlag !== this.currentStatus) {
              this.$modal.confirm('确认要修改物料信息的状态么？').then(function () {
                return updateMaterialinfo(thisForm);
              }).then(() => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }).catch(() => {
              });
            } else {
              updateMaterialinfo(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            }


          } else {
            addMaterialinfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 转换物料类型信息数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.matrialTypeId,
        label: node.typeName,
        children: node.children
      };
    },
    /** 查询物料类型信息下拉树结构 */
    getTreeselect() {
      listBaseMaterialType().then(response => {
        this.baseMaterialTypeOptions = this.handleTree(response.data, "matrialTypeId", "parentId");
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const materialIds = row.materialId || this.ids;
      this.$modal.confirm('是否确认删除物料信息编号为"' + materialIds + '"的数据项？').then(function () {
        return delMaterialinfo(materialIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mes/materialinfo/export', {
        ...this.queryParams
      }, `materialinfo_${new Date().getTime()}.xlsx`)
    },


    syncMaterialInfos(){
      this.$modal.confirm('确认要立即同步物料信息么？').then(function() {
        return runJob(161, 'DEFAULT');
      }).then(() => {
        this.$modal.msgSuccess("执行成功");
      }).catch(() => {});
    },

    setCellClassName(row) {
      const availableAmount = parseFloat(row.availableAmount) || 0;
      const safeStockAmount = parseFloat(row.safeStockAmount) || 0;
      if (availableAmount < safeStockAmount) {
        return 'color: #f56c6c';
      }
      return '';
    },

    // handleSafeStockChange(row) {
    //   // 记录修改的安全库存数据
    //   const existingIndex = this.modifiedSafeStockData.findIndex(item => item.materialId === row.materialId);
    //   if (existingIndex > -1) {
    //     this.modifiedSafeStockData[existingIndex] = {
    //       materialId: row.materialId,
    //       safeStockAmount: row.safeStockAmount
    //     };
    //   } else {
    //     this.modifiedSafeStockData.push({
    //       materialId: row.materialId,
    //       safeStockAmount: row.safeStockAmount
    //     });
    //   }
    // },

    handleBatchAdd() {
      this.reset();
      this.batchOpen = true;
      this.title = "批量修改安全库存数量";
    },
    /** 批量保存安全库存 */
    handleBatchSave() {
      const materialIds = this.ids;
      materialIds.forEach((materialId) => {
        const materialVO = {materialId: materialId,  safeStockAmount: this.form.safeStockAmount};
        this.modifiedSafeStockData.push(materialVO);
      })
      if (this.modifiedSafeStockData.length === 0) {
        this.$modal.msgWarning("没有需要保存的修改");
        return;
      }
      this.$modal.confirm('确认要保存修改的安全库存数量吗？').then(() => {
        this.loading = true;
        batchUpdateSafeStock(this.modifiedSafeStockData).then(response => {
          this.$modal.msgSuccess("批量保存成功");
          this.modifiedSafeStockData = []; // 清空修改记录
          this.batchOpen = false;
          this.getList(); // 刷新列表
        }).catch(() => {
          this.loading = false;
        });
      }).catch(() => {});
    },


  }
};
</script>

<style scoped>
/* 只读状态的输入框样式 */
.el-input-number.is-disabled {
  background-color: #f5f7fa;
  border-color: #e4e7ed;
  color: #606266;
}

/* 只读状态的输入框内部样式 */
.el-input-number.is-disabled .el-input__inner {
  background-color: #f5f7fa;
  color: #606266;
  cursor: not-allowed;
}

/* 只读状态的增减按钮样式 */
.el-input-number.is-disabled .el-input-number__decrease,
.el-input-number.is-disabled .el-input-number__increase {
  background-color: #f5f7fa;
  color: #c0c4cc;
  cursor: not-allowed;
}
</style>
