import React, { Component } from "react";
import { Card, CardBody, TabContent, TabPane, Nav, NavItem, NavLink, Button, Row, Col } from "reactstrap";
import Router from "next/router";
import classnames from "classnames";
import ContentWrapper from "@/components/Layout/ContentWrapper";
import DetailLaporan from "@/components/Main/DetailLaporan";
import Header from "@/components/Main/Header";
import TableRiwayat from "@/components/Pemeriksaan/TableRiwayat";
import UploadSurat from "@/components/Sanksi/UploadSurat";
import Ringkasan from "@/components/Sanksi/Ringkasan";
import TablePenetapanSanksi from "@/components/Sanksi/TablePenetapanSanksi";
import Link from "next/link";
import { getOneLaporan } from "@/actions/pelaporan";
import { createSanksi } from "@/actions/sanksi";
import { getPelanggaran } from "@/actions/pelanggaran";
import { connect } from "react-redux";
import Loader from "@/components/Common/Loader";
import { ToastContainer, toast } from "react-toastify";

const stepNavitemStyle = {
	backgroundColor: "#fcfcfc",
};

class ProsesSanksi extends Component {
	dataUpload = {};
	constructor(props) {
		super(props);
		this.state = {
			activeStep: "1",
			dataUpload: null,
			dataPelanggaran: {},
			pelaporan: {},
		};
	}

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

	componentDidMount = async () => {
		const { query, token } = this.props;
		const { id } = query;
		const pelaporan = await getOneLaporan(token, id);
		this.setState({ pelaporan });
	};

	done = async (e) => {
		const toastid = toast.loading("Please wait...");
		try {
			e.preventDefault();
			const { query, token } = this.props;
			const { id } = query;
			const formdata = new FormData();
			formdata.append("no_sanksi", this.state.dataUpload.nomorSanksi);
			formdata.append("keterangan", this.state.dataUpload.keterangan);
			formdata.append("pelanggaran_id", this.state.dataPelanggaran.data.map((e) => e._id).join());
			if (this.state.dataUpload.files.length > 0) {
				this.state.dataUpload.files.forEach((e) => {
					formdata.append("dokumen", e);
				});
			}
			await createSanksi(token, id, formdata);
			toast.update(toastid, { render: "All is good", type: "success", isLoading: false, autoClose: true, closeButton: true });
			Router.push({
				pathname: "/app/sanksi",
			});
		} catch (error) {
			toast.update(toastid, { render: "All is not good", type: "error", isLoading: false, autoClose: true, closeButton: true });
		}
	};

	toggleStep = (activeStep) => () => {
		if (this.state.activeStep !== activeStep) {
			this.setState({
				activeStep,
			});
		}
	};

	setCheckedData = async (data) => {
		const { token } = this.props;
		const result = await getPelanggaran(token, { id: data.join(",") });
		this.setState({ dataPelanggaran: result });
	};

	setUploadSuratSanksi = (data) => {
		this.setState({ dataUpload: data });
	};

