% ***********************************************************
% THESIS_MAIN_CLEAN.tex
% Author: Dr. Wan Nural Jawahir Hj Wan Yussof
% Optimized for Default LaTeX Font
% ***********************************************************
\PassOptionsToPackage{table}{xcolor} % Beritahu LaTeX untuk guna [table] bila-bila xcolor dipanggil
\PassOptionsToPackage{acronym,toc}{glossaries}
\documentclass[12pt,a4paper]{report}

% --- 1. FON & BAHASA ---
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage{lipsum}
\usepackage{tabularx} 
\usepackage{enumitem}
% Pakej fon seperti mathptmx dibuang untuk kekal default Computer Modern

% --- 2. MATEMATIK & SAINS ---
\usepackage{mathtools, amssymb, amsthm}
\usepackage{siunitx}
\numberwithin{equation}{chapter}

% --- 3. GEOMETRY & LAYOUT ---
\usepackage[left=3.8cm, right=2.5cm, top=2.5cm, bottom=2.5cm, includefoot, headheight=0cm]{geometry}
\usepackage{setspace}
\usepackage{pdflscape}
%\usepackage{parskip} % Menguruskan jarak antara perenggan secara automatik
%\setstretch{1.5}    % Jarak baris standard tesis
\usepackage{pdfpages}

% --- 4. GRAFIK& WARNA ---
\usepackage{graphicx}
\usepackage[table]{xcolor}
\usepackage{tikz}
\usepackage{pgfplots}
\usepackage[edges]{forest}
\graphicspath{{figure/}{images/}{img/}}

% --- 5. JADUAL & SENARAI ---
\usepackage{booktabs, multirow, longtable, ctable, rotating}
\usepackage{enumitem}
\usepackage[hang, small, margin=20pt, tableposition=top]{caption}

% --- 6. TOC, LOF, LOT (Kawal Jarak & Format) ---
\usepackage[titles]{tocloft}
\usepackage[nottoc]{tocbibind} % Memasukkan LOF/LOT/BIB ke dalam TOC secara automatik
\setlength{\cftbeforechapskip}{3pt} % Memaksa entri bertaraf 'Chapter' rapat dalam TOC

