import React from "react";
import { Button, Dropdown, MenuProps, Space, Typography } from "antd";
import { MoreOutlined } from "@ant-design/icons";

const { Text } = Typography;

/*
 * Author: 黎永顺
 * Description:社保福利台账列表项
 * Params:
 * Date: 2024/1/23
 */
export function renderCols(initialState: any[], type: string, i18n?: AnyObject, permission?: boolean) {
  return React.useMemo(() => {
    if (type === "welfareRecord") {
      return [..._.map(_.filter(initialState, o => o.dataIndex !== "id"), g => {
        let col = { ...g, ellipsis: true, fixed: g.dataIndex === "billMonth", width: 180 };
        switch (g.dataIndex) {
          case "billMonth":
            col = {
              ...col,
              width: 120,
              render: (text: string, record: any) => {
                const { billStatus } = record;
                return (<Button type="link"
                                onClick={() => postMessageToParent(billStatus === "0" ? "CALC" : "VIEW", record)}>{text}</Button>);
              }
            };
            break;
          case "billStatus":
            col = {
              ...col, width: 100, render: (text: string) => (
                <Text style={{ maxWidth: "100%" }} ellipsis>
                  {text === "1" ? i18n?.["已归档"] : i18n?.["未归档"]}
                </Text>
              )
            };
            break;
          case "paymentOrganization":
            col = { ...col, width: 200 };
            break;
          case "socialNum":
          case "otherNum":
          case "fundNum":
            col = { ...col, width: 118 };
            break;
          case "remarks":
            col = { ...col, width: 200 };
            break;
          default:
            col = { ...col };
            break;
        }
        return col;
      }), {
        dataIndex: "operate", title: i18n?.["操作"], width: 185, fixed: "right",
        render: (__: string, record: any) => {
          const { billStatus } = record;
          const items: MenuProps["items"] = [
            {
              key: "DeleteList",
              label: i18n?.["删除"],
              onClick: () => postMessageToParent("DELRC", record)
            }
          ];
          return (
            <Space>
              {
                !permission ?
                  <Button type="link" onClick={() => postMessageToParent("VIEW", record)}>{i18n?.["查看"]}</Button> :
                  <>
                    {
                      billStatus === "0" &&
                      <>
                        <Button type="link"
                                onClick={() => postMessageToParent("CALC", record)}>{i18n?.["核算"]}</Button>
                        <Button type="link"
                                onClick={() => postMessageToParent("FILE", record)}>{i18n?.["归档"]}</Button>
                        <Dropdown menu={{ items }} placement="bottomRight">
                          <Button type="link" icon={<MoreOutlined/>}/>
                        </Dropdown>
                      </>
                    }
                    {
                      billStatus === "1" &&
                      <>
                        <Button type="link"
                                onClick={() => postMessageToParent("VIEW", record)}>{i18n?.["查看"]}</Button>
                        <Button type="link"
                                onClick={() => postMessageToParent("RECALC", record)}>{i18n?.["重新核算"]}</Button>
                      </>
                    }
                  </>
              }
            </Space>
          );
        }
      }];
    } else if (type === "bonusplan") {
      return [..._.map(_.filter(initialState, o => o.dataIndex !== "id"), g => {
        let col = { ...g, ellipsis: true, fixed: g.dataIndex === "billMonth", width: 180 };
        switch (g.dataIndex) {
          case "billMonth":
            col = {
              ...col,
              width: 120,
              render: (text: string, record: any) => {
                const { billStatus } = record;
                return (<Button type="link"
                                onClick={() => postMessageToParent(billStatus === "0" ? "CALC" : "VIEW", record)}>{text}</Button>);
              }
            };
            break;
          case "billStatus":
            col = {
              ...col, width: 100, render: (text: string) => (
                <Text style={{ maxWidth: "100%" }} ellipsis>
                  {text === "1" ? i18n?.["已归档"] : i18n?.["未归档"]}
                </Text>
              )
            };
            break;
          case "paymentOrganization":
            col = { ...col, width: 200 };
            break;
          case "socialNum":
          case "otherNum":
          case "fundNum":
            col = { ...col, width: 118 };
            break;
          case "remarks":
            col = { ...col, width: 200 };
            break;
          default:
            col = { ...col };
            break;
        }
        return col;
      })];
    }
    return [];
  }, [initialState, type, i18n, permission]);
}

const postMessageToParent = (type: string, params: any) => {
  window.parent.postMessage({ type: "turn", payload: { id: type, params } }, "*");
};
