<template>
  <div class="p-2">
<el-row :gutter="9">
  <el-col :span="4" :xs="24">
    <div class="head-container">
      <el-input
        v-model="filterBoxName"
        placeholder="请输入名称"
        clearable
        size="small"
        prefix-icon="el-icon-search"
        style="margin-bottom: 20px"
      />
    </div>
    <div class="head-container">
      <div class="tree-container">
        <el-tree
          :data="InfoOptions"
          :props="props"
          :expand-on-click-node="false"
          :filter-node-method="filterNode"
          ref="tree"
          node-key="matrialTypeId"
          default-expand-all
          highlight-current
          @node-click="handleTreeNodeClick"
        />
      </div>
    </div>
  </el-col>

  <el-col :span="20" :xs="24">
    <transition :enter-active-class="proxy?.animate.searchAnimate.enter" :leave-active-class="proxy?.animate.searchAnimate.leave">
      <div v-show="showSearch" class="mb-[10px]">
        <el-card shadow="hover">
          <el-form ref="queryFormRef" :model="queryParams" :inline="true">
<!--            <el-form-item label="主键标识" prop="materialId">
              <el-input v-model="queryParams.materialId" placeholder="请输入主键标识" clearable @keyup.enter="handleQuery" />
            </el-form-item>-->
<!--            <el-form-item label="ERP信息" prop="erpId">
              <el-input v-model="queryParams.erpId" placeholder="请输入ERP信息" clearable @keyup.enter="handleQuery" />
            </el-form-item>-->
            <el-form-item label="物料编码" prop="materialCode">
              <el-input v-model="queryParams.materialCode" placeholder="请输入物料编码" clearable @keyup.enter="handleQuery" />
            </el-form-item>
<!--            <el-form-item label="旧物料编码" prop="oldMaterialCode">
              <el-input v-model="queryParams.oldMaterialCode" placeholder="请输入旧物料编码" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="物料名称" prop="materialName">
              <el-input v-model="queryParams.materialName" placeholder="请输入物料名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="物料类型ID" prop="materialTypeId">
              <el-input v-model="queryParams.materialTypeId" placeholder="请输入物料类型ID" clearable @keyup.enter="handleQuery" />
            </el-form-item>-->
            <el-form-item label="物料大类" prop="materialCategoryId">
              <el-select v-model="queryParams.materialCategoryId" placeholder="请选择物料大类" clearable >
                <el-option v-for="dict in materialCategoryOptions"
                           :key="dict.materialCategoryId"
                           :label="dict.materialCategoryName"
                           :value="dict.materialCategoryId"/>
              </el-select>
            </el-form-item>
<!--            <el-form-item label="物料小类" prop="materialSubclass">
              <el-input v-model="queryParams.materialSubclass" placeholder="请输入物料小类" clearable @keyup.enter="handleQuery" />
            </el-form-item>-->
<!--            <el-form-item label="批次标识" prop="batchFlag">
              <el-select v-model="queryParams.batchFlag" placeholder="请选择批次标识" clearable >
                <el-option v-for="dict in mes_batch_flag" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>-->
<!--            <el-form-item label="小批次数量" prop="batchAmount">
              <el-input v-model="queryParams.batchAmount" placeholder="请输入小批次数量" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="计量单位ID" prop="materialUnitId">
              <el-input v-model="queryParams.materialUnitId" placeholder="请输入计量单位ID" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="计量单位名称" prop="materialUnit">
              <el-input v-model="queryParams.materialUnit" placeholder="请输入计量单位名称" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="物料组" prop="materialMatkl">
              <el-input v-model="queryParams.materialMatkl" placeholder="请输入物料组" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="物料规格" prop="materialSpec">
              <el-input v-model="queryParams.materialSpec" placeholder="请输入物料规格" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="净重" prop="netWeight">
              <el-input v-model="queryParams.netWeight" placeholder="请输入净重" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="毛重" prop="grossWeight">
              <el-input v-model="queryParams.grossWeight" placeholder="请输入毛重" clearable @keyup.enter="handleQuery" />
            </el-form-item>-->
<!--            <el-form-item label="绑定标识" prop="alwaysFlag">
              <el-select v-model="queryParams.alwaysFlag" placeholder="请选择绑定标识" clearable >
                <el-option v-for="dict in always_flag" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>-->
<!--            <el-form-item label="所属工厂" prop="factoryId">
              <el-input v-model="queryParams.factoryId" placeholder="请输入所属工厂" clearable @keyup.enter="handleQuery" />
            </el-form-item>-->
<!--            <el-form-item label="创建组织" prop="createOrgId">
              <el-input v-model="queryParams.createOrgId" placeholder="请输入创建组织" clearable @keyup.enter="handleQuery" />
            </el-form-item>-->
<!--            <el-form-item label="使用组织" prop="useOrgId">
              <el-input v-model="queryParams.useOrgId" placeholder="请输入使用组织" clearable @keyup.enter="handleQuery" />
            </el-form-item>-->
<!--            <el-form-item label="所属产线" prop="prodLineId">
              <el-input v-model="queryParams.prodLineId" placeholder="请输入所属产线" clearable @keyup.enter="handleQuery" />
            </el-form-item>-->
<!--            <el-form-item label="激活标识" prop="activeFlag">
              <el-select v-model="queryParams.activeFlag" placeholder="请选择激活标识" clearable >
                <el-option v-for="dict in active_flag" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>-->
<!--            <el-form-item label="删除标识" prop="deletedFlag">
              <el-select v-model="queryParams.deletedFlag" placeholder="请选择删除标识(0否 1是)" clearable >
                <el-option v-for="dict in deleted_flag" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>-->
<!--            <el-form-item label="采购计价单位" prop="purchasePriceUnitId">
              <el-input v-model="queryParams.purchasePriceUnitId" placeholder="请输入采购计价单位" clearable @keyup.enter="handleQuery" />
            </el-form-item>-->
