<template>
  <div class="page-box" style="position: relative;">
    <el-form
      ref="formDeliveryOrderItemsRef"
      :model="formData"
      :size="layoutStore.defaultFormItemSize"
      label-width="120px"
      label-position="right"
      @submit.prevent
    >
      <filter-box :item-width="350" @search="refreshFormDeliveryOrderItems()" @reset="resetFormDeliveryOrderItems">
        <el-form-item label="食品名称">
          <el-input
            class="filter-item"
            v-model="formFilter.foodNameFilter"
            type="text"
            placeholder=""
            :clearable="true"
            :show-word-limit="false"
            maxlength=""
          />
        </el-form-item>
      </filter-box>
    </el-form>
    <table-box
      ref="formDeliveryOrderItemsTable"
      class="page-table"
      :data="formDeliveryOrderItemsTableWidgetDataList"
      :size="layoutStore.defaultFormItemSize"
      :row-config="{isCurrent: false, isHover: true}"
      :seq-config="{startIndex: ((formDeliveryOrderItemsTableWidgetCurrentPage - 1) * formDeliveryOrderItemsTableWidgetPageSize)}"
      :sort-config="{remote: true}"
      :hasExtend="true"
      @sort-change="formDeliveryOrderItemsTableWidget.onSortChange"
      @refresh="formDeliveryOrderItemsTableWidget.refreshTable()"
    >
      <template #operator>
        <el-button
          type="primary"
          :size="layoutStore.defaultFormItemSize"
          @click="onExportClick()"
          >
          导出
        </el-button>
        <el-upload
          class="btn-import"
          :auto-upload="false"
          action=""
          :show-file-list="false"
          accept=".xls,.xlsx"
          style="display: inline-block;"
          :on-change="onImportClick"
        >
          <template #trigger>
            <el-button
              type="primary"
              :size="layoutStore.defaultFormItemSize"
            >
              导入
            </el-button>
          </template>
        </el-upload>
      </template>
      <vxe-column title="序号" type="seq" :index="formDeliveryOrderItemsTableWidget.getTableIndex" :width="80" />
      <vxe-column title="id" field="id" sortable />
      <vxe-column title="食品名称" field="foodName" />
      <vxe-column title="订单id" field="orderId" />
      <vxe-column title="价格" field="price" sortable />
      <vxe-column title="数量" field="number" sortable />
      <vxe-column title="创建用户" field="createUserId" />
      <vxe-column title="创建时间" field="createTime">
        <template v-slot="scope">
          <span>{{formatDateByStatsType(scope.row.createTime, 'day')}}</span>
        </template>
      </vxe-column>
      <vxe-column title="更新用户" field="updateUserId" />
      <vxe-column title="更新时间" field="updateTime">
        <template v-slot="scope">
          <span>{{formatDateByStatsType(scope.row.updateTime, 'day')}}</span>
        </template>
      </vxe-column>
      <vxe-column title="删除" field="dataState" />
      <vxe-column title="操作" fixed="right">
        <template v-slot="scope">
          <el-button
            link
            type="primary"
            :size="layoutStore.defaultFormItemSize"
            @click.stop="onDeleteClick(scope.row)"
          >
            删除
          </el-button>
        </template>
      </vxe-column>
      <template slot="empty">
        <div class="table-empty unified-font">
          <img src="@/assets/img/empty.png">
          <span>暂无数据</span>
        </div>
      </template>
      <!-- 分页 -->
      <template #pagination>
        <el-row type="flex" justify="end" style="margin-top: 10px;">
          <el-pagination
            :total="formDeliveryOrderItemsTableWidgetTotalCount"
            :current-page="formDeliveryOrderItemsTableWidgetCurrentPage"
            :page-size="formDeliveryOrderItemsTableWidgetPageSize"
            :page-sizes="[10, 20, 50, 100]"
            layout="total, prev, pager, next, sizes"
            @current-change="formDeliveryOrderItemsTableWidget.onCurrentPageChange"
            @size-change="formDeliveryOrderItemsTableWidget.onPageSizeChange">
          </el-pagination>
        </el-row>
      </template>
    </table-box>
    <label v-if="subPage" class="page-close-box" @click="onCancel()">
      <img src="@/assets/img/back2.png" alt="">
    </label>
  </div>
</template>

<script lang="ts">
export default {
  name: 'formDeliveryOrderItems',
};
</script>

<script setup lang="ts">
import { VxeColumn, VxeTable } from 'vxe-table';
import { ANY_OBJECT } from '@/types/generic';
import { DictData, DictionaryBase } from '@/common/staticDict/types';
import { ElMessage, ElMessageBox, UploadFile } from 'element-plus';
import { useRoute, useRouter } from 'vue-router';
import { useCommon } from '@/common/hooks/useCommon';
import { useLayoutStore, useStaticDictStore } from '@/store';
import { useDownload } from '@/common/hooks/useDownload';
import { useDropdown } from '@/common/hooks/useDropdown';
import { DropdownOptions, ListData } from '@/common/types/list';
import { useTable } from '@/common/hooks/useTable';
import { TableOptions } from '@/common/types/pagination';
import { useUpload } from '@/common/hooks/useUpload';
import { useUploadWidget } from '@/common/hooks/useUploadWidget';
import { DictionaryController } from '@/api/system';
import { treeDataTranslate, findItemFromList, findTreeNodePath, findTreeNode } from '@/common/utils';
import { DeliveryOrderItemsData } from '@/api/generated/deliveryOrderItemsController';
import { DeliveryOrderItemsController } from '@/api/generated';

