<template>
  <div class="page-container">
    <n-card>
      <!-- 搜索表单 -->
      <div class="search-form">
        <n-form inline :model="searchForm" label-placement="left">
          <n-form-item label="物料编码">
            <n-input v-model:value="searchForm.code" placeholder="请输入物料编码" clearable />
          </n-form-item>
          <n-form-item label="项目名称">
            <n-input v-model:value="searchForm.projectName" placeholder="请输入产品名称" clearable />
          </n-form-item>
          <n-form-item label="开始时间">
            <n-date-picker v-model:value="searchForm.beginTime" type="datetime" clearable />
          </n-form-item>
          <n-form-item label="结束时间">
            <n-date-picker v-model:value="searchForm.endTime" type="datetime" clearable />
          </n-form-item>
          <n-form-item>
            <n-space>
              <n-button type="primary" @click="handleSearch">
                <template #icon><n-icon><SearchOutline /></n-icon></template>
                搜索
              </n-button>
              <n-button @click="handleReset">
                <template #icon><n-icon><RefreshOutline /></n-icon></template>
                重置
              </n-button>
            </n-space>
          </n-form-item>
        </n-form>
      </div>

      <!-- 工具栏 -->
      <div class="table-toolbar">
        <n-space>
          <!--<n-button
           size="small"
           type="primary" 
           @click="handleAdd"
          >
            <template #icon><n-icon><AddOutline /></n-icon></template>
            新增/补单
          </n-button>
           <n-button @click="importModalVisible = true">
            <template #icon><n-icon><CloudUploadOutline /></n-icon></template>
            导入
          </n-button>
          <n-button @click="handleExport">
            <template #icon><n-icon><DownloadOutline /></n-icon></template>
            导出{{ selectedIds.length > 0 ? `(${selectedIds.length})` : '' }}
          </n-button> 
           <n-button type="error" :disabled="selectedIds.length === 0" @click="handleBatchDelete">
            <template #icon><n-icon><TrashOutline /></n-icon></template>
            删除
          </n-button> -->
        </n-space>
      </div>

      <!-- 表格 -->
       <!--
        :row-key="(row) => row.id"
        :scroll-x="1200"
        @update:page="handlePageChange"
        @update:page-size="handlePageSizeChange"
        @update:checked-row-keys="handleCheck"
        :pagination="pagination"
         -->
        <!--flex-height-->
      <n-data-table
        size="small"
        remote
         striped
        :border="false"
        :single-line="false"
        :columns="columns"
        :data="tableData"
        :loading="loading"
      />
      <div class="pagination-container" style="display: flex; justify-content: flex-end; margin-top: 12px">
        <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-card>

    <!-- 新增/编辑弹窗 -->
    <n-modal 
        v-model:show="modalVisible" 
        preset="card" 
        :title="modalTitle" 
        style="width: 900px"
        :mask-closable="false"
    >
      <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="productionCode">
                <n-input v-if="formData.id" v-model:value="formData.productionCode" placeholder="请输入生产令号" />
                <n-input v-else v-model:value="formData.productionCode" @input="gongxu" placeholder="请输入生产令号" />
            </n-form-item>
          </n-gi> -->
          <n-gi>
            <n-form-item label="项目名称" path="project">
                <n-input  v-if="formData.id" v-model:value="formData.projectName" disabled placeholder="请选择项目" />
                <n-select
                  v-else   
                  v-model:value="formData.projectId" 
                  filterable
                  placeholder="请选择项目"
                  :options="xmlist"
                  :loading="xmloading"
                  clearable
                  remote
                  :clear-filter-after-select="false"
                  @update:value="xmselupdate"
                />
            </n-form-item>
          </n-gi>
          <n-gi>
            <n-form-item label="物料编码" path="code">
                <n-input v-if="formData.id" v-model:value="formData.code" disabled placeholder="请选择物料编码" />
                <!-- <n-input v-else v-model:value="formData.code" placeholder="请选择物料编码" /> -->
                <!-- @search="wlhand"-->
                <n-select
                  v-else   
                  v-model:value="formData.code"
                  filterable
                  placeholder="请选择物料编码"
                  :options="wllist"
                  :loading="wlloading"
                  clearable
                  remote
                  :clear-filter-after-select="false"
                  @update:value="gongxu"
                 
                />
            </n-form-item>
          </n-gi>
          <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="sort">
                <n-input v-if="formData.id" v-model:value="formData.sort" placeholder="请输入产品序列" />
                <n-input v-else v-model:value="formData.sort" placeholder="请输入产品序列" />
            </n-form-item>
          </n-gi>
          <n-gi>
            <n-form-item label="生产数量" path="quantity">
             <n-input v-if="formData.id" v-model:value="formData.quantity" disabled placeholder="请输入生产数量" />
             <n-input v-else v-model:value="formData.quantity" placeholder="请输入生产数量" disabled />
            </n-form-item>
          </n-gi>
          <n-gi>
            <n-form-item label="订单编号" path="orderCode">
              <n-input v-model:value="formData.orderCode" placeholder="订单编号" :disabled="!formData.id" />
            </n-form-item>
          </n-gi>
          <n-gi>
            <n-form-item label="工艺路线编码" path="routeCode">
              <n-input v-model:value="formData.routeCode" placeholder="工艺路线编码" :disabled="!formData.id" />
            </n-form-item>
          </n-gi>
          <n-gi>
            <n-form-item label="生产车间" path="proWorkshop">
              <n-input v-model:value="formData.proWorkshop" placeholder="生产车间" :disabled="!formData.id" />
            </n-form-item>
          </n-gi>
          
          <n-gi>
            <n-form-item label="是否半成品" path="sfProduct">
                <n-radio-group v-model:value="formData.sfProduct" name="radiogroup">
                  <n-space>
                    <n-radio :value="1">是</n-radio>
                    <n-radio :value="0">否</n-radio>
                  </n-space>
                </n-radio-group>
            </n-form-item>
          </n-gi>
          <n-gi>
            <n-form-item label="是否采购" path="procurement">
                <!-- <n-input v-model:value="formData.procurement" placeholder="请输入是否采购, 1是 0不是" /> -->
                <n-radio-group v-model:value="formData.procurement" name="radiogroup">
                  <n-space>
                    <n-radio :value="1">是</n-radio>
                    <n-radio :value="0">否</n-radio>
                  </n-space>
                </n-radio-group>
            </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="assingWorkList">
                <!-- <n-input v-model:value="formData.assingWorkId" placeholder="请选择派工接收人" /> -->
                <n-select
                  multiple
                  filterable
                  v-model:value="formData.assingWorkList"
                  placeholder="请输入用户名称搜索"
                  :options="yglist"
                  :loading="loadingRef"
                  clearable
                  remote
                  :clear-filter-after-select="false"
                  @search="slehand"
                />
            </n-form-item>
          </n-gi>
        </n-grid>
      </n-form>
      <template v-if="planList.length > 0">
        <n-divider title-placement="left" size="small">可拖动工序排序</n-divider>
      <VueDraggable 
        v-model="planList" 
        target=".sort-target" 
        :animation="150"
        @end="tuozChange"
      >
        <table style="width: 100%;text-align: center;">
          <thead>
            <tr style="height: 30px;background: #e5e7eb;">
              <th>订单名称</th>
              <th>工序编码</th>
              <th>工序名称</th>
              <th>开始时间</th>
              <th>结束时间</th>
            </tr>
          </thead>
          <tbody class="sort-target">
            <!-- <gxitem 
              v-for="(n,i) in planList" 
              :obj="n" 
              :index="i"
              @update="gxupdata"  
            /> -->
            <tr  
              v-for="(n,i) in planList" 
              :key="i" 
              class="cursor-move" 
              :style="{
                height: '40px',
                background:isEven(i)?'#fff':'#f3f3f3'
              }"
            >
              <td>{{ n.code }}{{n.sort}}</td>
              <td>{{ n.name }}</td>
              <td>{{ n.orderItemName }}</td>
              <td>
                <n-date-picker 
                  v-model:value="n.beginTime" 
                  type="datetime"
                  style="width: 100%;" 
                />
              </td>
              <td>
                <n-date-picker 
                  v-model:value="n.endTime" 
                  type="datetime"
                  style="width: 100%;"  
                />
              </td>
            </tr>
          </tbody>
        </table>
      </VueDraggable>
      </template>

      <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: 400px"
      :mask-closable="false"
    >
      <n-form
        ref="dispatchformRef"
        :model="dispatchform"
        :rules="dispatchrules"
        label-placement="top"
        label-width="80"
      >
          <n-form-item label="工单名称">
            <n-input v-model:value="dispatchform.name" disabled  />
          </n-form-item>
          <n-form-item label="指派员工" path="assingWorkList">
            <!-- <n-select 
              v-model:value="dispatchform.userid" 
              :options="yglist"
              clearable
              placeholder="请选择员工" 
            /> -->
            <!--v-model:value="dispatchform.assingWorkList"-->
             <n-select
              multiple
              filterable
              placeholder="请输入用户名称搜索"
              :options="yglist"
              :loading="loadingRef"
              clearable
              remote
              :clear-filter-after-select="false"
              @search="slehand"
              @update:value="updatesle"
            />

              <!-- <n-input v-model:value="dispatchform.userid" placeholder="请选择员工" /> -->
            </n-form-item>
      </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"
            :disabled="dispatchddbtn"
          >确定</n-button>
        </n-space>
      </template>
    </n-modal>
     <n-modal
      v-model:show="kingdeeModalVisible"
      preset="card"
      :title="kingdeeModalTitle"
      class="kingdee-modal"
      :style="{ width: '96vw', maxWidth: '1920px' }"
      :content-style="{ padding: '16px 24px', minHeight: '72vh' }"
      :mask-closable="false"
      :closable="true"
      :close-on-esc="false"
    >
      <KingdeeVue 
        :kingdeeTableData="kingdeeTableData" 
        :selectedId="kingdeeOrderOtemId"  
        @close-modal="kingdeeModalVisible = false" 
        @holdTemporarilyCallback="holdTemporarilySave" 
        @synchronizationCallback="synchronizationSave"
        @reCrawlCallback="reCrawlReorderForm"/>
    </n-modal>
    
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, h,  onMounted } from 'vue'
import { NButton, NSpace, NIcon, useMessage, useDialog, type UploadCustomRequestOptions} from 'naive-ui'
import { orderItemApi, type OrderItem } from '@/api/orderItem'
import { useUserStore } from '@/stores/user'
import { VueDraggable  } from 'vue-draggable-plus'

