<template>
  <el-dialog title="详情" v-model="setCrewShow" :before-close="cancel" fullscreen>
    <div class="tabs-container" v-loading="tabLoading">
      <div class="content-right">
        <span class="version-label" v-if="this.detailsType == 'taskProcessing'"
          >工艺版本：{{ version || '-' }}</span
        >
        <div class="version_select" v-else>
          工艺版本：<el-select
            v-model="version"
            placeholder="请输入"
            @change="versionChange()"
            style="width: 150px"
          >
            <el-option
              v-for="(item, index) in versionOptions"
              :key="index"
              :label="item.partVersion"
              :value="item.partVersion"
            />
          </el-select>
        </div>
      </div>

      <el-tabs v-if="setCrewShow" v-model="activeNameTab">
        <el-tab-pane label="零件信息" name="1"> </el-tab-pane>
        <el-tab-pane label="工艺编制" name="2"> </el-tab-pane>
      </el-tabs>

      <div v-show="activeNameTab === '1'">
        <el-card class="box-card-info">
          <div slot="header" class="clearfix">
            <span style="font-size: 20px">基本信息</span>
          </div>
          <el-form
            ref="testForm"
            :model="formData"
            label-width="80px"
            label-position="left"
            :validate-on-rule-change="false"
            class="vd-form-three"
          >
            <template v-if="partType == '热表'">
              <el-row :gutter="24">
                <el-col :span="6">
                  <el-form-item label="零件号:" prop="partCode">
                    {{ formData.partCode }}
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="零件名称:" prop="partName">
                    {{ formData.partName }}
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="产品型号:" prop="productType">
                    {{ formData.productType }}
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="是否印字:" prop="isPrint">
                    <el-radio-group v-model="formData.isPrint" disabled>
                      <el-radio value="2">是</el-radio>
                      <el-radio value="1">否</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="材料:" prop="material">
                    {{ formData.material }}
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="硬度:" prop="hardness">
                    {{ formData.hardness }}
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="物料编码:" prop="plateGoodsCode">
                    <span v-if="formData.plateGoodsCode == '04-05-023'">金钾-04-05-023</span>
                    <span v-if="formData.plateGoodsCode == '04-05-024'">银板-04-05-024</span>
                    <span v-else>-</span>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item
                    label="产品应用领域:"
                    prop="productApplicationArea"
                    label-width="90px"
                  >
                    {{ formData.productApplicationArea }}
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row>
                <el-col :span="6">
                  <el-form-item label="镀种1:" prop="plate">
                    {{ formData.plate }}
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="镀层代号1:" prop="plateCode">
                    {{ formData.plateCode }}
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="面积1(dm²):" prop="area">
                    {{ formData.area }}
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="镀层厚度1:" prop="plateThickness">
                    {{ formData.plateThickness }}
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="镀种2:" prop="plateTwo">
                    {{ formData.plateTwo }}
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="镀层代号2:" prop="plateCodeTwo">
                    {{ formData.plateCodeTwo }}
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="面积2(dm²):" prop="areaTwo">
                    {{ formData.areaTwo }}
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="镀层厚度2:" prop="plateThickness">
                    {{ formData.plateThickness }}
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="24">
                <el-col :span="16">
                  <el-form-item label="工艺路线:">
                    {{ formData.craftWay }}
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="24">
                <el-col :span="6">
                  <el-form-item label="涂色标个数:" prop="tsbNum">
                    {{ formData.tsbNum }}
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="涂色带个数:" prop="tsdNum">
                    {{ formData.tsdNum }}
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="涂箭头个数:" prop="tjtNum">
                    {{ formData.tjtNum }}
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="24">
                <el-col :span="14">
                  <el-form-item label="色标色带:" prop="memo">
                    {{ formData.memo }}
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="24">
                <el-col :span="14">
                  <el-form-item label="关键信息:" prop="keyInfo">
                    <el-input
                      v-model="formData.keyInfo"
                      type="textarea"
                      maxlength="150"
                      show-word-limit
                      rows="3"
                      disabled
                      placeholder="请输入"
                    />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="24">
                <el-col :span="6">
                  <el-form-item label="有效期:" prop="periodOfValidity">
                    <el-radio-group disabled v-model="formData.periodOfValidity">
                      <el-radio value="1">长期有效</el-radio>
                      <el-radio value="2">自定义</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="有效周期(天):" prop="expirationPeriod" label-width="100px">
                    {{ formData.expirationPeriod }}
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="到期提醒(天):" prop="advanceRemind" label-width="100px">
                    {{ formData.advanceRemind }}
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="24">
                <el-col :span="24">
                  <el-form-item label="工艺校验:" prop="sinTer">
                    <div style="width: 100%">
                      <el-checkbox v-model="formData.goldMark" disabled>镀金标识</el-checkbox>
                      <el-checkbox
                        disabled
                        v-model="formData.markingsTest"
                        :true-label="'1'"
                        :false-label="'0'"
                        >涂色标</el-checkbox
                      >
                      <el-checkbox
                        disabled
                        v-model="formData.fluorescenceCheck"
                        :true-label="'1'"
                        :false-label="'0'"
                        >荧光检</el-checkbox
                      >
                      <el-checkbox
                        v-model="formData.hotDispose"
                        disabled
                        :true-label="'1'"
                        :false-label="'0'"
                        >热处理检验</el-checkbox
                      >
                      <el-checkbox
                        v-model="formData.inkjetPrinting"
                        disabled
                        :true-label="'1'"
                        :false-label="'0'"
                        >喷码</el-checkbox
                      >
                    </div>
                  </el-form-item>
                </el-col>
              </el-row>
            </template>
            <template v-if="partType == '烧结'">
              <el-row :gutter="24">
                <el-col :span="6">
                  <el-form-item label="零件号:" prop="partCode">
                    {{ formData.partCode }}
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="零件名称:" prop="partName">
                    {{ formData.partName }}
                  </el-form-item>
                </el-col>
                <el-col :span="6" v-if="notSinTer && notGlassCake">
                  <el-form-item label="产品型号:" prop="productType">
                    {{ formData.productType }}
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="是否为玻璃饼:" prop="isClassCake">
                    <el-switch
                      v-model="formData.isClassCake"
                      disabled
                      active-color="#13ce66"
                      inactive-color="#ff4949"
                      active-text="是"
                      inactive-text="否"
                      active-value="1"
                      inactive-value="0"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="6" v-if="notGlassCake">
                  <el-form-item label="是否印字:" prop="isPrint">
                    <el-radio-group v-model="formData.isPrint" disabled>
                      <el-radio value="2">单</el-radio>
                      <el-radio value="3">双</el-radio>
                      <el-radio value="1">否</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </el-col>
                <el-col :span="6" v-if="notSinTer || !notGlassCake">
                  <el-form-item label="面积(dm²):">
                    {{ formData.area }}
                  </el-form-item>
                </el-col>
                <el-col :span="6" v-if="notSinTer || !notGlassCake">
                  <el-form-item label="材料:" prop="material">
                    {{ formData.material }}
                  </el-form-item>
                </el-col>
                <el-col :span="6" v-if="notSinTer && notGlassCake">
                  <el-form-item label="硬度:" prop="hardness">
                    {{ formData.hardness }}
                  </el-form-item>
                </el-col>
                <el-col :span="6" v-if="notSinTer && notGlassCake">
                  <el-form-item label="物料编码:" prop="plateGoodsCode">
                    <span v-if="formData.plateGoodsCode == '04-05-023'">金钾-04-05-023</span>
                    <span v-if="formData.plateGoodsCode == '04-05-024'">银板-04-05-024</span>
                    <span v-else>-</span>
                  </el-form-item>
                </el-col>
                <el-col :span="6" v-if="notSinTer && notGlassCake">
                  <el-form-item label="镀种:" prop="plate">
                    {{ formData.plate }}
                  </el-form-item>
                </el-col>
                <el-col :span="6" v-if="notSinTer && notGlassCake">
                  <el-form-item label="镀层代号:" prop="plateCode">
                    {{ formData.plateCode }}
                  </el-form-item>
                </el-col>
                <el-col :span="6" v-if="notSinTer && notGlassCake">
                  <el-form-item label="面积(dm²):" prop="area">
                    {{ formData.area }}
                  </el-form-item>
                </el-col>
                <el-col :span="6" v-if="(child || formData.glassCake) && formData.sinTerType != 5">
                  <el-form-item label="粉重:" prop="powderWeight">
                    {{ formData.powderWeight }}
                  </el-form-item>
                </el-col>
                <el-col :span="6" v-if="notSinTer && notGlassCake">
                  <el-form-item label="镀层厚度:" prop="plateThickness">
                    {{ formData.plateThickness }}
                  </el-form-item>
                </el-col>
                <el-col :span="6" v-if="!notGlassCake">
                  <el-form-item label="成型厚度:" prop="formingThickness">
                    {{ formData.formingThickness }}
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item
                    label="产品应用领域:"
                    prop="productApplicationArea"
                    label-width="90px"
                  >
                    {{ formData.productApplicationArea }}
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="玻璃饼直径" label-width="90px">
                    {{ formData.blbDiameter }}
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="壳体最大外径" label-width="90px">
                    {{ formData.externalDiameter }}
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="24">
                <el-col :span="6">
                  <el-form-item label="涂色标个数:" prop="tsbNum">
                    {{ formData.tsbNum }}
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="涂色带个数:" prop="tsdNum">
                    {{ formData.tsdNum }}
                  </el-form-item>
                </el-col>
                <el-col :span="6">
                  <el-form-item label="涂箭头个数:" prop="tjtNum">
                    {{ formData.tjtNum }}
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="24" v-if="notGlassCake || !notSinTer">
                <el-col :span="24">
                  <el-form-item label="工艺路线:">
                    {{ formData.craftWay }}
                  </el-form-item>
                </el-col>
                <el-col :span="14">
                  <el-form-item label="关键信息:" prop="keyInfo">
                    <el-input
                      v-model="formData.keyInfo"
                      type="textarea"
                      maxlength="150"
                      show-word-limit
                      disabled
                      rows="3"
                      placeholder="请输入"
                    />
                  </el-form-item>
                </el-col>
                <el-col :span="14">
                  <el-form-item label="色标色带:" prop="memo">
                    <el-input
                      v-model="formData.memo"
                      :rows="3"
                      type="textarea"
                      disabled
                      maxlength="150"
                      show-word-limit
                      placeholder="请输入"
                    />
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="24">
                <el-col :span="6">
                  <el-form-item label="有效期:" prop="periodOfValidity">
                    <el-radio-group v-model="formData.periodOfValidity" disabled>
                      <el-radio value="1">长期有效</el-radio>
                      <el-radio value="2">自定义</el-radio>
                    </el-radio-group>
                  </el-form-item>
                </el-col>
                <el-col :span="6" v-if="formData.periodOfValidity == '2'">
                  <el-form-item label="有效周期(天):" prop="expirationPeriod" label-width="100px">
                    {{ formData.expirationPeriod }}
                  </el-form-item>
                </el-col>
                <el-col :span="6" v-if="formData.periodOfValidity == '2'">
                  <el-form-item label="到期提醒(天):" prop="advanceRemind">
                    {{ formData.advanceRemind }}
                  </el-form-item>
                </el-col>
              </el-row>
              <el-row :gutter="24">
                <el-col :span="24">
                  <el-form-item label="工艺校验:" prop="sinTer">
                    <div style="width: 100%">
                      <el-checkbox v-model="formData.goldMark" disabled>镀金标识</el-checkbox>
                      <el-checkbox
                        v-model="formData.markingsTest"
                        @change="setDataLocal"
                        :true-label="'1'"
                        :false-label="'0'"
                        disbaled
                        >涂色标</el-checkbox
                      >
                    </div>
                  </el-form-item>
                </el-col>
              </el-row>
            </template>
          </el-form>
        </el-card>
        <el-card class="box-card" v-if="dataTable.length > 0" style="margin-top: 24px">
          <div slot="header" class="clearfix">
            <span style="font-size: 20px">子件信息</span>
          </div>
          <avue-crud
            :option="option"
            :table-loading="loading"
            :data="dataTable"
            v-model="form"
            ref="crud"
          >
          </avue-crud>
        </el-card>
      </div>
      <div v-show="activeNameTab === '2'" class="box-card box-card-two">
        <div class="process-left">
          <div class="process-select">
            <!-- 工艺级别：-->
            <el-form :model="form" label-width="auto">
              <el-form-item label="工艺级别:">
                <el-select
                  v-model="rank"
                  placeholder="请选择"
                  style="width: 220px"
                  @change="leavelChange"
                  clearable
                  filterable
                >
                  <el-option label="一级工艺" value="1" />
                  <el-option label="二级工艺" value="2" />
                  <el-option label="三级工艺" value="3" />
                </el-select>
              </el-form-item>
            </el-form>
          </div>
          <el-tree
            style="width: 240px"
            :data="data"
            :props="defaultProps"
            @node-click="handleNodeClick"
            :default-expand-all="true"
            :expand-on-click-node="false"
            node-key="id"
            ref="tree"
            :current-node-key="currentNodeKey"
          />
        </div>
        <div class="process-right">
          <el-form :model="form" label-width="auto" v-if="treeLeave == 2">
            <el-row :gutter="24">
              <el-col :span="8">
                <el-form-item label="重要度:" prop="prority">
                  <el-select
                    v-model="treeNodes.rawData.prority"
                    :placeholder="''"
                    clearable
                    filterable
                    disabled
                  >
                    <el-option
                      v-for="item in priorityList"
                      :key="item.value"
                      :label="item.label"
                      :value="item.value"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="流转编号:" prop="roamNo">
                  <el-input v-model="treeNodes.rawData.roamNo" disabled placeholder="请输入" />
                </el-form-item>
              </el-col>
              <el-col :span="8">
                <el-form-item label="备注:" prop="beizhu">
                  <el-input v-model="treeNodes.rawData.remarks" placeholder="" disabled />
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
          <el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick">
            <el-tab-pane label="工艺维护" name="1" v-if="treeLeave == 2"></el-tab-pane>
            <el-tab-pane label="项目" name="2" v-if="treeLeave == 3"></el-tab-pane>
            <el-tab-pane label="尺寸、量具" name="3" v-if="treeLeave == 3"></el-tab-pane>
            <el-tab-pane label="工装、模具表" name="4" v-if="treeLeave == 3"></el-tab-pane>
          </el-tabs>
          <el-table :data="form1.tableData1" style="width: 100%" v-if="activeName == '1'" border>
            <el-table-column prop="processNo" label="工序号"></el-table-column>
            <el-table-column prop="processName" label="工序"> </el-table-column>
            <el-table-column prop="craftName" label="工艺能力"> </el-table-column>
            <el-table-column prop="proDes" label="工序描述"> </el-table-column>
            <el-table-column prop="proHours" label="定额工时(分钟)"> </el-table-column>
            <el-table-column prop="setoutHours" label="准备工时(分钟)"> </el-table-column>
            <el-table-column prop="isMain" label="是否主工序">
              <template #default="scope">
                {{ scope.row.isMain == '1' ? '是' : '否' }}
              </template>
            </el-table-column>
          </el-table>
          <el-table :data="form2.tableData2" style="width: 100%" v-if="activeName == '2'" border>
            <el-table-column prop="trialNo" label="项目编号"></el-table-column>
            <el-table-column prop="projectName" label="检验项目"> </el-table-column>
            <el-table-column prop="projectStandardName" label="检验标准"> </el-table-column>
            <el-table-column prop="proHours" label="定额工时(分钟)"> </el-table-column>
            <el-table-column prop="prepareHours" label="准备工时(分钟)"></el-table-column>
          </el-table>
          <el-table :data="form3.tableData3" style="width: 100%" v-if="activeName == '3'" border>
            <el-table-column prop="trialNo" label="项目编号"></el-table-column>
            <el-table-column prop="testType" label="镀前镀后">
              <template #default="scope">
                {{ scope.row.testType == 1 ? '镀前' : '镀后' }}
              </template>
            </el-table-column>
            <el-table-column prop="tool" label="量具"> </el-table-column>
            <el-table-column prop="toolSize" label="量具尺寸"></el-table-column>
            <el-table-column prop="toolInfo" label="图纸尺寸"></el-table-column>
          </el-table>
          <el-table :data="form4.tableData4" style="width: 100%" v-if="activeName == '4'" border>
            <el-table-column prop="moldCode" label="对应工装"> </el-table-column>
            <el-table-column prop="moldName" label="工装名称"></el-table-column>
            <el-table-column prop="isPrint" label="是否印字"></el-table-column>
            <el-table-column prop="printNo" label="印字版编号"></el-table-column>
            <el-table-column prop="testFixtureNo" label="测试夹具号"></el-table-column>
          </el-table>
        </div>
      </div>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button @click="cancel()">取消</el-button>
        <!-- <el-button type="primary" @click="cancel()"> 确认 </el-button> -->
      </div>
    </template>
  </el-dialog>
