<template>
  <div class="mod-config">
    <!-- 查询条件 -->
    <el-form :inline="true" label-position="top" :model="searchData" @keyup.enter.native="getDataList()">
      <el-form-item :label="'物料编码'">
        <el-input v-model="searchData.partNo" clearable style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item :label="'物料名称'">
        <el-input v-model="searchData.partDesc" clearable style="width: 120px"></el-input>
      </el-form-item>
      <el-form-item :label="' '">
        <el-button @click="getDataList()">查询</el-button>
        <el-button type="primary" @click="addModal()">新增</el-button>
        <el-button type="primary" @click="delModal()">删除</el-button>
        <download-excel
          :fields="fields()"
          :data="exportData"
          type="xls"
          :name="exportName"
          :header="exportHeader"
          :footer="exportFooter"
          :fetch="createExportData"
          :before-generate="startDownload"
          :before-finish="finishDownload"
          worksheet="导出信息"
          class="el-button el-button--primary el-button--medium">
          {{ "导出" }}
        </download-excel>
      </el-form-item>
    </el-form>

    <!-- bom列表 -->
    <el-table
      :height="height"
      :data="dataList"
      border
      @selection-change="selectionBom"
      v-loading="dataListLoading"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        :selectable="selectFlag"
        width="50">
      </el-table-column>
      <el-table-column
        v-for="(item,index) in columnList" :key="index"
        :sortable="item.columnSortable"
        :prop="item.columnProp"
        :header-align="item.headerAlign"
        :show-overflow-tooltip="item.showOverflowTooltip"
        :align="item.align"
        :fixed="item.fixed === ''?false:item.fixed"
        :min-width="item.columnWidth"
        :label="item.columnLabel">
        <template slot-scope="scope">
          <span v-if="!item.columnHidden">{{ scope.row[item.columnProp] }}</span>
          <span v-if="item.columnImage"><img :src="scope.row[item.columnProp]" style="width: 100px; height: 80px"/></span>
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="100"
        label="操作">
        <template slot-scope="scope">
          <a type="text" size="small" @click="updateModal(scope.row)">编辑</a>
        </template>
      </el-table-column>
    </el-table>

    <!-- 复选统计 -->
    <selectDiv ref="selectDiv"></selectDiv>

    <!-- 分页插件 -->
    <el-pagination style="margin-top: 0px"
                   @size-change="sizeChangeHandle"
                   @current-change="currentChangeHandle"
                   :current-page="pageIndex"
                   :page-sizes="[20, 50, 100, 200, 500]"
                   :page-size="pageSize"
                   :total="totalPage"
                   layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>

    <!-- bom新增/编辑模态框 -->
    <el-dialog :close-on-click-modal="false" v-drag :visible.sync="modalFlag" width="1000px">
      <el-form :inline="true" label-position="top" :model="modalData" :rules="rules" style="margin-left: 5px">
        <el-form-item prop="partNo" :rules="rules.partNo">
          <span v-if="!modalDisableFlag" slot="label" @click="queryPartList()"><a herf="#">物料编码</a></span>
          <span v-if="modalDisableFlag" slot="label">物料编码</span>
          <el-input v-model="modalData.partNo" :disabled="modalDisableFlag" style="width: 221px"></el-input>
        </el-form-item>
        <el-form-item :label="'物料名称'" prop="partDesc" :rules="rules.partDesc">
          <el-input v-model="modalData.partDesc" disabled style="width: 221px"></el-input>
        </el-form-item>
        <el-form-item :label="'制造类型'" prop="bomType" :rules="rules.bomType">
          <el-select v-model="modalData.bomType" :disabled="modalDisableFlag" style="width: 142px">
            <el-option label="制造" value="manufacturing"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item :label="'类型'" prop="typeFlag" :rules="rules.typeFlag">
          <el-select v-model="modalData.typeFlag" style="width: 142px">
            <el-option label="BOM" value="B"></el-option>
            <el-option label="配方" value="F"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="!modalDisableFlag">
          <el-button type="primary" @click="saveBomHeader()" style="margin-top: 23px;width: 142px">保存</el-button>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" :model="modalData" :rules="rules" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item label="开始时间" prop="effPhaseInDate" :rules="rules.effPhaseInDate">
          <el-date-picker
            style="width: 221px"
            v-model="modalData.effPhaseInDate"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择时间"
            :editable=false>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="effPhaseOutDate" :rules="rules.effPhaseOutDate">
          <el-date-picker
            style="width: 221px"
            v-model="modalData.effPhaseOutDate"
            type="datetime"
            value-format="yyyy-MM-dd HH:mm:ss"
            format="yyyy-MM-dd HH:mm:ss"
            placeholder="请选择时间"
            :editable=false>
          </el-date-picker>
        </el-form-item>
        <el-form-item :label="'BOM版本号'" prop="engChgLevel" :rules="rules.engChgLevel">
          <el-input v-model="modalData.engChgLevel" :disabled="modalDisableFlag" style="width: 142px"></el-input>
        </el-form-item>
        <el-form-item :label="'工程版本号'">
          <el-input v-model="modalData.engRevision" style="width: 142px"></el-input>
        </el-form-item>
        <el-form-item :label="'净重'">
          <el-input v-model="modalData.netWeight" type="number" style="width: 141px"></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" :model="modalData" style="margin-left: 7px;margin-top: -5px;">
        <el-form-item label="备注">
          <el-input type="textarea" v-model="modalData.noteText" :rows="3" resize='none' maxlength="300" show-word-limit style="width: 925px;height: 20px"></el-input>
        </el-form-item>
      </el-form>
      <el-tabs v-model="detailTable" style="margin-top: 65px; width: 100%; min-height: 120px" type="border-card" @tab-click="tabDetailClick" class="detail-tab">
        <!-- BOM明细信息页签 -->
        <el-tab-pane label="Product Structure" name="bom_detail">
          <el-form label-position="top" style="margin-top: -10px">
            <el-form-item>
              <el-button type="primary" @click="saveBomDetail()" style="margin-left: 7px">新增</el-button>
              <el-button type="primary" @click="deleteBomDetail()">删除</el-button>
            </el-form-item>
          </el-form>
          <el-form :inline="true" label-position="top" :model="detailData" :rules="rules" style="margin-left: 7px">
            <el-form-item :label="'替代编码'" prop="alternativeNo" :rules="rules.alternativeNo">
              <el-select v-model="detailData.alternativeNo" @change="alternativeChange()" style="width: 221px">
                <el-option
                  v-for = "(i, index) in detailDataList"
                  :key = "index"
                  :label = "i.alternativeNo"
                  :value = "i.alternativeNo">
                </el-option>
              </el-select>
            </el-form-item>
            <el-form-item :label="'替代名称'">
              <el-input v-model="detailData.alternativeDescription" disabled style="width: 221px"></el-input>
            </el-form-item>
            <el-form-item :label="'最小订单数'">
              <el-input v-model="detailData.minLotQty" type="number" style="width: 221px"></el-input>
            </el-form-item>
          </el-form>
          <el-form :inline="true" label-position="top" :model="detailData" style="margin-left: 7px">
            <el-form-item label="备注">
              <el-input type="textarea" v-model="detailData.detailNoteText" :rows="3" resize='none' maxlength="300" show-word-limit style="width: 925px;height: 20px"></el-input>
            </el-form-item>
          </el-form>
        </el-tab-pane>
      </el-tabs>
      <el-tabs v-model="subDetailTable" style="width: 100%; min-height: 330px" type="border-card" @tab-click="tabSubDetailClick" class="sub_detail-tab">
        <!-- BOM子明细信息页签 -->
        <el-tab-pane label="Components" name="bom_sub_detail">
          <el-form label-position="top" style="margin-top: 5px">
            <el-form-item>
              <el-button type="primary" @click="saveComponentModal()" style="margin-left: 7px">新增</el-button>
              <el-button type="primary" @click="deleteComponentPart()">删除</el-button>
            </el-form-item>
          </el-form>
          <el-table
            :data="subDetailList"
            height="256px"
            border
            ref="componentTable"
            v-loading="dataListLoading"
            @row-click="componentClickRow"
            :row-class-name="rowClassName"
            @selection-change="componentSelectionChange"
            style="width:100%">
            <el-table-column type="selection" align="center" width="50"></el-table-column>
            <el-table-column
              v-for="(item,index) in columnSubDetailList" :key="index"
              :sortable="item.columnSortable"
              :prop="item.columnProp"
              :header-align="item.headerAlign"
              :show-overflow-tooltip="item.showOverflowTooltip"
              :align="item.align"
              :fixed="item.fixed == ''?false:item.fixed"
              :min-width="item.columnWidth"
              :label="item.columnLabel">
              <template slot-scope="scope">
                <span v-if="!item.columnHidden">{{scope.row[item.columnProp]}}</span>
                <span v-if="item.columnImage"><img :src="scope.row[item.columnProp]" style="width: 100px; height: 80px"/></span>
              </template>
            </el-table-column>
            <el-table-column
              fixed="right"
              header-align="center"
              align="center"
              width="100"
              label="操作">
              <template slot-scope="scope">
                <a type="text" size="small" @click="updateComponentModal(scope.row)">编辑</a>
              </template>
            </el-table-column>
          </el-table>
        </el-tab-pane>
      </el-tabs>
      <el-footer style="height:30px;margin-top:20px;text-align:center">
        <el-button type="primary" @click="saveData()">保存</el-button>
        <el-button type="primary" @click="closeModal()">关闭</el-button>
      </el-footer>
    </el-dialog>

    <!-- 明细新增模态框 -->
    <el-dialog title="新增替代" :close-on-click-modal="false" v-drag :visible.sync="saveDetailModalFlag" width="650px">
      <el-form :inline="true" label-position="top" :model="saveDetailData" :rules="detailRules" style="margin-left: 5px">
        <el-form-item :label="'替代编码'" prop="alternativeNo" :rules="detailRules.alternativeNo">
          <el-input v-model="saveDetailData.alternativeNo" style="width: 221px"></el-input>
        </el-form-item>
        <el-form-item :label="'替代名称'" prop="alternativeDescription" :rules="detailRules.alternativeDescription">
          <el-input v-model="saveDetailData.alternativeDescription" style="width: 221px"></el-input>
        </el-form-item>
        <el-form-item :label="'最小订单数'" prop="minLotQty" :rules="detailRules.minLotQty">
          <el-input v-model="saveDetailData.minLotQty" type="number" style="width: 120px"></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" :model="saveDetailData" style="margin-left: 5px">
        <el-form-item label="备注">
          <el-input type="textarea" v-model="saveDetailData.detailNoteText" :rows="3" resize='none' maxlength="300" show-word-limit style="width: 600px;height: 20px"></el-input>
        </el-form-item>
      </el-form>
      <el-footer style="height:35px;margin-top:65px;text-align:center">
        <el-button type="primary" @click="detailDataSave()">保存</el-button>
        <el-button type="primary" @click="saveDetailModalFlag = false">关闭</el-button>
      </el-footer>
    </el-dialog>

    <!-- 物料模态框 -->
    <el-dialog title="物料清单" :close-on-click-modal="false" v-drag :visible.sync="partModelFlag" width="520px">
      <div class="rq">
        <el-form :inline="true" label-position="top" :model="partData">
          <el-form-item :label="'物料编码'">
            <el-input v-model="partData.partNo" clearable style="width: 120px"></el-input>
          </el-form-item>
          <el-form-item :label="'物料名称'">
            <el-input v-model="partData.partDesc" clearable style="width: 120px"></el-input>
          </el-form-item>
          <el-form-item :label="' '">
            <el-button type="primary" @click="queryPartList()">查询</el-button>
          </el-form-item>
        </el-form>
        <el-table
          :height="300"
          :data="partList"
          @row-dblclick="getRowData"
          border
          v-loading="dataListLoading"
          style="width: 100%;">
          <el-table-column
            v-for="(item,index) in partColumnList" :key="index"
            :sortable="item.columnSortable"
            :prop="item.columnProp"
            :header-align="item.headerAlign"
            :show-overflow-tooltip="item.showOverflowTooltip"
            :align="item.align"
            :fixed="item.fixed==''?false:item.fixed"
            :min-width="item.columnWidth"
            :label="item.columnLabel">
            <template slot-scope="scope">
              <span v-if="!item.columnHidden">{{scope.row[item.columnProp]}}</span>
              <span v-if="item.columnImage"><img :src="scope.row[item.columnProp]" style="width: 100px; height: 80px"/></span>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <el-footer style="height:40px;margin-top: 20px;text-align:center">
        <el-button type="primary" @click="partModelFlag=false">关闭</el-button>
      </el-footer>
    </el-dialog>

    <!-- 子明细物料模态框 -->
    <el-dialog title="物料清单" :close-on-click-modal="false" v-drag :visible.sync="componentPartModelFlag" width="520px">
      <el-form :inline="true" label-position="top" :model="componentPartData">
        <el-form-item :label="'物料编码'">
          <el-input v-model="componentPartData.partNo" clearable style="width: 120px"></el-input>
        </el-form-item>
        <el-form-item :label="'物料名称'">
          <el-input v-model="componentPartData.partDesc" clearable style="width: 120px"></el-input>
        </el-form-item>
        <el-form-item :label="' '">
          <el-button type="primary" @click="queryComponentPartList()">查询</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :height="300"
        :data="componentPartList"
        @row-dblclick="getComponentRowData"
        border
        v-loading="dataListLoading"
        style="width: 100%;">
        <el-table-column
          v-for="(item,index) in componentPartColumnList" :key="index"
          :sortable="item.columnSortable"
          :prop="item.columnProp"
          :header-align="item.headerAlign"
          :show-overflow-tooltip="item.showOverflowTooltip"
          :align="item.align"
          :fixed="item.fixed==''?false:item.fixed"
          :min-width="item.columnWidth"
          :label="item.columnLabel">
          <template slot-scope="scope">
            <span v-if="!item.columnHidden">{{scope.row[item.columnProp]}}</span>
            <span v-if="item.columnImage"><img :src="scope.row[item.columnProp]" style="width: 100px; height: 80px"/></span>
          </template>
        </el-table-column>
      </el-table>
    </el-dialog>

    <!-- 子明细新增模态框 -->
    <el-dialog title="新增子物料" :close-on-click-modal="false" v-drag :visible.sync="componentSaveModal" width="500px">
      <el-form :inline="true" label-position="top" :model="componentData" :rules="componentRules" style="margin-left: 5px">
        <el-form-item prop="componentPart" :rules="componentRules.componentPart">
          <span v-if="!componentDisableFlag" slot="label" @click="queryComponentPartList()"><a herf="#">子物料编码</a></span>
          <span v-if="componentDisableFlag" slot="label">子物料编码</span>
          <el-input v-model="componentData.componentPart" :disabled="componentDisableFlag" style="width: 221px"></el-input>
        </el-form-item>
        <el-form-item :label="'子物料名称'" prop="componentPartDesc" :rules="componentRules.componentPartDesc">
          <el-input v-model="componentData.componentPartDesc" disabled style="width: 221px"></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" :model="componentData" :rules="componentRules" style="margin-left: 5px">
        <el-form-item :label="'单位'">
          <el-input v-model="componentData.printUnitName" disabled style="width: 221px"></el-input>
        </el-form-item>
        <el-form-item :label="'生产属性'" prop="issueType" :rules="componentRules.issueType">
          <el-select v-model="componentData.issueType" clearable style="width: 221px">
            <el-option label="领退料倒冲" value="A"></el-option>
            <el-option label="工单倒冲" value="B"></el-option>
            <el-option label="生产订单倒冲" value="C"></el-option>
            <el-option label="投料倒冲" value="D"></el-option>
            <el-option label="批次倒冲" value="E"></el-option>
            <el-option label="销售订单倒冲" value="F"></el-option>
            <el-option label="项目倒冲" value="G"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" :model="componentData" style="margin-left: 5px">
        <el-form-item>
          <span slot="label" @click="getBaseList(103)"><a herf="#">工序</a></span>
          <el-input v-model="componentData.operationName" style="width: 221px"></el-input>
        </el-form-item>
        <el-form-item>
          <span slot="label" @click="getBaseList(117)"><a herf="#">发料库位</a></span>
          <el-input v-model="componentData.issueToLocName" style="width: 221px"></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" :model="componentData" :rules="componentRules" style="margin-left: 5px">
        <el-form-item :label="'单位用量'" prop="qtyPerAssembly" :rules="componentRules.qtyPerAssembly">
          <el-input v-model="componentData.qtyPerAssembly" type="number" style="width: 142px"></el-input>
        </el-form-item>
        <el-form-item :label="'调机量'" prop="componentScrap" :rules="componentRules.componentScrap">
          <el-input v-model="componentData.componentScrap" type="number" style="width: 142px"></el-input>
        </el-form-item>
        <el-form-item :label="'损耗率'" prop="shrinkageFactor" :rules="componentRules.shrinkageFactor">
          <el-input v-model="componentData.shrinkageFactor" type="number" style="width: 142px"></el-input>
        </el-form-item>
      </el-form>
      <el-form :inline="true" label-position="top" :model="componentData" style="margin-left: 5px">
        <el-form-item label="备注">
          <el-input type="textarea" v-model="componentData.noteText" :rows="3" resize='none' maxlength="300" show-word-limit style="width: 455px;height: 20px"></el-input>
        </el-form-item>
      </el-form>
      <el-footer style="height:35px;margin-top:65px;text-align:center">
        <el-button type="primary" @click="componentDataSave()">保存</el-button>
        <el-button type="primary" @click="componentSaveModal = false">关闭</el-button>
      </el-footer>
    </el-dialog>

    <!-- chooseList模态框 -->
    <ChooseList ref="baseList" @getBaseData="getBaseData"></ChooseList>
  </div>
