<template>
  <div class="plan-board-page">
    <!-- 顶部工具栏 -->
    <div class="board-toolbar">
      <n-space :size="8" align="center">
        <n-button type="primary" size="small" @click="handleAdd">
          <template #icon><n-icon><AddOutline /></n-icon></template>
          新增工序
        </n-button>
        <n-button size="small" type="success" ghost disabled title="待后端接口支持">
          <template #icon><n-icon><CheckmarkCircleOutline /></n-icon></template>
          齐套自动下达
        </n-button>
        <n-button size="small" type="error" ghost disabled title="待后端接口支持">批量删除</n-button>
        <n-button size="small" type="primary" ghost disabled title="待后端接口支持">批量下达</n-button>
        <n-button size="small" quaternary disabled title="待后端接口支持">合并为组工单</n-button>
        <n-dropdown :options="batchOptions" trigger="click" @select="onBatchSelect">
          <n-button size="small" quaternary>
            批量操作
            <n-icon style="margin-left: 4px"><ChevronDownOutline /></n-icon>
          </n-button>
        </n-dropdown>
      </n-space>
      <n-space :size="8" align="center">
        <n-button size="small" quaternary @click="importModalVisible = true">
          <template #icon><n-icon><CloudUploadOutline /></n-icon></template>
          导入
        </n-button>
        <n-button size="small" quaternary @click="handleExport">
          <template #icon><n-icon><DownloadOutline /></n-icon></template>
          导出
        </n-button>
        <n-button size="small" quaternary @click="loadData">
          <template #icon><n-icon><RefreshOutline /></n-icon></template>
          同步
        </n-button>
      </n-space>
    </div>

    <!-- 筛选 -->
    <div class="board-filter">
      <n-form inline :model="searchForm" label-placement="left" size="small">
        <n-form-item label="工序名称">
          <n-input v-model:value="searchForm.name" placeholder="工序名称" clearable style="width: 140px" />
        </n-form-item>
        <n-form-item label="工序编码">
          <n-input v-model:value="searchForm.code" placeholder="工序编码" clearable style="width: 140px" />
        </n-form-item>
        <n-form-item>
          <n-space>
            <n-button type="primary" size="small" @click="handleSearch">
              <template #icon><n-icon><SearchOutline /></n-icon></template>
              搜索
            </n-button>
            <n-button size="small" @click="handleReset">重置</n-button>
          </n-space>
        </n-form-item>
      </n-form>
    </div>

    <div class="board-table-wrap">
      <!-- 表头 -->
      <div class="board-table-head">
        <div class="col-check" />
        <div class="col-expand" />
        <div class="col-product">产品 / 工单编号</div>
        <div class="col-qty">数量</div>
        <div class="col-mode">模式</div>
        <div class="col-kit">齐套率</div>
        <div class="col-flow">工序</div>
        <div class="col-plan">计划开始 / 结束</div>
        <div class="col-life">生命周期</div>
        <div class="col-actions">操作</div>
      </div>

      <!-- 列表 -->
      <n-spin :show="loading">
      <div v-if="!tableData.length && !loading" class="board-empty">
        <n-empty description="暂无工序计划数据" />
      </div>

      <div v-for="order in tableData" :key="orderRowKey(order)" class="order-block">
        <div class="order-row" :class="{ expanded: isExpanded(order), 'derived-expanded': isDerivedExpanded(order) }">
          <div class="col-check">
            <n-checkbox
              :checked="selectedOrderIds.includes(order.orderItemId!)"
              @update:checked="(v: boolean) => toggleSelect(order.orderItemId!, v)"
            />
          </div>
          <div class="col-expand">
            <n-button
              quaternary
              circle
              size="tiny"
              title="展开返工单 / 拆分工单"
              @click="toggleDerivedExpand(order)"
            >
              <template #icon>
                <n-icon>
                  <RemoveOutline v-if="isDerivedExpanded(order)" />
                  <AddOutline v-else />
                </n-icon>
              </template>
            </n-button>
          </div>
          <div class="col-product">
            <div class="product-name">{{ order.materialName || '-' }}</div>
            <div class="product-code">
              <span>{{ formatWorkOrderCodes(order) }}</span>
              <n-tag v-if="isOverdue(order)" size="tiny" type="error" :bordered="false">逾期</n-tag>
              <n-tag v-if="hasOutsource(order)" size="tiny" type="warning" :bordered="false">委外</n-tag>
            </div>
          </div>
          <div class="col-qty">{{ order.quantity ?? '-' }}</div>
          <div class="col-mode">
            <n-tag v-if="order.routeCode" size="tiny" :bordered="false">工艺型</n-tag>
            <n-tag v-if="order.proWorkshop" size="tiny" type="info" :bordered="false">{{ order.proWorkshop }}</n-tag>
          </div>
          <div class="col-kit">-</div>
          <div class="col-flow" :class="{ 'flow-expanded': isExpanded(order) }">
            <ProcessFlowStepper
              :list="order.processList"
              clickable
              @click="toggleExpand(order)"
            />
          </div>
          <div class="col-plan">
            <div>{{ formatShortTime(order.beginTime) }}</div>
            <div class="sub-text">{{ formatShortTime(order.endTime) }}</div>
          </div>
          <div class="col-life">
            <div class="life-cell">
              <n-icon :color="lifeIcon(order).color" size="18">
                <component :is="lifeIcon(order).icon" />
              </n-icon>
              <span>{{ lifeLabel(order) }}</span>
            </div>
          </div>
          <div class="col-actions">
            <n-button text type="primary" size="tiny" @click="openDetail(order)">详情</n-button>
            <n-button text type="info" size="tiny" @click="openPlmModel(order)">模型</n-button>
            <n-button text type="primary" size="tiny" disabled title="订单级编辑待支持">编辑</n-button>
            <n-dropdown :options="moreOptions" trigger="click" @select="(k) => onMoreSelect(k, order)">
              <n-button text size="tiny">更多</n-button>
            </n-dropdown>
          </div>
        </div>

        <div v-if="isDerivedExpanded(order)" class="order-derived-expand">
          <div class="derived-section">
            <div class="derived-section-head">
              <span class="derived-section-title">返工单</span>
              <n-button text type="primary" size="tiny" @click="onMoreSelect('createRework', order)">创建返工单</n-button>
            </div>
            <div v-if="!getDerivedOrders(order).rework.length" class="derived-empty">暂无返工单</div>
            <div v-else class="derived-list">
              <div
                v-for="item in getDerivedOrders(order).rework"
                :key="item.id"
                class="derived-item"
              >
                <span class="derived-code">{{ item.code }}</span>
                <span class="derived-meta">{{ item.quantity ?? '-' }} · {{ item.statusLabel }}</span>
              </div>
            </div>
          </div>
          <div class="derived-section">
            <div class="derived-section-head">
              <span class="derived-section-title">拆分工单</span>
              <n-button text type="primary" size="tiny" @click="onMoreSelect('splitWorkOrder', order)">拆分工单</n-button>
            </div>
            <div v-if="!getDerivedOrders(order).split.length" class="derived-empty">暂无拆分工单</div>
            <div v-else class="derived-list">
              <div
                v-for="item in getDerivedOrders(order).split"
                :key="item.id"
                class="derived-item"
              >
                <span class="derived-code">{{ item.code }}</span>
                <span class="derived-meta">{{ item.quantity ?? '-' }} · {{ item.statusLabel }}</span>
              </div>
            </div>
          </div>
        </div>

        <div v-if="isExpanded(order)" class="order-expand">
          <div v-if="!(order.processList?.length)" class="expand-empty">暂无工序明细</div>
          <div v-else class="card-flow-scroll">
            <ProcessCard
              v-for="(proc, idx) in sortedProcessList(order.processList)"
              :key="proc.planId ?? idx"
              :process="proc"
              :material-code="order.materialCode"
              :order-code="order.orderCode"
              :order-begin-time="order.beginTime"
              :order-end-time="order.endTime"
              :pro-workshop="order.proWorkshop"
              :is-last="idx === (order.processList?.length ?? 0) - 1"
              :can-edit="hasPermission('biz:orderItem:edit')"
              :can-assign="hasPermission('biz:orderItem:assign')"
              :can-outsource="hasPermission('biz:orderProcessPlan:add')"
              @edit="(p) => handleEdit(p, order)"
              @assign="(p) => disphand(p, order)"
              @outsource="(p) => openOutsource(order, p)"
              @view-model="(p) => openPlmModel(order, p)"
              @submit-detail = "(p) => openDetailModel(order,p)"
            />
          </div>
        </div>
      </div>
      </n-spin>
    </div>

    <!--模型抽屉 -->
    <PlmModelDrawer ref="plmModelDrawerRef" />



    <div class="board-pagination">
      <n-pagination
        v-model:page="pagination.page"
        v-model:page-size="pagination.pageSize"
        :item-count="pagination.itemCount"
        :page-sizes="[10, 20, 50, 100]"
        show-size-picker
        show-quick-jumper
        @update:page="handlePageChange"
        @update:page-size="handlePageSizeChange"
      >
        <template #prefix>共 {{ pagination.itemCount }} 条</template>
      </n-pagination>
    </div>

    <!-- 详情弹窗 -->
    <n-modal v-model:show="detailVisible" preset="card" title="工序计划详情" style="width: 900px">
      <n-spin :show="detailLoading">
        <template v-if="detailData">
          <n-descriptions :column="3" bordered size="small" label-placement="left">
            <n-descriptions-item label="生产令号">{{ detailData.mainCode || '-' }}</n-descriptions-item>
            <n-descriptions-item label="工单号">{{ formatWorkOrderCodes(detailData) }}</n-descriptions-item>
            <n-descriptions-item label="物料">{{ detailData.materialName || '-' }}</n-descriptions-item>
            <n-descriptions-item label="数量">{{ detailData.quantity ?? '-' }}</n-descriptions-item>
            <n-descriptions-item label="工艺路线">{{ detailData.routeCode || '-' }}</n-descriptions-item>
            <n-descriptions-item label="车间">{{ detailData.proWorkshop || '-' }}</n-descriptions-item>
            <n-descriptions-item label="计划开工">{{ detailData.beginTime || '-' }}</n-descriptions-item>
            <n-descriptions-item label="计划完工">{{ detailData.endTime || '-' }}</n-descriptions-item>
            <n-descriptions-item label="订单状态">{{ lifeLabel(detailData) }}</n-descriptions-item>
          </n-descriptions>
          <div class="detail-cards">
            <ProcessCard
              v-for="(proc, idx) in (detailData.processList)"
              :key="proc.planId ?? idx"
              :process="proc"
              :material-code="detailData.materialCode"
              :order-code="detailData.orderCode"
              :order-begin-time="detailData.beginTime"
              :order-end-time="detailData.endTime"
              :pro-workshop="detailData.proWorkshop"
              :is-last="idx === (detailData.processList?.length ?? 0) - 1"
              :can-edit="hasPermission('biz:orderItem:edit')"
              :can-assign="hasPermission('biz:orderItem:assign')"
              :can-outsource="hasPermission('biz:orderProcessPlan:add')"
              :can-submitDetail="hasPermission('biz:submitLog:detail')"
              @edit="(p) => handleEdit(p, detailData!)"
              @assign="(p) => disphand(p, detailData!)"
              @outsource="(p) => openOutsource(detailData!, p)"
              @view-model="(p) => openPlmModel(detailData!, p)"
            />
          </div>
        </template>
      </n-spin>
    </n-modal>

    <!-- 新增/编辑 -->
    <n-modal v-model:show="modalVisible" preset="card" :title="modalTitle" style="width: 800px">
      <n-form ref="formRef" :model="formData" :rules="formRules" label-placement="left" label-width="120px">
        <n-grid :cols="2">
          <n-gi>
            <n-form-item label="工序名称" path="name">
              <n-input v-model:value="formData.name" placeholder="请输入工序名称" />
            </n-form-item>
          </n-gi>
          <n-gi>
            <n-form-item label="工序编码" path="code">
              <n-input v-model:value="formData.code" placeholder="请输入工序编码" />
            </n-form-item>
          </n-gi>
          <n-gi>
            <n-form-item label="工序顺序" path="sort">
              <n-input v-model:value="formData.sort" placeholder="请输入工序顺序" />
            </n-form-item>
          </n-gi>
          <n-gi>
            <n-form-item label="生产订单id" path="orderItemId">
              <n-input v-model:value="formData.orderItemId" placeholder="请输入生产订单id" />
            </n-form-item>
          </n-gi>
          <n-gi>
            <n-form-item label="生产开始时间" path="beginTime">
              <n-date-picker v-model:value="formData.beginTime" type="datetime" clearable style="width: 100%" />
            </n-form-item>
          </n-gi>
          <n-gi>
            <n-form-item label="生产结束时间" path="endTime">
              <n-date-picker v-model:value="formData.endTime" type="datetime" clearable style="width: 100%" />
            </n-form-item>
          </n-gi>
          <n-gi>
            <n-form-item label="生产总数" path="quantity">
              <n-input v-model:value="formData.quantity" placeholder="请输入生产总数" />
            </n-form-item>
          </n-gi>
          <n-gi>
            <n-form-item label="转入数量" path="transferNum">
              <n-input v-model:value="formData.transferNum" placeholder="请输入转入数量" />
            </n-form-item>
          </n-gi>
        </n-grid>
      </n-form>
      <template #footer>
        <n-space justify="end">
          <n-button @click="modalVisible = false">取消</n-button>
          <n-button type="primary" @click="handleSubmit">确定</n-button>
        </n-space>
      </template>
    </n-modal>

    <!-- 派工 -->
    <n-modal v-model:show="dispatchmodal" title="工序派工" preset="card" style="width: 800px" :mask-closable="false">
      <n-divider />
      <n-card>
        <n-grid :cols="2">
          <n-gi>
            <span class="pgClass">当前工序：</span>
            <span>{{dispatchform.name}}</span>
          </n-gi>
           <n-gi>
            <span class="pgClass">计划数量：</span>
            <span>{{dispatchform.quantity}}</span>
          </n-gi>
        </n-grid>
        <n-grid :cols="2">
          <n-gi>
            <span class="pgClass">已完成：</span>
            <span>{{dispatchform.completedQty ?? 0}}</span>
          </n-gi>
          <n-gi>
            <span class="pgClass">本次待派：</span>
            <span style="color: #2080f0; font-weight: 600">{{dispatchform.remainQty}}</span>
          </n-gi>
        </n-grid>
        <n-grid :cols="2">
          <n-gi>
            <span class="pgClass">开始时间：</span>
            <span>{{dispatchform.beginTime}}</span>
          </n-gi>
         <n-gi>
            <span class="pgClass">结束时间：</span>
            <span>{{dispatchform.endTime}}</span>
          </n-gi>
        </n-grid>
      </n-card>
      <n-form ref="dispatchformRef" :model="dispatchform"
       label-placement="left" 
       :rules="dispatchrules"
       style="margin-top: 10px;"
       label-width="80">
        <n-grid :cols="2">
          <n-gi :span="24">
            <Pgitem
              v-for="(n, i) in dispatchform.list"
              :key="i"
              :obj="n"
              :index="i"
              :section="dispatchform.workCenterName"
              listname="list"
              @addhand="addpgnum"
              @delehand="deletenum(i)"
            />
          </n-gi>
        </n-grid>
      </n-form>
      <template #footer>
        <n-space justify="end">
          <n-button size="small" @click="dispatchmodal = false">取消</n-button>
          <n-button size="small" type="primary" :loading="dispatchLoading" @click="dispatchSubmit">确定</n-button>
        </n-space>
      </template>
    </n-modal>

    <!-- 工序委外 -->
    <n-modal
      v-model:show="outsourceModalVisible"
      preset="card"
      :title="outsourceStep === 1 ? '创建工序委外 - 选择工序' : '创建工序委外 - 填写信息'"
      style="width: 720px"
      :mask-closable="false"
      @after-leave="resetOutsourceForm"
    >
      <n-spin :show="outsourceOptionsLoading">
        <template v-if="outsourceStep === 1 && outsourceOptions">
          <n-descriptions :column="2" bordered size="small" label-placement="left" style="margin-bottom: 16px">
            <n-descriptions-item label="生产令号">{{ outsourceOptions.mainCode || '-' }}</n-descriptions-item>
            <n-descriptions-item label="订单编码">{{ outsourceOptions.orderCode || '-' }}</n-descriptions-item>
            <n-descriptions-item label="物料编码">{{ outsourceOptions.materialCode || '-' }}</n-descriptions-item>
            <n-descriptions-item label="物料名称">{{ outsourceOptions.materialName || '-' }}</n-descriptions-item>
            <n-descriptions-item label="生产数量">{{ outsourceOptions.quantity ?? '-' }}</n-descriptions-item>
          </n-descriptions>
          <n-radio-group v-model:value="outsourceSelectedPlanId" style="width: 100%">
            <n-space vertical :size="8" style="width: 100%">
              <div
                v-for="proc in outsourceOptions.processList"
                :key="proc.planId"
                class="outsource-process-item"
                :class="{ active: outsourceSelectedPlanId === proc.planId }"
                @click="outsourceSelectedPlanId = proc.planId!"
              >
                <n-radio :value="proc.planId" />
                <div class="outsource-process-body">
                  <div class="outsource-process-title">
                    <span>{{ proc.processName || '-' }}</span>
                    <n-tag v-if="proc.current" size="tiny" type="info" :bordered="false">当前</n-tag>
                    <n-tag v-if="proc.outsource === 1" size="tiny" type="warning" :bordered="false">已委外</n-tag>
                  </div>
                  <div class="outsource-process-meta">
                    <span>工单号：{{ proc.workOrderCode || '-' }}</span>
                    <span>工序号：{{ proc.operNumber ?? '-' }}</span>
                    <span>数量：{{ proc.quantity ?? '-' }}</span>
                  </div>
                </div>
              </div>
            </n-space>
          </n-radio-group>
        </template>

        <template v-else-if="outsourceStep === 2">
          <n-card size="small" style="margin-bottom: 16px">
            <n-grid :cols="2" :x-gap="12">
              <n-gi>
                <span class="pgClass">委外工序：</span>
                <span>{{ selectedOutsourceProcess?.processName || '-' }}</span>
              </n-gi>
              <n-gi>
                <span class="pgClass">工单号：</span>
                <span>{{ selectedOutsourceProcess?.workOrderCode || '-' }}</span>
              </n-gi>
              <n-gi>
                <span class="pgClass">物料：</span>
                <span>{{ outsourceOptions?.materialName || '-' }}</span>
              </n-gi>
              <n-gi>
                <span class="pgClass">工序数量：</span>
                <span>{{ selectedOutsourceProcess?.quantity ?? '-' }}</span>
              </n-gi>
            </n-grid>
          </n-card>
          <n-form
            ref="outsourceFormRef"
            :model="outsourceForm"
            :rules="outsourceRules"
            label-placement="left"
            label-width="110"
          >
            <n-form-item label="供应商" path="supplierName">
              <n-input v-model:value="outsourceForm.supplierName" placeholder="请输入供应商名称" />
            </n-form-item>
            <n-form-item label="委外数量" path="quantity">
              <n-input-number
                v-model:value="outsourceForm.quantity"
                :min="1"
                :max="selectedOutsourceProcess?.quantity ?? undefined"
                :precision="0"
                placeholder="请输入委外数量"
                style="width: 100%"
              />
            </n-form-item>
            <n-form-item label="单价" path="unitPrice">
              <n-input-number
                v-model:value="outsourceForm.unitPrice"
                :min="0"
                :precision="2"
                placeholder="请输入单价（选填）"
                style="width: 100%"
              />
            </n-form-item>
            <n-form-item label="计划开始时间" path="planStartTime">
              <n-date-picker
                v-model:value="outsourceForm.planStartTime"
                type="datetime"
                clearable
                style="width: 100%"
              />
            </n-form-item>
            <n-form-item label="计划结束时间" path="planEndTime">
              <n-date-picker
                v-model:value="outsourceForm.planEndTime"
                type="datetime"
                clearable
                style="width: 100%"
              />
            </n-form-item>
            <n-form-item label="备注" path="remark">
              <n-input
                v-model:value="outsourceForm.remark"
                type="textarea"
                placeholder="请输入备注（选填）"
                :autosize="{ minRows: 2, maxRows: 4 }"
              />
            </n-form-item>
          </n-form>
        </template>
      </n-spin>
      <template #footer>
        <n-space justify="end">
          <n-button @click="outsourceModalVisible = false">取消</n-button>
          <n-button v-if="outsourceStep === 2" @click="outsourceStep = 1">上一步</n-button>
          <n-button
            v-if="outsourceStep === 1"
            type="primary"
            :disabled="!outsourceSelectedPlanId"
            @click="goOutsourceStep2"
          >下一步</n-button>
          <n-button
            v-else
            type="primary"
            :loading="outsourceSubmitLoading"
            @click="submitOutsource"
          >确认创建</n-button>
        </n-space>
      </template>
    </n-modal>

    <!-- 导入 -->
    <n-modal v-model:show="importModalVisible" preset="card" title="导入工序计划" style="width: 500px">
      <n-space vertical>
        <n-alert type="info">
          <template #header>导入说明</template>
          请先下载模板，按格式填写后上传 .xlsx / .xls 文件。
        </n-alert>
        <n-button type="primary" @click="handleDownloadTemplate">
          <template #icon><n-icon><DownloadOutline /></n-icon></template>
          下载模板
        </n-button>
        <n-upload :max="1" accept=".xlsx,.xls" :custom-request="handleImportUpload">
          <n-upload-dragger>
            <n-text>点击或拖拽文件到此处上传</n-text>
          </n-upload-dragger>
        </n-upload>
      </n-space>
    </n-modal>


    <!-- 汇报详情弹窗 -->
    <n-modal v-model:show="submitLogModalVisible" preset="card" title="导入工序计划" style="width: 500px">

    </n-modal>

  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted, h, computed, type Component } from 'vue'