import  KingdeeVue from '@/views/biz/kingdee/index.vue'

import { type KingdeePrdMo, type KingdeeProcessRoute } from '@/api/orderProject'
import { userApi } from '@/api/system'

const message = useMessage()
const dialog = useDialog()

const userStore = useUserStore()


function isEven(num:any) {
  return num % 2 == 0
}

// 权限检查
const hasPermission = (permission: string) => userStore.hasPermission(permission)

// 搜索表单
const searchForm = reactive<any>({
  code: '',
  projectName:  '',
  orderCode: '',
  routeCode: '',
  proWorkshop: '',
  beginTime:  null as number | null,
  endTime:  null as number | null,
})



// 表格数据
const tableData = ref<any>([])
const loading = ref(false)
const selectedIds = ref<any>([])
const pagination = reactive({
  page: 1,
  pageSize: 10,
  itemCount: 0,
  showSizePicker: true,
  pageSizes: [10, 20, 50]
})



// 弹窗
const modalVisible = ref(false)
const modalTitle = ref('')
const importModalVisible = ref(false)
const formRef = ref()
const defaultFormData = {
  id: '',
  productionCode:'',
  name: '',
  code: '',
  sort: '',
  quantity: '',
  projectId: '',
  projectName:'',
  parentId: '',
  sfProduct: 0,
  procurement: 0,
  beginTime:  null,
  endTime:  null,
  actualStartTime:  null,
  actualEndTime:  null,
  assingWorkId: '',
  assingWorkList:[],
  assingWorkTime:  null, 
  assingWorkOperationId: '',
  assingWorkOperationTime:  null,
  starter: '',
  orderCode: '',
  routeCode: '',
  proWorkshop: '',
}
const formData = reactive<any>({ ...defaultFormData })

