<template>
  <div class="box-card">
    <div class="process-left">
      <el-tree
        style="width: 240px"
        :data="data"
        :props="defaultProps"
        @node-click="handleNodeClick"
        :default-expand-all="true"
        :expand-on-click-node="false"
      />
    </div>
    <div class="process-right">
      <el-tabs v-model="activeName" class="demo-tabs" @tab-click="handleClick">
        <el-tab-pane label="工序" name="1" v-if="treeLeave == 1"></el-tab-pane>
        <el-tab-pane label="项目" name="2" v-if="treeLeave == 2"></el-tab-pane>
        <el-tab-pane label="尺寸、量具" name="3" v-if="treeLeave == 2"></el-tab-pane>
        <el-tab-pane label="工装、模具表" name="4" v-if="treeLeave == 2"></el-tab-pane>
      </el-tabs>
      <div class="table-btn">
        <div class="table-btn-left">
          <div class="table-btn-left">
            <el-button type="primary" plain @click="addTable()">插入一行</el-button>
            <el-button type="danger" plain @click="delTable()">删除选择行</el-button>
          </div>
        </div>
        <div class="table-btn-right" v-if="activeName == '1'">
          <div class="table-btn-right-box">
            <el-select
              v-model="modelLevel"
              placeholder="选择工艺模板"
              style="width: 220px; margin-right: 12px"
            >
              <el-option
                v-for="(item, index) in modelOption"
                :key="index"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
            <el-button type="primary" @change="modelLevelChange()">导入模板</el-button>
          </div>
          <div class="table-btn-right-box">
            <el-select
              v-model="processLevel"
              placeholder="选择零件"
              style="width: 220px; margin-right: 12px"
            >
              <el-option label="模板一" value="1" />
              <el-option label="模板二" value="2" />
              <el-option label="模板三" value="3" />
            </el-select>
            <el-button type="primary" @click="getProcessList()">零件导入</el-button>
          </div>
          <!-- <div class="table-btn-right-box">
            <el-button type="primary">历史工序</el-button>
          </div> -->
        </div>
      </div>
      <el-form
        ref="tableForm1"
        :model="form1"
        :rules="formRules1"
        label-width="0px"
        v-if="activeName == '1'"
      >
        <el-table :data="form1.tableData1" style="width: 100%" v-if="activeName == '1'">
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column prop="date" label="返工序号">
            <template #header>
              <span><i style="color: red">*</i>返工序号</span>
            </template>
            <template #default="scope">
              <el-form-item
                :prop="`tableData1[${scope.$index}].processName`"
                :rules="formRules1.processName"
              >
                <el-input
                  v-model="scope.row.processNo"
                  placeholder="请输入"
                  style="width: 100%"
                ></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column prop="date" label="工序号">
            <template #header>
              <span><i style="color: red">*</i>工序号</span>
            </template>
            <template #default="scope">
              <el-form-item
                :prop="`tableData1[${scope.$index}].processName`"
                :rules="formRules1.processName"
              >
                <el-input
                  v-model="scope.row.processNo"
                  placeholder="请输入"
                  style="width: 100%"
                ></el-input>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column prop="name" label="工序">
            <template #header>
              <span><i style="color: red">*</i>工序</span>
            </template>
            <template #default="scope">
              <el-form-item
                :prop="`tableData1[${scope.$index}].processName`"
                :rules="formRules1.processName"
              >
                <el-select v-model="scope.row.processName" placeholder="请选择">
                  <el-option
                    v-for="(item, index) in processSetOption"
                    :key="index"
                    label="是"
                    value="1"
                  ></el-option>
                </el-select>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column prop="address" label="工艺能力">
            <template #header>
              <span><i style="color: red">*</i>工艺能力</span>
            </template>
            <template #default="scope">
              <el-form-item
                :prop="`tableData1[${scope.$index}].craftName`"
                :rules="formRules1.craftName"
              >
                <el-select v-model="scope.row.craftName" placeholder="请选择">
                  <el-option
                    v-for="(item, index) in craftAbilityOption"
                    :key="index"
                    label="是"
                    value="1"
                  ></el-option>
                </el-select>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column prop="address" label="加工班组">
            <template #default="scope">
              <el-select v-model="scope.row.value" placeholder="请选择">
                <el-option label="是" value="1"></el-option>
                <el-option label="否" value="2"></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column prop="address" label="外协厂商">
            <template #default="scope">
              <el-select v-model="scope.row.value" placeholder="请选择">
                <el-option label="是" value="1"></el-option>
                <el-option label="否" value="2"></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column prop="address" label="主工序">
            <template #header>
              <span><i style="color: red">*</i>主工序</span>
            </template>
            <template #default="scope">
              <el-form-item
                :prop="`tableData1[${scope.$index}].craftName`"
                :rules="formRules1.craftName"
              >
                <el-select v-model="scope.row.value" placeholder="请选择">
                  <el-option label="是" value="1"></el-option>
                  <el-option label="否" value="2"></el-option>
                </el-select>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column prop="address" label="工序描述">
            <template #header>
              <span><i style="color: red">*</i>工序描述</span>
            </template>
            <template #default="scope">
              <el-form-item :prop="`tableData1[${scope.$index}].proDes`" :rules="formRules1.proDes">
                <el-input
                  v-model="scope.row.proDes"
                  placeholder="请输入"
                  style="width: 100%"
                ></el-input>
              </el-form-item>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <el-table :data="form2.tableData2" style="width: 100%" v-if="activeName == '2'">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="date" label="工序号">
          <template #default="scope">
            <el-input
              v-model="scope.row.trialNo"
              placeholder="请输入"
              style="width: 100%"
            ></el-input>
          </template>
        </el-table-column>
        <el-table-column prop="date" label="检验项目">
          <template #header>
            <span><i style="color: red">*</i>检验项目</span>
          </template>
          <template #default="scope">
            <el-form-item
              :prop="`tableData2[${scope.$index}].projectName`"
              :rules="formRules2.projectName"
            >
              <el-select v-model="scope.row.projectName" placeholder="请选择">
                <el-option label="是" value="1"></el-option>
              </el-select>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column prop="date" label="检验标准">
          <template #header>
            <span><i style="color: red">*</i>检验标准</span>
          </template>
          <template #default="scope">
            <el-form-item
              :prop="`tableData2[${scope.$index}].projectStandard`"
              :rules="formRules2.projectStandard"
            >
              <el-input
                v-model="scope.row.projectStandard"
                placeholder="请输入"
                style="width: 100%"
              ></el-input>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column prop="date" label="标准值">
          <template #default="scope">
            <el-input
              v-model="scope.row.projectStandard"
              placeholder="请输入"
              style="width: 100%"
            ></el-input>
          </template>
        </el-table-column>
      </el-table>
      <el-table :data="form3.tableData3" style="width: 100%" v-if="activeName == '3'">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="date" label="工序号">
          <template #default="scope">
            <el-input
              v-model="scope.row.trialNo"
              placeholder="请输入"
              style="width: 100%"
            ></el-input>
          </template>
        </el-table-column>
        <el-table-column prop="date" label="镀前镀后">
          <template #header>
            <span><i style="color: red">*</i>镀前镀后</span>
          </template>
          <template #default="scope">
            <el-form-item
              :prop="`tableData3[${scope.$index}].testType`"
              :rules="formRules3.testType"
            >
              <el-select v-model="scope.row.testType" placeholder="请选择">
                <el-option label="是" value="1"></el-option>
                <el-option label="否" value="2"></el-option>
              </el-select>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column prop="date" label="量具">
          <template #header>
            <span><i style="color: red">*</i>量具</span>
          </template>
          <template #default="scope">
            <el-form-item :prop="`tableData3[${scope.$index}].tool`" :rules="formRules3.tool">
              <el-select v-model="scope.row.tool" placeholder="请选择">
                <el-option label="是" value="1"></el-option>
                <el-option label="否" value="2"></el-option>
              </el-select>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column prop="date" label="量具尺寸">
          <template #default="scope">
            <el-input
              v-model="scope.row.toolSize"
              placeholder="请输入"
              style="width: 100%"
            ></el-input>
          </template>
        </el-table-column>
        <el-table-column prop="date" label="图纸尺寸">
          <template #default="scope">
            <el-input
              v-model="scope.row.toolInfo"
              placeholder="请输入"
              style="width: 100%"
            ></el-input>
          </template>
        </el-table-column>
      </el-table>
      <el-table :data="form4.tableData4" style="width: 100%" v-if="activeName == '4'">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="date" label="工序号">
          <template #default="scope">
            <el-input
              v-model="scope.row.trialNo"
              placeholder="请输入"
              style="width: 100%"
            ></el-input>
          </template>
        </el-table-column>
        <el-table-column prop="date" label="对应工装">
          <template #header>
            <span><i style="color: red">*</i>对应工装</span>
          </template>
          <template #default="scope">
            <el-form-item
              :prop="`tableData4[${scope.$index}].moldCode`"
              :rules="formRules4.moldCode"
            >
              <el-input
                v-model="scope.row.moldCode"
                placeholder="请输入"
                style="width: 100%"
              ></el-input>
            </el-form-item>
          </template>
        </el-table-column>
        <el-table-column prop="date" label="工装名称">
          <template #default="scope">
            <el-input
              v-model="scope.row.moldName"
              placeholder="请输入"
              style="width: 100%"
            ></el-input>
          </template>
        </el-table-column>
        <el-table-column prop="date" label="是否印字">
          <template #default="scope">
            <el-input
              v-model="scope.row.isPrint"
              placeholder="请输入"
              style="width: 100%"
            ></el-input>
          </template>
        </el-table-column>
        <el-table-column prop="date" label="印字版编号">
          <template #default="scope">
            <el-input
              v-model="scope.row.printNo"
              placeholder="请输入"
              style="width: 100%"
            ></el-input>
          </template>
        </el-table-column>
        <el-table-column prop="date" label="测试夹具号">
          <template #default="scope">
            <el-input
              v-model="scope.row.testFixtureNo"
              placeholder="请输入"
              style="width: 100%"
            ></el-input>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