import {
  NButton, NSpace, NIcon, NTag, NSpin, NModal, NForm, NFormItem, NGrid, NGi,
  NInput, NInputNumber, NDatePicker, NCheckbox, NEmpty, NPagination, NDropdown, NDescriptions,
  NDescriptionsItem, NAlert, NUpload, NText, NCard, NRadioGroup, NRadio, useMessage, useDialog,
  type UploadCustomRequestOptions, type FormRules,
} from 'naive-ui'
import {
  AddOutline, RemoveOutline, SearchOutline, RefreshOutline, CloudUploadOutline,
  DownloadOutline, ChevronDownOutline, CheckmarkCircleOutline, CheckmarkCircle,
  EllipseOutline, TimeOutline, PrintOutline, SyncOutline, PeopleOutline,
  GitBranchOutline, CloseCircleOutline, LockClosedOutline, TrashOutline,
} from '@vicons/ionicons5'
import {
  orderProcessPlanApi,
  toOrderProcessPlanPayload,
  processItemToEntity,
  ORDER_STATUS_MAP,
  type OrderProcessPlanVO,
  type ProcessPlanItemVO,
  type OutsourcingOptions,
  type OutsourcingProcessOption,
} from '@/api/orderProcessPlan'
import { useUserStore } from '@/stores/user'
import ProcessFlowStepper from './components/ProcessFlowStepper.vue'
import ProcessCard from './components/ProcessCard.vue'
import PlmModelDrawer from '@/components/PlmModelDrawer.vue'
import type { PlmModelOpenContext } from '@/api/plmModel'
import Pgitem from './pgitem.vue'
import { submitLogApi } from '@/api/submitLog.ts'



