<template>
  <div class="table-box">
    <LjVxeTable
      ref="vxeTableRef"
      row-key="mattressid"
      :columns="columns"
      :init-param="initParams"
      :request-api="getData"
      :data-callback="dataCallback"
      :dwname="DwnameEnum.mattressQuote"
      :table-props="tableProps"
      :table-events="tableEvents"
      :auto-load-layout="false"
      :search-btn-size-extent="[]"
      pagination
    >
      <!-- 表格 header 按钮 -->
      <template #tableHeader>
        <el-button-group>
          <el-button>{{ $t("common.add") }}</el-button>
          <el-button>{{ $t("common.delText") }}</el-button>
          <el-button>{{ $t("common.auditFinance") }}</el-button>
          <el-button>{{ $t("common.copyQuote") }}</el-button>
          <el-button>{{ $t("common.businessOrder") }}</el-button>
          <el-button>{{ $t("common.viewHistoricalQuotes") }}</el-button>
        </el-button-group>
      </template>
    </LjVxeTable>
  </div>
</template>

<script setup lang="ts" name="mattressQuote">
import { ref, onMounted, inject } from "vue";
import { useRouter } from "vue-router";
import { getPriceList } from "@/api/modules/saleprice";
import { CommonDynamicSelect } from "@/api/modules/common";
import { ColumnProps } from "@/components/LjVxeTable/interface";
import LjDrawer from "@/components/LjDrawer/index.vue";
// import PriceListDetail from "./detail.vue";
import { useHooks } from "./hooks/index";
import LjDialog from "@/components/LjDialog/index.vue";
import LjHeaderMenu from "@/components/LjHeaderMenu/index.vue";
import { useI18n } from "vue-i18n";
import { useAuthButtons } from "@/hooks/useAuthButtons";
import { DwnameEnum } from "@/enums/dwnameEnum";
import { formatToDateTime, formatToDate } from "@/utils/dateUtil";
import { cloneDeep } from "lodash-es";
import { useGlobalStore } from "@/stores/modules/global";

const { t } = useI18n();
const router = useRouter();
const globalStore = useGlobalStore();
const { columns } = useHooks();
const { CheckPower, CheckOption, buttonNew, buttonDefault } = useAuthButtons(t);

const initParams = ref({});

const getData = (params: any) => {
  console.log("getData mattress params :>> ", params);
  let newParams: any = {};
  params.pageNum && (newParams.pageindex = params.pageNum);
  params.pageSize && (newParams.pagesize = params.pageSize);
  params.orderstr && (newParams.orderstr = params.orderstr);
  delete params.pageNum;
  delete params.pageSize;
  delete params.orderstr;
  let _params = cloneDeep(params);
  switch (_params.status_flag) {
    case "1": // 待下单
      _params.arg_xd_flag = 0;
      break;
    case "2": // 已下单
      _params.arg_xd_flag = 1;
      break;
    case "3": // 待财务审核
      _params.arg_cp_flag = 1;
      _params.arg_flag = 0;
      break;
    case "4": // 已财务审核
      _params.arg_flag = 1;
      break;
    default:
      break;
  }
  newParams.queryParams = _params;
  newParams.dsname = "web_mattress";

  return CommonDynamicSelect(newParams, DwnameEnum.mattressQuote);
  // return [];
};

const dataCallback = (data: any) => {
  console.log("data :>> ", data);
  return {
    list: data.datatable,
    tableinfo: data.tableinfo,
    total: data.totalcnt,
    pageNum: data.pageindex,
    pageSize: data.pagesize
  };
};

const handleDBlClickTable = ({ row, rowIndex, $rowIndex, column, columnIndex, $columnIndex, $event }: any) => {
  if (globalStore.detailBlank) {
    // 打开新的窗口
    const routeUrl = router.resolve({
      path: `/mattressQuote/detail`,
      query: {
        id: row.mattressid,
        code: row.cuscode
      }
    });
    window.open(routeUrl.href, "_blank");
  } else {
    // if (currentLayout.value.right.hidden && globalStore.mxFloat?.includes("custCrmDetail")) {
    //   // 弹窗
    //   mainData.value = row;
    //   LjDrawerRef.value.show();
    // } else {
    // 打开新的标签页
    router.push(`/mattressQuote/detail?id=${row.mattressid}&code=${row.mattresscode}`);
    // }
  }
};

const rowClsNameFunc = (data: any) => {
  const { row, rowIndex, $rowIndex } = data;
  //   if (row.task_status == 2) {
  //     return "vxecol-disable";
  //   }
  return "";
};
const tableProps = {
  height: "auto",
  editConfig: { trigger: "click", mode: "cell" },
  rowClassName: rowClsNameFunc,
  exportConfig: {
    filename: t("menu.rpMsttake") + formatToDate(new Date(), "YYYY-MM-DD HH:mm:ss")
  }
};

// 返回绑定的事件
const tableEvents = {
  // "checkbox-change": handleCheckboxChange,
  // "checkbox-all": handleCheckboxChange,
  // "checkbox-range-change": handleCheckboxChange,
  // "current-change": handleCurrentChanged
  "cell-dblclick": handleDBlClickTable
  // "cell-click": handleClickTable
};
</script>
