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 Riwayat from "@/components/PT/Keberatan/Riwayat";
import ModalPermohonan from "@/components/PT/Keberatan/ModalPermohonan";
import Link from "next/link";
import moment from "moment";
import { Row, Col, Card, CardBody, Button, Modal, ModalBody, ModalFooter } from "reactstrap";
import { connect } from "react-redux";
import Router from "next/router";
import Loader from "@/components/Common/Loader";
import { ToastContainer, toast } from "react-toastify";

class Sanksi extends Component {
	state = {
		modal: false,
		sanksi: {},
		pt: null,
	};

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

	componentDidMount = async () => {
		const { token, query } = this.props;
		const sanksi = await getOneSanksi(token, query.id);
		updatePT(token, query.id, { is_read: true })
		this.setState({ sanksi, pt: sanksi.data.laporan.pt });
	};
	setModal = (modal) => {
		this.setState({
			modal: !this.state.modal
		})
	}

	render() {
		const { sanksi, pt } = this.state;
		console.log(sanksi)
		return (
			<ContentWrapper unwrap>
				<Modal isOpen={this.state.modal} toggle={this.props.toggleModal}>
					<ModalBody>Apakah anda akan tidak mengajukan permohonan keberatan 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_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.setModal}>
							Tidak
						</Button>
					</ModalFooter>
				</Modal>
				{pt && <Header data={pt} />}
				<div className="p-3">
					<div className="content-heading">
						<span className="font-color-white">
							Sanksi
						</span>
						<div className="ml-auto">
							<Link href="/pt/keberatan">
								<Button className="color-3e3a8e" 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>
											<Col lg={12}>
												<DetailSanksi data={sanksi.data} />
												{new Date(sanksi.data.batas_waktu.keberatan).getTime() > Date.now() ? (
													<>
														<p style={{ fontSize: '1vw' }}>
															<strong>
																Setelah membaca surat keputusan sanksi tersebut, Apakah Perguruan Tinggi bermaksud mengajukan keberatan?
															</strong>
														</p>
														<p style={{ fontSize: '0.9vw' }}>
															Pengajuan dilakukan paling lambat tanggal {moment(sanksi.data.batas_waktu.keberatan).locale("id").format("DD MMMM YYYY")}
														</p>
														<p className="lead">
															<Link href={{ pathname: "/pt/keberatan/detail", query: { id: sanksi.data._id } }}>
																<span className="btn-radius">
																	<Button color="" className="btn-labeled-notHover" onClick={async () => {
																		// const toastid = toast.loading("Please wait...");
																		try {
																			const { token, query } = this.props;
																			await updatePT(token, query.id, { is_pengajuan_keberatan: true })
																			// toast.update(toastid, { render: "Berhasil", type: "success", isLoading: false, autoClose: true, closeButton: true });
																		} catch (error) {
																			// toast.update(toastid, { render: "Gagal", type: "error", isLoading: false, autoClose: true, closeButton: true });
																		}
																	}

																	}>
																		<h4 className="mt-2 mb-md-2 text-center font-color-white pl-3 pr-3">Ya</h4>
																	</Button>
																</span>
															</Link>
															<span className="btn-radius">
																<Button disabled={sanksi.data.is_pengajuan_keberatan === true || sanksi.data.is_pengajuan_keberatan === false} color className="btn-labeled-3-notHover" onClick={this.setModal} >
																	<h4 className=" mt-1 mb-md-2 text-center">Tidak</h4>
																</Button>
															</span>
														</p>
													</>
												) : (
													<p>Pengajuan ditutup</p>
												)}
											</Col>
										</Row>
									</CardBody>
								</Card>
							</Col>
						) : (
							<Loader />
						)}
						<Col xl="3">{pt && <DetailPT data={pt} />}</Col>
					</Row>
					{/* {sanksi.data && (
						<Row>
							<Col>
								<Riwayat data={sanksi.data?.pengajuan?.keberatan ? sanksi.data.pengajuan.keberatan : null} />
							</Col>
						</Row>
					)} */}
				</div>
			</ContentWrapper >
		);
	}
}

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