const message = useMessage()
const dialog = useDialog()
const userStore = useUserStore()
const hasPermission = (p: string) => userStore.hasPermission(p)

const searchForm = reactive({
  name: '',
  code: '',
})

const tableData = ref<OrderProcessPlanVO[]>([])
const expandedIds = ref<Set<number>>(new Set())
const derivedExpandedIds = ref<Set<number>>(new Set())
const selectedOrderIds = ref<number[]>([])
const loading = ref(false)
const pagination = reactive({ page: 1, pageSize: 10, itemCount: 0 })

const detailVisible = ref(false)
const detailLoading = ref(false)
const detailData = ref<OrderProcessPlanVO | null>(null)

const modalVisible = ref(false)
const modalTitle = ref('')
const formRef = ref()
const defaultFormData = {
  name: '', code: '', sort: '', orderItemId: '',
  beginTime: null as number | null, endTime: null as number | null,
  quantity: '', transferNum: '', transferOutNum: '',
  procurement: '', outsource: '', qualityInspection: '', storageEntry: '',
}
const formData = reactive<any>({ ...defaultFormData })
const formRules = {}

const dispatchmodal = ref(false)
const dispatchLoading = ref(false)
const dispatchformRef = ref()
const dispatchform = reactive<any>({
  id: '', name: '', beginTime: '', endTime: '', quantity: '', workCenterName: '',
  completedQty: 0,
  remainQty: 0,
  list: [{ sectionId: '', deviceId: '', userList: '', quantity: '' }],
})
const dispatchrules = {}