</template>
<script>
import dsPartBasicInfo from './dsPartBasicInfo.vue';
import processPlanning from './processPlanning.vue';
import {
  getPartManagementDetail,
  getProcessManagementAuthorized,
  getPartDetail,
  getProcessAuthorized,
  getVersion,
  getPlatingList,
  getProject,
} from '@/api/processManagement/taskProcessing';

export default {
  name: 'DsPartIndex',
  components: {
    dsPartBasicInfo,
    processPlanning,
  },
  props: {
    // partType: {
    //   type: String,
    //   default: '',
    // },
    isOpen: {
      type: Boolean,
      default: false,
    },
    rowData: {
      type: Object,
      default: () => ({}),
    },
    detailsType: {
      type: String,
      default: '',
    },
    partId: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      currentNodeKey: '',
      showProcessVersion: false,
      setCrewShow: false,
      activeName: '1',
      activeNameTab: '1',
      craftData: {},
      treeNodes: {},
      formData: {},
      form: {},
      processLevel: '1',
      rankList: [],
      rank: '2',
      priorityList: [
        {
          label: '本次使用',
          value: 1,
        },
        {
          label: '正式工艺',
          value: 2,
        },
        {
          label: '临时工艺',
          value: 3,
        },
      ],
      form1: {
        tableData1: [], //工序
      },
      form2: {
        tableData2: [], //项目
      },
      form3: {
        tableData3: [], //量具
      },
      form4: {
        tableData4: [], //工装
      },
      activeName: '1',
      treeLeave: 2, //组织树选中层级
      data: [],
      dataTable: [],
      option: {
        tip: false,
        simplePage: false,
        searchShow: true,
        searchMenuSpan: 6,
        dialogWidth: '60%',
        border: true,
        selection: false,
        viewBtn: true,
        menuWidth: 240,
        dialogClickModal: false,
        excelBtn: true,
        viewBtn: false,
        editBtnText: '编辑',
        refreshBtn: false,
        searchShowBtn: false,
        gridBtn: false,
        menu: false,
        header: false,
        index: true,
        searchLabelPosition: 'left',
        searchGutter: 24,
        searchSpan: 6,
        menuAlign: 'left',
        gridBtn: false,
        searchMenuPosition: 'right',
        addBtnIcon: ' ',
        viewBtnIcon: ' ',
        delBtnIcon: ' ',
        editBtnIcon: ' ',
        align: 'center',
        column: [
          {
            label: '零件号',
            prop: 'partCode',
            search: false,
            sortable: true,
            overHidden: true,
          },
          {
            label: '零件名称',
            prop: 'partName',
            search: false,
            sortable: true,
            overHidden: true,
          },
          {
            label: '产品型号',
            prop: 'productType',
            search: false,
            sortable: true,
            overHidden: true,
          },
          {
            label: '面积',
            prop: 'area',
            search: false,
            sortable: true,
            overHidden: true,
          },
          {
            label: '数量',
            prop: 'quota',
            search: false,
            sortable: true,
            overHidden: true,
          },
          {
            label: '总面积',
            prop: 'totalArea',
            search: false,
            sortable: true,
            overHidden: true,
          },
          {
            label: '零件类型',
            prop: 'sinTerType',
            search: false,
            sortable: true,
            overHidden: true,
            type: 'select',
            filterable: true,
            clearable: true,
            cell: true,
            dicData: [
              {
                label: '插针',
                value: '1',
              },
              {
                label: '壳体',
                value: '2',
              },
              {
                label: '石墨模',
                value: '5',
              },
              {
                label: '自制玻璃饼',
                value: '3',
              },
              {
                label: '其他',
                value: '4',
              },
            ],
          },
          {
            label: '修改人',
            prop: 'updateUserStr',
            search: false,
            sortable: true,
            overHidden: true,
          },
          {
            label: '修改时间',
            prop: 'updateTime',
            search: false,
            sortable: true,
            overHidden: true,
          },
        ],
      },
      loading: false,
      defaultProps: {
        children: 'children',
        label: 'label',
      },
      version: '', //版本号
      versionOptions: [], //
      plateList: [], //镀种信息
      projectOptions: [],
      standardList: [], //检验标准
      tabLoading: false, //加载状态
    };
  },
  mounted() {
    this.setCrewShow = this.isOpen;
    if (this.detailsType == 'processPlanning') {
      this.version = this.rowData.partVersion;
      this.partChange(this.rowData.id);
    }
    this.getCcraftInfo();
    this.getPartInfo();
    this.getPlatingList();
    this.getStandardList();

    //
  },
  methods: {
    getStandardList() {
      getStandardList().then(res => {
        this.standardList = res.data.data;
      });
    },
    getProject() {
      getProject({ size: 999999, current: 1 }).then(res => {
        this.projectOptions = res.data.data.records;
      });
    },
    getPlatingList() {
      getPlatingList().then(res => {
        this.plateList = res.data.data;
      });
    },
    // 获取零件号的版本号列表
    partChange(id) {
      getVersion({ partId: id }).then(res => {
        this.versionOptions = res.data.data;
      });
    },
    // 切换版本号
    versionChange() {
      this.tabLoading = true;
      this.getCcraftInfo();
      this.getPartInfo();
    },
    // 组织树点击
    handleNodeClick(nodes, node, self) {
      if (nodes.disabled) {
        return false;
      }
      this.treeNodes = nodes;
      this.treeLeave = node.level;

      // 清空现有表格数据
      this.form1.tableData1 = [];
      this.form2.tableData2 = [];
      this.form3.tableData3 = [];
      this.form4.tableData4 = [];
      // 根据节点层级切换标签页
      if (this.treeLeave === 2) {
        this.activeName = '1';
        this.form1.tableData1 = nodes.processList;
        console.log('默认点击选中第一个数据', this.treeNodes);
        this.treeNodes.rawData.prority = Number(this.treeNodes.rawData.prority);
      }
      // 根据节点层级加载对应数据
      if (this.treeLeave === 3) {
        this.activeName = '2';
        // 三级节点：工序级，加载工序相关数据
        const { processInfo, projectList, measuringToolList, moldToolList } = nodes.rawData;

        // 加载项目表格数据
        this.form2.tableData2 = projectList.map(item => ({
          trialNo: item.trialNo,
          projectName: item.projectName,
          projectStandard: item.projectStandard,
          proHours: item.proHours,
          prepareHours: item.prepareHours,
        }));

        // 加载量具表格数据
        this.form3.tableData3 = measuringToolList.map(item => ({
          trialNo: item.trialNo,
          testType: item.testType,
          tool: item.tool,
          toolSize: item.toolSize,
          toolInfo: item.toolInfo,
        }));

        // 加载工装表格数据
        this.form4.tableData4 = moldToolList.map(item => ({
          trialNo: item.trialNo,
          moldCode: item.moldCode,
          moldName: item.moldName,
          isPrint: item.isPrint,
          printNo: item.printNo,
          testFixtureNo: item.testFixtureNo,
        }));
      }
    },
    setDataLocal() {
      this.$setItem('零件信息信息验证', this.validateObj);
    },
    leavelChange(value) {
      this.tabLoading = true;
      this.getCcraftInfo();
      this.getPartInfo();
    },
    // ✅ 自动选中第一个子节点
    autoSelectFirstNode() {
      this.$nextTick(() => {
        if (!this.$refs || !this.$refs.tree) {
          console.warn('el-tree 组件未找到，跳过自动选中');
          return;
        }
        if (!this.data || this.data.length === 0) {
          console.log('树数据为空，不自动选中');
          return;
        }

        const rootNode = this.data[0];

        if (
          !rootNode.children ||
          !Array.isArray(rootNode.children) ||
          rootNode.children.length === 0
        ) {
          console.warn('根节点无子节点，跳过自动选中');
          return;
        }

        const firstChild = rootNode.children[0];
        // 4. 检查节点 ID
        if (!firstChild.id) {
          console.error('第一个子节点 ID 为空，请检查 generateId 函数');
          return;
        }

        // 5. 设置选中状态
        try {
          this.$nextTick(() => {
            this.currentNodeKey = firstChild.id
            this.$refs.tree.setCurrentKey([firstChild.id], false);
          });
        } catch (error) {
          console.error('setCurrentKey 执行失败:', error);
          return;
        }

        // 6. 触发节点点击，加载数据
        this.handleNodeClick(firstChild, {
          level: firstChild.level,
          parent: {
            data: rootNode,
            isRoot: false,
          },
        });
      });
    },
    // 获取工艺信息
    getCcraftInfo() {
      // 零件工艺管理-详情
      if (this.detailsType == 'processPlanning') {
        let query = {
          partCode: this.rowData.partCode,
          partVersion: this.version,
          rank: this.rank,
        };
        getProcessManagementAuthorized(query).then(res => {
          this.data = this.transformCraftTree(res.data.data);
          this.tabLoading = false;
          this.autoSelectFirstNode();
        });
      } else if (this.detailsType === 'taskProcessing') {
        let query_ = {};
        if (!this.rowData.partVersion) {
          query_ = {
            partId: this.partId,
            rank: this.rank,
            taskId: this.rowData.id,
            version: this.rowData.version,
          };
        } else {
          query_ = {
            version: this.rowData.partVersion,
            partCode: this.rowData.partCode,
            rank: this.rank,
            partId: this.partId,
          };
        }

        getProcessAuthorized(query_).then(res => {
          this.craftData = res.data.data;
          this.data = this.transformCraftTree(res.data.data);
          this.tabLoading = false;
          this.autoSelectFirstNode();
        });
      }
    },
    // 获取零件信息
    getPartInfo() {
      // 零件工艺管理-详情
      if (this.detailsType == 'processPlanning') {
        let query = {
          partCode: this.rowData.partCode,
          partVersion: this.version,
          rank: this.rank,
        };
        getPartManagementDetail(query).then(res => {
          this.formData = res.data.data.dsPartEntity;
          //  res.data.data.dsPartVOList=null
          this.dataTable = res.data.data.dsPartVOList == null ? [] : res.data.data.dsPartVOList;
          this.tabLoading = false;
          if (this.dataTable.length > 0) {
            this.dataTable.forEach(item => {
              item.totalArea = (item.quota * item.area).toFixed(5); // 保留5位小数（字符串）
            });
          }
          if (this.formData.isSintering != '1') {
            this.partType = '热表';
          } else {
            this.partType = '烧结';
          }
        });
      } else if (this.detailsType === 'taskProcessing') {
        let query_ = {};
        if (!this.rowData.partVersion) {
          query_ = { taskId: this.rowData.id, version: this.rowData.version };
        } else {
          query_ = { version: this.rowData.partVersion, partCode: this.rowData.partCode };
        }
        getPartDetail(query_).then(res => {
          this.formData = res.data.data.dsPartEntity;
          this.dataTable = res.data.data.dsPartVOList;
          this.version = res.data.data.partVersion;
          this.tabLoading = false;
          if (this.formData.isSintering != '1') {
            this.partType = '热表';
          } else {
            this.partType = '烧结';
          }

          if (this.dataTable.length > 0) {
            this.dataTable.forEach(item => {
              item.totalArea = (item.quota * item.area).toFixed(5); // 保留5位小数（字符串）
            });
          }
        });
      }
    },
    // 组织树转换
    transformCraftTree(responseData) {
      const { partInfo, craftList } = responseData;
      const partCode = partInfo.partCode;

      const generateId = (prefix, id, fallback) => {
        return id;
      };

      const formatCraftLabel = craftInfo => {
        return `${craftInfo.roamNo || '临时工艺'}-${craftInfo.craftNo || '工艺文件'}`;
      };

      const tree = [
        {
          id: generateId('part', partInfo.id, partCode),
          label: partCode,
          disabled: false,
          level: 1,
          rawData: {
            partCode: partInfo.partCode,
            partId: partInfo.id,
            rank: this.rank,
            version: partInfo.version,
            craftNo: 'C000000',
          },

          children: (craftList || []).map(craft => {
            const { craftInfo, processList } = craft;

            return {
              ...craftInfo,
              id: generateId('craft', craftInfo.id, craftInfo.craftNo),
              label: formatCraftLabel(craftInfo),
              level: 2,
              rawData: craftInfo,
              // ✅ 工序列表
              processList: (processList || []).map(process => ({
                ...process.processInfo,
                projectList: process.projectList || [],
                measuringToolList: process.measuringToolList || [],
                moldToolList: process.moldToolList || [],
              })),
              // ✅ 子节点（工序）
              children: (processList || []).map(process => {
                const { processInfo, projectList, measuringToolList, moldToolList } = process;

                return {
                  ...processInfo,
                  id: generateId('process', processInfo.id, processInfo.processNo),
                  label: processInfo.processNo + '-' + processInfo.processName,
                  level: 3,
                  rawData: {
                    processInfo,
                    projectList: projectList || [],
                    measuringToolList: measuringToolList || [],
                    moldToolList: moldToolList || [],
                  },
                  // ✅ 子节点（项目）
                  children: (projectList || []).map(project => ({
                    id: generateId('project', project.id, project.projectCode),
                    label: project.trialNo + '-' + project.projectName,
                    level: 4,
                    disabled: true,
                    rawData: project,
                  })),
                };
              }),
            };
          }),
        },
      ];

      return tree;
    },
    handleClick(value) {
      console.log(999999, value);
    },
    partSubRefLoad() {
      this.$refs.partSubRef.loadData(this.partId, true);
    },
    cancelClose(flag, dsPart) {
      if (flag) {
        this.cancel(true);
      } else {
        this.partId = dsPart.partId;
      }
    },
    cancel(refresh) {
      this.activeName = '1';
      this.setCrewShow = false;
      this.$emit('cancel', typeof refresh === 'boolean' && refresh);
    },
  },
};
</script>
<style lang="scss" scoped>
.tabs-container {
  position: relative;
}