	render() {
		const { dataUpload, dataPelanggaran, pelaporan } = this.state;
		return (
			<ContentWrapper unwrap>
				{/* <Header /> */}
				<div className="p-3">
					<div className="content-heading">
						<div>Penetapan Sanksi</div>
						<div className="ml-auto">
							<Link href="/app/sanksi">
								<button className="btn btn-sm btn-secondary text-sm">&lt; back</button>
							</Link>
						</div>
					</div>
					<Card className="card-default">
						<CardBody>
							<Row>
								<Col xs="4">
									<Nav pills vertical={true}>
										<NavItem style={stepNavitemStyle}>
											<NavLink
												tag="div"
												className={classnames({
													active: this.state.activeStep === "1",
												})}
												onClick={this.toggleStep("1")}
											>
												<h4 className="text-left my-3">1. Detail Laporan</h4>
											</NavLink>
										</NavItem>
										<NavItem style={stepNavitemStyle}>
											<NavLink
												tag="div"
												className={classnames({
													active: this.state.activeStep === "2",
												})}
												onClick={this.toggleStep("2")}
											>
												<h4 className="text-left my-3">2. Hasil Evaluasi</h4>
											</NavLink>
										</NavItem>
										<NavItem style={stepNavitemStyle}>
											<NavLink
												tag="div"
												className={classnames({
													active: this.state.activeStep === "3",
												})}
												onClick={this.toggleStep("3")}
											>
												<h4 className="text-left my-3">3. Penetapan Sanksi</h4>
											</NavLink>
										</NavItem>
										<NavItem style={stepNavitemStyle}>
											<NavLink
												tag="div"
												className={classnames({
													active: this.state.activeStep === "4",
												})}
												onClick={this.toggleStep("4")}
											>
												<h4 className="text-left my-3">4. Nomor Surat Keputusan Sanksi</h4>
											</NavLink>
										</NavItem>
										<NavItem style={stepNavitemStyle}>
											<NavLink
												tag="div"
												className={classnames({
													active: this.state.activeStep === "5",
												})}
												onClick={this.toggleStep("5")}
											>
												<h4 className="text-left my-3">5. Ringkasan</h4>
											</NavLink>
										</NavItem>
									</Nav>
								</Col>
								<Col xs="8">
									<TabContent activeTab={this.state.activeStep} className="border-0">
										<TabPane tabId="1">
											<div className="pt-3 mb-3">
												<h2>Detail Laporan</h2>
												{pelaporan.data ? <DetailLaporan noTitle data={pelaporan.data} /> : <Loader />}
											</div>
											<hr />
											<div className="d-flex">
												<Button className="ml-auto" color="primary" onClick={this.toggleStep("2")}>
													Next
												</Button>
											</div>
										</TabPane>
										<TabPane tabId="2">
											<div className="pt-3 mb-3">
												<h2>Hasil Evaluasi</h2>
												<Card className="card-default">
													{pelaporan.data ? (
														<CardBody>
															<TableRiwayat data={pelaporan.data} />
														</CardBody>
													) : (
														<Loader />
													)}
												</Card>
											</div>
											<hr />
											<div className="d-flex">
												<Button color="secondary" onClick={this.toggleStep("1")}>
													Previous
												</Button>
												<Button className="ml-auto" color="primary" onClick={this.toggleStep("3")}>
													Next
												</Button>
											</div>
										</TabPane>
										<TabPane tabId="3">
											<div className="pt-3 mb-3">
												<h2>Penetapan Sanksi</h2>
												<TablePenetapanSanksi setCheckedData={this.setCheckedData} />
											</div>
											<hr />
											<div className="d-flex">
												<Button color="secondary" onClick={this.toggleStep("2")}>
													Previous
												</Button>
												<Button className="ml-auto" color="primary" onClick={this.toggleStep("4")}>
													Next
												</Button>
											</div>
										</TabPane>
										<TabPane tabId="4">
											<div className="pt-3 mb-3">
												<h2>Nomor Surat Keputusan Sanksi</h2>
												<UploadSurat setUploadSuratSanksi={this.setUploadSuratSanksi} />
											</div>
											<hr />
											<div className="d-flex">
												<Button color="secondary" onClick={this.toggleStep("3")}>
													Previous
												</Button>
												<Button className="ml-auto" color="primary" onClick={this.toggleStep("5")}>
													Next
												</Button>
											</div>
										</TabPane>
										<TabPane tabId="5">
											<div className="pt-3 mb-3">
												<h2>Ringkasan</h2>
												{pelaporan.data ? <Ringkasan dataLaporan={pelaporan.data} dataPelanggaran={dataPelanggaran.data} dataUpload={dataUpload} /> : <Loader />}
											</div>
											<hr />
											<div className="d-flex">
												<Button color="secondary" onClick={this.toggleStep("4")}>
													Previous
												</Button>
												<Button className="ml-auto" color="primary" onClick={this.done}>
													Done
												</Button>
											</div>
										</TabPane>
									</TabContent>
								</Col>
							</Row>
						</CardBody>
					</Card>
				</div>
			</ContentWrapper>
		);
	}
}

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