<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="108px"
    >
      <el-form-item label="产品/物料编码" prop="productCode">
        <el-input
          v-model="queryParams.productCode"
          placeholder="请输入产品/物料编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品/物料名称" prop="productDescZh">
        <el-input
          v-model="queryParams.productDescZh"
          placeholder="请输入产品/物料名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检验标准" prop="mvgr5">
        <el-input
          v-model="queryParams.mvgr5"
          placeholder="请输入检验标准码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="CPK品类" prop="cpkType">
        <el-select
            v-model="queryParams.cpkType"
            placeholder="请选择品类"
          >
            <el-option
              v-for="dict in dict.type.qms_category"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </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="['wms:product: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="['wms:product:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdateAttached"
          v-hasPermi="['wms:attached: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="['wms:product: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="['wms:product:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-download"
          :disabled="multiple"
          size="mini"
          @click="handleSyncProductSAP"
          v-hasPermi="['wms:product:edit']"
          >同步物料工艺
        </el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="productList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键ID" align="center" prop="productId" /> -->
      <el-table-column
        label="产品/物料编码"
        align="center"
        prop="productCode"
        width="200"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="handleView(scope.row)"
            v-hasPermi="['wms:equipment:query']"
            >{{ scope.row.productCode }}</el-button
          >
        </template>
      </el-table-column>
      <el-table-column
        label="产品/物料名称"
        align="left"
        prop="productDescZh"
        width="350"
      />
      <!-- <el-table-column
        label="产品型号"
        align="left"
        prop="productModel"
        width="350"
      /> -->
      <el-table-column
        label="物料组编码"
        align="center"
        prop="productGroup"
        width="150"
      />
      <el-table-column
        label="物料组名称"
        align="center"
        prop="productGroupName"
        width="200"
      />
      <el-table-column label="物料类别" align="center" prop="mtart">
        <template slot-scope="scope">
          {{ scope.row.mtart == "material_type1" ? "料罐物料" : "其他类别" }}
        </template>
      </el-table-column>
      <el-table-column
        label="报工折算率"
        align="center"
        prop="reportRate"
        width="150"
      />
      <el-table-column
        label="检验标准"
        align="center"
        prop="mvgr5"
        width="150"
      />
      <el-table-column
        label="CPK品类"
        align="center"
        prop="cpkTypeName"
        width="150"
      />
      <!-- 中文描述也就是产品名称 -->
      <!-- <el-table-column label="英文描述" align="center" prop="productDescEn" />
						<el-table-column label="产品类别" align="center" prop="productGroup">
      			  <template slot-scope="scope">
                {{ scope.row.productGroup == "material_type1" ? "产品类别1" : "产品类别2" }}
              </template>
            </el-table-column>
      <el-table-column label="产品型号" align="center" prop="productModel" />
			<el-table-column label="序列号规则代码" align="center" prop="ruleCode" />
			<el-table-column label="旧产品号" align="center" prop="oldProductCode" />
			<el-table-column label="散件产品号" align="center" prop="partsProductCode" />
			<el-table-column label="69码" align="center" prop="skuBarcode" />
			<el-table-column label="长" align="center" prop="length" />
			<el-table-column label="宽" align="center" prop="width" />
			<el-table-column label="高" align="center" prop="height" /> -->
      <!-- <el-table-column label="毛重" align="center" prop="grossWeight" />
			<el-table-column label="净重" align="center" prop="netWeight" />
			<el-table-column label="皮重" align="center" prop="tareWeight" />
			<el-table-column label="体积" align="center" prop="volume" />
			<el-table-column label="单价" align="center" prop="unitPrice" />
			<el-table-column label="工厂编码" align="center" prop="factoryCode" /> -->
      <el-table-column label="是否启用" align="center" prop="activeFlag">
        <template slot-scope="scope">
          {{ scope.row.activeFlag == 1 ? "是" : "否" }}
        </template>
      </el-table-column>
      <!-- <el-table-column label="${comment}" align="center" prop="syncFlag" />
			<el-table-column label="${comment}" align="center" prop="primaryUom" />
			<el-table-column label="订单单位" align="center" prop="bstme" />
			<el-table-column label="${comment}" align="center" prop="basicOrder" />
			<el-table-column label="${comment}" align="center" prop="convOrder" />
			<el-table-column label="发货单位" align="center" prop="ausme" />
			<el-table-column label="${comment}" align="center" prop="basicIssue" />
			<el-table-column label="${comment}" align="center" prop="convIssue" />
			<el-table-column label="是否补料" align="center" prop="appendFlag" />
			<el-table-column label="补料比例" align="center" prop="appendPercent" /> -->
      <el-table-column label="创建人" align="center" prop="createBy" />
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        width="200"
      />
      <el-table-column
        label="操作"
        align="center"
        width="150"
        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="['wms:product:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wms:product: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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="产品/物料编码" prop="productCode">
          <el-input
            v-model="form.productCode"
            placeholder="请输入产品编码"
            style="width: 320px"
          />
        </el-form-item>
        <el-form-item label="产品/物料名称" prop="productDescZh">
          <el-input
            v-model="form.productDescZh"
            placeholder="请输入产品名称"
            style="width: 320px"
          />
        </el-form-item>
        <!-- <el-form-item label="产品/物料型号" prop="productModel">
          <el-input
            v-model="form.productModel"
            placeholder="请输入产品型号"
            style="width: 320px"
          />
        </el-form-item> -->
        <el-form-item label="产品/物料组编码" prop="productGroup">
          <el-input
            v-model="form.productGroup"
            placeholder="请输入物料组编码"
            style="width: 320px"
          />
        </el-form-item>
        <el-form-item label="产品/物料组名称" prop="productGroupName">
          <el-input
            v-model="form.productGroupName"
            placeholder="请输入物料组名称"
            style="width: 320px"
          />
        </el-form-item>
        <el-form-item label="物料类别" prop="mtart">
          <el-select
            v-model="form.mtart"
            placeholder="请选择物料类别"
            style="width: 320px"
          >
            <el-option
              v-for="dict in dict.type.material_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="英文描述" prop="productDescEn">
					<el-input v-model="form.productDescEn" type="textarea" placeholder="请输入内容" />
				</el-form-item>
				<el-form-item label="序列号规则代码" prop="ruleCode">
					<el-input v-model="form.ruleCode" placeholder="请输入序列号规则代码" />
				</el-form-item>
				<el-form-item label="旧产品号" prop="oldProductCode">
					<el-input v-model="form.oldProductCode" placeholder="请输入旧产品号" />
				</el-form-item>
				<el-form-item label="散件产品号" prop="partsProductCode">
					<el-input v-model="form.partsProductCode" placeholder="请输入散件产品号" />
				</el-form-item>
				<el-form-item label="69码" prop="skuBarcode">
					<el-input v-model="form.skuBarcode" placeholder="请输入69码" />
				</el-form-item>

				<el-form-item label="毛重" prop="grossWeight">
					<el-input v-model="form.grossWeight" placeholder="请输入毛重" />
				</el-form-item>
				<el-form-item label="净重" prop="netWeight">
					<el-input v-model="form.netWeight" placeholder="请输入净重" />
				</el-form-item>
				<el-form-item label="皮重" prop="tareWeight">
					<el-input v-model="form.tareWeight" placeholder="请输入皮重" />
				</el-form-item>
				<el-form-item label="体积" prop="volume">
					<el-input v-model="form.volume" placeholder="请输入体积" />
				</el-form-item>
				<el-form-item label="单价" prop="unitPrice">
					<el-input v-model="form.unitPrice" placeholder="请输入单价" />
				</el-form-item>
				<el-form-item label="产品组" prop="productGroup">
					<el-input v-model="form.productGroup" placeholder="请输入产品组" />
				</el-form-item>
				<el-form-item label="${comment}" prop="productGroupName">
					<el-input v-model="form.productGroupName" placeholder="请输入${comment}" />
				</el-form-item>
				<el-form-item label="用户自定义1" prop="userDefined1">
					<el-input v-model="form.userDefined1" placeholder="请输入用户自定义1" />
				</el-form-item>
				<el-form-item label="用户自定义2" prop="userDefined2">
					<el-input v-model="form.userDefined2" placeholder="请输入用户自定义2" />
				</el-form-item>
				<el-form-item label="用户自定义3" prop="userDefined3">
					<el-input v-model="form.userDefined3" placeholder="请输入用户自定义3" />
				</el-form-item>
				<el-form-item label="${comment}" prop="userDefined4">
					<el-input v-model="form.userDefined4" placeholder="请输入${comment}" />
				</el-form-item>
				<el-form-item label="${comment}" prop="userDefined5">
					<el-input v-model="form.userDefined5" placeholder="请输入${comment}" />
				</el-form-item>
				<el-form-item label="${comment}" prop="userDefined6">
					<el-input v-model="form.userDefined6" placeholder="请输入${comment}" />
				</el-form-item>
				<el-form-item label="工厂编码" prop="factoryCode">
					<el-input v-model="form.factoryCode" placeholder="请输入工厂编码" />
				</el-form-item> -->
        <el-form-item label="报工折算率" prop="reportRate">
          <el-input-number
            :min="0"
            :step="1"
            v-model="form.reportRate"
            style="width: 320px"
          />
        </el-form-item>
         <el-form-item label="每批次最大数量" prop="mvgr5Nm">
          <el-input-number
            :min="0"
            :step="1"
            v-model="form.mvgr5Nm"
            style="width: 320px"
          />
        </el-form-item>

         <el-form-item label="标准模板" prop="mvgr5">
					<el-input v-model="form.mvgr5" placeholder="请输入建议标准模板" style="width: 320px" />
				</el-form-item>

        <el-form-item label="是否启用" prop="activeFlag">
          <el-select
            v-model="form.activeFlag"
            placeholder="请选择是否启用"
            @change="$forceUpdate()"
            clearable
            style="width: 320px"
          >
            <el-option
              v-for="item in options"
              :key="item.activeFlag"
              :label="item.label"
              :value="item.activeFlag"
            ></el-option>
          </el-select>
        </el-form-item>
        <!--
        <el-form-item label="箱型" prop="mtart">
          <el-select
            v-model="form.boxType"
            placeholder="请选择箱型"
            style="width: 320px"
            @change="handleBoxTypeChange"
          >
            <el-option
              v-for="box in boxTypes"
              :key="box.boxType"
              :label="box.boxType + '【长：' + (box.length ? box.length : '') + ' 宽：' + (box.width ? box.width : '') + ' 高：' + (box.height ? box.height : '') + '】'"
              :value="box.boxType"
            ></el-option>
          </el-select>
        </el-form-item>-->
        <el-row>
          <el-col :span="8">
            <el-form-item label="长" prop="length" label-width="50px">
              <el-input type="number" v-model="form.length" placeholder="请输入长"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="宽" prop="width" label-width="50px">
              <el-input type="number" v-model="form.width" placeholder="请输入宽"/>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="高" prop="height" label-width="50px">
              <el-input type="number" v-model="form.height" placeholder="请输入高"/>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-form-item label="${comment}" prop="syncFlag">
					<el-input v-model="form.syncFlag" placeholder="请输入${comment}" />
				</el-form-item>
				<el-form-item label="${comment}" prop="primaryUom">
					<el-input v-model="form.primaryUom" placeholder="请输入${comment}" />
				</el-form-item>
				<el-form-item label="${comment}" prop="delFlag">
					<el-input v-model="form.delFlag" placeholder="请输入${comment}" />
				</el-form-item>
				<el-form-item label="订单单位" prop="bstme">
					<el-input v-model="form.bstme" placeholder="请输入订单单位" />
				</el-form-item>
				<el-form-item label="${comment}" prop="basicOrder">
					<el-input v-model="form.basicOrder" placeholder="请输入${comment}" />
				</el-form-item>
				<el-form-item label="${comment}" prop="convOrder">
					<el-input v-model="form.convOrder" placeholder="请输入${comment}" />
				</el-form-item>
				<el-form-item label="发货单位" prop="ausme">
					<el-input v-model="form.ausme" placeholder="请输入发货单位" />
				</el-form-item>
				<el-form-item label="${comment}" prop="basicIssue">
					<el-input v-model="form.basicIssue" placeholder="请输入${comment}" />
				</el-form-item>
				<el-form-item label="${comment}" prop="convIssue">
					<el-input v-model="form.convIssue" placeholder="请输入${comment}" />
				</el-form-item>
				<el-form-item label="是否补料" prop="appendFlag">
					<el-input v-model="form.appendFlag" placeholder="请输入是否补料" />
				</el-form-item>
				<el-form-item label="补料比例" prop="appendPercent">
					<el-input v-model="form.appendPercent" placeholder="请输入补料比例" />
				</el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 更新附属属性 -->
    <el-dialog
      :title="title"
      :visible.sync="openAttached"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">

        <el-form-item label="品类" prop="category">
          <el-select
            v-model="form.category"
            placeholder="请选择品类"
            style="width: 320px"
          >
            <el-option
              v-for="dict in dict.type.sys_category"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>



        <el-form-item label="仓库检验周期" prop="warehouseCycle">
          <el-input-number
            v-model="form.warehouseCycle"
            style="width: 320px"
          />
        </el-form-item>
        <el-form-item label="每托盘最大箱数" prop="palletNum">
          <el-input-number
            v-model="form.palletNum"
            style="width: 320px"
          />
        </el-form-item>
        <el-form-item label="垛型" prop="packType">
          <el-select
            v-model="form.packType"
            placeholder="请选择垛型"
            style="width: 320px"
          >
            <el-option
              v-for="dict in dict.type.pack_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>


        <el-form-item label="CPK品类" prop="cpkType">
          <el-select
            v-model="form.cpkType"
            placeholder="请选择质量品类"
            style="width: 320px"
          >
            <el-option
              v-for="dict in dict.type.qms_category"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="库区" prop="waCode">
          <el-input
            v-model="form.waCode"
            placeholder="请输入库区"
            style="width: 320px"
          />
        </el-form-item>


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

    <!-- 查看物料附属信息 -->
    <el-dialog
      :title="title"
      :visible.sync="openAttachedView"
      width="1000px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-descriptions
          :column="2"
          border
          :contentStyle="CS"
          :label-style="LS"
        >
          <el-descriptions-item label="产品编码">{{
            form.productCode
          }}</el-descriptions-item>
          <el-descriptions-item label="产品名称">{{
            form.productDescZh
          }}</el-descriptions-item>
          <!-- <el-descriptions-item label="产品型号">{{
                form.productModel
              }}</el-descriptions-item> -->
          <el-descriptions-item label="物料组编码">{{
            form.productGroup
          }}</el-descriptions-item>
          <el-descriptions-item label="物料组名称">{{
            form.productGroupName
          }}</el-descriptions-item>
          <el-descriptions-item label="物料类别">{{
            form.mtart
          }}</el-descriptions-item>
          <el-descriptions-item label="报工折算率">{{
            form.reportRate
          }}</el-descriptions-item>
          <el-descriptions-item label="品类">{{
            form.category
          }}</el-descriptions-item>
          <el-descriptions-item label="每PC单圈">{{
            form.pc
          }}</el-descriptions-item>
          <el-descriptions-item label="标准效率">{{
            form.iei
          }}</el-descriptions-item>
          <el-descriptions-item label="标准用人">{{
            form.manStandar
          }}</el-descriptions-item>
          <el-descriptions-item label="喷药方式">{{
            form.sprayWay
          }}</el-descriptions-item>
          <el-descriptions-item label="白坯直径">{{
            form.blankDiameter
          }}</el-descriptions-item>
          <el-descriptions-item label="白胚物料号">{{
            form.blankNo
          }}</el-descriptions-item>
          <el-descriptions-item label="标准喷药量">{{
            form.sprayVolume
          }}</el-descriptions-item>
          <el-descriptions-item label="药液料号">{{
            form.liquidNo
          }}</el-descriptions-item>
          <el-descriptions-item label="标准内膜用量">{{
            form.endometrialDosage
          }}</el-descriptions-item>
          <el-descriptions-item label="标准外膜用量（KG/PC）">{{
            form.outerFilmDosage
          }}</el-descriptions-item>
          <el-descriptions-item label="支架">{{
            form.support
          }}</el-descriptions-item>
          <el-descriptions-item label="支架物料号">{{
            form.supportNo
          }}</el-descriptions-item>
          <el-descriptions-item label="吸塑">{{
            form.pvc
          }}</el-descriptions-item>
          <el-descriptions-item label="支架盘">{{
            form.supportPlate
          }}</el-descriptions-item>
          <el-descriptions-item label="其他">{{
            form.other
          }}</el-descriptions-item>
        </el-descriptions>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelAttachedView">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listProduct,
  getProduct,
  delProduct,
  addProduct,
  updateProduct,
  updateProductAttached,
  addProductAttached,
} from "@/api/wms/product";
import { syncProductSAP } from "@/api/technology/proroute";

export default {
  name: "Product",
  dicts: ["sys_normal_disable", "material_type","pack_type","sys_category","qms_category"],
  data() {
    return {
      LS: {
        width: "250px",
      },
      CS: {
        width: "250px",
      },
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      productCodes: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品信息表格数据
      productList: [],
      boxTypes: [], // 用于存储返回的 boxType 数据
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openAttachedView: false,
      openAttached: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        productCode: null,
        productDescZh: null,
        // productModel: null,
        productDescEn: null,
        ruleCode: null,
        oldProductCode: null,
        partsProductCode: null,
        skuBarcode: null,
        length: null,
        width: null,
        height: null,
        grossWeight: null,
        netWeight: null,
        tareWeight: null,
        volume: null,
        unitPrice: null,
        productGroup: null,
        productGroupName: null,
        userDefined1: null,
        userDefined2: null,
        userDefined3: null,
        userDefined4: null,
        userDefined5: null,
        userDefined6: null,
        factoryCode: null,
        activeFlag: null,
        syncFlag: null,
        primaryUom: null,
        bstme: null,
        basicOrder: null,
        convOrder: null,
        ausme: null,
        basicIssue: null,
        convIssue: null,
        appendFlag: null,
        appendPercent: null,
        reportRate: null,
      },
      options: [
        {
          activeFlag: "1",
          label: "是",
        },
        {
          activeFlag: "0",
          label: "否",
        },
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        productCode: [
          { required: true, message: "产品代码不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 生成表头序号
    indexMethod(index) {
      return index + 1;
    },
    /** 查询产品信息列表 */
    getList() {
      this.loading = true;
      listProduct(this.queryParams).then((response) => {
        this.productList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 查看取消按钮
    cancelAttachedView() {
      this.openAttachedView = false;
      this.reset();
    },
    // 更新附属表取消按钮
    cancelOpenAttached() {
      this.openAttached = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        iei: null,
        manStandar: null,
        productId: null,
        productCode: null,
        productDescZh: null,
        // productModel: null,
        productDescEn: null,
        ruleCode: null,
        oldProductCode: null,
        partsProductCode: null,
        skuBarcode: null,
        packType: null,
        length: null,
        width: null,
        height: null,
        grossWeight: null,
        netWeight: null,
        tareWeight: null,
        volume: null,
        unitPrice: null,
        productGroup: null,
        productGroupName: null,
        userDefined1: null,
        userDefined2: null,
        userDefined3: null,
        userDefined4: null,
        userDefined5: null,
        userDefined6: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        factoryCode: null,
        activeFlag: null,
        syncFlag: null,
        primaryUom: null,
        delFlag: null,
        bstme: null,
        basicOrder: null,
        convOrder: null,
        ausme: null,
        basicIssue: null,
        convIssue: null,
        appendFlag: null,
        appendPercent: null,
        reportRate: 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.productId);
      this.productCodes = selection.map((item) => item.productCode);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加产品信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const productId = row.productId || this.ids;
      getProduct(productId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产品信息";
      });
      // 请求 MesBox 列表数据
      let param ={
        "pageNum":1,
        "pageSize":"50"
      }
      listMesBox(param).then((response) => {
        // 将返回的 boxType 数据赋值给 boxTypes
        this.boxTypes = response.rows.map((item) => ({
          boxType: item.boxType, // 假设返回数据的字段名为 boxType
          length:item.length,
          width:item.width,
          height:item.height,

        }));
      }).catch((error) => {
        console.error("请求 MesBox 数据失败:", error); // 捕获错误并输出
      });
    },

    // 查询明细按钮操作
    handleView(row) {
      this.reset();
      const productId = row.productId || this.ids;
      getProduct(productId).then((response) => {
        this.form = response.data;
        this.openAttachedView = true;
        this.title = "查看产品信息";
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.productId != null) {
            updateProduct(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProduct(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 修改按钮操作 */
    handleUpdateAttached(row) {
      this.reset();
      const productId = row.productId || this.ids;
      getProduct(productId).then((response) => {
        this.form = response.data;
        this.form.productCode = response.data.productCode.slice(7,18);
        console.log(this.form.productCode);
        this.openAttached = true;
        if(this.form.id == null){
          this.title = "修改产品附属信息";
        }else{
          this.title = "修改产品附属信息";
        }
      });
    },

    /** 提交按钮 */
    submitAttachedForm() {
      this.$refs["form"].validate((valid) => {
        console.log(this.form);
        if (valid) {
          if (this.form.id != null) {
            console.log(this.form);
            updateProductAttached(this.form).then((response) => {
              this.$modal.msgSuccess("修改产品附属信息成功");
              this.openAttached = false;
              this.getList();
            });
          }else {
            addProductAttached(this.form).then((response) => {
              this.$modal.msgSuccess("新增产品附属信息成功");
              this.openAttached = false;
              this.getList();
            });
          }
        }
      });
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const productIds = row.productId || this.ids;
      this.$modal
        .confirm(
          '是否确认删除产品/物料信息编号为"' + productIds + '"的数据项？'
        )
        .then(function () {
          return delProduct(productIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "wms/product/export",
        {
          ...this.queryParams,
        },
        `product_${new Date().getTime()}.xlsx`
      );
    },
    /**选择产品进行工艺同步**/
    handleSyncProductSAP() {
      const productCodes = this.productCodes;
      return syncProductSAP(productCodes);
    },

    handleBoxTypeChange(selectedBoxType) {
      console.log(selectedBoxType)
      // 根据选择的 boxType 查找对应的长宽高
      const selectedBox = this.boxTypes.find(
        (box) => box.boxType === selectedBoxType
      );
      console.log(selectedBox)
      if (selectedBox) {
        console.log("222")
        this.form.length = selectedBox.length;
        this.form.width = selectedBox.width;
        this.form.height = selectedBox.height;
      } else {
        // 清空长宽高
        this.form.length = null;
        this.form.width = null;
        this.form.height = null;
      }
    },
  },
};
</script>