// 字典选项（下拉框/单选框/复选框关联字典时使用）

// 表单校验规则
const formRules = {
}

// 表格列
const columns = [
  { 
    type: 'selection' 
  },
  { 
    align:'center',
    title: '产品名称', 
    key: 'projectName',
    // ellipsis: {
    //   tooltip: true
    // } 
     minWidth: 150,
  },
  {
    align:'center',
    title: '物料名称', 
    key: 'name',
     minWidth: 150, 
  },
  {
    align:'center',
    title: '物料编码', 
    key: 'code',
    width:"120" 
  },
  {
    align:'center',
    title: '订单编号',
    key: 'orderCode',
    width:"120" 
  },
  {
    align:'center',
    title: '工艺路线编码',
    key: 'routeCode',
    width:"120" 
  },
  {
    align:'center',
    title: '生产车间',
    key: 'proWorkshop',
    width:"120" 
  },
  { 
    align:'center',
    title: '产品序列', 
    key: 'sort',
    width:"120"  
  },
  {
    align:'center',
    title: '生产数量', 
    key: 'quantity',
    width:"120"  
  },
  {
    align:'center',
    title: '是否半成品', 
    key: 'sfProduct',
    width:"120"  
  },
  {
    align:'center',
    title: '是否采购', 
    key: 'procurement',
    width:"120"  
  },
  {
    align:'center',
    title: '开始时间', 
    key: 'beginTime',
    width:"120"  
  },
  {
    align:'center',
    title: '结束时间', 
    key: 'endTime',
    width:"120"  
  },
  {
    align:'center',
    title: '实际开始时间', 
    key: 'actualStartTime',
    width:"120"  
  },
  {
    align:'center',
    title: '实际结束时间', 
    key: 'actualEndTime',
    width:"120"  
  },
  { 
    align:'center',
    title: '派工操作人',
    key: 'assingWorkOperationName',
    width:"120"  
  },
  {
    align:'center',
    title: '派工接收人', 
    key: 'assingWorkName',
    width:"200"  
  },
  {
    align:'center',
    title: '派工时间', 
    key: 'assingWorkTime',
    width:"120"  
  },
  {
    align:'center',
    title: '是否开工', 
    key: 'starter',
    width:"120"  
  },
  { 
    align:'center',
    title: '备注', 
    key: 'remark', 
    minWidth: 100,
  },
  {
    align:'center',
    title: '操作',
    key: 'actions',
    width: 190,
    fixed:"right",
    render(row:any) {

      const buttons:any = []
      
          if(hasPermission('biz:orderItem:edit')){
              buttons.push(h(NButton, { 
              size: 'small',
              type:'primary',  
              ghost:true,
              onClick: () => { handleEdit(row) } }, 
              { default:  () =>  '编辑'}
            ))
          }
          if(hasPermission('biz:orderItem:assign')){
            buttons.push(h(NButton, { 
              size: 'small',
              type:'success',
              ghost:true,
              onClick: () => { disphand(row) } }, 
              { default:  () =>  '派工'}
            ))
          }
           if(hasPermission('biz:orderItem:reorderForm')){
              buttons.push(h(NButton, { 
              size: 'small',
              type:'info',  
              ghost:true,
              onClick: () =>  handleReorderForm(row, false) },  //, disabled: row.starter !==1 
              { default:  () =>  '补单'}
            ))
          }

      return buttons.length > 0 ? h(NSpace, {justify:'center'}, { default: () => buttons }) : '-'


      // return h('div', { style: { display: 'flex', alignItems: 'center', gap: '8px', flexWrap: 'nowrap' } }, [
      //   h(NButton, { size: 'small', quaternary: true, onClick: () => handleEdit(row) }, {
      //     default: () => [h(NIcon, null, { default: () => h(CreateOutline) }), ' 编辑']
      //   }),
      //   h(NButton, { size: 'small', quaternary: true, type: 'error', onClick: () => handleDelete(row) }, {
      //     default: () => [h(NIcon, null, { default: () => h(TrashOutline) }), ' 派工']
      //   })
      // ])
    }
  }
]

