/*  出库单管理
 * @Author: Li Hanlin
 * @Date: 2023-12-21 11:10:07
 * @Last Modified by: Li Hanlin
 * @Last Modified time: 2023-12-21 16:28:23
 */

import React, { useState, useEffect, useMemo, useRef } from "react";
import DrawerPro from "@/components/DrawerPro";
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";
import { message } from "antd";
import DetailPro from "@/components/DetailPro";
function User() {
  let actionRef = useRef();
  const [drawer, setdrawer] = useState({
      open: false,
    }),
    [activeTabKey, setactiveTabKey] = useState("1");

  const { run, loading, runAsync } = useRequest(doFetch, {
    manual: true,
    onSuccess: (res, params) => {
      if (res?.code == "0000") {
        reload();
      }
    },
  });
  const columns = useMemo(() => {
    let defcolumn = getcolumns(setdrawer).filter(
      (it) => it.key == activeTabKey
    )[0]?.columns;
    let defpath =
      getcolumns(setdrawer).filter((it) => it.key == activeTabKey)[0]
        ?.pathconfig ?? {};
    return defcolumn;
  }, [activeTabKey]);

  const tabList = useMemo(() => {
    return getcolumns();
  }, [activeTabKey]);
  const pathconfig = useMemo(() => {
    let defpath =
      getcolumns(setdrawer).filter((it) => it.key == activeTabKey)[0]
        ?.pathconfig ?? {};
    return defpath;
  }, [activeTabKey]);
  const rightExtra = (text, row, _, action) => {
    return [
      <PremButton
        key="delete"
        pop={{
          title: "是否关闭该申请单?",
          okText: "确认",
          cancelText: "取消",
          onConfirm: async () => {
            await runAsync({
              url: "/ta_wms_auth/sysUser/deleteById",
              params: { id: row.id },
            });
          },
        }}
        btn={{
          size: "small",
          danger: true,
        }}
      >
        关单
      </PremButton>,
    ];
  };
  function reload() {
    actionRef?.current?.reload();
    setdrawer((s) => ({
      ...s,
      open: false,
    }));
  }
  return (
    <div>
      <AutoTable
        pagetitle="调拨单管理"
        columns={columns}
        path={pathconfig?.list}
        actionRef={actionRef}
        addconfig={{
          // access: 'sysDepartment_save',
          name: "新增",
          btn: {
            type: "primary",
            onClick: () => {
              setdrawer((s) => ({
                ...s,
                open: true,
                title: "新增",
                val: "add",
                item: {},
                detailpath: "",
              }));
            },
          },
        }}
        tabList={tabList}
        activeTabKey={activeTabKey}
        onTabChange={(key) => {
          setactiveTabKey(key);
        }}
      />
      <DrawerPro
        fields={columns}
        detailpath={drawer.detailpath}
        params={{ id: drawer?.item?.id }}
        placement="right"
        onClose={() => {
          setdrawer((s) => ({
            ...s,
            open: false,
          }));
        }}
        onFinish={async (vals) => {
          let newfields = JSON.parse(JSON.stringify(vals));
          await runAsync({
            url: "/ta_wms_auth/sysUser/save",
            params: {
              ...newfields,
              id: drawer?.val === "edit" ? drawer?.item?.id : "",
            },
          });
        }}
        {...drawer}
      >
        <AutoTable
          withCard={false}
          columns={[
            {
              title: "物料编号-名称",
              dataIndex: "materialNo",
              key: "materialNo",
              render: (_, row) => {
                return (
                  <span>
                    {row?.materialNo}-{row?.materialName}
                  </span>
                );
              },
            },
            {
              title: "出库数量",
              dataIndex: "storeNum",
              key: "storeNum",
            },
            {
              title: "库存单位",
              dataIndex: "productionUnitName",
              key: "productionUnitName",
            },
            {
              title: "未下架数量",
              dataIndex: "noStoreNum",
              key: "noStoreNum",
            },
          ]}
          expandable={{
            expandedRowRender: (record) => (
              <AutoTable
                withCard={false}
                pagination={false}
                className="expandableTable"
                columns={[
                  {
                    title: "库位名称",
                    dataIndex: "storeName",
                    key: "storeName",
                  },
                  {
                    title: "工单号",
                    dataIndex: "jobNo",
                    key: "jobNo",
                  },
                  {
                    title: "供应商编号-名称",
                    dataIndex: "supplierName",
                    key: "supplierName",
                    render: (_, row) => {
                      return (
                        <span>
                          {row?.supplierNo}-{row?.supplierName}
                        </span>
                      );
                    },
                  },
                  {
                    title: "计划数量",
                    dataIndex: "planStoreNum",
                    key: "planStoreNum",
                  },
                  {
                    title: "下架数量",
                    dataIndex: "storeNum",
                    key: "storeNum",
                  },
                  {
                    title: "状态",
                    dataIndex: "statusName",
                    key: "statusName",
                  },
                ]}
                dataSource={record?.taskMaterialOutstoreDetails ?? []}
              />
            ),
            rowExpandable: (record) => record.name !== "Not Expandable",
          }}
          dataSource={drawer?.detailData?.taskMaterialDetails ?? []}
          actionRef={actionRef}
          pagination={false}
        />
        <DetailPro
          fields={[
            { title: "强制关单信息", valueType: "split" },
            { title: "关单人员", dataIndex: "closeUserName" },
            { title: "关单时间", dataIndex: "closeTime" },
          ]}
          detailData={drawer?.detailData}
        />
      </DrawerPro>
    </div>
  );
}

export default useKeepAlive(User);