<!--            <el-form-item label="审核日期" style="width: 308px">
              <el-date-picker
                v-model="dateRangeApproveDate"
                value-format="YYYY-MM-DD HH:mm:ss"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                :default-time="[new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 1, 1, 23, 59, 59)]"
              />
            </el-form-item>-->
<!--            <el-form-item label="erp最后更新日期" prop="erpModifyDate">
              <el-date-picker clearable
                v-model="queryParams.erpModifyDate"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择erp最后更新日期"
              />
            </el-form-item>-->
<!--            <el-form-item label="最大库存数量" prop="maxStockAmount">
              <el-input v-model="queryParams.maxStockAmount" placeholder="请输入最大库存数量" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="最小库存数量" prop="minStockAmount">
              <el-input v-model="queryParams.minStockAmount" placeholder="请输入最小库存数量" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="安全库存数量" prop="safeStockAmount">
              <el-input v-model="queryParams.safeStockAmount" placeholder="请输入安全库存数量" clearable @keyup.enter="handleQuery" />
            </el-form-item>-->
<!--            <el-form-item label="申请标识" prop="applyFlag">
              <el-select v-model="queryParams.applyFlag" placeholder="请选择申请标识(1是 0否)" clearable >
                <el-option v-for="dict in apply_flag" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
            <el-form-item label="物料分类" prop="materialClassfication">
              <el-select v-model="queryParams.materialClassfication" placeholder="请选择物料分类" clearable >
                <el-option v-for="dict in material_classfication" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
            <el-form-item label="自动出库标识(1是 0否)" prop="autoOutstockFlag">
              <el-select v-model="queryParams.autoOutstockFlag" placeholder="请选择自动出库标识(1是 0否)" clearable >
                <el-option v-for="dict in auto_outstock_flag" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
            <el-form-item label="辅料标识(1是 0否)" prop="accessoriesFlag">
              <el-select v-model="queryParams.accessoriesFlag" placeholder="请选择辅料标识(1是 0否)" clearable >
                <el-option v-for="dict in mes_accessories_flag" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
            <el-form-item label="低值易耗品标识(1是 0否)" prop="lowValueConsumableFlag">
              <el-select v-model="queryParams.lowValueConsumableFlag" placeholder="请选择低值易耗品标识(1是 0否)" clearable >
                <el-option v-for="dict in mes_low_value_consumable_flag" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>-->
<!--            <el-form-item label="品牌" prop="brand">
              <el-input v-model="queryParams.brand" placeholder="请输入品牌" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="层级" prop="plyrating">
              <el-input v-model="queryParams.plyrating" placeholder="请输入层级" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="花纹" prop="pattern">
              <el-input v-model="queryParams.pattern" placeholder="请输入花纹" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="速度级别" prop="speedLevel">
              <el-input v-model="queryParams.speedLevel" placeholder="请输入速度级别" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="负荷载重" prop="load">
              <el-input v-model="queryParams.load" placeholder="请输入负荷载重" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="轮胎标记" prop="tireMarkings">
              <el-select v-model="queryParams.tireMarkings" placeholder="请选择轮胎标记" clearable >
                <el-option v-for="dict in tire_markings" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
            <el-form-item label="最小停放时间" prop="minParkingTime">
              <el-input v-model="queryParams.minParkingTime" placeholder="请输入最小停放时间" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="最大停放时间" prop="maxParkingTime">
              <el-input v-model="queryParams.maxParkingTime" placeholder="请输入最大停放时间" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="标准重量" prop="standardWeight">
              <el-input v-model="queryParams.standardWeight" placeholder="请输入标准重量" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="标准重量上限" prop="weightUpperLimit">
              <el-input v-model="queryParams.weightUpperLimit" placeholder="请输入标准重量上限" clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="标准重量下限" prop="weightLowerLimit">
              <el-input v-model="queryParams.weightLowerLimit" placeholder="请输入标准重量下限" clearable @keyup.enter="handleQuery" />
            </el-form-item>-->
<!--            <el-form-item label="内胎标记" prop="innerTubeFlag">
              <el-select v-model="queryParams.innerTubeFlag" placeholder="请选择内胎标记" clearable >
                <el-option v-for="dict in inner_tube_flag" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>-->
            <el-form-item label="销售类型" prop="saleType">
              <el-select v-model="queryParams.saleType" placeholder="请选择销售类型" clearable >
                <el-option v-for="dict in sale_type" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
              <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </div>
    </transition>

    <el-card shadow="never">
      <template #header>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['mes:baseMaterialInfo:add']">新增</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="success" plain icon="Edit" :disabled="single" @click="handleUpdate()" v-hasPermi="['mes:baseMaterialInfo:edit']">修改</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" plain icon="Delete" :disabled="multiple" @click="handleDelete()" v-hasPermi="['mes:baseMaterialInfo:remove']">删除</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="warning" plain icon="Download" @click="handleExport" v-hasPermi="['mes:baseMaterialInfo:export']">导出</el-button>
          </el-col>
          <right-toolbar v-model:showSearch="showSearch" :columns="columns" :search="true" @queryTable="getList"></right-toolbar>
        </el-row>
      </template>

      <el-table v-loading="loading" :data="baseMaterialInfoList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="60" align="center" />
<!--        <el-table-column label="主键标识" align="center" prop="materialId" v-if="columns[0].visible"/>-->
<!--        <el-table-column label="租户编号" align="center" prop="tenantId" v-if="columns[1].visible"/>-->
        <el-table-column label="ERP信息" align="center" prop="erpId" v-if="columns[0].visible"/>
        <el-table-column label="物料编码" align="center" prop="materialCode" v-if="columns[1].visible"/>
        <el-table-column label="旧物料编码" align="center" prop="oldMaterialCode" v-if="columns[2].visible"/>
        <el-table-column label="物料名称" align="center" prop="materialName" v-if="columns[3].visible" width="220px"/>
