import moment from 'moment';
import { IMAGE_ROOT } from "@/shared/constants/common";
import { useState, useEffect } from "react";
import { LinkPaymentShippingHistoryFilter } from "./filter/link-payment-shipping-history-filter";
import { useNavigate } from '@/shared/lib/hooks/use-navigate';
import { PATHS } from "@/shared/constants/paths";
import { LinkPaymentList } from "./link-payment-list";
import { SortOptionsBox } from "./sort-options-box";
import { LinkPaymentSendMethod, LinkPaymentTransactionStatus, ProcessResult, SortByKeys } from "../../model/types";
import { LinkPaymentSearchType, } from "../../model/types";

const processResultBtnGroup = [
    { name: '전체', value: ProcessResult.ALL },
    { name: '성공', value: ProcessResult.SUCCESS },
    { name: '실패', value: ProcessResult.FAILURE }
]

export const LinkPaymentShippingHistoryWrap = () => {
    const { navigate } = useNavigate();

    const [filterOn, setFilterOn] = useState<boolean>(false);
    const [sortBy, setSortBy] = useState<SortByKeys>(SortByKeys.New);
    const [listItems, setListItems] = useState({});
    const [mid, setMid] = useState<string>('nictest001m');
    const [searchType, setSearchType] = useState<LinkPaymentSearchType>(LinkPaymentSearchType.ALL)
    const [searchKeyword, setSearchKeyword] = useState<string>('');
    const [startDate, setStartDate] = useState(moment().format('YYYY-MM-DD'));
    const [endDate, setEndDate] = useState(moment().format('YYYY-MM-DD'));
    const [transactionStatus, setTransactionStatus] = useState<LinkPaymentTransactionStatus>(LinkPaymentTransactionStatus.ALL)
    const [processResult, setProcessResult] = useState<ProcessResult>(ProcessResult.ALL)
    const [sendMethod, setSendMethod] = useState<LinkPaymentSendMethod>(LinkPaymentSendMethod.ALL)

    const onClickToOpenFilter = () => {
        setFilterOn(!filterOn);
    };
    const onClickToNavigate = () => {
        navigate(PATHS.additionalService.linkPayment.request)
    }
    const onClickToSort = (sort: SortByKeys) => {
        setSortBy(sort);
        callList({ sortBy: sort });
    };

    const callList = (option?: { sortBy?: string, val?: string }) => {
        setListItems({
            '20250608': [
                {
                    transactionId: 'txn1',
                    customerName: '김*환(7000)',
                    status: '결제완료',
                    channel: 'SMS',
                    paymentMethod: '신용카드',
                    amount: 5254000
                },
                {
                    transactionId: 'txn2',
                    customerName: '김*환(7000)',
                    status: '결제완료',
                    channel: '이메일',
                    paymentMethod: '신용카드',
                    amount: 5254000
                },
                {
                    transactionId: 'txn3',
                    customerName: '김*환(7000)',
                    status: '입금요청',
                    channel: '이메일',
                    paymentMethod: '신용카드',
                    amount: 5254000
                },
                {
                    transactionId: 'txn4',
                    customerName: '김*환(7000)',
                    status: '결제중단',
                    channel: 'SMS',
                    paymentMethod: '',
                    amount: 5254000
                },
                {
                    transactionId: 'txn5',
                    customerName: '김*환(7000)',
                    status: '결제실패',
                    channel: 'SMS',
                    paymentMethod: '',
                    amount: 5254000
                }
            ]
        });
    };

    const onClickProcessResult = (val: ProcessResult) => {
        setProcessResult(val);
    }

    useEffect(() => {
        callList();
    }, []);

    return (
        <>
            <section className="summary-section pt-30">
                <div className="credit-controls">
                    <div>
                        <input
                            className="credit-period"
                            type="text"
                            value={moment(startDate).format('YYYY.MM.DD') + '-' + moment(endDate).format('YYYY.MM.DD')}
                            readOnly={true}
                        />
                        <button
                            className="filter-btn">
                            <img
                                src={IMAGE_ROOT + '/ico_setting.svg'}
                                alt="검색옵션"
                                onClick={() => onClickToOpenFilter()}
                            />
                        </button>
                    </div>
                    <button
                        className="download-btn"
                        aria-label="다운로드"
                    >
                        <img
                            src={IMAGE_ROOT + '/ico_download.svg'}
                            alt="다운로드"
                        />
                    </button>
                </div>
            </section>

            <div className="filter-section">
                <SortOptionsBox
                    sortBy={sortBy}
                    onClickToSort={onClickToSort}
                >
                </SortOptionsBox>
                <div className="excrow">
                    <div className="full-menu-keywords no-padding">
                        {
                            processResultBtnGroup.map((value, index) => (
                                <span
                                    key={`key-service-code=${index}`}
                                    className={`keyword-tag ${(processResult === value.value) ? 'active' : ''}`}
                                    onClick={() => onClickProcessResult(value.value)}
                                >{value.name}</span>
                            ))
                        }
                    </div>
                </div>
            </div>

            <LinkPaymentList
                listItems={listItems}
            />
            <div className="apply-row">
                <button
                    className="btn-50 btn-blue flex-1"
                    onClick={() => onClickToNavigate()}
                >결제 신청</button>
            </div>
            <LinkPaymentShippingHistoryFilter
                filterOn={filterOn}
                setFilterOn={setFilterOn}
                mid={mid}
                searchType={searchType}
                searchKeyword={searchKeyword}
                startDate={startDate}
                endDate={endDate}
                transactionStatus={transactionStatus}
                processResult={processResult}
                sendMethod={sendMethod}
                setMid={setMid}
                setSearchType={setSearchType}
                setSearchKeyword={setSearchKeyword}
                setStartDate={setStartDate}
                setEndDate={setEndDate}
                setTransactionStatus={setTransactionStatus}
                setProcessResult={setProcessResult}
                setSendMethod={setSendMethod}
            ></LinkPaymentShippingHistoryFilter>
        </>
    )
}