import React, { Component } from "react";
import Router from "next/router";
import { Row, Col, FormGroup, Button, Modal, ModalHeader, ModalBody, ModalFooter } from "reactstrap";
import { addKeberatan } from "@/actions/keberatan";
import { connect } from "react-redux";
import { notifKeberatan } from "@/actions/notifikasi";

let Dropzone = null;
class DropzoneWrapper extends Component {
	state = {
		isClient: false,
	};
	componentDidMount = () => {
		Dropzone = require("react-dropzone").default;
		this.setState({ isClient: true });
	};
	render() {
		return Dropzone ? <Dropzone {...this.props}>{this.props.children}</Dropzone> : null;
	}
}

export class ModalPermohonan extends Component {
	constructor(props) {
		super(props);
		this.state = {
			modal1: false,
			files: [],
		};
	}

	onDrop = (files) => {
		this.setState({
			files: files.map((file) =>
				Object.assign(file, {
					preview: URL.createObjectURL(file),
				})
			),
			stat: "Added " + files.length + " file(s)",
		});
	};

	uploadFiles = (e) => {
		e.preventDefault();
		e.stopPropagation();
		this.setState({
			stat: this.state.files.length ? "Dropzone ready to upload " + this.state.files.length + " file(s)" : "No files added.",
		});
	};

	clearFiles = (e) => {
		e.preventDefault();
		e.stopPropagation();
		this.setState({
			stat: this.state.files.length ? this.state.files.length + " file(s) cleared." : "No files to clear.",
		});
		this.setState({
			files: [],
		});
	};

	toggleModal1 = () => {
		this.props.toggleModal(false);
		this.setState({
			modal1: !this.state.modal1,
		});
	};

	onSubmit = async (e) => {
		e.preventDefault();
		const { user, query, data } = this.props;
		const { noSanksi } = query;
		const formdata = new FormData();
		if (this.state.files.length > 0) {
			this.state.files.forEach((e) => {
				formdata.append("files", e);
			});

			const added = await addKeberatan({ noSanksi, ptId: user.peran[0].organisasi.id }, formdata);
			// formdata.append("keberatan", added.add.sanksi.keberatan._id);
			// formdata.append("data", added.add.sanksi.keberatan._id);
			// formdata.append("model", "Keberatan");
			// await addDocPerbaikan({ noSanksi, ptId: "0BCE4DB7-B207-445D-8D03-0C54B7688252" }, formdata);
			// console.log(added);
			if (added) {
				// console.log({ lembaga: data.sanksi.user.lembaga, pt_name: user.peran[0].organisasi.nama, no_laporan: data.sanksi.no_sanksi });
				await notifKeberatan({ lembaga: data.sanksi.user.lembaga, pt_name: user.peran[0].organisasi.nama, no_sanksi: data.sanksi.no_sanksi });
				// console.log(notif);
				Router.push({
					pathname: "/app/pt/keberatan",
				});
			}
		}
	};

	handleKirim = (e) => {
		this.setState({
			modal1: !this.state.modal1,
		});
		this.onSubmit(e);
	};

	render() {
		const { files } = this.state;
		// console.log(this.props.data.sanksi);

		const thumbs = files.map((file, index) => (
			<Col md={3} key={index}>
				<img className="img-fluid mb-2" src={file.preview} alt="Item" />
			</Col>
		));
		return (
			<>
				<Modal isOpen={this.props.modal} toggle={this.props.toggleModal}>
					<ModalBody>Apakah anda akan mengajukan permohonan keberatan atas pengenaan sanksi?</ModalBody>
					<ModalFooter>
						<Button color="primary" onClick={this.toggleModal1}>
							Ya
						</Button>{" "}
						<Button color="secondary" onClick={this.props.toggleModal}>
							Tidak
						</Button>
					</ModalFooter>
				</Modal>
				<Modal isOpen={this.state.modal1} toggle={this.toggleModal1}>
					<ModalHeader toggle={this.toggleModal1}>Unggah Dokumen Permohonan Keberatan</ModalHeader>
					<ModalBody>
						<form className="form-horizontal" method="get" action="/" onSubmit={this.onSubmit}>
							<FormGroup>
								<label>Dalam hal mengajukan permohonan keberatan maka wajib mengunggah surat permohonan keberatan atas pengenaan sanksi administratif & dokumen pendukungnya</label>
								<div>
									<DropzoneWrapper className="" onDrop={this.onDrop}>
										{({ getRootProps, getInputProps, isDragActive }) => {
											return (
												<div {...getRootProps()} className={"dropzone card p-3 " + (isDragActive ? "dropzone-drag-active" : "")}>
													<input {...getInputProps()} />
													<div className="dropzone-previews flex">{this.state.files.length > 0 ? <Row>{thumbs}</Row> : <div className="text-center dz-default dz-message">Drop files here to upload</div>}</div>
													<div className="d-flex align-items-center">
														<small className="ml-auto">
															<button type="button" className="btn btn-link" onClick={this.clearFiles}>
																Clear files
															</button>
														</small>
													</div>
												</div>
											);
										}}
									</DropzoneWrapper>
									<span className="form-text">Multiple files upload</span>
								</div>
							</FormGroup>
						</form>
					</ModalBody>
					<ModalFooter>
						<Button color="primary" onClick={this.handleKirim}>
							Kirim
						</Button>
					</ModalFooter>
				</Modal>
			</>
		);
	}
}

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