const router = useRouter();
const route = useRoute();
const layoutStore = useLayoutStore();
const { downloadFile } = useDownload();
const { getUploadHeaders, getUploadActionUrl, fileListToJson, parseUploadData, getPictureList } = useUpload();
const { 
  Delete,
  Search,
  Edit,
  Plus,
  Refresh,
  Picture,
  Dialog,
  mainContextHeight,
  clientHeight,
  checkPermCodeExist,
  parseParams,
  parseArrayParams,
  formatDateByStatsType,
  getDateRangeFilter,
} = useCommon();
// 静态字典
const { staticDict: StaticDict } = useStaticDictStore();

const props = withDefaults(
  defineProps<{
    subPage?: number | string | boolean;
    id?: ANY_OBJECT;
  }>(),
  {
    subPage: 0,
    id: undefined,
  },
);

const formFilter = reactive({
  // 食品名称
  foodNameFilter: undefined,
});
const formFilterCopy = reactive({
  // 食品名称
  foodNameFilter: undefined,
});

const onCancel = () => {
  layoutStore.removeCachePage(route.name as string);
  route.meta.refreshParentCachedPage = true;
  router.go(-1);
};

const onResume = () => {
  refreshFormDeliveryOrderItems();
};

/**
 * 表格组件数据获取函数，返回Promise
 */
const loadFormDeliveryOrderItemsTableWidgetData = (params: ANY_OBJECT) => {
  if (params == null) params = {};
  params = {
    ...params,
    deliveryOrderItemsDtoFilter: {
      foodName: formFilter.foodNameFilter,
    }
  };
  return new Promise((resolve, reject) => {
    DeliveryOrderItemsController.list(params).then(res => {
      resolve({
        dataList: res.data.dataList,
        totalCount: res.data.totalCount
      });
    }).catch(e => {
      reject(e);
    });
  });
};
/**
 * 表格组件数据获取检测函数，返回true正常获取数据，返回false停止获取数据
 */
const loadFormDeliveryOrderItemsTableVerify = () => {
  formFilterCopy.foodNameFilter = formFilter.foodNameFilter;
  return true;
};
/**
 * 导出
 */
const onExportClick = (row?: DeliveryOrderItemsData) => {
  let params: ANY_OBJECT = {
  };

  DeliveryOrderItemsController.export(params, '表格组件.xlsx').then(res => {
    ElMessage.success('导出成功');
  }).catch(e => {
    ElMessage.error(e.errorMessage);
  });
};
/**
 * 导入
 */
const onImportClick = (file) => {
  let params: ANY_OBJECT = {
    importFile: file.raw,
    // 是否忽略表头
    skipHeader: false
  };

  DeliveryOrderItemsController.import(params).then(res => {
    ElMessage.success('导入成功');
    formDeliveryOrderItemsTableWidget.refreshTable();
  }).catch(e => {
    // TODO: 异常处理
    console.error(e);
  });
};
/**
 * 删除
 */
const onDeleteClick = (row?: DeliveryOrderItemsData) => {
  let params: ANY_OBJECT = {
    id: props.id,
  };

  DeliveryOrderItemsController.delete(params).then(res => {
    ElMessage.success('删除成功');
    formDeliveryOrderItemsTableWidget.refreshTable(false, 1);
  }).catch(e => {
    // TODO: 异常处理
    console.error(e);
  });
};
// 表格组件表格组件参数
const formDeliveryOrderItemsTableOptions: TableOptions<DeliveryOrderItemsData> = {
  loadTableData: loadFormDeliveryOrderItemsTableWidgetData,
  verifyTableParameter: loadFormDeliveryOrderItemsTableVerify,
  paged: true,
  rowSelection: false,
  orderFieldName: undefined,
  ascending: true,
};
// 表格组件表格组件
const formDeliveryOrderItemsTable = ref();
const formDeliveryOrderItemsTableWidget = useTable(formDeliveryOrderItemsTableOptions);
const {
  dataList: formDeliveryOrderItemsTableWidgetDataList,
  currentPage: formDeliveryOrderItemsTableWidgetCurrentPage,
  pageSize: formDeliveryOrderItemsTableWidgetPageSize,
  totalCount: formDeliveryOrderItemsTableWidgetTotalCount,
} = formDeliveryOrderItemsTableWidget;
const refreshFormDeliveryOrderItems = () => {
  // 刷新段落
  formDeliveryOrderItemsTableWidget.refreshTable();
};
/**
 * 重置过滤值
 */
const resetFormDeliveryOrderItems = () => {
  formFilter.foodNameFilter = undefined;
  formFilterCopy.foodNameFilter = undefined;
  refreshFormDeliveryOrderItems();
};
/**
 * 重置所有过滤值
 */
const resetFilter = () => {
  resetFormDeliveryOrderItems();
};
const formInit = () => {
  refreshFormDeliveryOrderItems();
};

onMounted(() => {
  formInit();
});
</script>