import React, { Component } from "react";
import Router from "next/router";
import { getSanksi } from "@/actions/sanksi";
import { addDocPerbaikan } from "@/actions/docPerbaikan";
import Link from "next/link";
import Header from "@/components/Main/Header";
import DetailPT from "@/components/Main/DetailPT";
import DetailSanksi from "@/components/Main/DetailSanksi";
import Riwayat from "@/components/PT/DocPerbaikan/Riwayat";
import ContentWrapper from "@/components/Layout/ContentWrapper";
import { Row, Col, Card, CardBody, FormGroup, Button, Input } from "reactstrap";
import { connect } from "react-redux";
import { notifDocPerbaikan } from "@/actions/notifikasi";
import Loader from "@/components/Common/Loader";
import { toast } from "react-toastify";
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 DetailPerbaikanDoc extends Component {
	constructor(props) {
		super(props);
		this.state = {
			files: [],
			sanksi: {},
			keterangan: "",
		};
	}
	static getInitialProps = ({ query }) => ({ query });
	componentDidMount = async () => {
		const { user } = this.props;
		const { noSanksi } = this.props.query;
		const sanksi = await getSanksi({ noSanksi, ptId: user.peran[0].organisasi.id });
		this.setState({ sanksi });
	};
	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 (e) => {
		e.preventDefault();
		const { keterangan, sanksi } = this.state;
		const { noSanksi } = this.props.query;
		const { user } = this.props;
		const org_id = user.peran[0].organisasi.id;
		const formdata = new FormData();
		formdata.append("description", keterangan);
		if (this.state.files.length > 0) {
			this.state.files.forEach((e) => {
				formdata.append("files", e);
			});
			const id = toast.loading("Please wait...");
			const added = await addDocPerbaikan({ noSanksi, ptId: org_id }, formdata);
			if (added) {
				await notifDocPerbaikan({ lembaga: sanksi.data[0].sanksi.user.lembaga, pt_name: user.peran[0].organisasi.nama, no_sanksi: sanksi.data[0].sanksi.no_sanksi });
				toast.update(id, { render: "All is good", type: "success", isLoading: false, autoClose: true, closeButton: true });
				Router.push({
					pathname: "/app/pt/dokumen-perbaikan",
				});
			}
		}
	};
	render() {
		const { files, sanksi } = this.state;
		const thumbs = files.map((file, index) => (
			
				
			
		));
		return (
			
				
				
					
					
						{sanksi?.data ? (
							
								
									
										
											
												
												Dokumen Perbaikan
												
											
										
									
								
							
						) : (
							
						)}
						{this.props.pt && }
					
					{sanksi?.data && (
						
							
								
							
						
					)}
				
 
			
		);
	}
}
const mapStateToProps = (state) => ({ user: state.user, pt: state.pt });
export default connect(mapStateToProps)(DetailPerbaikanDoc);