//根据内容设置表格滚动条
let scrollX = ref(0)

function updateScrollX() {
  const tableElement = document.querySelector('#my-table .n-data-table-table')
  if (tableElement) {
    // 使用 scrollWidth 可以获取表格内容的总宽度，确保滚动条出现
    scrollX.value = tableElement.scrollWidth
  } else {
    scrollX.value = 2000;
  }
};

// 加载数据
async function loadData() {
  loading.value = true
  try {
    const res = await orderItemApi.page({
      page: pagination.page,
      pageSize: pagination.pageSize,
      code: searchForm.code,
      projectName: searchForm.projectName,
      orderCode: searchForm.orderCode,
      routeCode: searchForm.routeCode,
      proWorkshop: searchForm.proWorkshop,
      beginTime: searchForm.beginTime,
      endTime: searchForm.endTime
    })
    tableData.value = res.list
    pagination.itemCount = res.total
  } finally {
    loading.value = false
  }
}

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

// 重置
function handleReset() {
  searchForm.code = ''
  searchForm.projectName = ''
  searchForm.orderCode = ''
  searchForm.routeCode = ''
  searchForm.proWorkshop = ''
  searchForm.beginTime =  null
  searchForm.endTime =  null
  handleSearch()
}

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

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

// 选择
function handleCheck(keys: Array<string | number>) {
  selectedIds.value = keys as number[]
}

