import React, { Component } from "react";
import ContentWrapper from "@/components/Layout/ContentWrapper";
import { getOneSanksi } from "@/actions/sanksi";
import Header from "@/components/Main/Header";
import DetailPT from "@/components/Main/DetailPT";
import DetailSanksi from "@/components/Main/DetailSanksi";
import DetailJawaban from "@/components/PT/JawabanKeberatan/DetailJawaban";
import ModalPermohonan from "@/components/PT/JawabanKeberatan/ModalPermohonan";
import Riwayat from "@/components/PT/JawabanKeberatan/Riwayat";
import Link from "next/link";
import { Row, Col, Card, CardBody, Button } from "reactstrap";
import { connect } from "react-redux";
import { withRouter } from "next/router";
import Loader from "@/components/Common/Loader";

class JawabanKeberatan extends Component {
	constructor(props) {
		super(props);
		this.state = {
			modal: false,
			sanksi: {},
			pt: null,
		};
	}

	static getInitialProps = ({ query }) => ({ query });

	componentDidMount = async () => {
		const { token, query } = this.props;
		const sanksi = await getOneSanksi(token, query.id);
		this.setState({ sanksi, pt: sanksi.data.laporan.pt });
	};

	toggleModal = (value = true) => {
		if (!value) {
			this.setState({ modal: false });
		} else {
			this.setState({ modal: !this.state.modal });
		}
	};

	render() {
		const { sanksi, pt } = this.state;
		return (
			<ContentWrapper unwrap>
				{sanksi.data && <ModalPermohonan toggleModal={this.toggleModal} modal={this.state.modal} query={this.props.router.query} data={sanksi.data} />}
				{pt && <Header data={pt} />}
				<div className="p-3">
					<div className="content-heading">
						<div>Jawaban Atas Permohonan Keberatan</div>
						<div className="ml-auto">
							<Link href="/pt/jawaban-keberatan">
								<button className="btn btn-sm btn-secondary text-sm">&lt; back</button>
							</Link>
						</div>
					</div>
					<Row>
						{sanksi.data ? (
							<Col xl="9">
								<Card className="card-default">
									<CardBody>
										<Row>
											<Col lg={12}>
												<DetailSanksi data={sanksi.data} />
												<DetailJawaban data={sanksi.data.jawaban.keberatan} />
												{sanksi.data.jawaban.keberatan.status !== "Membatalkan Keputusan" &&
													(new Date(sanksi.data.batas_waktu.banding).getTime() > Date.now() ? (
														<>
															<p>
																Setelah membaca jawaban permohonan keberatan atas pengenaan sanksi, jika Perguruan Tinggi bermaksud mengajukan permohonan banding kepada atasan pemberi sanksi maka dapat
																menekan tombol di bawah ini paling lambat {moment(sanksi.data.batas_waktu.banding).format("DD MMMM YYYY")}
															</p>
															<Button color="primary" onClick={this.toggleModal} disabled={sanksi.data.pengajuan.banding || false}>
																Ajukan Banding
															</Button>
														</>
													) : (
														<p>Pengajuan ditutup</p>
													))}
											</Col>
										</Row>
									</CardBody>
								</Card>
							</Col>
						) : (
							<Loader />
						)}
						<Col xl="3">{pt && <DetailPT data={pt} />}</Col>
					</Row>
					{sanksi.data && (
						<Row>
							<Col>{sanksi.data.jawaban.keberatan.status !== "Membatalkan Keputusan" && <Riwayat data={sanksi.data.pengajuan?.banding ? sanksi.data.pengajuan.banding : null} />}</Col>
						</Row>
					)}
				</div>
			</ContentWrapper>
		);
	}
}

const mapStateToProps = (state) => ({ user: state.user, token: state.token });
export default connect(mapStateToProps)(withRouter(JawabanKeberatan));