const importModalVisible = ref(false)
const plmModelDrawerRef = ref<InstanceType<typeof PlmModelDrawer> | null>(null)


const outsourceModalVisible = ref(false)
const outsourceOptionsLoading = ref(false)
const outsourceSubmitLoading = ref(false)
const outsourceStep = ref(1)
const outsourceOptions = ref<OutsourcingOptions | null>(null)
const outsourceSelectedPlanId = ref<number | null>(null)
const outsourceFormRef = ref()
const outsourceForm = reactive({
  supplierName: '',
  quantity: null as number | null,
  unitPrice: null as number | null,
  planStartTime: null as number | null,
  planEndTime: null as number | null,
  remark: '',
})
const outsourceRules: FormRules = {
  supplierName: [{ required: true, message: '请输入供应商', trigger: 'blur' }],
  quantity: [{ required: true, type: 'number', message: '请输入委外数量', trigger: ['blur', 'change'] }],
}

const selectedOutsourceProcess = computed<OutsourcingProcessOption | null>(() => {
  if (!outsourceSelectedPlanId.value) return null
  return outsourceOptions.value?.processList?.find(p => p.planId === outsourceSelectedPlanId.value) ?? null
})

function openPlmModel(order: OrderProcessPlanVO, process?: ProcessPlanItemVO) {
  const ctx: PlmModelOpenContext = {
    orderItemId: order.orderItemId,
    materialCode: order.materialCode,
    materialName: order.materialName,
    drawingNo: order.materialCode,
    title: process
      ? `三维模型 · ${process.processName || process.processCode || ''}`
      : `三维模型 · ${order.materialName || order.materialCode || ''}`,
  }
  if (process?.planId) ctx.processPlanId = process.planId
  plmModelDrawerRef.value?.open(ctx)
}

