import { ref, reactive, toRefs } from "vue";
import type { ColumnProps } from "@/components/LjVxeTable/interface";
import { CommonDynamicSelect } from "@/api/modules/common";
import { DwnameEnum } from "@/enums/dwnameEnum";
import { cloneDeep } from "lodash-es";

const statusOptions = [
  { label: "待处理", value: 0 },
  { label: "执行中", value: 1 },
  { label: "成功", value: 2 },
  { label: "失败", value: 3 },
  { label: "部分成功", value: 4 }
];

interface defaultState {
  /**
   * @description 列表Ref
   */
  VxeTableRef: any;
}

const state = reactive<defaultState>({
  VxeTableRef: null
});

export const useHooks = (t?: any) => {
  const columns: ColumnProps<any>[] = [
    {
      field: "task_type",
      title: "类型",
      search: {
        order: 1,
        el: "select",
        props: { filterable: true, clearable: true },
        defaultValue: "mattress_recalc"
      },
      render: (scope: any) => {
        const keys = Object.keys(scope);
        const data: any = keys.includes("row") ? scope.row : keys.includes("searchParam") ? scope.searchParam : scope;
        const field = scope?.column.field ?? "";
        let item: any = {};
        scope.enum && (item = scope.enum.find((enumItem: any) => enumItem.value == data[field]));
        return item?.label ?? "";
      }
    },
    {
      field: "status",
      title: "状态",
      width: 120,
      enum: statusOptions,
      tag: true,
      search: {
        order: 2,
        el: "select",
        props: { clearable: true }
      }
    },
    {
      field: "schedule_time",
      title: "计划时间",
      format: "yyyy-MM-dd HH:mm:ss",
      search: {
        order: 3,
        el: "date-picker",
        key: "schedule_time_begin",
        props: { type: "date", placeholder: "计划时间开始" }
      }
    },
    {
      field: "schedule_time_end",
      title: "",
      visible: false,
      search: {
        order: 4,
        el: "date-picker",
        key: "schedule_time_end",
        labelHidden: true,
        props: { type: "date", placeholder: "计划时间结束" }
      }
    },
    {
      field: "create_emp",
      title: "创建人",
      search: {
        order: 5,
        el: "input",
        props: { clearable: true }
      }
    },
    {
      field: "create_time",
      title: "创建时间",
      format: "yyyy-MM-dd HH:mm:ss",
      search: {
        order: 6,
        el: "date-picker",
        key: "create_time_begin",
        props: { type: "date", placeholder: "创建时间开始" }
      }
    },
    {
      field: "create_time_end",
      title: "",
      visible: false,
      search: {
        order: 7,
        el: "date-picker",
        key: "create_time_end",
        props: { type: "date", placeholder: "创建时间结束" }
      }
    },
    { field: "start_time", title: "开始时间", format: "yyyy-MM-dd HH:mm:ss" },
    { field: "end_time", title: "结束时间", format: "yyyy-MM-dd HH:mm:ss" },
    { field: "log_msg", title: "信息" }
  ];

  const tableProps = {
    height: "auto"
  };

  const getData = (params: any) => {
    const newParams: any = {};
    params.pageNum && (newParams.pageindex = params.pageNum);
    params.pageSize && (newParams.pagesize = params.pageSize);
    delete params.pageNum;
    delete params.pageSize;
    const queryParams = cloneDeep(params);
    newParams.queryParams = queryParams;
    newParams.dsname = "web_taskloglist";

    return CommonDynamicSelect(newParams, DwnameEnum.tasklog);
  };
  const formatStatus = (status?: number) => {
    return statusOptions.find(item => item.value === status)?.label ?? "";
  };

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

  const getDetailData = (params: any) => {
    const newParams: any = {};
    params.pageNum && (newParams.pageindex = params.pageNum);
    params.pageSize && (newParams.pagesize = params.pageSize);
    delete params.pageNum;
    delete params.pageSize;
    const queryParams = cloneDeep(params);
    newParams.queryParams = queryParams;
    newParams.dsname = DwnameEnum.tasklogMx;

    return CommonDynamicSelect(newParams, DwnameEnum.tasklogMx);
  };

  const detailColumns: ColumnProps<any>[] = [
    { field: "printid", title: "编号", width: 80 },
    { field: "biz_code", title: "业务编码" },
    { field: "biz_name", title: "业务名称" },
    {
      field: "status",
      title: "状态",
      enum: statusOptions,
      render: ({ row }: any) => formatStatus(row.status)
    },
    { field: "err_msg", title: "错误信息" },
    { field: "start_time", title: "开始时间" },
    { field: "end_time", title: "结束时间" }
  ];

  return {
    ...toRefs(state),
    columns,
    detailColumns,
    statusOptions,
    getData,
    getDetailData,
    dataCallback,
    formatStatus,
    tableProps
  };
};