//工序
//根据生产令号查询工序
let planList = ref<any>([])
 function gongxu(){
  if(formData.code){
    planList.value.splice(0)
    orderItemApi.preCreation({
      code:formData.code,
      projectCode:formData.projectCode
    }).then((rps:any) => {
        planList.value = rps.planList ? rps.planList : []
        formData.orderCode = rps.orderCode ?? ''
        formData.routeCode = rps.routeCode ?? ''
        formData.proWorkshop = rps.proWorkshop ?? ''
        if (rps.quantity != null) formData.quantity = rps.quantity
    })
  }else{
    planList.value.splice(0)
    formData.orderCode = ''
    formData.routeCode = ''
    formData.proWorkshop = ''
  }
 } 

function tuozChange() {
  console.log(planList.value)
}

// function gxupdata(v:any) {
//   console.log(planList.value,'planList')
//   console.log(v,'gx')
// }


// 新增
function handleAdd() {
  modalTitle.value = '新增生产订单'
  
  Object.assign(formData, defaultFormData)
  modalVisible.value = true
  xmhand()
  planList.value.splice(0)
}

//查询项目
let xmlist = ref<any>([]) 
let xmloading = ref(false) 


async function xmhand() {
  xmloading.value = true
  xmlist.value = await orderItemApi.xmoptions({});
  xmloading.value = false
}

let wllist = ref<any>([])
function  xmselupdate(v:any,opt:any) {
  planList.value.splice(0)
  wllist.value.splice(0)
  formData.code = ''
  formData.orderCode = ''
  formData.routeCode = ''
  formData.proWorkshop = ''
  formData.projectName = opt.label
  formData.projectCode = opt?.additionParams?.projectCode || ''
  if(v){
    wlhand(opt.value)
  }
}

//根据项目查询物料