//汇报记录详情
const submitLogModalVisible = ref<Boolean>(false)


//汇报详情
function openDetailModel(order:ProcessPlanItemVO,procces?:OrderProcessPlanVO) {
 
  // submitLogModalVisible.value = true
  // const processId = procces.planId

}

const batchOptions = [
  { label: '全部展开', key: 'expandAll' },
  { label: '全部收起', key: 'collapseAll' },
]

function dropdownIcon(icon: Component) {
  return () => h(NIcon, null, { default: () => h(icon) })
}

function dangerDropdownItem(label: string, key: string, icon: Component) {
  return {
    label,
    key,
    icon: () => h(NIcon, { color: '#d03050' }, { default: () => h(icon) }),
    props: { style: { color: '#d03050' } },
  }
}

const moreOptions = [
  {
    type: 'group',
    label: '查看与编辑',
    key: 'group-view-edit',
    children: [
      { label: '打印', key: 'print', icon: dropdownIcon(PrintOutline) },
    ],
  },
  {
    type: 'group',
    label: '派生操作',
    key: 'group-derived',
    children: [
      { label: '创建返工单', key: 'createRework', icon: dropdownIcon(SyncOutline) },
      { label: '创建工序委外', key: 'createOutsource', icon: dropdownIcon(PeopleOutline) },
      { label: '拆分工单', key: 'splitWorkOrder', icon: dropdownIcon(GitBranchOutline) },
    ],
  },
  {
    type: 'group',
    label: '状态控制',
    key: 'group-status',
    children: [
      dangerDropdownItem('撤回', 'withdraw', CloseCircleOutline),
      dangerDropdownItem('冻结工单', 'freeze', LockClosedOutline),
    ],
  },
  {
    type: 'group',
    label: '危险操作',
    key: 'group-danger',
    children: [
      dangerDropdownItem('删除', 'delete', TrashOutline),
    ],
  },
]

function orderRowKey(row: OrderProcessPlanVO) {
  return row.orderItemId ?? `order-${row.mainCode}-${formatWorkOrderCodes(row)}`
}

/** 汇总展示订单下各道工序的主工单号 */
function formatWorkOrderCodes(order?: OrderProcessPlanVO | null) {
  const codes = (order?.processList ?? []).map((p) => p.workOrderCode).filter(Boolean)
  return codes.length ? codes.join('、') : '-'
}

function sortedProcessList(list?: ProcessPlanItemVO[]) {
  return [...(list ?? [])].sort((a, b) => (a.operNumber ?? 0) - (b.operNumber ?? 0))
}

function isExpanded(order: OrderProcessPlanVO) {
  return order.orderItemId != null && expandedIds.value.has(order.orderItemId)
}

function isDerivedExpanded(order: OrderProcessPlanVO) {
  return order.orderItemId != null && derivedExpandedIds.value.has(order.orderItemId)
}

function toggleExpand(order: OrderProcessPlanVO) {
  if (order.orderItemId == null) return
  const next = new Set(expandedIds.value)
  if (next.has(order.orderItemId)) next.delete(order.orderItemId)
  else next.add(order.orderItemId)
  expandedIds.value = next
}

