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, updateLaporan } from "@/actions/pelaporan";
import { createSanksi } from "@/actions/sanksi";
import { getPelanggaran, getPelanggaranSanksi } from "@/actions/pelanggaran";
import { connect } from "react-redux";
import Loader from "@/components/Common/Loader";
import { ToastContainer, toast } from "react-toastify";
import BeritaAcara from "@/components/Sanksi/BeritaAcara_A";
import { getAutoSave, inputAutoSave } from "@/actions/autosave";



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

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

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

	componentDidMount = async () => {
		const { query, token } = this.props;
		const { id } = query;
		const pelaporan = await getOneLaporan(token, id);
		const { data: listSanksi } = await getPelanggaranSanksi(token)
		const getDataSave = await getAutoSave({ id, laporan: true });
		const autoSaveDataSuratBA = getDataSave.data?.PenetapanSanksi?.dataSuratBA;
		const autoSaveDataPelanggaran = getDataSave.data?.PenetapanSanksi?.dataPelanggaran.data;
		this.setState({ pelaporan, listSanksi, autoSaveDataSuratBA, autoSaveDataPelanggaran });
	};

	handleAutoSave = async () => {
		const { query, token } = this.props;
		const { id } = query;
		const { dataSuratBA, dataUpload, dataPelanggaran } = this.state
		await inputAutoSave({ data: { PenetapanSanksi: { dataSuratBA, dataUpload, dataPelanggaran } }, token, id, laporan: true })

	}
	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("from_date", this.state.dataUpload.awalsanksi);
			formdata.append("to_date", this.state.dataUpload.akhirsanksi);
			formdata.append("tanggal_terima_sanksi", this.state.dataUpload.terimaSuratSanksi);
			formdata.append("tanggal_akhir_keberatan", this.state.dataUpload.tglAkhirKeberatan);
			formdata.append("pelanggaran_id", this.state.dataPelanggaran.data.map((e) => e._id).join());
			if (this.state.dataUpload.files && this.state.dataUpload.files.length > 0) {
				this.state.dataUpload.files.forEach((e) => {
					formdata.append("dokumen", e);
				});
			}
			if (this.state.dataUpload.filesBeritaAcara && this.state.dataUpload.filesBeritaAcara.length > 0) {
				this.state.dataUpload.filesBeritaAcara.forEach((e) => {
					formdata.append("berita_acara", e);
				});
			}
			if (this.state.dataUpload.fileTandaTerimaSS && this.state.dataUpload.fileTandaTerimaSS.length > 0) {
				this.state.dataUpload.fileTandaTerimaSS.forEach((e) => {
					formdata.append("dokumen_terima_sanksi", 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 });
		}
	};


	handleDelegasi = async (data) => {
		const { token, query } = this.props;
		const { id } = query;
		let update = null;
		const toastid = toast.loading("Please wait...");
		data.change_role = "true";
		data.keterangan = "delegasi ke DIKTI"
		update = await updateLaporan(token, id, data);

		if (!update) {
			toast.update(toastid, { render: "Laporan gagal didelegasi", type: "error", isLoading: false, autoClose: true, closeButton: true });
		} else {
			toast.update(toastid, { render: "Laporan berhasil didelegasi", type: "success", isLoading: false, autoClose: true, closeButton: true });
			Router.push("/app/sanksi");
		}
	};

	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 });
	};
	setDataSuratBA = (data) => {
		this.setState({ dataSuratBA: data });
	};

	render() {
		const { dataUpload, dataPelanggaran, pelaporan, dataSuratBA, autoSaveDataSuratBA, autoSaveDataPelanggaran } = this.state;
		return (
			<ContentWrapper unwrap>
				{/* <Header /> */}
				<div className="p-3">
					<div className="content-heading">
						<span className="font-color-white">Penetapan Sanksi</span>
						<div className="ml-auto">
							<Link href="/app/sanksi">
								<Button className="color-3e3a8e" color>
									<span className="font-color-white">&lt; Kembali</span>
								</Button>
							</Link>
						</div>
					</div>
					<Card className="card-default">
						<CardBody>
							<Row>
								<Col xs="4">
									<Nav pills vertical={true} className="cursor-pointer">
										<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 Jenis Pelanggaran</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. Berita Acara Pleno</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. Penetapan Sanksi</h4>
											</NavLink>
										</NavItem>
										<NavItem style={stepNavitemStyle}>
											<NavLink
												tag="div"
												className={classnames({
													active: this.state.activeStep === "6",
												})}
												onClick={this.toggleStep("6")}
											>
												<h4 className="text-left my-3">6. 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 btn-login color-3e3a8e" color onClick={this.toggleStep("2")}>
													<span className="font-color-white">Next</span>
												</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 className="btn-login color-3e3a8e" onClick={this.toggleStep("1")}>
													<span className="font-color-white">Previous</span>
												</Button>
												<Button className="ml-auto btn-login color-3e3a8e" color onClick={this.toggleStep("3")}>
													<span className="font-color-white">Next</span>
												</Button>
											</div>
										</TabPane>
										<TabPane tabId="3">
											<div className="pt-3 mb-3">
												<h2>Penetapan Jenis Pelanggaran</h2>
												<TablePenetapanSanksi handleDelegasi={this.handleDelegasi} handleAutoSave={this.handleAutoSave} setCheckedData={this.setCheckedData} dataPelanggaran={dataPelanggaran.data} data={pelaporan.data} />
											</div>
											<hr />
											<div className="d-flex">
												<Button color className="btn-login color-3e3a8e" onClick={this.toggleStep("2")}>
													<span className="font-color-white">Previous</span>
												</Button>
												<Button className="ml-auto btn-login color-3e3a8e" color onClick={this.toggleStep("4")}>
													<span className="font-color-white">Next</span>
												</Button>
											</div>
										</TabPane>
										<TabPane tabId="4">
											<Card>
												<BeritaAcara query={this.props.query} handleAutoSave={this.handleAutoSave} dataPelanggaran={dataPelanggaran.data} setDataSuratBA={this.setDataSuratBA} dataLaporan={pelaporan} dataSuratBA={dataSuratBA} />
											</Card>
											<div className="d-flex">
												<Button color className="btn-login color-3e3a8e" onClick={this.toggleStep("3")}>
													<span className="font-color-white">Previous</span>
												</Button>
												<Button className="ml-auto btn-login color-3e3a8e" color onClick={this.toggleStep("5")}>
													<span className="font-color-white">Next</span>
												</Button>
											</div>
										</TabPane>
										<TabPane tabId="5">
											<div className="pt-3 mb-3">
												<h2>Penetapan Sanksi</h2>
												{this.state.listSanksi && <UploadSurat setUploadSuratSanksi={this.setUploadSuratSanksi} listSanksi={this.state.listSanksi} dataPelanggaran={dataPelanggaran.data} query={this.props.query} handleAutoSave={this.handleAutoSave} />}
											</div>
											<hr />
											<div className="d-flex">
												<Button color className="btn-login color-3e3a8e" onClick={this.toggleStep("4")}>
													<span className="font-color-white">Previous</span>
												</Button>
												<Button className="ml-auto btn-login color-3e3a8e" color onClick={this.toggleStep("6")}>
													<span className="font-color-white">Next</span>
												</Button>
											</div>
										</TabPane>
										<TabPane tabId="6">
											<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 className="btn-login color-3e3a8e" onClick={this.toggleStep("5")}>
													<span className="font-color-white">Previous</span>
												</Button>
												<Button className="ml-auto btn-login color-3e3a8e" color onClick={this.done}>
													<span className="font-color-white">Done</span>
												</Button>
											</div>
										</TabPane>
									</TabContent>
								</Col>
							</Row>
						</CardBody>
					</Card>
				</div>
			</ContentWrapper>
		);
	}
}

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