import React, { Component } from "react";
import Router from "next/router";
import Link from "next/link";
import Select from "react-select";
import DetailSanksi from "@/components/Main/DetailSanksi";
import Header from "@/components/Main/Header";
import DetailPT from "@/components/Main/DetailPT";
import PermohonanPT from "@/components/Main/PermohonanPT";
import Riwayat from "@/components/Banding/Riwayat";
import { getOneSanksi } from "@/actions/sanksi";
import { addJawabanBanding } from "@/actions/banding";
import ContentWrapper from "@/components/Layout/ContentWrapper";
import { Row, Col, Card, CardBody, FormGroup, Button, Modal, ModalHeader, ModalBody, ModalFooter } from "reactstrap";
import { getPT } from "@/actions/PT";
import Loader from "@/components/Common/Loader";
import { toast } from "react-toastify";
import { connect } from "react-redux";
import { Formik, Form, Field, ErrorMessage } from "formik";
import * as Yup from "yup";
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;
	}
}
const selectInstanceId = 1;
const jawabanBandingSchema = Yup.object().shape({
	status: Yup.string().required("Harap Diisi"),
	dokumen: Yup.array().notRequired(),
});
class JawabanBanding extends Component {
	constructor(props) {
		super(props);
		this.state = {
			modal: false,
			selectedOption: null,
			files: [],
			sanksi: {},
			pt: null,
		};
	}
	static getInitialProps = async ({ query }) => {
		return { query };
	};
	componentDidMount = async () => {
		const { query, token } = this.props;
		const sanksi = await getOneSanksi(token, query.id, { banding: true });
		const pt = sanksi.data.laporan.pt;
		this.setState({ sanksi, pt });
	};
	toggleModal = () => {
		this.setState({
			modal: !this.state.modal,
		});
	};
	handleChangeSelect = (selectedOption) => {
		this.setState({ selectedOption });
	};
	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: [],
		});
	};
	handelSimpan = async () => {
		if (this.state.modal === true) {
			this.toggleModal();
		}
		const { data } = this.state;
		const { query, token } = this.props;
		const { id } = query;
		const formdata = new FormData();
		formdata.append("status", data.status);
		data.dokumen.forEach((e) => {
			formdata.append("dokumen", e);
		});
		const toastid = toast.loading("Please wait...");
		const added = await addJawabanBanding(token, id, formdata);
		if (!added) {
			toast.update(toastid, { render: "All is not good", type: "error", isLoading: false, autoClose: true, closeButton: true });
		} else {
			toast.update(toastid, { render: "All is good", type: "success", isLoading: false, autoClose: true, closeButton: true });
			Router.push({
				pathname: "/app/banding",
			});
		}
	};
	render() {
		const { files, sanksi, pt } = this.state;
		const thumbs = files.map((file, index) => (
			
				 ));
		return (
			
				{/*  */}
			
		));
		return (
			
				{/*  */}
				
					
						Jawaban Permohonan Banding
						
							
								
							
						
					 
					
						{sanksi.data ? (
							
								
									
										
											
												
												
												Jawaban
												 {
														this.setState({ data });
														if (sanksi.data.jawaban?.banding) this.toggleModal();
														else await this.handelSimpan();
													}}
												>
													{() => (
														
													)}
												
											
										
									
								
							
						) : (
							
						)}
						{pt ?  : }
					
					{sanksi.data && (
						
							
								
							
						
					)}
				
 
				
					Apakah anda yakin ingin mengubah jawaban sebelumnya?
					
						{" "}
						
					
				
			
		);
	}
}
const mapStateToProps = (state) => ({ user: state.user, token: state.token });
export default connect(mapStateToProps)(JawabanBanding);