import React, { Component } from "react";
import Router from "next/router";
import Link from "next/link";
import { getOneSanksi } 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/PencabutanSanksi/Riwayat";
import ContentWrapper from "@/components/Layout/ContentWrapper";
import { Row, Col, Card, CardBody, FormGroup, Button } from "reactstrap";
import { addCabutSanksi } from "@/actions/cabutSanksi";
import { connect } from "react-redux";
import Loader from "@/components/Common/Loader";
import { toast } from "react-toastify";
import { Formik, Form, Field, ErrorMessage } from "formik";
import * as Yup from "yup";
const checkIfFilesAreTooBig = (files) => {
	let valid = true;
	if (files) {
		files.map((file) => {
			if (file.size > 15 * 1024 * 1024) {
				valid = false;
			}
		});
	}
	return valid;
};
const checkIfFilesAreCorrectType = (files) => {
	let valid = true;
	if (files) {
		files.map((file) => {
			if (!["image/jpeg", "image/png"].includes(file.type)) {
				valid = false;
			}
		});
	}
	return valid;
};
const docSchema = Yup.object().shape({
	dokumen: Yup.array().min(1, "Minimal terdapat 1 dokumen").required("Required").test("filesize", "Maksimal ukuran dokumen 15mb", checkIfFilesAreTooBig),
});
let Dropzone = null;
class DropzoneWrapper extends Component {
	state = {
		isClient: false,
	};
	componentDidMount = () => {
		Dropzone = require("react-dropzone").default;
		this.setState({ isClient: true });
	};
	render() {
		return Dropzone ? {this.props.children} : null;
	}
}
class DetailPencabutanSanksi extends Component {
	constructor(props) {
		super(props);
		this.state = {
			files: [],
			sanksi: {},
			pt: null,
			error: null,
		};
	}
	static async getInitialProps({ query }) {
		return { query };
	}
	componentDidMount = async () => {
		const { token, query } = this.props;
		const sanksi = await getOneSanksi(token, query.id);
		this.setState({ sanksi, pt: sanksi.data.laporan.pt });
	};
	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: [],
		});
	};
	handleKirim = async (data) => {
		const { user, query, token } = this.props;
		const formdata = new FormData();
		data.dokumen.forEach((e) => {
			formdata.append("dokumen", e);
		});
		const id = toast.loading("Please wait...");
		const added = await addCabutSanksi(token, query.id, formdata);
		if (!added) {
			toast.update(id, { render: "All is not good", type: "error", isLoading: false, autoClose: true, closeButton: true });
		} else {
			toast.update(id, { render: "All is good", type: "success", isLoading: false, autoClose: true, closeButton: true });
			Router.push({
				pathname: "/pt/jawaban-pencabutan-sanksi",
			});
		}
	};
	render() {
		const { files, sanksi, pt } = this.state;
		const thumbs = files.map((file, index) => (
			
				{/* 

 */}
				
{file.name}
			 
		));
		return (
			
				{pt && }
				
					
						
							Permohonan Pencabutan Sanksi
						
						
							
								
							
						
					 
					
						{sanksi.data ? (
							
								
									
										
											
												
												Permohonan Pencabutan Sanksi
												
													{() => (
														
													)}
												
											
										
									
								
							
						) : (
							
						)}
						{pt && }
					
					{sanksi.data && (
						
							
								
							
						
					)}
				
 
			
		);
	}
}
const mapStateToProps = (state) => ({ user: state.user, token: state.token });
export default connect(mapStateToProps)(DetailPencabutanSanksi);