\makeatletter
\let\oldl@chapter\l@chapter
\renewcommand*\l@chapter[2]{%
  \vspace{0.5em}%
  \oldl@chapter{#1}{#2}%
}
\makeatother

% Mematikan dots pada TOC
\renewcommand{\cftsecdotsep}{\cftnodots}
\renewcommand{\cftsubsecdotsep}{\cftnodots}
\renewcommand{\cfttabdotsep}{\cftnodots}
\renewcommand{\cftfigdotsep}{\cftnodots}

% Format Tajuk TOC/LOF/LOT
\renewcommand{\cfttoctitlefont}{\hfill\normalsize\bfseries\MakeUppercase}
\renewcommand{\cftaftertoctitle}{\hfill}
\renewcommand{\cftchapfont}{\normalsize\bfseries\MakeUppercase}

% --- 7. HEADER & FOOTER (Page Numbering) ---
\usepackage{fancyhdr}
\fancypagestyle{plain}{
	\fancyhf{}
	\fancyfoot[C]{\thepage}
	\renewcommand{\headrulewidth}{0pt}
}
\pagestyle{fancy}
\fancyhf{}
\fancyfoot[C]{\thepage}
\renewcommand{\headrulewidth}{0pt}

% --- 8. ACRONYMS & GLOSSARIES ---
\usepackage{longtable}
\usepackage[nonumberlist,acronym]{glossaries}
\newglossarystyle{mylong}{%
	\setglossarystyle{long}%
	\renewenvironment{theglossary}%
	{\begin{longtable}[l]{@{}p{2cm}p{0.8\hsize}}}%
		{\end{longtable}}%
}
\makeglossaries
\include{front/abb}

% --- 9. APPENDICES MACRO ---
\newcommand{\listappendicesname}{List of Appendices}
\newlistof{appendices}{apc}{\listappendicesname}
\newcommand{\appendices}[1]{\addcontentsline{apc}{appendices}{#1}}
\newcommand{\newappendix}[1]{\section*{#1}\appendices{#1}}



% --- 11. TETAPAN TAJUK & CHAPTER ---
\usepackage{titlesec}
%\titleformat{\chapter}[display]
%{\normalfont\normal\bfseries\centering}
%{\MakeUppercase{\chaptertitlename}\ \thechapter}{1.5em}{\MakeUppercase}
%\titlespacing{\chapter}{0pt}{2.5cm}{1cm}
% 1. Format untuk CHAPTER
\titleformat{\chapter}[display]
{\normalfont\normalsize\bfseries\centering} % Kekal normalsize & Bold
{\MakeUppercase{\chaptertitlename}\ \thechapter}{1.5em}{\MakeUppercase}
\titlespacing{\chapter}{0pt}{2.5cm}{1cm}

% 2. Format untuk SECTION (Contoh: 1.1 Project Background)
\titleformat{\section}
{\normalfont\normalsize\bfseries} % Paksa jadi normalsize & Bold
{\thesection}{1em}{}

% 3. Format untuk SUBSECTION (Contoh: 1.1.1 Problem Statement)
\titleformat{\subsection}
{\normalfont\normalsize\bfseries} % Paksa jadi normalsize & Bold
{\thesubsection}{1em}{}



\addto\captionsenglish{
	\renewcommand{\contentsname}{Table of Contents}
	\renewcommand{\bibname}{References}
}

% --- 12. INCLUDE ONLY ---
\includeonly{
	front/titlepage, front/approval, front/declare, 
	front/ack, front/abstract, front/abstrak, front/abb,
	chapter/ch1, chapter/ch2, chapter/ch3, chapter/ch4,
	chapter/ch5, chapter/ch6, chapter/ch7, 
	appendix/one, back/biodata
}

% --- Pakej untuk Singkatan ---
%\usepackage[acronym, toc]{glossaries} % 'toc' supaya ia muncul dalam Table of Contents
% --- 10. BIBLIOGRAFI & HYPERREF ---
\usepackage{natbib}
\usepackage{hyperref}
\hypersetup{colorlinks=true, allcolors=black, linktocpage=true}

% ***********************************************************
% MULAKAN DOKUMEN
% ***********************************************************
\begin{document}
	
	% --- FRONT MATTER ---
	\pagenumbering{roman}
	
	\include{front/titlepage}
\begin{singlespace}
	\addcontentsline{toc}{chapter}{Thesis Confirmation and Approval}
	\include{front/approval}
	
	\cleardoublepage
	\addcontentsline{toc}{chapter}{Declaration}
	\include{front/declare}

	\cleardoublepage
	\addcontentsline{toc}{chapter}{Acknowledgements}
	\include{front/ack}
	
	\cleardoublepage
	\addcontentsline{toc}{chapter}{Abstract}
	\include{front/abstract}
	
	\cleardoublepage
	\addcontentsline{toc}{chapter}{Abstrak}
	\include{front/abstrak}
		% --- LISTS ---
	\clearpage
%	\begin{singlespace} % Paksa jarak rapat hanya untuk bahagian ini
	\tableofcontents
%\end{singlespace}
	\clearpage
	\listoftables
	\addtocontents{lot}{\bfseries Table \hfill Pages\par}
	
	\clearpage
	\listoffigures
	\addtocontents{lof}{\bfseries Figure \hfill Pages\par}
	
\end{singlespace}
	% --- 1. LIST OF ABBREVIATIONS ---
	%\clearpage
	\phantomsection
	% Nota: Kita buang \addcontentsline manual di sini sebab 
	% dalam preamble anda sudah ada option 'toc'.
	\renewcommand*{\arraystretch}{0.5}
	\glsaddall 
	\printglossary[style=mylong, type=\acronymtype, title=List of Abbreviations]
	
	% --- 2. LIST OF APPENDICES ---
	\clearpage % <--- WAJIB: Buang tanda '%' supaya dia jadi muka surat xiii yang betul
	\phantomsection 
	\addcontentsline{toc}{chapter}{List of Appendices}
		\listofappendices
	\addtocontents{apc}{\protect\textbf{Appendix} \protect\hfill \protect\textbf{Pages}\par}
	\addtocontents{apc}{\vspace{0.5em}}
	\addtocontents{toc}{\vspace{0.8em}}

	% --- MAIN MATTER ---
	\clearpage
	\pagenumbering{arabic}
	%\addtocontents{toc}{\protect\vspace{1em}\textbf{CHAPTER}\par}
	\doublespacing
	
	\include{chapter/ch1}
	\include{chapter/ch2}
	\include{chapter/ch3}
	\include{chapter/ch4}
	\include{chapter/ch5}
	\include{chapter/ch6}
	\include{chapter/ch7}
	
	% --- BACK MATTER ---
	\clearpage
	\bibliographystyle{apalike}
	\bibliography{bibliography}
	
	\include{appendix/one}
	
	\clearpage
	\addcontentsline{toc}{chapter}{Biodata of the Author}
	\include{back/biodata}
	
\end{document}