import React, { Component } from "react";
import ContentWrapper from "@/components/Layout/ContentWrapper";
import { getSanksi } 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 } from "reactstrap";
import { connect } from "react-redux";
import { withRouter } from "next/router";
import Loader from "@/components/Common/Loader";

class Keberatan extends Component {
	state = {
		modal: false,
		sanksi: {},
	};

	componentDidMount = async () => {
		const { user } = this.props;
		const { noSanksi } = this.props.router.query;
		const sanksi = await getSanksi({ noSanksi, ptId: user.peran[0].organisasi.id });
		this.setState({ sanksi });
	};

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

	render() {
		const { sanksi } = this.state;
		return (
			<ContentWrapper unwrap>
				{sanksi?.data && <ModalPermohonan toggleModal={this.toggleModal} modal={this.state.modal} query={this.props.router.query} data={sanksi.data[0]} />}
				<Header data={this.props.pt[0]} />
				<div className="p-3">
					<div className="content-heading">
						<div>Permohonan Keberatan</div>
						<div className="ml-auto">
							<Link href="/app/pt/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[0]} />
												{new Date(sanksi.data[0].sanksi.close_keberatan).getTime() > Date.now() ? (
													<>
														<p>
															Setelah membaca surat keputusan sanksi tersebut, jika Perguruan Tinggi bermaksud mengajukan permohonan keberatan maka dapat menekan tombol di bawah ini paling lambat{" "}
															{moment(sanksi.data[0].sanksi.close_keberatan).format("DD MMMM YYYY")}
														</p>
														<Button color="primary" onClick={this.toggleModal} disabled={sanksi.data[0].sanksi.keberatan || false}>
															Ajukan Permohonan Keberatan
														</Button>
													</>
												) : (
													<p>Pengajuan ditutup</p>
												)}
											</Col>
										</Row>
									</CardBody>
								</Card>
							</Col>
						) : (
							<Loader />
						)}
						<Col xl="3">{this.props.pt && <DetailPT data={this.props.pt[0]} />}</Col>
					</Row>
					{sanksi?.data && (
						<Row>
							<Col>
								<Riwayat data={sanksi.data[0]} />
							</Col>
						</Row>
					)}
				</div>
			</ContentWrapper>
		);
	}
}

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