import {
  getList,
  getPartDetail,
  getProcessAuthorized,
  getModelList,
  getModelDetailList,
  reworkAuthorizedAccomplish,
  getProcessList
} from '@/api/processManagement/taskProcessing';

export default {
  name: 'CraftIndex',
  components: {},
  props: {
    height: {
      type: Number,
      default: 600,
    },
    partId: {
      type: [Number, String],
      default: null,
    },
    dialogType: {
      type: String,
      default: '',
    },
    updateRow: {
      type: Object,
      default: {},
    },
  },
  data() {
    return {
      processLevel: null,
      data: [
        {
          label: '返工单号',
          disabled: true,
          children: [
            {
              label: '工序列表',
              children: [
                {
                  label: '检验项目',
                  disabled: true,
                },
              ],
            },
          ],
        },
      ],
      defaultProps: {
        children: 'children',
        label: 'label',
      },
      form: {
        zhongyaodu: '',
        qualityGrade: '', //生产标识
        beizhu: '', //备注
      },
      rules: {
        zhongyaodu: [{ required: true, message: '请选择', trigger: 'blur' }],
      },
      priorityList: [
        {
          label: '本次使用',
          value: 1,
        },
        {
          label: '正式工艺',
          value: 2,
        },
        {
          label: '临时工艺',
          value: 3,
        },
      ],
      rankList: [
        {
          label: '一级',
          value: 1,
        },
        {
          label: '二级',
          value: 2,
        },
        {
          label: '三级',
          value: 3,
        },
      ],
      activeName: '1',
      treeLeave: 1, //组织树选中层级

      modelLevel: null,
      modelOption: [], //工艺模板列表数据
      partInfoData: {}, //零件信息

      form1: {
        tableData1: [], //工序
      },
      form2: {
        tableData2: [], //项目
      },
      form3: {
        tableData3: [], //量具
      },
      form4: {
        tableData4: [], //工装
      },
      formRules1: {
        processName: [{ required: true, message: '请选择', trigger: ['change', 'submit'] }],
        craftName: [{ required: true, message: '请选择', trigger: ['change', 'submit'] }],
        proDes: [{ required: true, message: '请选择', trigger: ['change', 'submit'] }],
        proHours: [{ required: true, message: '请输入', trigger: ['change', 'submit'] }],
        isMain: [{ required: true, message: '请选择', trigger: ['change', 'submit'] }],
      },
      formRules2: {
        projectName: [{ required: true, message: '请选择', trigger: ['change', 'submit'] }],
        projectStandard: [{ required: true, message: '请输入', trigger: ['change', 'submit'] }],
        proHours: [{ required: true, message: '请输入', trigger: ['change', 'submit'] }],
      },
      formRules3: {
        testType: [{ required: true, message: '请选择', trigger: ['change', 'submit'] }],
        tool: [{ required: true, message: '请选择', trigger: ['change', 'submit'] }],
      },
      formRules4: {
        moldCode: [{ required: true, message: '请输入', trigger: ['change', 'submit'] }],
      },
    };
  },
  mounted() {
    this.getDetails();
    this.getModelList();
  },
  methods: {
    // 工艺模板列表
    getModelList() {
      getModelList().then(res => {
        this.modelOption = res.data.data;
      });
    },
    // 导入模板
    modelLevelChange() {
      getModelDetailList({ modelId: this.modelLevel }).then(res => {
      });
    },
    // 导入零件信息
      getProcessList(){
          getProcessList().then(res=>{

          })
      },
    // 获取零件信息详情数据
    getDetails() {
      getProcessAuthorized({ partId: this.partId, taskId: this.updateRow.id }).then(res => {
        this.data = this.transformCraftTree(res.data.data);
        this.partInfoData = res.data.data.partInfo;
      });
    },
    // 组织树转换
    transformCraftTree(responseData) {
      const { partInfo, craftList } = responseData;
      const partCode = partInfo.partCode;

      // 构建树形结构并保留原始数据引用
      const tree = [
        {
          label: partCode,
          disabled: true,
          rawData: partInfo, // 保存零件原始数据
          children: craftList.map(craft => {
            const { craftInfo, processList } = craft;
            // 拼接二级节点标签：roamNo-craftNo
            const craftLabel = `${craftInfo.roamNo}-${craftInfo.craftNo}`;

            return {
              ...craftInfo,
              label: craftLabel,
              processList: processList.map(process => {
                return process.processInfo;
              }),
              rawData: craftInfo, // 保存工艺原始数据
              children: processList.map(process => {
                const { processInfo, projectList, measuringToolList, moldToolList } = process;
                // 三级节点：工序名称
                const processLabel = processInfo.processName;

                return {
                  ...processInfo,
                  label: processLabel,
                  rawData: {
                    processInfo,
                    projectList,
                    measuringToolList,
                    moldToolList,
                  }, // 保存工序及关联数据
                  children: projectList.map(project => ({
                    label: project.projectName, // 四级节点：项目名称
                    rawData: project, // 保存项目原始数据
                    disabled: true,
                  })),
                };
              }),
            };
          }),
        },
      ];

      return tree;
    },

    // getDetails() {
    //   if (this.partId) {
    //     getPartDetail({ partId: this.partId }).then((res) => {
    //       this.form = Object.assign({}, this.form, res.data);
    //     });
    //   }
    // },
    // 组织树点击
    handleNodeClick(nodes, node, self) {
      if (nodes.disabled && node.level == 3) {
        // 阻止默认的点击行为
        return false;
      }
      this.treeLeave = node.level;
      if (this.treeLeave == 2) {
        this.activeName = '2';
      }
      if (this.treeLeave == 1) {
        this.activeName = '1';
      }
    },
    // 右侧tab切换
    handleClick(value) {
      console.log(999999, value);
    },
    // 切换等级
    leavelChange() {
      this.$message.success('保存成功');
    },
    // 插入一行
    addTable() {
      if (this.activeName == '1') {
        this.form1.tableData1.push({});
      }
      if (this.activeName == '2') {
        this.form2.tableData2.push({});
      }
      if (this.activeName == '3') {
        this.form3.tableData3.push({});
      }
      if (this.activeName == '4') {
        this.form4.tableData4.push({});
      }
    },
    // 删除选择行
    delTable() {
      if (this.activeName == '1') {
      }
      if (this.activeName == '2') {
      }
      if (this.activeName == '3') {
      }
      if (this.activeName == '4') {
      }
    },
    submit(){
      let query_={
        craft:{},//工艺信息
        reworkProcessVOList:[],//工序集合
        reworkProcessDeleteIds:[],//工序删除id
        processProjectDeleteIds:[],//项目删除id
        processMeasuringToolDeleteIds:[],//量具删除id
        processMoldToolDeleteIds:[],//工装删除合集
      }
      // reworkAuthorizedAccomplish(query_).then(res=>{
      //   console.log(9999,res)
      // })
    }
  },
};
</script>
<style lang="scss" scoped>
.box-card {
  display: flex;
  flex-direction: row;
  height: calc(100vh - 200px);

  .process-left {
    // padding: 20px;
    border: 1px solid #ccc;

    .process-select {
      padding: 12px;
      background: #e4e7ed;
    }
  }
  :deep(.el-form-item) {
    margin-bottom: 0px !important;
  }
  .process-right {
    padding: 12px;
    flex: 1;
    border: 1px solid #ccc;
    border-left: 0;

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

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

      .table-btn-right {
        float: right;
        display: flex;
        margin-right: 40px;
        .table-btn-right-box {
          margin-right: 12px;
        }
      }
    }
  }
}
:deep(.el-table .el-table__cell) {
  height: 50px !important;
  padding: 0 !important;
  line-height: 50px !important;
}
</style>