.content-right {
  width: 50%;
  position: absolute;
  right: 0;
  top: 10px;
  /* 或者使用 flex: 0 0 200px; 根据需要调整宽度 */
  .version-label {
    float: right;
  }
  .version_select {
    float: right;
    height: 50px;
    display: flex;
    line-height: 35px;
    z-index: 99;
    position: relative;
    .el-select {
      flex: 1;
    }
  }
}
.box-card-two {
  height: calc(100vh - 200px);
}
.box-card {
  display: flex;
  flex-direction: row;
  border: 1px solid #ccc;

  .process-left {
    // padding: 20px;
    width: 240px;
    min-width: 240px;
    height: 100%;
    overflow-y: auto;
    overflow-x: hidden;
    border-right: 1px solid #ccc;

    .process-select {
      padding: 12px;
      background: #e4e7ed;

      :deep(.el-form-item) {
        margin-bottom: 0px !important;
      }
    }
  }

  .process-right {
    padding: 12px;
    flex: 1;
    border-left: 0;

    .table-btn {
      width: 100%;
      height: 50px;

      .table-btn-left {
        float: left;
      }

      .table-btn-right {
        float: right;
      }
    }
  }
}
:deep(.el-card__body) {
  width: 100%;
}
.clearfix {
  margin-bottom: 12px;
}
:deep(.el-table .el-table__body-wrapper .el-table__cell) {
  height: 36px !important;
  padding: 0 !important;
  line-height: 36px !important;
}
:deep(.header_height) {
  height: 36px !important; /* 设置你想要的高度 */
  line-height: 36px !important;
}
:deep(.el-form-item__label) {
  font-weight: 550 !important;
  // color: #333;
}
:deep(.el-tree-node.is-current > .el-tree-node__content) {
  background-color: rgba(40, 76, 135, 0.9) !important;

  .el-tree-node__label {
    color: #fff;
  }
}
:deep(.el-tree-node__label) {
  font-size: 14px;
}
:deep(.el-form-item) {
  margin-bottom: 0px !important;
}
</style>