</template>

<script>
import {
  bomManagementSearch, // bom列表查询
  bomManagementSave, // bom新增
  bomManagementEdit, // bom编辑
  bomManagementDelete, // bom删除
  queryBomHeader, // 查询bom主信息
  saveBomHeader, // 新增bom主信息
  queryBomDetail, // 查bom明细
  bomDetailSave, // bom明细新增
  bomDetailDelete, // bom明细删除
  queryPartList, // 查询物料清单
  queryBomComponent, // 查询bom子明细
  saveBomComponent, // 新增bom子明细
  updateBomComponent, // 修改bom子明细
  deleteBomComponent, // 删除bom子明细
} from '@/api/part/bomManagement.js'
import ChooseList from '@/views/modules/common/Chooselist'
import dayjs from "dayjs";
import fa from "element-ui/src/locale/lang/fa";

export default {
  // 组件
  components: {
    ChooseList
  },
  // 监听
  watch: {
    searchData: {
      deep: true,
      handler: function (newV, oldV) {
        this.searchData.partNo = this.searchData.partNo.toUpperCase()
      }
    },
    saveDetailData: {
      deep: true,
      handler: function (newV, oldV) {
        this.saveDetailData.alternativeNo = this.saveDetailData.alternativeNo.toUpperCase()
      }
    }
  },
  // 对象
  data () {
    return {
      // 导出
      exportData: [],
      exportName: 'bom物料列表' + this.dayjs().format('YYYYMMDDHHmmss'),
      exportHeader: ['bom物料列表'],
      exportFooter: [],
      resultList: [],
      // ======== 行高 ========
      height: 200,
      // ======== 分页 ========
      pageIndex: 1,
      pageSize: 50,
      totalPage: 0,
      selectedDataNum: 0,
      // 条件查询
      searchData: {
        site: this.$store.state.user.site,
        partNo: '',
        partDesc: '',
        page: 1,
        limit: 10
      },
      // 其它
      dataListLoading: false,
      // 初始页签
      detailTable: 'bom_detail',
      subDetailTable: 'bom_sub_detail',
      // ======== 数据对象 ========
      modalData: {
        flag: '',
        title: '',
        site: this.$store.state.user.site,
        partNo: '',
        partDesc: '',
        engChgLevel: '',
        bomType: '',
        noteText: '',
        effPhaseInDate: '',
        effPhaseOutDate: '',
        engRevision: '',
        typeFlag: '',
        netWeight: '',
        createDate: '',
        createBy: '',
        updateDate: '',
        updateBy: ''
      },
      detailData: {
        site: this.$store.state.user.site,
        partNo: '',
        engChgLevel: '',
        bomType: '',
        alternativeNo: '',
        alternativeDescription: '',
        minLotQty: '',
        defaultFlag: '',
        detailNoteText: '',
        createDate: '',
        createBy: '',
        updateDate: '',
        updateBy: ''
      },
      componentData: {
        flag: '',
        site: this.$store.state.user.site,
        partNo: '',
        engChgLevel: '',
        bomType: '',
        alternativeNo: '',
        componentPart: '',
        componentPartDesc: '',
        printUnit: '',
        printUnitName: '',
        qtyPerAssembly: '',
        componentScrap: '',
        issueType: '',
        shrinkageFactor: '',
        lineItemNo: '',
        operationNo: '',
        operationName: '',
        issueToLoc: '',
        issueToLocName: '',
        noteText: '',
        createDate: '',
        createBy: '',
        updateDate: '',
        updateBy: ''
      },
      partData: {
        site: this.$store.state.user.site,
        partNo: '',
        partDesc: '',
      },
      componentPartData: {
        site: this.$store.state.user.site,
        partNo: '',
        partDesc: '',
      },
      saveDetailData: {
        site: this.$store.state.user.site,
        partNo: '',
        engChgLevel: '',
        bomType: '',
        alternativeNo: '',
        alternativeDescription: '',
        minLotQty: '',
        defaultFlag: '',
        detailNoteText: '',
        createDate: '',
        createBy: '',
        updateDate: '',
        updateBy: ''
      },
      // ======== 数据列表 ========
      dataList: [],
      partList: [],
      componentPartList: [],
      componentPartSelections: [],
      subDetailList: [],
      detailDataList: [],
      checkedDetail: [],
      // ======== 列表表头 ========
      columnList: [
        {
          userId: this.$store.state.user.name,
          functionId: 104002,
          serialNumber: '104002Table1PartNo',
          tableId: '104002Table1',
          tableName: 'BOM信息表',
          columnProp: 'partNo',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '物料编码',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },
        {
          userId: this.$store.state.user.name,
          functionId: 104002,
          serialNumber: '104002Table1PartDesc',
          tableId: '104002Table1',
          tableName: 'BOM信息表',
          columnProp: 'partDesc',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '物料名称',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },
        {
          userId: this.$store.state.user.name,
          functionId: 104002,
          serialNumber: '104002Table1CreateDate',
          tableId: '104002Table1',
          tableName: 'BOM信息表',
          columnProp: 'createDate',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '创建时间',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 160
        },
        {
          userId: this.$store.state.user.name,
          functionId: 104002,
          serialNumber: '104002Table1CreateBy',
          tableId: '104002Table1',
          tableName: 'BOM信息表',
          columnProp: 'createBy',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '创建人',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 80
        },
        {
          userId: this.$store.state.user.name,
          functionId: 104002,
          serialNumber: '104002Table1UpdateDate',
          tableId: '104002Table1',
          tableName: 'BOM信息表',
          columnProp: 'updateDate',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '更新时间',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 160
        },
        {
          userId: this.$store.state.user.name,
          functionId: 104002,
          serialNumber: '104002Table1UpdateBy',
          tableId: '104002Table1',
          tableName: 'BOM信息表',
          columnProp: 'updateBy',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '更新人',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 80
        }
      ],
      partColumnList: [
        {
          columnProp: 'partNo',
          headerAlign: "center",
          align: "center",
          columnLabel: '物料编码',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
        },
        {
          columnProp: 'partDesc',
          headerAlign: "center",
          align: "center",
          columnLabel: '物料名称',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
        },
      ],
      componentPartColumnList: [
        {
          columnProp: 'partNo',
          headerAlign: "center",
          align: "center",
          columnLabel: '物料编码',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
        },
        {
          columnProp: 'partDesc',
          headerAlign: "center",
          align: "center",
          columnLabel: '物料名称',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
        }
      ],
      columnSubDetailList: [
        {
          userId: this.$store.state.user.name,
          functionId: 104002,
          serialNumber: '104002Table2ComponentPart',
          tableId: '104002Table2',
          tableName: 'BOM子物料表',
          columnProp: 'componentPart',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '物料编码',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },
        {
          userId: this.$store.state.user.name,
          functionId: 104002,
          serialNumber: '104002Table2ComponentPartDesc',
          tableId: '104002Table2',
          tableName: 'BOM子物料表',
          columnProp: 'componentPartDesc',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '物料名称',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 120
        },
        {
          userId: this.$store.state.user.name,
          functionId: 104002,
          serialNumber: '104002Table2QtyPerAssembly',
          tableId: '104002Table2',
          tableName: 'BOM子物料表',
          columnProp: 'qtyPerAssembly',
          headerAlign: 'center',
          align: 'right',
          columnLabel: '单位用量',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 80
        },
        {
          userId: this.$store.state.user.name,
          functionId: 104002,
          serialNumber: '104002Table2ComponentScrap',
          tableId: '104002Table2',
          tableName: 'BOM子物料表',
          columnProp: 'componentScrap',
          headerAlign: 'center',
          align: 'right',
          columnLabel: '调机量',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 80
        },
        {
          userId: this.$store.state.user.name,
          functionId: 104002,
          serialNumber: '104002Table2ShrinkageFactor',
          tableId: '104002Table2',
          tableName: 'BOM子物料表',
          columnProp: 'shrinkageFactor',
          headerAlign: 'center',
          align: 'right',
          columnLabel: '损耗率',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 80
        },
        {
          userId: this.$store.state.user.name,
          functionId: 104002,
          serialNumber: '104002Table2PrintUnitName',
          tableId: '104002Table2',
          tableName: 'BOM子物料表',
          columnProp: 'printUnitName',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '单位',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 60
        },
        {
          userId: this.$store.state.user.name,
          functionId: 104002,
          serialNumber: '104002Table2IssueType',
          tableId: '104002Table2',
          tableName: 'BOM子物料表',
          columnProp: 'issueTypeName',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '生产属性',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 80
        },
        {
          userId: this.$store.state.user.name,
          functionId: 104002,
          serialNumber: '104002Table2OperationDesc',
          tableId: '104002Table2',
          tableName: 'BOM子物料表',
          columnProp: 'operationNo',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '工序',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 80
        },
        {
          userId: this.$store.state.user.name,
          functionId: 104002,
          serialNumber: '104002Table2IssueToLoc',
          tableId: '104002Table2',
          tableName: 'BOM子物料表',
          columnProp: 'issueToLocName',
          headerAlign: 'center',
          align: 'center',
          columnLabel: '发料库位',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 80
        },
        {
          userId: this.$store.state.user.name,
          functionId: 104002,
          serialNumber: '104002Table2NoteText',
          tableId: '104002Table2',
          tableName: 'BOM子物料表',
          columnProp: 'componentNoteText',
          headerAlign: 'center',
          align: 'left',
          columnLabel: '备注',
          columnHidden: false,
          columnImage: false,
          columnSortable: false,
          sortLv: 0,
          status: true,
          fixed: '',
          columnWidth: 150
        },
      ],
      // ======== 必填规则 ========
      rules: {
        partNo: [
          {
            required: true,
            message: ' ',
            trigger: 'change'
          }
        ],
        partDesc: [
          {
            required: true,
            message: ' ',
            trigger: 'change'
          }
        ],
        bomType: [
          {
            required: true,
            message: ' ',
            trigger: 'change'
          }
        ],
        effPhaseInDate: [
          {
            required: true,
            message: ' ',
            trigger: 'change'
          }
        ],
        effPhaseOutDate: [
          {
            required: true,
            message: ' ',
            trigger: 'change'
          }
        ],
        engChgLevel: [
          {
            required: true,
            message: ' ',
            trigger: 'change'
          }
        ],
        alternativeNo: [
          {
            required: true,
            message: ' ',
            trigger: 'change'
          }
        ],
        typeFlag: [
          {
            required: true,
            message: ' ',
            trigger: 'change'
          }
        ]
      },
      detailRules: {
        alternativeNo: [
          {
            required: true,
            message: ' ',
            trigger: 'change'
          }
        ],
        alternativeDescription: [
          {
            required: true,
            message: ' ',
            trigger: 'change'
          }
        ],
        minLotQty: [
          {
            required: true,
            message: ' ',
            trigger: 'change'
          }
        ],
      },
      componentRules: {
        componentPart: [
          {
            required: true,
            message: ' ',
            trigger: 'change'
          }
        ],
        componentPartDesc: [
          {
            required: true,
            message: ' ',
            trigger: 'change'
          }
        ],
        qtyPerAssembly: [
          {
            required: true,
            message: ' ',
            trigger: 'change'
          }
        ],
        componentScrap: [
          {
            required: true,
            message: ' ',
            trigger: 'change'
          }
        ],
        shrinkageFactor: [
          {
            required: true,
            message: ' ',
            trigger: 'change'
          }
        ],
        issueType: [
          {
            required: true,
            message: ' ',
            trigger: 'change'
          }
        ]
      },
      // ======== 复选数据集 ========
      bomSelections: [],
      // ======== 模态框开关控制 ========
      modalFlag: false,
      modalDisableFlag: false,
      partModelFlag: false,
      componentPartModelFlag: false,
      saveDetailModalFlag :false,
      componentSaveModal: false,
      componentDisableFlag: false
    }
  },
  mounted () {
    this.$nextTick(() => {
      this.height = window.innerHeight - 180
    })
  },
  created () {
    this.getDataList()
  },
  // js
  methods: {
    // ======== 分页相关方法 ========
    /**
     * 每页数
     * @param val
     */
    sizeChangeHandle (val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    /**
     * 当前页
     * @param val
     */
    currentChangeHandle (val) {
      this.pageIndex = val
      this.getDataList()
    },

    // ======== 页签切换相关方法 ========
    /**
     * 明细页签选择替换
     * @param tab
     * @param event
     */
    tabDetailClick (tab, event) {
      // 刷新列表数据
      this.refreshDetailTable()
    },
    /**
     * 子明细页签选择替换
     * @param tab
     * @param event
     */
    tabSubDetailClick (tab, event) {
      // 刷新列表数据
      this.refreshSubDetailTable()
    },
    /**
     * 刷新明细页签的table数据
     */
    refreshDetailTable () {
      if (this.detailTable === 'bom_detail') {

      }
    },
    /**
     * 刷新子明细页签的table数据
     */
    refreshSubDetailTable () {
      if (this.subDetailTable === 'bom_sub_detail') {

      }
    },

    // ======== 列表数据操作方法 ========
    /**
     * 未知
     * @returns {boolean}
     */
    selectFlag () {
      return true
    },
    /**
     * 获取数据列表
     */
    getDataList () {
      this.searchData.limit = this.pageSize
      this.searchData.page = this.pageIndex
      bomManagementSearch(this.searchData).then(({data}) => {
        if (data.code === 0) {
          this.dataList = data.page.list
          this.pageIndex = data.page.currPage
          this.pageSize = data.page.pageSize
          this.totalPage = data.page.totalCount
          this.$refs.selectDiv.setLengthAll( this.dataList.length)
        }
        this.dataListLoading = false
      })
    },
    /**
     * 复选列表信息
     * @param val
     */
    selectionBom (val) {
      this.bomSelections = val
      this.$refs.selectDiv.setLengthselected(this.bomSelections.length)
    },

    // ======== 新增/编辑模态框 ========
    /**
     * bom新增模态框
     */
    addModal () {
      this.modalData = {
        flag: '1',
        title: 'bom新增',
        site: this.$store.state.user.site,
        partNo: '',
        partDesc: '',
        engChgLevel: '',
        bomType: '',
        noteText: '',
        effPhaseInDate: '',
        effPhaseOutDate: '',
        engRevision: '',
        typeFlag: 'B',
        netWeight: 0,
        createBy: this.$store.state.user.name
      }
      this.detailData = {
        site: this.$store.state.user.site,
        partNo: '',
        engChgLevel: '',
        bomType: '',
        alternativeNo: '',
        alternativeDescription: '',
        minLotQty: '',
        defaultFlag: '',
        detailNoteText: '',
        createDate: '',
        createBy: '',
        updateDate: '',
        updateBy: ''
      }
      this.subDetailList = []
      this.modalDisableFlag = false
      this.modalFlag = true
    },
    /**
     * bom编辑模态框
     */
    async updateModal (row) {
      this.modalData = {
        flag: '2',
        site: row.site,
        partNo: row.partNo,
        partDesc: row.partDesc,
        engChgLevel: row.engChgLevel,
        bomType: row.bomType,
        noteText: row.noteText,
        effPhaseInDate: row.effPhaseInDate,
        effPhaseOutDate: row.effPhaseOutDate,
        engRevision: row.engRevision,
        typeFlag: row.typeFlag,
        netWeight: row.netWeight,
        updateBy: this.$store.state.user.name
      }
      // 查bom明细
      queryBomDetail(this.modalData).then(({data}) => {
        if (data && data.code === 0) {
          this.detailDataList = data.rows.detailList
          this.subDetailList = data.rows.componentList
          this.detailData = data.rows.detailData
          this.modalDisableFlag = true
          this.modalFlag = true
        } else {
          this.$alert(data.msg, '错误', {
            confirmButtonText: '确定'
          })
        }
      })
    },
    /**
     * 新增子明细
     */
    saveComponentModal () {
      if (this.detailData.alternativeNo === '' || this.detailData.alternativeNo == null) {
        this.$message.warning('请先选择替代！')
        return
      }
      this.componentData = {
        flag: '1',
        site: this.$store.state.user.site,
        partNo: this.modalData.partNo,
        engChgLevel: this.modalData.engChgLevel,
        bomType: this.modalData.bomType,
        alternativeNo: this.detailData.alternativeNo,
        componentPart: '',
        componentPartDesc: '',
        printUnit: '',
        printUnitName: '',
        qtyPerAssembly: '',
        componentScrap: '',
        issueType: '',
        shrinkageFactor: '',
        lineItemNo: '',
        operationNo: '',
        operationName: '',
        issueToLoc: '',
        issueToLocName: '',
        noteText: '',
        createBy: this.$store.state.user.name,
      }
      this.componentDisableFlag = false
      this.componentSaveModal = true
    },
    /**
     * 编辑模态框
     */
    updateComponentModal (row) {
      this.componentData = {
        flag: '2',
        site: row.site,
        partNo: row.partNo,
        engChgLevel: row.engChgLevel,
        bomType: row.bomType,
        alternativeNo: row.alternativeNo,
        componentPart: row.componentPart,
        componentPartDesc: row.componentPartDesc,
        printUnit: row.printUnit,
        printUnitName: row.printUnitName,
        qtyPerAssembly: row.qtyPerAssembly,
        componentScrap: row.componentScrap,
        issueType: row.issueType,
        shrinkageFactor: row.shrinkageFactor,
        lineItemNo: row.lineItemNo,
        operationNo: row.operationNo,
        operationName: row.operationName,
        issueToLoc: row.issueToLoc,
        issueToLocName: row.issueToLocName,
        noteText: row.noteText,
        updateBy: this.$store.state.user.name,
      }
      this.componentDisableFlag = true
      this.componentSaveModal = true
    },


    // ======== 新增/编辑/删除方法 ========
    /**
     * bom新增/编辑
     */
    saveData () {
      if (this.modalData.partNo === '' || this.modalData.partNo == null) {
        this.$message.warning('请选择物料编码！')
        return
      }
      if (this.modalData.partDesc === '' || this.modalData.partDesc == null) {
        this.$message.warning('请选择物料名称！')
        return
      }
      if (this.modalData.bomType === '' || this.modalData.bomType == null) {
        this.$message.warning('请选择制造类型！')
        return
      }
      if (this.modalData.effPhaseInDate === '' || this.modalData.effPhaseInDate == null) {
        this.$message.warning('请选择开始时间！')
        return
      }
      if (this.modalData.effPhaseOutDate === '' || this.modalData.effPhaseOutDate == null) {
        this.$message.warning('请选择结束时间！')
        return
      }
      if (this.modalData.engChgLevel === '' || this.modalData.engChgLevel == null) {
        this.$message.warning('请填写BOM版本号！')
        return
      }
      if (this.detailData.alternativeNo === '' || this.detailData.alternativeNo == null) {
        this.$message.warning('请选择替代！')
        return
      }
      let tempData = {
        site: this.$store.state.user.site,
        partNo: this.modalData.partNo,
        partDesc: this.modalData.partDesc,
        engChgLevel: this.modalData.engChgLevel,
        bomType: this.modalData.bomType,
        noteText: this.modalData.noteText,
        effPhaseInDate: this.modalData.effPhaseInDate,
        effPhaseOutDate: this.modalData.effPhaseOutDate,
        engRevision: this.modalData.engRevision,
        typeFlag: this.modalData.typeFlag,
        netWeight: this.modalData.netWeight,
        alternativeNo: this.detailData.alternativeNo,
        alternativeDescription: this.detailData.alternativeDescription,
        minLotQty: this.detailData.minLotQty,
        defaultFlag: this.detailData.defaultFlag,
        detailNoteText: this.detailData.detailNoteText,
        createBy: this.$store.state.user.name,
        updateBy: this.$store.state.user.name,
        informationList: this.subDetailList
      }
      if (this.modalData.flag === '1') {
        bomManagementSave(tempData).then(({data}) => {
          if (data && data.code === 0) {
            this.getDataList()
            this.modalFlag = false
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {}
            })
          } else {
            this.$alert(data.msg, '错误', {
              confirmButtonText: '确定'
            })
          }
        })
      } else {
        bomManagementEdit(tempData).then(({data}) => {
          if (data && data.code === 0) {
            this.getDataList()
            this.modalFlag = false
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {}
            })
          } else {
            this.$alert(data.msg, '错误', {
              confirmButtonText: '确定'
            })
          }
        })
      }
    },
    /**
     * 新增编辑模态框关闭
     */
    closeModal () {
      let tempArr = []
      tempArr.push(this.modalData)
      if (this.modalData.flag === '1') {
        this.$confirm(`是否不保存且删除该条BOM记录?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let tempData = {
            informationList: tempArr
          }
          bomManagementDelete(tempData).then(({data}) => {
            if (data && data.code === 0) {
              this.getDataList()
              this.modalFlag = false
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {}
              })
            } else {
              this.$alert(data.msg, '错误', {
                confirmButtonText: '确定'
              })
            }
          })
        }).catch(() => {
        })
      } else {
        this.modalFlag = false
      }
    },
    /**
     * bom删除
     */
    delModal () {
      if(this.bomSelections.length === 0){
        this.$message.warning('请勾选要删除的BOM！')
        return
      }
      this.$confirm(`是否删除这 `+ this.bomSelections.length +` 条BOM?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let tempData = {
          informationList: this.bomSelections
        }
        bomManagementDelete(tempData).then(({data}) => {
          if (data && data.code === 0) {
            this.getDataList()
            this.bomSelections = []
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {}
            })
          } else {
            this.$alert(data.msg, '错误', {
              confirmButtonText: '确定'
            })
          }
        })
      }).catch(() => {
      })
    },
    /**
     * 查询物料
     */
    queryPartList () {
      queryPartList(this.partData).then(({data}) => {
        if (data && data.code === 0) {
          this.partList = data.rows
          this.partModelFlag = true
        } else {
          this.$alert(data.msg, '错误', {
            confirmButtonText: '确定'
          })
        }
      })
    },
    /**
     * 双击选中物料
     * @param row
     */
    getRowData (row) {
      this.modalData.partNo = row.partNo
      this.modalData.partDesc = row.partDesc
      this.partModelFlag = false
    },
    getComponentRowData (row) {
      this.componentData.componentPart = row.partNo
      this.componentData.componentPartDesc = row.partDesc
      this.componentPartModelFlag = false
    },
    /**
     * 物料列表
     */
    queryComponentPartList () {
      // 查询所有物料
      queryPartList(this.componentPartData).then(({data}) => {
        if (data && data.code === 0) {
          this.componentPartList = data.rows
          this.componentPartModelFlag = true
        } else {
          this.$alert(data.msg, '错误', {
            confirmButtonText: '确定'
          })
        }
      })
    },
    /**
     * 表格的新增
     * @param row
     * @param rowIndex
     */
    rowClassName({ row, rowIndex }) {
      row.xh = rowIndex + 1;
    },
    /**
     * 单选框选中数据
     * @param selection
     */
    componentSelectionChange(selection) {
      this.checkedDetail = selection
    },
    /**
     * 删除子物料
     */
    deleteComponentPart () {
      if (this.checkedDetail.length === 0) {
        this.$message.warning('请选择要删除子物料！')
        return
      } else {
        this.$confirm("请是否确认删除该子明细记录?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          let tempData = {
            informationList: this.checkedDetail
          }
          deleteBomComponent(tempData).then(({data}) => {
            if (data && data.code === 0) {
              this.subDetailList = data.rows
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {}
              })
            } else {
              this.$alert(data.msg, '错误', {
                confirmButtonText: '确定'
              })
            }
          })
        }).catch(() => {
        })
      }
    },
    /**
     * 子物料
     * @param row
     */
    // componentPartClickRow (row) {
    //   this.$refs.componentPartTable.toggleRowSelection(row)
    // },
    componentClickRow (row) {
      this.$refs.componentTable.toggleRowSelection(row)
    },
    // /**
    //  * 多选子物料
    //  * @param val
    //  */
    // selectionComponentPart (val) {
    //   this.componentPartSelections = val
    // },
    // getRowKeys(row) {
    //   // 唯一值，一般都为id
    //   return row.partNo
    // },
    /**
     * 新增子明细方法
     */
    componentDataSave () {
      if (this.componentData.partNo === '' || this.componentData.partNo == null) {
        this.$message.warning('请选择主记录物料编码！')
        return
      }
      if (this.componentData.bomType === '' || this.componentData.bomType == null) {
        this.$message.warning('请选择主记录制造类型！')
        return
      }
      if (this.componentData.engChgLevel === '' || this.componentData.engChgLevel == null) {
        this.$message.warning('请填写主记录BOM版本号！')
        return
      }
      if (this.componentData.componentPart === '' || this.componentData.componentPart == null) {
        this.$message.warning('请选择子物料编码！')
        return
      }
      if (this.componentData.componentPartDesc === '' || this.componentData.componentPartDesc == null) {
        this.$message.warning('请选择子物料名称！')
        return
      }
      if (this.componentData.qtyPerAssembly === '' || this.componentData.qtyPerAssembly == null) {
        this.$message.warning('请填写单位用量！')
        return
      }
      if (this.componentData.componentScrap === '' || this.componentData.componentScrap == null) {
        this.$message.warning('请填写调机量！')
        return
      }
      if (this.componentData.shrinkageFactor === '' || this.componentData.shrinkageFactor == null) {
        this.$message.warning('请填写损耗率！')
        return
      }
      if (this.componentData.issueType === '' || this.componentData.issueType == null) {
        this.$message.warning('请选择生产属性！')
        return
      }
      if (this.componentData.flag === '1') {
        saveBomComponent(this.componentData).then(({data}) => {
          if (data && data.code === 0) {
            this.subDetailList = data.rows
            this.componentSaveModal = false
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {}
            })
          } else {
            this.$alert(data.msg, '错误', {
              confirmButtonText: '确定'
            })
          }
        })
      } else if (this.componentData.flag === '2') {
        updateBomComponent(this.componentData).then(({data}) => {
          if (data && data.code === 0) {
            this.subDetailList = data.rows
            this.componentSaveModal = false
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {}
            })
          } else {
            this.$alert(data.msg, '错误', {
              confirmButtonText: '确定'
            })
          }
        })
      }
    },
    /**
     * 回车事件
     */
    focusNextInput (index, type) {
      let aaa = ''
      if (this.subDetailList.length - 1 === index) {
        aaa = `${type}0`
      } else {
        aaa = `${type}${index + 1}`
      }
      this.$nextTick(() => {
        this.$refs[aaa].focus()
      })
    },
    /**
     * 保存主表信息
     */
    saveBomHeader () {
      if (this.modalData.partNo === '' || this.modalData.partNo == null) {
        this.$message.warning('请先选择Bom物料！')
        return
      }
      if (this.modalData.engChgLevel === '' || this.modalData.engChgLevel == null) {
        this.$message.warning('请先填写Bom版本号！')
        return
      }
      if (this.modalData.bomType === '' || this.modalData.bomType == null) {
        this.$message.warning('请先选择制造类型！')
        return
      }
      if (this.modalData.typeFlag === '' || this.modalData.typeFlag == null) {
        this.$message.warning('请先选择类型！')
        return
      }
      // 新增主表信息
      saveBomHeader(this.modalData).then(({data}) => {
        if (data && data.code === 0) {
          this.modalData = {
            flag: '1',
            title: 'bom新增',
            site: data.rows.modalData.site,
            partNo: data.rows.modalData.partNo,
            partDesc: data.rows.modalData.partDesc,
            engChgLevel: data.rows.modalData.engChgLevel,
            bomType: data.rows.modalData.bomType,
            noteText: data.rows.modalData.noteText,
            effPhaseInDate: data.rows.modalData.effPhaseInDate,
            effPhaseOutDate: data.rows.modalData.effPhaseOutDate,
            engRevision: data.rows.modalData.engRevision,
            typeFlag: data.rows.modalData.typeFlag,
            netWeight: data.rows.modalData.netWeight,
          }
          this.detailDataList = data.rows.detailDataList
          this.detailData = data.rows.detailData
          this.subDetailList = []
          this.modalDisableFlag = true
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {}
          })
        } else {
          this.$alert(data.msg, '错误', {
            confirmButtonText: '确定'
          })
        }
      })
    },
    /**
     * 新增替代模态框
     */
    saveBomDetail () {
      if (this.modalData.partNo === '' || this.modalData.partNo == null) {
        this.$message.warning('请先选择Bom物料！')
        return
      }
      if (this.modalData.engChgLevel === '' || this.modalData.engChgLevel == null) {
        this.$message.warning('请先填写Bom版本号！')
        return
      }
      if (this.modalData.bomType === '' || this.modalData.bomType == null) {
        this.$message.warning('请先选择制造类型！')
        return
      }
      this.saveDetailData = {
        site: this.$store.state.user.site,
        partNo: this.modalData.partNo,
        engChgLevel: this.modalData.engChgLevel,
        bomType: this.modalData.bomType,
        alternativeNo: '',
        alternativeDescription: '',
        minLotQty: '',
        defaultFlag: '',
        detailNoteText: '',
        createDate: '',
        createBy: this.$store.state.user.name,
      }
      // 查询bom主信息
      queryBomHeader(this.modalData).then(({data}) => {
        if (data && data.code === 0) {
          if (data.rows.length > 0) {
            this.saveDetailModalFlag = true
          } else {
            this.$message.warning('请先保存BOM主记录！')
          }
        } else {
          this.$alert(data.msg, '提示', {
            confirmButtonText: '确定'
          })
        }
      })
    },
    /**
     * 新增替代方法
     */
    detailDataSave () {
      if (this.saveDetailData.partNo === '' || this.saveDetailData.partNo == null) {
        this.$message.warning('请先选择Bom物料！')
        return
      }
      if (this.saveDetailData.engChgLevel === '' || this.saveDetailData.engChgLevel == null) {
        this.$message.warning('请先填写Bom版本号！')
        return
      }
      if (this.saveDetailData.bomType === '' || this.saveDetailData.bomType == null) {
        this.$message.warning('请先选择制造类型！')
        return
      }
      if (this.saveDetailData.alternativeNo === '' || this.saveDetailData.alternativeNo == null) {
        this.$message.warning('请填写替代编码！')
        return
      }
      if (this.saveDetailData.alternativeDescription === '' || this.saveDetailData.alternativeDescription == null) {
        this.$message.warning('请填写替代名称！')
        return
      }
      if (this.saveDetailData.minLotQty === '' || this.saveDetailData.minLotQty == null) {
        this.$message.warning('请填写最小订单数！')
        return
      }
      bomDetailSave(this.saveDetailData).then(({data}) => {
        if (data && data.code === 0) {
          this.detailDataList = data.rows
          this.detailData = this.saveDetailData
          this.subDetailList = []
          this.saveDetailModalFlag = false
          this.$message({
            message: '操作成功',
            type: 'success',
            duration: 1500,
            onClose: () => {}
          })
        } else {
          this.$alert(data.msg, '错误', {
            confirmButtonText: '确定'
          })
        }
      })
    },
    /**
     * 删除替代
     */
    deleteBomDetail () {
      if (this.detailData.alternativeNo === '' || this.detailData.alternativeNo == null) {
        this.$message.warning('请选择要删除的替代编码！')
        return
      }
      if (this.detailData.alternativeNo === '*') {
        this.$message.warning('当前为默认替代，不可以删除！')
        return
      }
      this.$confirm(`是否删除这项替代?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        bomDetailDelete(this.detailData).then(({data}) => {
          if (data && data.code === 0) {
            this.detailData = data.rows.detailData
            this.detailDataList = data.rows.detailDataList
            this.subDetailList = data.rows.subDetailList
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {}
            })
          } else {
            this.$alert(data.msg, '错误', {
              confirmButtonText: '确定'
            })
          }
        })
      }).catch(() => {
      })
    },
    /**
     * 替代改变事件
     */
    alternativeChange () {
      queryBomComponent(this.detailData).then(({data}) => {
        if (data && data.code === 0) {
          this.detailData = data.rows.detailData
          this.subDetailList = data.rows.componentList
        } else {
          this.$alert(data.msg, '错误', {
            confirmButtonText: '确定'
          })
        }
      })
    },

    // ======== chooseList相关方法 ========
    /**
     * 获取基础数据列表S
     * @param val
     * @param type
     */
    getBaseList (val, type) {
      this.tagNo = val
      this.$nextTick(() => {
        let strVal = ''
        if (val === 117) {
          strVal = this.componentData.issueToLoc
        }
        this.$refs.baseList.init(val, strVal)
      })
    },
    /**
     * 列表方法的回调
     * @param val
     */
    getBaseData (val) {
      if (this.tagNo === 117) {
        this.componentData.issueToLoc = val.location_id
        this.componentData.issueToLocName = val.location_name
      }
    },

    // ======== 导出相关方法 ========
    /**
     * 导出excel
     */
    async createExportData () {
      this.searchData.limit = -1
      this.searchData.page = 1
      await bomManagementSearch(this.searchData).then(({data}) => {
        this.resultList = data.page.list
      })
      return this.resultList
    },
    startDownload () {
    },
    finishDownload () {
    },
    fields () {
      let json = '{'
      this.columnList.forEach((item, index) => {
        if (index == this.columnList.length - 1) {
          json += '"' + item.columnLabel + '"' + ':' + '"' + item.columnProp + '"'
        } else {
          json += '"' + item.columnLabel + '"' + ':' + '"' + item.columnProp + '"' + ','
        }
      })
      json += '}'
      let s = eval('(' + json + ')')
      return s
    }
  }
}
</script>

<style scoped lang="scss">
/deep/ .detail-tab .el-tabs__content {
  height: 180px;
  padding: 15px 0px 0px 0px;
}
/deep/ .sub_detail-tab .el-tabs__content {
  height: 280px;
  padding: 0px;
}

</style>










