import React, { Component } from "react";
import { Row, Col, Input, FormGroup, Label, Progress } from "reactstrap";
import Select from "react-select";
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
// import "react-datepicker/dist/react-datepicker.css";
import ms from "ms";
import { addDays, addMonths } from 'date-fns';
import id from 'date-fns/locale/id';
// registerLocale('id', id)
import { getAutoSave, inputAutoSave } from "@/actions/autosave";

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;
	}
}

const data = [
	{
		value: "Sanksi Administratif Sedang",
		// label_sanksi: "- Sanksi Administratif ringan"
	},
	{
		sanksi: "Penghentian Pembinaan PT",
		value: "Penghentian Pembinaan PT",
		label_sanksi: "- Sanksi Administratif Berat",
	},
	{
		sanksi: "Pencabutan Izin Perguruan Tinggi Swasta",
		value: "Pencabutan Izin Perguruan Tinggi Swasta",
		label_sanksi: "- Sanksi Administratif Berat",
	},
	{
		sanksi: "Penghentian Pembinaan Program Studi",
		value: "Penghentian Pembinaan Program Studi",
		label_sanksi: "- Sanksi Administratif Berat",
	},
	{
		sanksi: "Pencabutan Izin Program Studi",
		value: "Pencabutan Izin Program Studi",
		label_sanksi: "- Sanksi Administratif Berat",
	},
	{
		sanksi: "Pembubaran Perguruan Tinggi Negeri",
		value: "Pembubaran Perguruan Tinggi Negeri",
		label_sanksi: "- Sanksi Administratif Berat",
	},
];

const listSanksi = data.map((d) => ({
	value: d.value,
	label: d.sanksi,
	label_sanksi: d.label_sanksi,
}));
const formatOptionLabel = ({ value, sanksi, label_sanksi }) => (
	<div style={{ display: "flex" }}>
		<span className="">{value}</span>
		<div style={{ marginLeft: "10px", color: "#adaca8" }}>{label_sanksi}</div>
	</div>
);
export class UploadSurat extends Component {
	constructor(props) {
		super(props);
		const tmt_awal = new Date();
		this.state = {
			files: [],
			nomorSanksi: "",
			keterangan: "",
			listSanksi: "",
			startDay: tmt_awal,
			maxDay: "",
			isiTmt: "",
			awalsanksi: "",
			akhirsanksi: "",
			tmtCheck: false,
			filesBeritaAcara: [],
			saveData: [],
			terimaSuratSanksi: "",
			tglAkhirKeberatan: "",
			filesTandaTerimaSS: [],

		};
	}

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

	onDropBA = (filesBeritaAcara) => {
		this.setState({
			filesBeritaAcara: filesBeritaAcara.map((file) =>
				Object.assign(file, {
					preview: URL.createObjectURL(file),
				})
			),
			stat: "Added " + filesBeritaAcara.length + " file(s)",
		});
		this.props.setUploadSuratSanksi(this.state);
	};
	onDropTandaTerimaSS = (filesTandaTerimaSS) => {
		this.setState({
			filesTandaTerimaSS: filesTandaTerimaSS.map((file) =>
				Object.assign(file, {
					preview: URL.createObjectURL(file),
				})
			),
			stat: "Added " + filesTandaTerimaSS.length + " file(s)",
		});
		this.props.setUploadSuratSanksi(this.state);
	};

	componentDidMount = async () => {
		const { query, token } = this.props;
		const { id } = query;
		const getDataSave = await getAutoSave({ token, id, laporan: true });
		const saveData = getDataSave.data?.penetapanSanksi;
		this.setState(saveData)
	}

	// handleAutoSave = async () => {
	// 	const { query, token } = this.props;
	// 	const { id } = query;
	// 	const { nomorSanksi, keterangan } = this.state
	// 	const saveData = await inputAutoSave({ data: { penetapanSanksi: { nomorSanksi, keterangan } }, token, id, laporan: true })