let wlloading = ref(false)  
async function wlhand(v:any) {
  wlloading.value = true
  wllist.value = await orderItemApi.wloptions({projectId:v });
  wlloading.value = false
}

const kingdeeOrderOtemId = ref<number | null>(null) // 记录当前正在查询的订单ID
const kingdeeModalVisible = ref(false)
const kingdeeModalTitle = ref('金蝶生产订单')
const kingdeeLoading = ref(false)

type KingdeeMoTableRow = Omit<KingdeePrdMo, 'children'> & { processRoutes: KingdeeProcessRoute[] }
const kingdeeTableData = ref<KingdeeMoTableRow[]>([])
// const kingdeeExpandedKeys = ref<Array<string | number>>([])

async function reCrawlReorderForm(id:number,reCrawl:Boolean){
  try {
    const data = await orderItemApi.preCreationInfo(id, {reCrawl:reCrawl})
    kingdeeTableData.value = (Array.isArray(data) ? data : []).map((mo) => {
      const { children, ...rest } = mo
      const routes = [...(children ?? [])].sort((a, b) => (a.operNumber ?? 0) - (b.operNumber ?? 0)).map((r) => ({
        ...r,
        planStartTime: r.planStartTime ?? null,
        planFinishTime: r.planFinishTime ?? null,
      }))
      return { ...rest, processRoutes: routes }
    })
  } finally {
    kingdeeLoading.value = false
  }
}

async function handleReorderForm(row:OrderItem, reCrawl:Boolean){
  if (!row.id) {
    message.warning('缺少物料订单ID，无法查询')
    return
  }
  kingdeeOrderOtemId.value = row.id
  kingdeeModalTitle.value = `金蝶生产订单${row.code ? ` - ${row.code}` : ''}`
  kingdeeModalVisible.value = true
  kingdeeLoading.value = true
  reCrawlReorderForm(row.id, false)
  kingdeeLoading.value = false
}

// 暂存金蝶生产订单数据（草稿）
async function holdTemporarilySave(id: number, data: KingdeePrdMo[]) {
  try {
    // 恢复数据结构，将 processRoutes 转换为 children
    await orderItemApi.holdTemporarilySave(id, data)
  } catch (error) {
    // 错误拦截器已处理
  } 
}

// const synchronizationSave = async (id: number, data: KingdeePrdMo[], callback:Function) => {
//   try {
//     await orderItemApi.synchronizationSave(id, data)  .then((rps:any) =>{
      
//     }).catch((res) => {
//        callback(res)
//     })
//     // 接口拿到数据，调用子的回调，回传结果
//   } catch (err) {
//     callback(err)
//   }
// }

// 同步订单和计划表
async function synchronizationSave(id: number, data: KingdeePrdMo[], callback:Function) {
  try {
    // 恢复数据结构，将 processRoutes 转换为 children
     await orderItemApi.synchronizationSave(id, data)
     .then((rps:any) =>{
       callback(rps)
    }).catch((res) => {
       callback(res)
    })
  } catch (error) {
    // 错误拦截器已处理
  } 
}



// 编辑
function handleEdit(row: OrderItem) {
  modalTitle.value = '编辑生产订单'
  modalVisible.value = true
  planList.value.splice(0)
  Object.assign(formData, row)
  if(formData.sfProduct == null){
    formData.sfProduct = 0
  }
  if(formData.procurement == null){
    formData.procurement = 0
  }

  if(formData.assingWorkId){
    formData.assingWorkList = formData.assingWorkId.split(',')
  }

  if (formData.beginTime && typeof formData.beginTime === 'string') {
    formData.beginTime = new Date(formData.beginTime.replace(' ', 'T')).getTime()
  }
  if (formData.endTime && typeof formData.endTime === 'string') {
    formData.endTime = new Date(formData.endTime.replace(' ', 'T')).getTime()
  }
  if (formData.actualStartTime && typeof formData.actualStartTime === 'string') {
    formData.actualStartTime = new Date(formData.actualStartTime.replace(' ', 'T')).getTime()
  }
  if (formData.actualEndTime && typeof formData.actualEndTime === 'string') {
    formData.actualEndTime = new Date(formData.actualEndTime.replace(' ', 'T')).getTime()
  }
  if (formData.createTime && typeof formData.createTime === 'string') {
    formData.createTime = new Date(formData.createTime.replace(' ', 'T')).getTime()
  }
  if (formData.assingWorkTime && typeof formData.assingWorkTime === 'string') {
    formData.assingWorkTime = new Date(formData.assingWorkTime.replace(' ', 'T')).getTime()
  }
  if (formData.assingWorkOperationTime && typeof formData.assingWorkOperationTime === 'string') {
    formData.assingWorkOperationTime = new Date(formData.assingWorkOperationTime.replace(' ', 'T')).getTime()
  }

  slehand()

}

