import React, { useState, useEffect, useMemo, useRef } from "react";
import AutoTable from "@/components/AutoTable";
import PremButton from "@/components/PremButton";
import useKeepAlive from "@/components/useKeepAlive";
import getcolumns from "./columns";
import { useRequest } from "ahooks";
import { doFetch } from "@/utils/doFetch";
function Taskmanage() {
  let actionRef = useRef();
  const [tabKey, setTabKey] = useState("1");
  const { runAsync } = useRequest(doFetch, {
    manual: true,
    onSuccess: (res, params) => {
      if (res.code == "0000") {
        actionRef?.current?.reload();
      }
    },
  });
  const tabList = useMemo(() => {
    return getcolumns();
  }, [tabKey]);
  const columns = useMemo(() => {
    let defcolumn =
      getcolumns().filter((it) => it.key == tabKey)[0]?.columns ?? [];
    return tabKey === "2"
      ? defcolumn
      : defcolumn.concat({
          title: "操作",
          valueType: "option",
          width: 80,
          render: (text, row, _, action) => rightExtra(text, row, _, action),
        });
  }, [tabKey]);
  const pathconfig = useMemo(() => {
    let defpath =
      getcolumns().filter((it) => it.key == tabKey)[0]?.pathconfig ?? {};
    return defpath;
  }, [tabKey]);
  const rightExtra = (text, row, _, action) => {
    return [
      (row.documentStatus == 0 || row.documentStatus == 1) && (
        <PremButton
          key="gd"
          pop={{
            title: "是否关闭该任务单?",
            okText: "确认",
            cancelText: "取消",
            onConfirm: async () => {
              await runAsync({
                url: "/ta_wms_workmanship/pmTaskManagement/taskColse",
                params: { id: row.id },
              });
              actionRef?.current?.reload();
            },
          }}
          btn={{
            size: "small",
            danger: true,
          }}
        >
          关单
        </PremButton>
      ),
      (row.documentStatus === 2 || row.documentStatus === 1) && (
        <PremButton
          key="sggd"
          pop={{
            title: "手工完成有风险，请谨慎处理，是否进行操作？",
            okText: "确认",
            cancelText: "取消",
            onConfirm: async () => {
              await runAsync({
                url: "/ta_wms_workmanship/pmTaskManagement/taskManualFinish",
                params: { ticketNo: row.taskNo, status: 0 },
              });
              actionRef?.current?.reload();
            },
          }}
          btn={{
            size: "small",
            danger: true,
          }}
        >
          手工完成
        </PremButton>
      ),
    ];
  };

  return (
    <div>
      <AutoTable
        pagetitle="任务管理"
        columns={columns}
        path={pathconfig?.list}
        actionRef={actionRef}
        resizeable={false}
        tabList={tabList}
        tabKey={tabKey}
        onTabChange={(key) => {
          setTabKey(key);
        }}
      />
    </div>
  );
}

export default useKeepAlive(Taskmanage);