function toggleDerivedExpand(order: OrderProcessPlanVO) {
  if (order.orderItemId == null) return
  const next = new Set(derivedExpandedIds.value)
  if (next.has(order.orderItemId)) next.delete(order.orderItemId)
  else next.add(order.orderItemId)
  derivedExpandedIds.value = next
}

/** 派生工单（返工 / 拆分），待后端字段接入后替换占位逻辑 */
function getDerivedOrders(order: OrderProcessPlanVO) {
  const raw = order as OrderProcessPlanVO & {
    reworkOrders?: Array<{ id: string | number; code?: string; quantity?: number; statusLabel?: string }>
    splitOrders?: Array<{ id: string | number; code?: string; quantity?: number; statusLabel?: string }>
  }
  return {
    rework: raw.reworkOrders ?? [],
    split: raw.splitOrders ?? [],
  }
}

function toggleSelect(id: number, checked: boolean) {
  if (checked) selectedOrderIds.value = [...selectedOrderIds.value, id]
  else selectedOrderIds.value = selectedOrderIds.value.filter(v => v !== id)
}

function formatShortTime(val?: string | null) {
  if (!val) return '-'
  return val.length > 16 ? val.slice(0, 16) : val
}

function isOverdue(order: OrderProcessPlanVO) {
  if (order.orderStatus === 2 || !order.endTime) return false
  return new Date(order.endTime.replace(' ', 'T')).getTime() < Date.now()
}

function hasOutsource(order: OrderProcessPlanVO) {
  return order.processList?.some(p => p.outsource === 1)
}

function lifeLabel(order: OrderProcessPlanVO) {
  const s = order.orderStatus
  if (s == null) return '-'
  return ORDER_STATUS_MAP[s]?.label ?? String(s)
}

function lifeIcon(order: OrderProcessPlanVO) {
  const s = order.orderStatus
  if (s === 2) return { icon: CheckmarkCircle, color: '#18a058' }
  if (s === 1) return { icon: TimeOutline, color: '#2080f0' }
  return { icon: EllipseOutline, color: '#8c8c8c' }
}

async function loadData() {
  loading.value = true
  try {
    const res = await orderProcessPlanApi.page({
      page: pagination.page,
      pageSize: pagination.pageSize,
      name: searchForm.name || undefined,
      code: searchForm.code || undefined,
    })
    tableData.value = res.list ?? []
    pagination.itemCount = res.total
  } finally {
    loading.value = false
  }
}

function handleSearch() {
  pagination.page = 1
  loadData()
}

function handleReset() {
  searchForm.name = ''
  searchForm.code = ''
  handleSearch()
}

function handlePageChange(page: number) {
  pagination.page = page
  loadData()
}

function handlePageSizeChange(pageSize: number) {
  pagination.pageSize = pageSize
  pagination.page = 1
  loadData()
}

function onBatchSelect(key: string) {
  if (key === 'expandAll') {
    expandedIds.value = new Set(tableData.value.map(o => o.orderItemId!).filter(Boolean))
  } else if (key === 'collapseAll') {
    expandedIds.value = new Set()
  }
}

function onMoreSelect(key: string, order: OrderProcessPlanVO) {
  if (key === 'createOutsource') {
    openOutsource(order)
    return
  }
  const labels: Record<string, string> = {
    print: '打印',
    createRework: '创建返工单',
    splitWorkOrder: '拆分工单',
    withdraw: '撤回',
    freeze: '冻结工单',
    delete: '删除',
  }
  if (labels[key]) message.info(`${labels[key]}功能待后端接口支持`)
}

function resolveDefaultPlanId(order: OrderProcessPlanVO, process?: ProcessPlanItemVO) {
  if (process?.planId) return process.planId
  const list = sortedProcessList(order.processList)
  if (!list.length) return null
  const current = list.find(p => p.operNumber === order.currentOperNumber)
  return current?.planId ?? list[0].planId ?? null
}

function resetOutsourceForm() {
  outsourceStep.value = 1
  outsourceOptions.value = null
  outsourceSelectedPlanId.value = null
  outsourceForm.supplierName = ''
  outsourceForm.quantity = null
  outsourceForm.unitPrice = null
  outsourceForm.planStartTime = null
  outsourceForm.planEndTime = null
  outsourceForm.remark = ''
  outsourceFormRef.value?.restoreValidation()
}

function fillOutsourceFormFromProcess(proc?: OutsourcingProcessOption | null) {
  if (!proc) return
  outsourceForm.quantity = proc.quantity ?? null
  outsourceForm.planStartTime = parseDateTime(proc.planStartTime)
  outsourceForm.planEndTime = parseDateTime(proc.planFinishTime)
}

async function openOutsource(order: OrderProcessPlanVO, process?: ProcessPlanItemVO) {
  const planId = resolveDefaultPlanId(order, process)
  if (!planId) {
    message.warning('当前订单暂无可委外工序')
    return
  }
  resetOutsourceForm()
  outsourceModalVisible.value = true
  outsourceOptionsLoading.value = true
  try {
    const options = await orderProcessPlanApi.getOutsourcingOptions(planId)
    outsourceOptions.value = options
    const preferredId = process?.planId ?? options.currentProcessPlanId ?? options.processList?.[0]?.planId ?? null
    outsourceSelectedPlanId.value = preferredId
    if (process?.planId) {
      outsourceStep.value = 2
      fillOutsourceFormFromProcess(selectedOutsourceProcess.value)
    }
  } catch {
    outsourceModalVisible.value = false
  } finally {
    outsourceOptionsLoading.value = false
  }
}

function goOutsourceStep2() {
  if (!outsourceSelectedPlanId.value) {
    message.warning('请选择委外工序')
    return
  }
  fillOutsourceFormFromProcess(selectedOutsourceProcess.value)
  outsourceStep.value = 2
}

function formatSubmitDateTime(ts: number | null) {
  if (!ts) return undefined
  return new Date(ts).toISOString().slice(0, 19).replace('T', ' ')
}