// 提交
async function handleSubmit() {
  
  await formRef.value?.validate()
  try {
    const submitData = { ...formData } as OrderItem
    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 (typeof submitData.actualStartTime === 'number') {
      submitData.actualStartTime = new Date(submitData.actualStartTime).toISOString().slice(0, 19).replace('T', ' ')
    }
    if (typeof submitData.actualEndTime === 'number') {
      submitData.actualEndTime = new Date(submitData.actualEndTime).toISOString().slice(0, 19).replace('T', ' ')
    }
    if (typeof submitData.createTime === 'number') {
      submitData.createTime = new Date(submitData.createTime).toISOString().slice(0, 19).replace('T', ' ')
    }
    if (typeof submitData.assingWorkTime === 'number') {
      submitData.assingWorkTime = new Date(submitData.assingWorkTime).toISOString().slice(0, 19).replace('T', ' ')
    }
    if (typeof submitData.assingWorkOperationTime === 'number') {
      submitData.assingWorkOperationTime = new Date(submitData.assingWorkOperationTime).toISOString().slice(0, 19).replace('T', ' ')
    }

    

    if (submitData.id) {
      await orderItemApi.update(submitData)
      message.success('修改成功')
    } else {
      let savedata = Object.assign(
      {},
      submitData,
      {
        planList:planList.value
      }
    )
      await orderItemApi.create(savedata)
      message.success('新增成功')
    }
    modalVisible.value = false
    loadData()
  } catch (error) {
    // 错误已在拦截器处理
  }
}

//派工
let dispatchmodal = ref(false) 
let dispatchform = reactive<any>({
  id:'',
  name:'',
  //userid:'',
  username:'',
  assingWorkList:''
}) 

let yglist = ref<any>([])

const dispatchrules = {
  assingWorkList:[{ required: true, message: '请选择员工', trigger: 'blur' }]
}

function updatesle(v:any) {
  dispatchform.assingWorkList = v.join()
  console.log(v)
}

let dispatchLoading = ref(false) 
let dispatchddbtn = ref(false)

const dispatchformRef = ref()

let loadingRef = ref(false)

function slehand(v?:any) {
  loadingRef.value = true
  userApi.page({
      page: 1,
      pageSize: 200,
      username: v,
    }).then((rps:any) =>{
      if(rps.list && rps.list.length > 0){
        yglist.value = rps.list.map((n:any) => {
          return {
            label:n.username,
            value:n.id+''
          }
        })
      }
      console.log(yglist.value,'12')
      loadingRef.value = false
    }).catch(() => {
      loadingRef.value = false
    })
}

function disphand(v:any){
  dispatchmodal.value = true
  dispatchformRef.value?.restoreValidation()
  dispatchform.id = v.id
  dispatchform.name = v.name
  //dispatchform.userid = ''
  dispatchform.assingWorkList = ''
  slehand()
}

