import React, { Component } from "react";
import ContentWrapper from "@/components/Layout/ContentWrapper";
import { getOneSanksi, updatePT } 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, Modal, ModalBody, ModalFooter } from "reactstrap";
import { connect } from "react-redux";
import { withRouter } from "next/router";
import Loader from "@/components/Common/Loader";
import Router from "next/router";
import { ToastContainer, toast } from "react-toastify";

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

	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 });
		}
	};
	toPerbaikan = () => {
		Router.push("/pt/dokumen-perbaikan");
	}
	setModalTidak = (modalTidak) => {
		this.setState({
			modalTidak: !this.state.modalTidak
		})
	}

	render() {
		const { sanksi, pt } = this.state;
		return (
			<ContentWrapper unwrap>
				<Modal isOpen={this.state.modalTidak} >
					<ModalBody>Apakah anda ingin membatalkan permohonan banding atas pengenaan sanksi?</ModalBody>
					<ModalFooter>

						<Button color className="btn-login" onClick={async () => {
							const toastid = toast.loading("Please wait...");
							try {
								const { token, query } = this.props;
								await updatePT(token, query.id, { is_pengajuan_banding: false, is_pengajuan_keberatan: false })
								toast.update(toastid, { render: "All is good", type: "success", isLoading: false, autoClose: true, closeButton: true });
								Router.push("/pt/dokumen-perbaikan");
							} catch (error) {
								toast.update(toastid, { render: "All is not good", type: "error", isLoading: false, autoClose: true, closeButton: true });
							}
						}
						}>
							<span className="font-color-white">Ya</span>
						</Button>
						<Button color className="btn-v2" onClick={this.setModalTidak}>
							Tidak
						</Button>
					</ModalFooter>
				</Modal>
				{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">
						<span className="font-color-white">
							Jawaban Atas Permohonan Keberatan
						</span>
						<div className="ml-auto">
							<Link href="/pt/jawaban-keberatan">
								<Button className="btn-login" color>
									<span className="font-color-white">
										&lt; Kembali
									</span>
								</Button>
							</Link>
						</div>
					</div>
					<Row>
						{sanksi.data ? (
							<Col xl="9">
								<Card className="card-default">
									<CardBody>
										<Row>
											{sanksi.data.jawaban?.keberatan ? (
												<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 style={{ fontSize: '1vw' }}>
																	<strong>
																		Setelah membaca surat keputusan sanksi tersebut, Apakah Perguruan Tinggi bermaksud mengajukan permohonan banding?
																	</strong>
																</p>
																<p style={{ fontSize: '0.9vw' }}>
																	Pengajuan dilakukan paling lambat tanggal {moment(sanksi.data.batas_waktu.banding).locale("id").format("DD MMMM YYYY")}
																</p>
																<p className="lead">
																	<span className="btn-radius">
																		<Button color="" disabled={sanksi.data.pengajuan.banding || false} className="btn-labeled-notHover" onClick={this.toggleModal}>
																			<h4 className="mt-2 mb-md-2 text-center font-color-white pl-3 pr-3">Ya</h4>
																		</Button>
																	</span>
																	<span className="btn-radius">
																		<Button disabled={sanksi.data.pengajuan.banding || false} color className="btn-labeled-3-notHover" onClick={this.setModalTidak}
																		>
																			<h4 className=" mt-1 mb-md-2 text-center">Tidak</h4>
																		</Button>
																	</span>
																</p>
															</>
														) : (
															<p>Pengajuan ditutup</p>
														))}


												</Col>) : (
												<Col lg={12}>
													<h3 className=" font-weight-bold">Menunggu Jawaban Atas Permohonan Keberatan</h3>
													<div className=" tengah"><img className=" tengah" src="/static/img/Wait-Jawaban.png" alt="gambar" /></div>
												</Col>
											)}
										</Row>
									</CardBody>
								</Card>
							</Col>
						) : (
							<Loader />
						)}
						<Col xl="3">{pt && <DetailPT data={pt} />}</Col>
					</Row>
					{sanksi.data ? (

						<Col xl="9">
							<p className="pb-5">
								<Link href={{ pathname: "/pt/keberatan/detail", query: { id: sanksi.data._id } }}>
									<span className="btn-radius float-left">
										<Button color="" className="btn-labeled-3-notHover" >
											<h4 className=" mt-1 mb-md-2 text-center">Previous</h4>
										</Button>
									</span>
								</Link>
								{sanksi.data.pengajuan?.banding ? (
									<Link href={{ pathname: "/pt/jawaban-banding/detail", query: { id: sanksi.data._id } }}>
										<span className="btn-radius float-rigt">
											<Button color="" className="btn-labeled-notHover" >
												<h4 className="mt-2 mb-md-2 text-center font-color-white pl-3 pr-3">Next</h4>
											</Button>
										</span>
									</Link>
								) : (
									<span className="btn-radius float-rigt">
										<Button color="" className="btn-labeled-notHover" disabled={true} >
											<h4 className="mt-2 mb-md-2 text-center font-color-white pl-3 pr-3">Next</h4>
										</Button>
									</span>
								)}

							</p>
						</Col>
					) : ("")}
					{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));