async function submitOutsource() {
  await outsourceFormRef.value?.validate()
  if (!outsourceSelectedPlanId.value) {
    message.warning('请选择委外工序')
    return
  }
  const maxQty = selectedOutsourceProcess.value?.quantity
  if (maxQty != null && (outsourceForm.quantity ?? 0) > maxQty) {
    message.error(`委外数量不能大于工序数量 ${maxQty}`)
    return
  }
  if (outsourceForm.planStartTime && outsourceForm.planEndTime
    && outsourceForm.planEndTime < outsourceForm.planStartTime) {
    message.error('计划结束时间不能早于计划开始时间')
    return
  }
  outsourceSubmitLoading.value = true
  try {
    await orderProcessPlanApi.createOutsourcing({
      processPlanId: outsourceSelectedPlanId.value,
      supplierName: outsourceForm.supplierName.trim(),
      quantity: outsourceForm.quantity!,
      unitPrice: outsourceForm.unitPrice ?? undefined,
      planStartTime: formatSubmitDateTime(outsourceForm.planStartTime),
      planEndTime: formatSubmitDateTime(outsourceForm.planEndTime),
      remark: outsourceForm.remark || undefined,
    })
    message.success('委外单创建成功')
    outsourceModalVisible.value = false
    loadData()
  } finally {
    outsourceSubmitLoading.value = false
  }
}

async function openDetail(order: OrderProcessPlanVO) {
  if (!order.orderItemId) return
  detailVisible.value = true
  detailLoading.value = true
  detailData.value = null
  try {
    detailData.value = await orderProcessPlanApi.detail(order.orderItemId)
  } catch {
    detailData.value = order
  } finally {
    detailLoading.value = false
  }
}

function handleAdd() {
  modalTitle.value = '新增工序计划'
  Object.assign(formData, defaultFormData)
  modalVisible.value = true
}

function parseDateTime(val: string | null | undefined) {
  if (!val) return null
  const ts = new Date(val.replace(' ', 'T')).getTime()
  return Number.isNaN(ts) ? null : ts
}

function handleEdit(process: ProcessPlanItemVO, order?: OrderProcessPlanVO) {
  modalTitle.value = '编辑工序计划'
  const entity = processItemToEntity(process, order?.orderItemId)
  Object.assign(formData, {
    ...defaultFormData,
    ...entity,
    sort: entity.sort != null ? String(entity.sort) : '',
    orderItemId: entity.orderItemId != null ? String(entity.orderItemId) : '',
    quantity: entity.quantity != null ? String(entity.quantity) : '',
    transferNum: entity.transferNum != null ? String(entity.transferNum) : '',
    beginTime: parseDateTime(entity.beginTime),
    endTime: parseDateTime(entity.endTime),
  })
  modalVisible.value = true
}

async function handleSubmit() {
  await formRef.value?.validate()
  const submitData = toOrderProcessPlanPayload({ ...formData })
  if (typeof submitData.beginTime === 'number') {
    submitData.beginTime = new Date(submitData.beginTime).toISOString().slice(0, 19).replace('T', ' ')
  }
  if (typeof submitData.endTime === 'number') {
    submitData.endTime = new Date(submitData.endTime).toISOString().slice(0, 19).replace('T', ' ')
  }
  if (submitData.id) {
    await orderProcessPlanApi.update(submitData)
    message.success('修改成功')
  } else {
    await orderProcessPlanApi.create(submitData)
    message.success('新增成功')
  }
  modalVisible.value = false
  loadData()
}

function disphand(process: ProcessPlanItemVO, order?: OrderProcessPlanVO) {
  const entity = processItemToEntity(process, order?.orderItemId)

  const planQty = Number(entity.quantity) || 0
  const completedQty = Number(process.completedQty) || 0
  const remainQty = Math.max(0, planQty - completedQty)
  dispatchmodal.value = true
  dispatchformRef.value?.restoreValidation()
  dispatchform.id = entity.id
  dispatchform.name = entity.name
  // 工序计划时间优先，没有则用订单头 beginTime/endTime
  dispatchform.beginTime = entity.beginTime || order?.beginTime || ''
  dispatchform.endTime = entity.endTime || order?.endTime || ''
  // quantity 保持工序计划总数，提交时不能改掉计划数量
  dispatchform.quantity = entity.quantity
  dispatchform.completedQty = completedQty
  dispatchform.remainQty = remainQty
  dispatchform.orderItemId = entity.orderItemId
  dispatchform.sort = entity.sort
  dispatchform.workCenterName = entity.workCenterName
  dispatchform.list = [{ sectionId: '', deviceId: '', userList: '', quantity: remainQty || '' }]
}

function addpgnum() {
  dispatchform.list.push({ sectionId: '', deviceId: '', userList: '', quantity: '' })
}

function deletenum(index: number) {
  dispatchform.list.splice(index, 1)
}

function dispatchSubmit() {
  let total = 0
  dispatchform.list.forEach((n: any) => {
    total += Number(n.quantity) || 0
  })
  const remainQty = Number(dispatchform.remainQty)
  if (remainQty <= 0) {
    message.warning('当前工序已全部完成，无需再派工')
    return
  }

  dispatchformRef.value?.validate((errors: any) => {
    // Naive UI：有 errors 表示校验失败
    if (errors) return
    if (total !== remainQty) {
      message.error(`派工总数量需等于待派数量 ${remainQty}（计划 ${dispatchform.quantity}，已完成 ${dispatchform.completedQty ?? 0}）`, {
        duration: 4000,
      })
      return
    }

    dispatchLoading.value = true
    // 提交时 quantity 仍传计划总数，避免把工序计划数量改成待派数
    orderProcessPlanApi.assignWork(dispatchform).then(() => {
      message.success('派工成功')
      dispatchmodal.value = false
      loadData()
    }).finally(() => { dispatchLoading.value = false })
  })
}

async function handleExport() {
  const blob = await orderProcessPlanApi.export({
    name: searchForm.name || undefined,
    code: searchForm.code || undefined,
  })
  const url = window.URL.createObjectURL(blob)
  const link = document.createElement('a')
  link.href = url
  link.download = '工序计划数据.xlsx'
  link.click()
  window.URL.revokeObjectURL(url)
}