function dispatchSubmit() {
  
  dispatchformRef.value?.validate((v:any) => {
    if(!v){
      // dispatchLoading.value = true
      // dispatchddbtn.value = true
      // message.success('操作成功!')
      //   setTimeout(()=> {
      //     dispatchmodal.value = false
      //     dispatchform.value = false
      //     dispatchLoading.value = false
      //     dispatchddbtn.value = false
      //     loadData()
      //   },1000)
      orderItemApi.assignWork(Object.assign({},dispatchform,{assingWorkList:dispatchform.assingWorkList.split(',')})  ).then(() => {
        message.success('操作成功!')
        setTimeout(()=> {
          dispatchmodal.value = false
          dispatchLoading.value = false
          dispatchddbtn.value = false
          loadData()
        },1000)
      }).catch(() => {
        dispatchddbtn.value = false
        dispatchLoading.value = false
      })
    }
  })
}

// 删除
function handleDelete(row: OrderItem) {
  dialog.warning({
    title: '提示',
    content: '确定要删除该记录吗？',
    positiveText: '确定',
    negativeText: '取消',
    onPositiveClick: async () => {
      try {
        await orderItemApi.delete([row.id!])
        message.success('删除成功')
        loadData()
      } catch (error) {
        // 错误已在拦截器处理
      }
    }
  })
}

// 批量删除
function handleBatchDelete() {
  dialog.warning({
    title: '提示',
    content: `确定要删除选中的 ${selectedIds.value.length} 条记录吗？`,
    positiveText: '确定',
    negativeText: '取消',
    onPositiveClick: async () => {
      try {
        await orderItemApi.delete(selectedIds.value)
        message.success('删除成功')
        selectedIds.value = []
        loadData()
      } catch (error) {
        // 错误已在拦截器处理
      }
    }
  })
}

// 导出
async function handleExport() {
  try {
    const params: Record<string, any> = {}
    if (selectedIds.value.length > 0) params.ids = selectedIds.value
    if (searchForm.code) params.code = searchForm.code
    if (searchForm.projectName) params.projectName = searchForm.projectName
    if (searchForm.orderCode) params.orderCode = searchForm.orderCode
    if (searchForm.routeCode) params.routeCode = searchForm.routeCode
    if (searchForm.proWorkshop) params.proWorkshop = searchForm.proWorkshop
    if (searchForm.beginTime != null) params.beginTime = searchForm.beginTime
    if (searchForm.endTime != null) params.endTime = searchForm.endTime
    const blob = await orderItemApi.export(params)
    const url = window.URL.createObjectURL(blob)
    const link = document.createElement('a')
    link.href = url
    link.download = '生产订单数据.xlsx'
    link.click()
    window.URL.revokeObjectURL(url)
  } catch (error) {
    // 错误已在拦截器处理
  }
}

// 下载导入模板
async function handleDownloadTemplate() {
  try {
    const blob = await orderItemApi.downloadTemplate()
    const url = window.URL.createObjectURL(blob)
    const link = document.createElement('a')
    link.href = url
    link.download = '生产订单导入模板.xlsx'
    link.click()
    window.URL.revokeObjectURL(url)
  } catch (error) {
    // 错误已在拦截器处理
  }
}

// 导入上传
async function handleImportUpload({ file }: UploadCustomRequestOptions) {
  if (!file.file) return
  try {
    const result = await orderItemApi.importData(file.file)
    if (result.fail > 0) {
      dialog.warning({
        title: '导入结果',
        content: `成功: ${result.success} 条，失败: ${result.fail} 条\n错误信息: ${(result.errors || []).join('\n') || '无'}`,
        positiveText: '确定'
      })
    } else {
      message.success(`导入成功，共 ${result.success} 条数据`)
      importModalVisible.value = false
    }
    loadData()
  } catch (error) {
    // 错误已在拦截器处理
  }
}

// 加载字典选项
async function loadDictOptions() {
}

onMounted(() => {
  loadData()
  loadDictOptions()

  updateScrollX()
  window.addEventListener('resize', updateScrollX)
})

</script>

<style scoped>
.search-form {
  margin-bottom: 16px;
}

.table-toolbar {
  margin-bottom: 16px;
}
</style>
