import { ChangeEvent, useEffect, useState } from 'react';
import { PATHS } from '@/shared/constants/paths';
import { useNavigate } from '@/shared/lib/hooks/use-navigate';
import { useNoticeListMutation } from '@/entities/support/api/use-notice-list-mutation';
import { DEFAULT_PAGE_PARAM } from '@/entities/common/model/constants';
import { NoticeItem } from '@/entities/support/model/types';
import { SupportNoticeItem } from '@/entities/support/ui/notice-item';
import { HeaderType } from '@/entities/common/model/types';
import { 
	useSetHeaderTitle,
	useSetHeaderType,
	useSetFooterMode,
  useSetOnBack
} from '@/widgets/sub-layout/use-sub-layout';

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

  const [pageParam, setPageParam] = useState(DEFAULT_PAGE_PARAM);
  const [searchKeyword, setSearchKeyword] = useState<string>('');
  const [resultList, setResultList] = useState<Array<NoticeItem>>([]);

  useSetHeaderTitle('공지사항');
  useSetHeaderType(HeaderType.LeftArrow);
  useSetFooterMode(true);
  useSetOnBack(() => {
    navigate(PATHS.home);
  });

  const { mutateAsync: noticeList } = useNoticeListMutation();
  const callList = () => {
    let listParams = {
      category: 'ALL',
      searchKeyword: searchKeyword,
      ...{page: pageParam}
    };
    
    noticeList(listParams).then((rs) => {
      console.log(rs)
      setResultList(rs.content);
    });
  };

  const onClickToAction = () => {
    callList();
  };

  const getNoticeList = () => {
    let rs = [];
    for(let i=0;i<resultList.length;i++){
      rs.push(
        <SupportNoticeItem
          key={ `key-support-notice-item-${i}` }
          id={ resultList[i]?.id }
          title={ resultList[i]?.title }
          category={ resultList[i]?.category }
          regDate={ resultList[i]?.regDate }
          isNew={ resultList[i]?.isNew }
        ></SupportNoticeItem>
      )
    }
    return rs;
  };

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


  return (
    <>
      <main>
        <div className="tab-content">
          <div className="tab-pane sub active">
            <div className="notice114">
              <div className="notice-controls">
                <div className="notice-search">
                  <span 
                    className="ic16 search" 
                    aria-hidden="true"
                    onClick={ () => onClickToAction() }
                  ></span>
                  <input 
                    type="text"
                    placeholder="검색어를 입력하세요"
                    value={ searchKeyword }
                    onChange={ (e: ChangeEvent<HTMLInputElement>) => setSearchKeyword(e.target.value) }
                  />
                </div>
                <div className="notice-filter">
                  <select className="flex-1">
                    <option>전체</option>
                  </select>
                </div>
              </div>
              <div className="notice-list-114">
                { getNoticeList() } 
              </div>
            </div>
          </div>
        </div>
      </main>
    </>
  );
};