async function handleDownloadTemplate() {
  const blob = await orderProcessPlanApi.downloadTemplate()
  const url = window.URL.createObjectURL(blob)
  const link = document.createElement('a')
  link.href = url
  link.download = '工序计划导入模板.xlsx'
  link.click()
  window.URL.revokeObjectURL(url)
}

async function handleImportUpload({ file }: UploadCustomRequestOptions) {
  if (!file.file) return
  const result = await orderProcessPlanApi.importData(file.file)
  if (result.fail > 0) {
    dialog.warning({
      title: '导入结果',
      content: `成功 ${result.success} 条，失败 ${result.fail} 条`,
      positiveText: '确定',
    })
  } else {
    message.success(`导入成功，共 ${result.success} 条`)
    importModalVisible.value = false
  }
  loadData()
}

onMounted(loadData)
</script>

<style scoped>
.plan-board-page {
  padding: 0;
  background: #f5f7fa;
  min-height: 100%;
}

.board-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  padding: 12px 16px;
  background: #fff;
  border-radius: 8px 8px 0 0;
  border-bottom: 1px solid #eef0f3;
}

.board-filter {
  padding: 12px 16px;
  background: #fff;
  border-bottom: 1px solid #eef0f3;
}

.board-table-wrap {
  overflow-x: auto;
  background: #fff;
}

.board-table-head,
.order-row {
  display: grid;
  /* 勾选 | 展开 | 产品工单 | 数量 | 模式 | 齐套率 | 工序轴 | 计划时间 | 生命周期 | 操作 */
  grid-template-columns:
    40px
    40px
    minmax(220px, 1.6fr)
    64px
    108px
    72px
    minmax(200px, 1.4fr)
    168px
    96px
    140px;
  align-items: center;
  gap: 8px;
  padding: 0 16px;
  min-width: 1320px;
}

.board-table-head {
  height: 40px;
  background: #fafbfc;
  border-bottom: 1px solid #e8eaed;
  font-size: 12px;
  font-weight: 600;
  color: #8c8c8c;
}

.order-block {
  background: #fff;
  border-bottom: 1px solid #eef0f3;
}

.order-row {
  min-height: 64px;
  transition: background 0.15s;
}

.order-row:hover {
  background: #f8faff;
}

.order-row.expanded {
  background: rgba(32, 128, 240, 0.04);
}

.order-row.derived-expanded {
  background: rgba(24, 160, 88, 0.04);
}

.order-row.expanded.derived-expanded {
  background: linear-gradient(90deg, rgba(24, 160, 88, 0.04) 0%, rgba(32, 128, 240, 0.04) 100%);
}

.col-check,
.col-expand {
  display: flex;
  justify-content: center;
  flex-shrink: 0;
}

.col-product {
  min-width: 0;
}

.col-qty,
.col-kit {
  text-align: center;
  font-size: 13px;
  white-space: nowrap;
}

.product-name {
  font-size: 14px;
  font-weight: 600;
  color: #1a1a1a;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.product-code {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-top: 4px;
  font-size: 12px;
  color: #8c8c8c;
}

.col-mode {
  display: flex;
  flex-direction: column;
  gap: 4px;
  align-items: flex-start;
}

.col-plan {
  font-size: 13px;
  line-height: 1.4;
  white-space: nowrap;
}

.col-plan .sub-text {
  font-size: 12px;
  color: #8c8c8c;
  margin-top: 2px;
}

.col-life {
  white-space: nowrap;
}

.col-flow {
  min-width: 0;
  overflow: hidden;
}

.col-flow.flow-expanded :deep(.flow-stepper) {
  background: rgba(32, 128, 240, 0.1);
}

.life-cell {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
}

.col-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-wrap: nowrap;
  white-space: nowrap;
}

.order-expand {
  padding: 12px 16px 16px 68px;
  background: linear-gradient(180deg, rgba(32, 128, 240, 0.03) 0%, #fff 100%);
  border-top: 1px dashed #e8eaed;
}

.order-derived-expand {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  padding: 12px 16px 12px 68px;
  background: linear-gradient(180deg, rgba(24, 160, 88, 0.03) 0%, #fff 100%);
  border-top: 1px dashed #e8eaed;
}

.derived-section {
  min-width: 0;
  padding: 10px 12px;
  background: #fff;
  border: 1px solid #eef0f3;
  border-radius: 8px;
}

.derived-section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 8px;
}

.derived-section-title {
  font-size: 13px;
  font-weight: 600;
  color: #1a1a1a;
}

.derived-empty {
  font-size: 12px;
  color: #8c8c8c;
  padding: 4px 0;
}

.derived-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.derived-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 8px;
  background: #fafbfc;
  border-radius: 6px;
  font-size: 12px;
}

.derived-code {
  font-weight: 500;
  color: #333;
}

.derived-meta {
  color: #8c8c8c;
  white-space: nowrap;
}

.card-flow-scroll,
.detail-cards {
  display: flex;
  align-items: flex-start;
  gap: 0;
  overflow-x: auto;
  padding-bottom: 8px;
}

.detail-cards {
  margin-top: 16px;
  padding-top: 8px;
  border-top: 1px solid #f0f0f0;
}

.expand-empty {
  font-size: 13px;
  color: #8c8c8c;
  padding: 8px 0;
}

.board-empty {
  padding: 48px;
  background: #fff;
}

.board-pagination {
  display: flex;
  justify-content: flex-end;
  padding: 12px 16px;
  background: #fff;
  border-radius: 0 0 8px 8px;
}
.pgClass {
  font-weight: bold;
}

.outsource-process-item {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  padding: 12px;
  border: 1px solid #e8e8e8;
  border-radius: 8px;
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s;
}

.outsource-process-item:hover,
.outsource-process-item.active {
  border-color: #2080f0;
  background: rgba(32, 128, 240, 0.04);
}

.outsource-process-body {
  flex: 1;
  min-width: 0;
}

.outsource-process-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
  font-weight: 600;
  color: #1a1a1a;
}

.outsource-process-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  margin-top: 6px;
  font-size: 12px;
  color: #8c8c8c;
}
</style>