	// }

	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.",
		});
		this.props.setUploadSuratSanksi(this.state);
	};

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

	toRingkasan = () => {
		this.props.setUploadSuratSanksi(this.state);
	};

	setNomorSanksi = (e) => {
		this.setState({ nomorSanksi: e.target.value }, this.toRingkasan);
	};

	setKeterangan = (e) => {
		this.setState({ keterangan: e.target.value }, this.toRingkasan);
	};

	setListSanksi = (ls) => {
		this.setState({ listSanksi: ls.target.value }, this.toRingkasan);
	};
	handleChangeListSanksi = (listSanksi) => {
		this.setState({ listSanksi }, this.toRingkasan);
	};
	handleTmtCheck = () => {
		this.setState({ tmtCheck: !this.state.tmtCheck }, this.toRingkasan);
	}
	render() {
		const { files, filesBeritaAcara, terimaSuratSanksi, tglAkhirKeberatan, filesTandaTerimaSS, tmtCheck } = this.state;
		const removeFile = file => () => {
			const newFiles = [...files]
			newFiles.splice(newFiles.indexOf(file), 1)
			this.setState({
				files: newFiles,
			});
		}
		const removeFileBA = file => () => {
			const newFiles = [...filesBeritaAcara]
			newFiles.splice(newFiles.indexOf(file), 1)
			this.setState({
				filesBeritaAcara: newFiles,
			});
		}
		const removeFileTandaTerimaSS = file => () => {
			const newFiles = [...filesTandaTerimaSS]
			newFiles.splice(newFiles.indexOf(file), 1)
			this.setState({
				filesTandaTerimaSS: newFiles,
			});
		}
		const thumbs = files.map((file, index) => (
			<p>
				<em className="far fa-file" />&nbsp;&nbsp;{file.name}
				<button className="bg-transparent button-transparent border-0 fas fa-trash text-danger float-right" onClick={removeFile(file)} />
			</p>
		));
		const thumbsBA = filesBeritaAcara.map((file, index) => (
			<p>
				<em className="far fa-file" />&nbsp;&nbsp;{file.name}
				<button className="bg-transparent button-transparent border-0 fas fa-trash text-danger float-right" onClick={removeFileBA(file)} />
			</p>
		));
		const thumbsTandaTerimaSS = filesTandaTerimaSS.map((file, index) => (
			<p>
				<em className="far fa-file" />&nbsp;&nbsp;{file.name}
				<button className="bg-transparent button-transparent border-0 fas fa-trash text-danger float-right" onClick={removeFileTandaTerimaSS(file)} />
			</p>
		));
		return (
			<form className="form-horizontal" method="get" action="/" onSubmit={this.onSubmit}>
				<FormGroup row>
					<label className="col-md-2 col-form-label">Nomor Surat:</label>
					<div className="col-md-10">
						<Input type="text" value={this.state.nomorSanksi} onChange={(e) => { this.setNomorSanksi(e); this.props.handleAutoSave() }} />
					</div>
				</FormGroup>
				<FormGroup row className="mt-3">
					<label className="col-md-2 col-form-label">Keterangan</label>
					<div className="col-md-10">
						<Input type="textarea" value={this.state.keterangan} onChange={(e) => { this.setKeterangan(e); this.props.handleAutoSave() }} required />
					</div>
				</FormGroup>
				<FormGroup row>
					<label className="col-md-2 col-form-label">Tidak Perlu TMT</label>
					<div className="col-md-10 mt-2">
						<div className="checkbox c-checkbox">
							<label>
								<Input type="checkbox" onChange={this.handleTmtCheck} defaultChecked={this.state.tmtCheck} />
								<span className="fa fa-check"></span></label>
						</div>
					</div>
				</FormGroup>
				{this.state.tmtCheck && (
					<FormGroup row className="mt-3">
						<label className="col-md-2 col-form-label">Tanggal Penetapan Sanksi</label>
						<span className="col-sm-3 float-left">
							<DatePicker
								selected={this.state.awalsanksi}
								onChange={(awalsanksi) => {
									this.setState({ awalsanksi }, this.toRingkasan)
								}}
								dateFormat="dd/MM/yyyy"
								maxDate={this.state.startDay}
								placeholderText="Isi Tanggal"
								locale={id}
								className="form-control bg-white"
							/>
						</span>
					</FormGroup>
				)}
				{!this.state.tmtCheck && (
					<FormGroup row className="mt-3">
						<label className="col-md-2 col-form-label">Isi TMT</label>
						<Row >
							<Col>
								<FormGroup>
									<span className="ml-3">
										<DatePicker
											selected={this.state.awalsanksi}
											onChange={(awalsanksi) => {
												this.setState({ awalsanksi }, this.toRingkasan)
											}}
											dateFormat="dd/MM/yyyy"
											maxDate={this.state.startDay}
											placeholderText="Dari Tanggal"
											locale={id}
											className="form-control bg-white"
										/>
									</span>

								</FormGroup>
							</Col>
							<Col>
								<FormGroup>
									<span className="ml-3">
										<DatePicker
											selected={this.state.akhirsanksi}
											onChange={(akhirsanksi) => {
												this.setState({ akhirsanksi }, this.toRingkasan)
											}}
											dateFormat="dd/MM/yyyy"
											minDate={this.state.awalsanksi}
											maxDate={addMonths(new Date(this.state.awalsanksi), 6)}
											placeholderText="Sampai tanggal"
											locale={id}
											className="form-control bg-white"
										/>
									</span>
								</FormGroup>
							</Col>
						</Row>
					</FormGroup>
				)}
				{!this.state.tmtCheck && (
					<FormGroup row className="mt-1">
						<label className="col-md-2 col-form-label">TMT berlaku</label>
						<div className="col-md-10 mt-2">
							<b>{this.state.awalsanksi ? moment(this.state.awalsanksi).format("DD-MM-YYYY") : "-"}</b> hingga <b>{this.state.akhirsanksi ? moment(this.state.akhirsanksi).format("DD-MM-YYYY") : "-"}</b>
						</div>
					</FormGroup>
				)}
				{!this.state.tmtCheck && (
					<FormGroup row className="mt-1">
						<label className="col-md-2 col-form-label">TMT</label>
						<div className="col-md-10 mt-2">
							<b>{this.state.akhirsanksi ? moment(this.state.akhirsanksi).diff(this.state.awalsanksi, 'month') : "-"} bulan</b>
						</div>
					</FormGroup>
				)}
				<FormGroup row className="mt-3">
					<label className="col-md-2 col-form-label">List sanksi </label>
					<div className="col-md-10">
						<Select
							options={this.props.listSanksi.map(e => ({ value: e, label: e }))}
							formatOptionLabel={formatOptionLabel}
							isMulti
							onChange={(e) => {
								this.handleChangeListSanksi(e);
							}}
						/>
					</div>
				</FormGroup>
				<FormGroup row>
					<label className="col-md-2 col-form-label">
						Dokumen Surat Sanksi<span>: &nbsp;</span><span className="text-danger">*</span>
					</label>
					<div className="col-md-10">
						<DropzoneWrapper className="" onDrop={this.onDrop}>
							{({ getRootProps, getInputProps, isDragActive }) => {
								return (
									<div {...getRootProps()} className={"dropzone card" + (isDragActive ? "dropzone-drag-active" : "")}>
										<input {...getInputProps()} />
										<div className="dropzone-style-1">
											<div className="center-ver-hor dropzone-previews flex">
												{this.state.files.length > 0 ? (
													<div className="text-center fa-2x icon-cloud-upload mr-2 ">
														<h5 className="text-center dz-default dz-message">Klik untuk tambah file</h5>
													</div>
												) : (
													<div className="text-center fa-2x icon-cloud-upload mr-2 ">
														<h5 className="text-center dz-default dz-message">Klik untuk upload dokumen</h5>
													</div>
												)}
											</div>
										</div>
										<div className="d-flex align-items-center">
											<small className="ml-auto">
												<button type="button" className="btn btn-link" onClick={this.clearFiles}>
													Reset dokumen
												</button>
											</small>
										</div>
									</div>
								);
							}}
						</DropzoneWrapper>
						<div>
							{thumbs}
						</div>
						<p className="mrgn-top-5 font-color-black">Ukuran setiap dokumen maksimal 15mb</p>
					</div>
				</FormGroup>
				<FormGroup row>
					<label className="col-md-2 col-form-label">
						Dokumen Laporan Evaluasi dan Pembahasan<span>: &nbsp;</span><span className="text-danger">*</span>
					</label>
					<div className="col-md-10">
						<DropzoneWrapper className="" onDrop={this.onDropBA}>
							{({ getRootProps, getInputProps, isDragActive }) => {
								return (
									<div {...getRootProps()} className={"dropzone card" + (isDragActive ? "dropzone-drag-active" : "")}>
										<input {...getInputProps()} />
										<div className="dropzone-style-1">
											<div className="center-ver-hor dropzone-previews flex">
												{this.state.filesBeritaAcara.length > 0 ? (
													<div className="text-center fa-2x icon-cloud-upload mr-2 ">
														<h5 className="text-center dz-default dz-message">Klik untuk tambah file</h5>
													</div>
												) : (
													<div className="text-center fa-2x icon-cloud-upload mr-2 ">
														<h5 className="text-center dz-default dz-message">Klik untuk upload dokumen</h5>
													</div>
												)}
											</div>
										</div>
										<div className="d-flex align-items-center">
											<small className="ml-auto">
												<button type="button" className="btn btn-link" onClick={this.clearFilesBA}>
													Reset dokumen
												</button>
											</small>
										</div>
									</div>
								);
							}}
						</DropzoneWrapper>
						<div>
							{thumbsBA}
						</div>
						<p className="mrgn-top-5 font-color-black">Ukuran setiap dokumen maksimal 15mb</p>
					</div>
				</FormGroup>
				<FormGroup row className="mt-3">
					<label className="col-md-2 col-form-label">Tanggal Terima Surat Sanksi</label>
					<span className="col-sm-3 float-left">
						<DatePicker
							selected={terimaSuratSanksi}
							onChange={(terimaSuratSanksi) => {
								this.setState({ terimaSuratSanksi }, this.toRingkasan)
							}}
							dateFormat="dd/MM/yyyy"
							minDate={this.state.awalsanksi}
							placeholderText="Isi Tanggal"
							locale={id}
							className="form-control bg-white"
						/>
					</span>
				</FormGroup>
				<FormGroup row>
					<label className="col-md-2 col-form-label">
						Dokumen Tanda Terima Surat Sanksi<span>: &nbsp;</span><span className="text-danger">*</span>
					</label>
					<div className="col-md-10">
						<DropzoneWrapper className="" onDrop={this.onDropTandaTerimaSS}>
							{({ getRootProps, getInputProps, isDragActive }) => {
								return (
									<div {...getRootProps()} className={"dropzone card" + (isDragActive ? "dropzone-drag-active" : "")}>
										<input {...getInputProps()} />
										<div className="dropzone-style-1">
											<div className="center-ver-hor dropzone-previews flex">
												{this.state.filesBeritaAcara.length > 0 ? (
													<div className="text-center fa-2x icon-cloud-upload mr-2 ">
														<h5 className="text-center dz-default dz-message">Klik untuk tambah file</h5>
													</div>
												) : (
													<div className="text-center fa-2x icon-cloud-upload mr-2 ">
														<h5 className="text-center dz-default dz-message">Klik untuk upload dokumen</h5>
													</div>
												)}
											</div>
										</div>
										<div className="d-flex align-items-center">
											<small className="ml-auto">
												<button type="button" className="btn btn-link" onClick={this.clearFilesTerimaSanksi}>
													Reset dokumen
												</button>
											</small>
										</div>
									</div>
								);
							}}
						</DropzoneWrapper>
						<div>
							{thumbsTandaTerimaSS}
						</div>
						<p className="mrgn-top-5 font-color-black">Ukuran setiap dokumen maksimal 15mb</p>
					</div>
				</FormGroup>
				<FormGroup row className="mt-3">
					<label className="col-md-2 col-form-label">Akhir Permohonan Keberatan Sanksi</label>
					<span className="col-sm-3 float-left">
						<DatePicker
							selected={tglAkhirKeberatan}
							onChange={(tglAkhirKeberatan) => {
								this.setState({ tglAkhirKeberatan }, this.toRingkasan)
							}}
							dateFormat="dd/MM/yyyy"
							minDate={terimaSuratSanksi}
							placeholderText="Isi Tanggal"
							locale={id}
							className="form-control bg-white"
						/>
					</span>
				</FormGroup>
			</form >
		);
	}
}

export default UploadSurat;
