<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <Search :schema="PackageoverRetrospect.allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />
  </ContentWrap>

    <!-- 列表头部 -->
    <TableHead
      :HeadButttondata="HeadButttondata"
      @button-base-click="buttonBaseClick"
      :routeName="routeName"
      @updataTableColumns="updataTableColumns"
      @searchFormClick="searchFormClick"
      :allSchemas="PackageoverRetrospect.allSchemas"
    />

  <!-- 列表 -->
  <ContentWrap>
    <Table v-clientTable
      :columns="tableColumns"
      :data="tableObject.tableList"
      :loading="tableObject.loading"
      :pagination="{
        total: tableObject.total
      }"
      v-model:pageSize="tableObject.pageSize"
      v-model:currentPage="tableObject.currentPage"
      v-model:sort="tableObject.sort"
    >
      <template #number="{row}">
        <el-button type="primary" link @click="openDetail(row, '单据号', row.number)">
          <span>{{ row.number }}</span>
        </el-button>
      </template>
      <template #action="{ row }">
        <ButtonBase :Butttondata="[defaultButtons.mainListPointBtn(null)]" @button-base-click="buttonTableClick($event,row)" />
      </template>
    </Table>
  </ContentWrap>

  <!-- 详情 -->
  <Detail ref="detailRef" :isBasic="true" :allSchemas="PackageoverRetrospect.allSchemas" />

</template>

<script setup lang="ts">
import download from '@/utils/download'
import { getAccessToken } from '@/utils/auth'
import { PackageoverRetrospect } from './packageoverRetrospect.data'
import * as PackageoverRetrospectApi from '@/api/wms/packageoverRetrospect'
import * as defaultButtons from '@/utils/disposition/defaultButtons'
import * as PackageApi from '@/api/wms/package'
import { getJmreportBaseUrl } from '@/utils/systemParam'

defineOptions({ name: 'PackageoverRetrospect' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const route = useRoute() // 路由信息
const routeName = ref()
routeName.value = route.name
const tableColumns = ref(PackageoverRetrospect.allSchemas.tableColumns)

// 字段设置 更新主列表字段
const updataTableColumns = (val) => {
  tableColumns.value = val
}

const { tableObject, tableMethods } = useTable({
  getListApi: PackageoverRetrospectApi.getPackageoverRetrospectPage // 分页接口
})

// 获得表格的各种操作
const { getList, setSearchParams } = tableMethods

// 列表头部按钮
const HeadButttondata = [
  defaultButtons.defaultAddBtn({hasPermi:'wms:packageoverRetrospect:create'}), // 新增
  defaultButtons.defaultImportBtn({hasPermi:'wms:packageoverRetrospect:import'}), // 导入
  defaultButtons.defaultExportBtn({hasPermi:'wms:packageoverRetrospect:export'}), // 导出
  defaultButtons.defaultFreshBtn(null), // 刷新
  defaultButtons.defaultFilterBtn(null), // 筛选
  defaultButtons.defaultSetBtn(null), // 设置
]
// 列表-操作按钮事件
const buttonTableClick = async (val, row) => {
  if (val == 'point') { // 标签打印
    handlePoint(row)
  }
}

const BASE_URL = getJmreportBaseUrl()
const labelType = ref('') // 标签类别 采购还是制造等
// 标签打印
const handlePoint = async (row) => {
  // 查询是否已创建过标签
  await PackageApi.getPackagePage({
    requestNumber: row.number
  }).then((res) => {
    if(res.list.length > 0) {
      if (res.list[0].productionLineCode != null) {
        labelType.value = 'zz'
      } else {
        labelType.value = 'cg'
      }
      PackageApi.batchPrintingLable(res.list.map(item1=>item1.number).join(',')).then((resLable) =>{
        console.log(159,resLable )

        // 判断是采购还是制造
        if (labelType.value == 'cg') {
          const src = ref(BASE_URL + '/jmreport/view/922729953438072832?token=' + getAccessToken())
          window.open(src.value+'&asn_number='+resLable)
        } else {
          const src = ref(BASE_URL + '/jmreport/view/922734157577715712?token=' + getAccessToken())
          window.open(src.value+'&asn_number='+resLable)
        }
      })
    }
  }).catch(err => {
    console.log(err)
  })
}
// 头部按钮事件
const buttonBaseClick = (val, item) => {
  if (val == 'export') { // 导出
    handleExport()
  } else if (val == 'refresh') { // 刷新
    if (tableObject.params.filters && tableObject.params.filters.length > 0 ) {
      searchFormClick({
        filters: tableObject.params.filters
      })
    } else {
      getList()
    }
  } else if (val == 'filtrate') { // 筛选
  } else { // 其他按钮
    console.log('其他按钮', item)
  }
}

/** 详情操作 */
const detailRef = ref()
const openDetail = (row: any, titleName: any, titleValue: any) => {
  detailRef.value.openDetail(row, titleName, titleValue, 'basicPackageoverRetrospect')
}

/** 导出按钮操作 */
const exportLoading = ref(false) // 导出的加载中
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await PackageoverRetrospectApi.exportPackageoverRetrospect(tableObject.params)
    download.excel(data, '翻包记录主.xlsx')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

// 筛选提交
const searchFormClick = (searchData) => {
  tableObject.params = {
    isSearch: true,
    filters: searchData.filters
  }
  getList() // 刷新当前列表
}

/** 初始化 **/
onMounted(async () => {
  getList()
})

</script>