<!--        <el-table-column label="物料类型ID" align="center" prop="materialTypeId" v-if="columns[4].visible"/>-->
        <el-table-column label="物料类型" align="center" prop="matrialTypeName" v-if="columns[4].visible"/>
        <el-table-column label="物料大类" align="center" prop="materialCategoryName" v-if="columns[5].visible"/>
        <el-table-column label="物料小类" align="center" prop="materialSubclass" v-if="columns[6].visible"/>
        <el-table-column label="批次标识" align="center" prop="batchFlag" v-if="columns[7].visible">
          <template #default="scope">
            <dict-tag :options="mes_batch_flag" :value="scope.row.batchFlag"/>
          </template>
        </el-table-column>
        <el-table-column label="小批次数量" align="center" prop="batchAmount" v-if="columns[8].visible"/>
        <el-table-column label="计量单位" align="center" prop="materialUnitId" v-if="columns[9].visible"/>
        <el-table-column label="计量单位名称" align="center" prop="materialUnit" v-if="columns[10].visible"/>
        <el-table-column label="物料组" align="center" prop="materialMatkl" v-if="columns[11].visible"/>
        <el-table-column label="物料规格" align="center" prop="materialSpec" v-if="columns[12].visible"/>
        <el-table-column label="净重" align="center" prop="netWeight" v-if="columns[13].visible"/>
        <el-table-column label="毛重" align="center" prop="grossWeight" v-if="columns[14].visible"/>
        <el-table-column label="绑定标识" align="center" prop="alwaysFlag" v-if="columns[15].visible">
          <template #default="scope">
            <dict-tag :options="always_flag" :value="scope.row.alwaysFlag"/>
          </template>
        </el-table-column>
        <el-table-column label="所属工厂" align="center" prop="factoryName" v-if="columns[16].visible"/>
        <el-table-column label="创建组织" align="center" prop="createOrgId" v-if="columns[17].visible"/>
        <el-table-column label="使用组织" align="center" prop="useOrgId" v-if="columns[18].visible"/>
        <el-table-column label="所属产线" align="center" prop="prodLineId" v-if="columns[19].visible"/>
        <el-table-column label="激活标识" align="center" prop="activeFlag" v-if="columns[20].visible">
          <template #default="scope">
            <dict-tag :options="active_flag" :value="scope.row.activeFlag"/>
          </template>
        </el-table-column>
        <el-table-column label="删除标识" align="center" prop="deletedFlag" v-if="columns[21].visible">
          <template #default="scope">
            <dict-tag :options="deleted_flag" :value="scope.row.deletedFlag"/>
          </template>
        </el-table-column>
        <el-table-column label="采购计价单位" align="center" prop="purchasePriceUnitId" v-if="columns[22].visible"/>
        <el-table-column label="审核日期" align="center" prop="approveDate" width="180" v-if="columns[23].visible">
          <template #default="scope">
            <span>{{ parseTime(scope.row.approveDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="erp最后更新日期" align="center" prop="erpModifyDate" width="180" v-if="columns[24].visible">
          <template #default="scope">
            <span>{{ parseTime(scope.row.erpModifyDate, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="最大库存数量" align="center" prop="maxStockAmount" v-if="columns[25].visible"/>
        <el-table-column label="最小库存数量" align="center" prop="minStockAmount" v-if="columns[26].visible"/>
        <el-table-column label="安全库存数量" align="center" prop="safeStockAmount" v-if="columns[27].visible"/>
        <el-table-column label="申请标识" align="center" prop="applyFlag" v-if="columns[28].visible">
          <template #default="scope">
            <dict-tag :options="apply_flag" :value="scope.row.applyFlag"/>
          </template>
        </el-table-column>
        <el-table-column label="物料分类" align="center" prop="materialClassfication" v-if="columns[29].visible">
          <template #default="scope">
            <dict-tag :options="material_classfication" :value="scope.row.materialClassfication"/>
          </template>
        </el-table-column>
        <el-table-column label="自动出库标识" align="center" prop="autoOutstockFlag" v-if="columns[30].visible">
          <template #default="scope">
            <dict-tag :options="auto_outstock_flag" :value="scope.row.autoOutstockFlag"/>
          </template>
        </el-table-column>
        <el-table-column label="辅料标识" align="center" prop="accessoriesFlag" v-if="columns[31].visible">
          <template #default="scope">
            <dict-tag :options="mes_accessories_flag" :value="scope.row.accessoriesFlag"/>
          </template>
        </el-table-column>
        <el-table-column label="低值易耗品标识" align="center" prop="lowValueConsumableFlag" v-if="columns[32].visible">
          <template #default="scope">
            <dict-tag :options="mes_low_value_consumable_flag" :value="scope.row.lowValueConsumableFlag"/>
          </template>
        </el-table-column>
        <el-table-column label="品牌" align="center" prop="brand" v-if="columns[33].visible"/>
        <el-table-column label="层级" align="center" prop="plyrating" v-if="columns[34].visible"/>
        <el-table-column label="花纹" align="center" prop="pattern" v-if="columns[35].visible"/>
        <el-table-column label="速度级别" align="center" prop="speedLevel" v-if="columns[36].visible"/>
        <el-table-column label="负荷载重" align="center" prop="load" v-if="columns[37].visible"/>
        <el-table-column label="轮胎标记" align="center" prop="tireMarkings" v-if="columns[38].visible">
          <template #default="scope">
            <dict-tag :options="tire_markings" :value="scope.row.tireMarkings"/>
          </template>
        </el-table-column>
<!--        <el-table-column label="最小停放时间" align="center" prop="minParkingTime" v-if="columns[39].visible"/>-->
        <el-table-column label="最小停放时间" align="center" prop="minParkingTime" v-if="columns[39].visible">
          <template #default="scope">
            <span>{{ formatDayHourMinutes(scope.row.minParkingTime) }}</span>
          </template>
        </el-table-column>
<!--        <el-table-column label="最大停放时间" align="center" prop="maxParkingTime" v-if="columns[40].visible"/>-->
        <el-table-column label="最大停放时间" align="center" prop="maxParkingTime" v-if="columns[40].visible">
          <template #default="scope">
            <span>{{ formatDayHourMinutes(scope.row.maxParkingTime) }}</span>
          </template>
        </el-table-column>

        <el-table-column label="标准重量" align="center" prop="standardWeight" v-if="columns[41].visible"/>
        <el-table-column label="标准重量上限" align="center" prop="weightUpperLimit" v-if="columns[42].visible"/>
        <el-table-column label="标准重量下限" align="center" prop="weightLowerLimit" v-if="columns[43].visible"/>
        <el-table-column label="内胎标记" align="center" prop="innerTubeFlag" v-if="columns[44].visible">
          <template #default="scope">
            <dict-tag :options="inner_tube_flag" :value="scope.row.innerTubeFlag"/>
          </template>
        </el-table-column>
        <el-table-column label="销售类型" align="center" prop="saleType" v-if="columns[45].visible">
          <template #default="scope">
            <dict-tag :options="sale_type" :value="scope.row.saleType"/>
          </template>
        </el-table-column>
        <el-table-column label="备注" align="center" prop="remark" v-if="columns[46].visible"/>
<!--        <el-table-column label="创建部门" align="center" prop="createDept" v-if="columns[49].visible"/>
        <el-table-column label="创建人" align="center" prop="createBy" v-if="columns[50].visible"/>
        <el-table-column label="创建时间" align="center" prop="createTime" width="180" v-if="columns[51].visible">
          <template #default="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="更新人" align="center" prop="updateBy" v-if="columns[52].visible"/>
        <el-table-column label="更新时间" align="center" prop="updateTime" width="180" v-if="columns[53].visible">
          <template #default="scope">
            <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
          </template>
        </el-table-column>-->
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-tooltip content="修改" placement="top">
              <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['mes:baseMaterialInfo:edit']"></el-button>
            </el-tooltip>
            <el-tooltip content="删除" placement="top">
              <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['mes:baseMaterialInfo:remove']"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>



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

  </el-col>

</el-row>

    <!-- 添加或修改物料信息对话框 -->
    <el-dialog :title="dialog.title" v-model="dialog.visible" width="1200px" append-to-body>
      <el-form ref="baseMaterialInfoFormRef" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="物料编码" prop="materialCode">
              <el-input v-model="form.materialCode" placeholder="请输入物料编码" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="旧物料编码" prop="oldMaterialCode">
              <el-input v-model="form.oldMaterialCode" placeholder="请输入旧物料编码" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="物料名称" prop="materialName">
              <el-input v-model="form.materialName" placeholder="请输入物料名称" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="物料类型" prop="materialTypeId">
              <el-select v-model="form.materialTypeId" placeholder="请选择物料类型" @change="materialCategoryIdBymaterialTypeId">
                <el-option
                    v-for="item in materialTypeList"
                    :key="item.matrialTypeId"
                    :label="item.matrialTypeName"
                    :value="item.matrialTypeId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="物料小类" prop="materialSubclass">
              <el-input v-model="form.materialSubclass" placeholder="请输入物料小类" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="批次标识" prop="batchFlag">
              <el-radio-group v-model="form.batchFlag">
                <el-radio
                  v-for="dict in mes_batch_flag"
                  :key="dict.value"
                  :value="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="小批次数量" prop="batchAmount">
<!--              <el-input v-model="form.batchAmount" placeholder="请输入小批次数量" />-->
              <el-input-number v-model="form.batchAmount" :min="0"  />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="计量单位" prop="materialUnitId">
              <el-tree-select
                v-model="form.materialUnitId"
                :data="MeasureUnitList"
                :props="{ value: 'unitId', label: 'unitName', children: 'children' }"
                value-key="unitId"
                placeholder="请选择计量单位"
                check-strictly
                @node-click="handleNodeClick"
              />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="物料组" prop="materialMatkl">
              <el-input v-model="form.materialMatkl" placeholder="请输入物料组" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="物料规格" prop="materialSpec">
              <el-input v-model="form.materialSpec" placeholder="请输入物料规格" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="净重" prop="netWeight">
              <el-input-number v-model="form.netWeight" :min="0"  />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="毛重" prop="grossWeight">
              <el-input-number v-model="form.grossWeight" :min="0"  />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="绑定标识" prop="alwaysFlag">
              <el-radio-group v-model="form.alwaysFlag">
                <el-radio
                  v-for="dict in always_flag"
                  :key="dict.value"
                  :value="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="所属工厂" prop="factoryId">
              <el-select v-model="form.factoryId" placeholder="请选择所属工厂">
                <el-option
                    v-for="item in factoryList"
                    :key="item.factoryId"
                    :label="item.factoryName"
                    :value="item.factoryId"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="创建组织" prop="createOrgId">
              <el-input v-model="form.createOrgId" placeholder="请输入创建组织" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="使用组织" prop="useOrgId">
              <el-input v-model="form.useOrgId" placeholder="请输入使用组织" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="所属产线" prop="prodLineId">
              <el-input v-model="form.prodLineId" placeholder="请输入所属产线" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="激活标识" prop="activeFlag">
              <el-radio-group v-model="form.activeFlag">
                <el-radio
                  v-for="dict in active_flag"
                  :key="dict.value"
                  :value="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="删除标识" prop="deletedFlag">
              <el-radio-group v-model="form.deletedFlag">
                <el-radio
                  v-for="dict in deleted_flag"
                  :key="dict.value"
                  :value="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="采购计价单位" prop="purchasePriceUnitId">
              <el-input v-model="form.purchasePriceUnitId" placeholder="请输入采购计价单位" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="审核日期" prop="approveDate">
              <el-date-picker clearable
                v-model="form.approveDate"
                type="datetime"
                value-format="YYYY-MM-DD HH:mm:ss"
                placeholder="请选择审核日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="最大库存数量" prop="maxStockAmount">
              <el-input-number v-model="form.maxStockAmount" :min="0" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="最小库存数量" prop="minStockAmount">
              <el-input-number v-model="form.minStockAmount" :min="0"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="安全库存数量" prop="safeStockAmount">
              <el-input-number v-model="form.safeStockAmount" :mi="0" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="申请标识" prop="applyFlag">
              <el-radio-group v-model="form.applyFlag">
                <el-radio
                  v-for="dict in apply_flag"
                  :key="dict.value"
                  :value="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="物料分类" prop="materialClassfication">
              <el-select v-model="form.materialClassfication" placeholder="请选择物料分类">
                <el-option
                    v-for="dict in material_classfication"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="自动出库标识" prop="autoOutstockFlag">
              <el-radio-group v-model="form.autoOutstockFlag">
                <el-radio
                  v-for="dict in auto_outstock_flag"
                  :key="dict.value"
                  :value="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="辅料标识" prop="accessoriesFlag">
              <el-radio-group v-model="form.accessoriesFlag">
                <el-radio
                  v-for="dict in mes_accessories_flag"
                  :key="dict.value"
                  :value="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="低值易耗品标识" prop="lowValueConsumableFlag" label-width="160px">
              <el-radio-group v-model="form.lowValueConsumableFlag">
                <el-radio
                  v-for="dict in mes_low_value_consumable_flag"
                  :key="dict.value"
                  :value="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="品牌" prop="brand">
              <el-select v-model="form.brand" placeholder="请选择品牌">
                <el-option v-for="item in brandList"
                           :key="item.masterDataDetailId"
                           :label="item.dataDetailName"
                           :value="item.masterDataDetailId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="层级" prop="plyrating">
              <el-select v-model="form.plyrating" placeholder="请选择层级">
                <el-option v-for="item in plyratingList"
                           :key="item.masterDataDetailId"
                           :label="item.dataDetailName"
                           :value="item.masterDataDetailId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="花纹" prop="pattern">
              <el-select v-model="form.pattern" placeholder="请选择花纹">
                <el-option v-for="item in patternList"
                           :key="item.masterDataDetailId"
                           :label="item.dataDetailName"
                           :value="item.masterDataDetailId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="速度级别" prop="speedLevel">
              <el-select v-model="form.speedLevel" placeholder="请选择速度级别">
                <el-option v-for="item in speedLevelList" :key="item.masterDataDetailId" :label="item.dataDetailName" :value="item.masterDataDetailId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="负荷载重" prop="load">
              <el-select v-model="form.load" placeholder="请选择负荷载重">
                <el-option v-for="item in loadList" :key="item.masterDataDetailId" :label="item.dataDetailName" :value="item.masterDataDetailId"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="轮胎标记" prop="tireMarkings">
              <el-select v-model="form.tireMarkings" placeholder="请选择轮胎标记">
                <el-option
                    v-for="dict in tire_markings"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="最小停放时间" prop="minParkingTime">
              <el-input-number
                v-model="form.minParkingDays"
                placeholder="请输入天数"
                :min="0"
                :max="10000"
                :controls="false"
                :style="{ width: '50px' }"
              />
              <span>&nbsp;天&nbsp;</span>
              <el-input-number v-model="form.minParkingHours" placeholder="请输入小时" :min="0" :max="23" :controls="false" :style="{ width: '50px' }" />
              <span>&nbsp;小时&nbsp;</span>
              <el-input-number
                v-model="form.minParkingMinutes"
                placeholder="请输入分钟"
                :min="0"
                :max="59"
                :controls="false"
                :style="{ width: '50px' }"
              />
              <span>&nbsp;分钟&nbsp;</span>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="最大停放时间" prop="maxParkingTime">
              <el-input-number
                v-model="form.maxParkingDays"
                placeholder="请输入天数"
                :min="0"
                :max="10000"
                :controls="false"
                :style="{ width: '50px' }"
              />
              <span>&nbsp;天&nbsp;</span>
              <el-input-number v-model="form.maxParkingHours" placeholder="请输入小时" :min="0" :max="23" :controls="false" :style="{ width: '50px' }" />
              <span>&nbsp;小时&nbsp;</span>
              <el-input-number
                v-model="form.maxParkingMinutes"
                placeholder="请输入分钟"
                :min="0"
                :max="59"
                :controls="false"
                :style="{ width: '50px' }"
              />
              <span>&nbsp;分钟&nbsp;</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="标准重量" prop="standardWeight">
<!--              <el-input v-model="form.standardWeight" placeholder="请输入标准重量" />-->
              <el-input-number v-model="form.standardWeight" :min="0" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="标准重量上限" prop="weightUpperLimit">
              <el-input-number v-model="form.weightUpperLimit" :min="0" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="标准重量下限" prop="weightLowerLimit">
              <el-input-number v-model="form.weightLowerLimit" :min="0" />
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="内胎标记" prop="innerTubeFlag">
              <el-radio-group v-model="form.innerTubeFlag">
                <el-radio
                  v-for="dict in inner_tube_flag"
                  :key="dict.value"
                  :value="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="销售类型" prop="saleType">
              <el-select v-model="form.saleType" placeholder="请选择销售类型">
                <el-option
                    v-for="dict in sale_type"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="BaseMaterialInfo" lang="ts">
import { listBaseMaterialInfo, getBaseMaterialInfo, delBaseMaterialInfo, addBaseMaterialInfo, updateBaseMaterialInfo } from '@/api/mes/baseMaterialInfo';
import { BaseMaterialInfoVO, BaseMaterialInfoQuery, BaseMaterialInfoForm } from '@/api/mes/baseMaterialInfo/types';
import { getFactoryList } from "@/api/mes/baseFactoryInfo";
import {getBaseMeasurementUnitInfoList} from "@/api/mes/baseMeasurementUnitInfo";
import {BaseMeasurementUnitInfoVO} from "@/api/mes/baseMeasurementUnitInfo/types";
import { getBaseMaterialTypeList } from "@/api/mes/baseMaterialType";
import {getSysMasterDataDetailList} from "@/api/system/masterDataDetail";
import {getBaseMaterialCategoryList} from "@/api/mes/baseMaterialCategory";

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { apply_flag, inner_tube_flag,
   always_flag, mes_accessories_flag,
  auto_outstock_flag, mes_batch_flag, active_flag, sale_type, deleted_flag, tire_markings,
  material_classfication, mes_low_value_consumable_flag } = toRefs<any>(proxy?.useDict('apply_flag', 'inner_tube_flag',   'always_flag', 'mes_accessories_flag', 'auto_outstock_flag', 'mes_batch_flag', 'active_flag', 'sale_type', 'deleted_flag', 'tire_markings', 'material_classfication', 'mes_low_value_consumable_flag'));

const baseMaterialInfoList = ref<BaseMaterialInfoVO[]>([]);
const buttonLoading = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref<Array<string | number>>([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const dateRangeApproveDate = ref<[DateModelType, DateModelType]>(['', '']);

const queryFormRef = ref<ElFormInstance>();
const baseMaterialInfoFormRef = ref<ElFormInstance>();

/*type MeasureUnitList = {
  unitId: number;
  unitName: string;
  children? : MeasureUnitList[];
}*/


const dialog = reactive<DialogOption>({
  visible: false,
  title: ''
});

// 列显隐信息
const columns = ref<FieldOption[]>([
/*        { key: 0, label: `主键标识`, visible: true },
        { key: 1, label: `租户编号`, visible: true },*/
        { key: 0, label: `ERP信息`, visible: false },
        { key: 1, label: `物料编码`, visible: true },
        { key: 2, label: `旧物料编码`, visible: true },
        { key: 3, label: `物料名称`, visible: true },
        { key: 4, label: `物料类型ID`, visible: true },
        { key: 5, label: `物料大类`, visible: true },
        { key: 6, label: `物料小类`, visible: true },
        { key: 7, label: `批次标识`, visible: true },
        { key: 8, label: `小批次数量`, visible: true },
        { key: 9, label: `计量单位ID`, visible: false },
       { key: 10, label: `计量单位名称`, visible: true },
         { key: 11, label: `物料组`, visible: true },
          { key: 12, label: `物料规格`, visible: true },
          { key: 13, label: `净重`, visible: false },
         { key: 14, label: `毛重`, visible: false },
           { key: 15, label: `绑定标识`, visible: false },
            { key: 16, label: `所属工厂`, visible: false },
            { key: 17, label: `创建组织`, visible: false },
            { key: 18, label: `使用组织`, visible: false },
            { key: 19, label: `所属产线`, visible: false },
            { key: 20, label: `激活标识`, visible: false },
            { key: 21, label: `删除标识`, visible: false },
            { key: 22, label: `采购计价单位`, visible: false },
            { key: 23, label: `审核日期`, visible: false },
            { key: 24, label: `erp最后更新日期`, visible: false },
            { key: 25, label: `最大库存数量`, visible: false },
            { key: 26, label: `最小库存数量`, visible: false },
            { key: 27, label: `安全库存数量`, visible: false },
            { key: 28, label: `申请标识`, visible: false },
            { key: 29, label: `物料分类`, visible: false },
            { key: 30, label: `自动出库标识`, visible: false },
            { key: 31, label: `辅料标识`, visible: false },
            { key: 32, label: `低值易耗品标识`, visible: false },
            { key: 33, label: `品牌`, visible: false },
            { key: 34, label: `层级`, visible: false },
            { key: 35, label: `花纹`, visible: false },
            { key: 36, label: `速度级别`, visible: false },
            { key: 37, label: `负荷载重`, visible: false },
            { key: 38, label: `轮胎标记`, visible: false },
            { key: 39, label: `最小停放时间`, visible: false },
            { key: 40, label: `最大停放时间`, visible: false },
            { key: 41, label: `标准重量`, visible: false },
            { key: 42, label: `标准重量上限`, visible: false },
            { key: 43, label: `标准重量下限`, visible: false },
            { key: 44, label: `内胎标记`, visible: false },
            { key: 45, label: `销售类型`, visible: false },
            { key: 46, label: `备注`, visible: false },
/*        { key: 49, label: `创建部门`, visible: true },
        { key: 50, label: `创建人`, visible: true },
        { key: 51, label: `创建时间`, visible: true },
        { key: 52, label: `更新人`, visible: true },
        { key: 53, label: `更新时间`, visible: true },*/
]);

const initFormData: BaseMaterialInfoForm = {
  materialId: undefined,
  erpId: undefined,
  materialCode: undefined,
  oldMaterialCode: undefined,
  materialName: undefined,
  materialTypeId: undefined,

  materialCategoryId: undefined,// 物料大类id
  materialCategoryName: undefined,// 物料大类名称

  materialSubclass: undefined,
  batchFlag: undefined,
  batchAmount: undefined,
  materialUnitId: undefined,
  materialUnit: undefined,
  materialMatkl: undefined,
  materialSpec: undefined,
  netWeight: undefined,
  grossWeight: undefined,
  alwaysFlag: undefined,
  factoryId: undefined,
  createOrgId: undefined,
  useOrgId: undefined,
  prodLineId: undefined,
  activeFlag: '1',
  deletedFlag: undefined,
  purchasePriceUnitId: undefined,
  approveDate: undefined,
  erpModifyDate: undefined,
  maxStockAmount: undefined,
  minStockAmount: undefined,
  safeStockAmount: undefined,
  applyFlag: undefined,
  materialClassfication: undefined,
  autoOutstockFlag: undefined,
  accessoriesFlag: undefined,
  lowValueConsumableFlag: undefined,
  brand: undefined,
  plyrating: undefined,
  pattern: undefined,
  speedLevel: undefined,
  load: undefined,
  tireMarkings: undefined,
  minParkingTime: undefined,
  maxParkingTime: undefined,
  standardWeight: undefined,
  weightUpperLimit: undefined,
  weightLowerLimit: undefined,
  innerTubeFlag: undefined,
  saleType: undefined,
  remark: undefined,
  minParkingDays:0,
  minParkingHours:0,
  minParkingMinutes:0,
  maxParkingDays:0,
  maxParkingHours:0,
  maxParkingMinutes:0,

}
const data = reactive<PageData<BaseMaterialInfoForm, BaseMaterialInfoQuery>>({
  form: {...initFormData},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    materialId: undefined,
    erpId: undefined,
    materialCode: undefined,
    oldMaterialCode: undefined,
    materialName: undefined,
    materialTypeId: undefined,

    materialCategoryId: undefined,
    materialCategoryName: undefined,

    materialSubclass: undefined,
    batchFlag: undefined,
    batchAmount: undefined,
    materialUnitId: undefined,
    materialUnit: undefined,
    materialMatkl: undefined,
    materialSpec: undefined,
    netWeight: undefined,
    grossWeight: undefined,
    alwaysFlag: undefined,
    factoryId: undefined,
    createOrgId: undefined,
    useOrgId: undefined,
    prodLineId: undefined,
    activeFlag: undefined,
    deletedFlag: undefined,
    purchasePriceUnitId: undefined,
    erpModifyDate: undefined,
    maxStockAmount: undefined,
    minStockAmount: undefined,
    safeStockAmount: undefined,
    applyFlag: undefined,
    materialClassfication: undefined,
    autoOutstockFlag: undefined,
    accessoriesFlag: undefined,
    lowValueConsumableFlag: undefined,
    brand: undefined,
    plyrating: undefined,
    pattern: undefined,
    speedLevel: undefined,
    load: undefined,
    tireMarkings: undefined,
    minParkingTime: undefined,
    maxParkingTime: undefined,
    standardWeight: undefined,
    weightUpperLimit: undefined,
    weightLowerLimit: undefined,
    innerTubeFlag: undefined,
    saleType: undefined,
    params: {
      approveDate: undefined,
    }
  },
  rules: {
/*    erpId: [
      { required: true, message: "ERP信息不能为空", trigger: "blur" }
    ],*/
    materialCode: [
      { required: true, message: "物料编码不能为空", trigger: "blur" }
    ],
    materialName: [
      { required: true, message: "物料名称不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 下拉框查询工厂信息列表 */
let factoryList = ref([])
const getFactorySelect = async () => {
  let res = await getFactoryList(null);
  factoryList.value = res.data;
}

/** 查询物料信息列表 */
const getList = async () => {
  loading.value = true;
  queryParams.value.params = {};
  proxy?.addDateRange(queryParams.value, dateRangeApproveDate.value, 'ApproveDate');
  const res = await listBaseMaterialInfo(queryParams.value);
  baseMaterialInfoList.value = res.rows;
  total.value = res.total;
  loading.value = false;
}

/** 取消按钮 */
const cancel = () => {
  reset();
  dialog.visible = false;
}

/** 表单重置 */
const reset = () => {
  form.value = {...initFormData};
  baseMaterialInfoFormRef.value?.resetFields();
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryParams.value.materialTypeId = undefined; // 清除物料类型ID查询条件（树状结构）
  if (tree.value) {
    tree.value.setCurrentKey(null);
  }
  dateRangeApproveDate.value = ['', ''];
  queryFormRef.value?.resetFields();
  handleQuery();
}

/** 多选框选中数据 */
const handleSelectionChange = (selection: BaseMaterialInfoVO[]) => {
  ids.value = selection.map(item => item.materialId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
const handleAdd = () => {
  reset();
  getFactorySelect();

  getbrandList();
  getplyratingList();
  getpatternList();
  getspeedLevelList();
  getloadList();


  getMeasureUnit();
  getMaterialType();
  dialog.visible = true;
  dialog.title = "添加物料信息";
}

/** 修改按钮操作 */
const handleUpdate = async (row?: BaseMaterialInfoVO) => {
  reset();
  getFactorySelect();

  getbrandList();
  getplyratingList();
  getpatternList();
  getspeedLevelList();
  getloadList();


  getMeasureUnit();
  getMaterialType();
  const _materialId = row?.materialId || ids.value[0]
  const res = await getBaseMaterialInfo(_materialId);
  Object.assign(form.value, res.data);
  convertToTime();
  dialog.visible = true;
  dialog.title = "修改物料信息";
}

/** 提交按钮 */
const submitForm = () => {
  baseMaterialInfoFormRef.value?.validate(async (valid: boolean) => {
    if (valid) {
      buttonLoading.value = true;
      convertToSeconds();
      if (form.value.materialId) {
        await updateBaseMaterialInfo(form.value).finally(() =>  buttonLoading.value = false);
      } else {
        await addBaseMaterialInfo(form.value).finally(() =>  buttonLoading.value = false);
      }
      proxy?.$modal.msgSuccess("操作成功");
      dialog.visible = false;
      await getList();
    }
  });
}

/** 删除按钮操作 */
const handleDelete = async (row?: BaseMaterialInfoVO) => {
  const _materialIds = row?.materialId || ids.value;
  await proxy?.$modal.confirm('是否确认删除物料信息编号为"' + _materialIds + '"的数据项？').finally(() => loading.value = false);
  await delBaseMaterialInfo(_materialIds);
  proxy?.$modal.msgSuccess("删除成功");
  await getList();
}

/** 导出按钮操作 */
const handleExport = () => {
  proxy?.download('mes/baseMaterialInfo/export', {
    ...queryParams.value
  }, `baseMaterialInfo_${new Date().getTime()}.xlsx`)
}

//获取计量单位下拉框
let MeasureUnitList = ref<BaseMeasurementUnitInfoVO[]>([]);
const getMeasureUnit = async() => {
  const res = await getBaseMeasurementUnitInfoList(null);
  const data = proxy?.handleTree<BaseMeasurementUnitInfoVO>(res.data, "unitId", "parentId");
  if (data){
    MeasureUnitList.value = data;
  }
}


//点击计量单位，根据id同时将unitName赋值给form.value.materialUnit
const handleNodeClick = (data: BaseMeasurementUnitInfoVO) => {
  form.value.materialUnitId = data.unitId;
  form.value.materialUnit = data.unitName;
}

//物料类型对话框
let materialTypeList = ref([]);
const getMaterialType = async () => {
  const res = await getBaseMaterialTypeList(null);
  materialTypeList.value = res.data;
}


//左侧物料类型树
let InfoOptions = ref([]);
const getInfoOptions = async () => {
  const res = await getBaseMaterialTypeList(null);
  InfoOptions.value = proxy?.handleTree(res.data, "matrialTypeId", "parentId");
}

//点击左侧物料类型树节点,(选中左侧物料类型，点击"新增"，新增页面物料类型绑定)
const handleTreeNodeClick = async (data: any) => {
  queryParams.value.materialTypeId = data.matrialTypeId;
  initFormData.materialTypeId = data.matrialTypeId;
  handleQuery();
}

/**
 * 过滤节点函数
 * 用于筛选或显示数据节点，基于提供的值和数据对象
 *
 * @param {string} value - 用户输入的过滤值如果为空，则显示所有节点
 * @param {any} data - 当前数据节点对象，包含需要过滤的属性
 * @returns {boolean} - 返回一个布尔值，决定节点是否应该显示
 */
const filterNode = (value: string, data: any) => {
  // 当没有输入值时，显示所有节点
  if (!value) return true;
  // 检查数据节点的类型名称是否包含输入值，不区分大小写
  return data.matrialTypeName.toLowerCase().includes(value.toLowerCase());
}


/**
 * 树形组件过滤功能相关逻辑
 *
 * @var filterBoxName 用于绑定过滤关键词的响应式字符串，控制树节点过滤条件
 * @var props 树形组件配置对象，包含字段映射配置：
 *   @property {string} children 子节点字段名（默认：'children'）
 *   @property {string} label 节点显示文本字段名（默认：'matrialTypeName'）
 *   @property {string} value 节点值字段名（默认：'matrialTypeId'）
 */
const filterBoxName = ref('');
const props = reactive({
  children: 'children',
  label: 'matrialTypeName',
  value: 'matrialTypeId'
});

/**
 * 树形组件实例引用，用于调用组件方法
 */
const tree = ref();

/**
 * 监听过滤关键词变化，实时执行树节点过滤
 * @param newVal 新的过滤关键词，将传递给树组件的filter方法
 */
watch(filterBoxName, (newVal) => {
  if (tree.value) {
    tree.value.filter(newVal);
  }
});


/** 天小时分钟转换为秒 */
const convertToSeconds = () => {
  const maxDaysInSeconds = form.value.maxParkingDays * 24 * 60 * 60;
  const maxHoursInSeconds = form.value.maxParkingHours * 60 * 60;
  const maxMinutesInSeconds = form.value.maxParkingMinutes * 60;
  form.value.maxParkingTime = maxDaysInSeconds + maxHoursInSeconds + maxMinutesInSeconds;

  const minDaysInSeconds = form.value.minParkingDays * 24 * 60 * 60;
  const minHoursInSeconds = form.value.minParkingHours * 60 * 60;
  const minMinutesInSeconds = form.value.minParkingMinutes * 60;
  form.value.minParkingTime = minDaysInSeconds + minHoursInSeconds + minMinutesInSeconds;

}

/** 秒转换为天小时分钟 */
const convertToTime = () => {
  // 处理最大停放时间
  const maxTotalSeconds = form.value.maxParkingTime || 0;
  const maxDays = Math.floor(maxTotalSeconds / (24 * 60 * 60));
  const maxHours = Math.floor((maxTotalSeconds % (24 * 60 * 60)) / 3600);
  const maxMinutes = Math.floor((maxTotalSeconds % 3600) / 60);
  // 更新到表单中
  form.value.maxParkingDays = maxDays;
  form.value.maxParkingHours = maxHours;
  form.value.maxParkingMinutes = maxMinutes;

  // 处理最小停放时间
  const minTotalSeconds = form.value.minParkingTime || 0;
  const minDays = Math.floor(minTotalSeconds / (24 * 60 * 60));
  const minHours = Math.floor((minTotalSeconds % (24 * 60 * 60)) / 3600);
  const minMinutes = Math.floor((minTotalSeconds % 3600) / 60);
  form.value.minParkingDays = minDays;
  form.value.minParkingHours = minHours;
  form.value.minParkingMinutes = minMinutes;
}

/*brand、plyrating、pattern、speed_level、load*/
//获取品牌列表
let brandList = ref([]);
const getbrandList = async () => {
  const res = await getSysMasterDataDetailList({masterDataId: 1});
  brandList.value = res.data;
};
//获取层级列表
let plyratingList = ref([]);
const getplyratingList = async () => {
  const res = await getSysMasterDataDetailList({masterDataId: 4});
  plyratingList.value = res.data;
};

//获取花纹列表
let patternList = ref([]);
const getpatternList = async () => {
  const res = await getSysMasterDataDetailList({masterDataId: 5});
  patternList.value = res.data;
};

//获取速度等级列表
let speedLevelList = ref([]);
const getspeedLevelList = async () => {
  const res = await getSysMasterDataDetailList({masterDataId: 7});
  speedLevelList.value = res.data;
};

//获取负荷载重列表
let loadList = ref([]);
const getloadList = async () => {
  const res = await getSysMasterDataDetailList({masterDataId: 6});
  loadList.value = res.data;
};

//获取物料大类
let materialCategoryOptions = ref([]);
const getBaseMaterialCategorySelect = async () => {
  const res = await getBaseMaterialCategoryList(null);
  materialCategoryOptions.value = res.data;
}

//根据物料类型id获取物料大类，选择物料类型时自动绑定该物料类型的物料大类;
const materialCategoryIdBymaterialTypeId = (materialTypeId: string | number) => {
  const selectmaterialTypeId = materialTypeList.value.find(item => item.matrialTypeId === materialTypeId);
  if (selectmaterialTypeId){
    form.value.materialCategoryId = selectmaterialTypeId.materialCategoryId;
  }
}

onMounted(() => {
  getBaseMaterialCategorySelect();
  getInfoOptions();
  getList();
});
</script>
