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/Keberatan/Riwayat";
import { getSanksi } from "@/actions/sanksi";
import { addJawabanKeberatan } from "@/actions/keberatan";
import ContentWrapper from "@/components/Layout/ContentWrapper";
import { Row, Col, Card, CardBody, FormGroup, Input, Button } from "reactstrap";
import { getPT } from "@/actions/PT";
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;
class DetailKeberatan extends Component {
	constructor(props) {
		super(props);
		this.state = {
			selectedOption: null,
			files: [],
			keterangan: "",
			sanksi: {},
		};
	}
	static getInitialProps = async ({ query }) => {
		return { query };
	};
	componentDidMount = async () => {
		const { query } = this.props;
		const sanksi = await getSanksi(query);
		const pt = await getPT({ id: query.ptId });
		this.setState({ pt, sanksi });
	};
	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 (e) => {
		e.preventDefault();
		const { keterangan, selectedOption } = this.state;
		const { noSanksi, ptId } = this.props.query;
		const formdata = new FormData();
		formdata.append("description", keterangan);
		formdata.append("status", selectedOption.value);
		if (this.state.files.length > 0) {
			this.state.files.forEach((e) => {
				formdata.append("files", e);
			});
		}
		const added = await addJawabanKeberatan({ noSanksi, ptId }, formdata);
		if (added) {
			Router.push({
				pathname: "/app/keberatan",
			});
		}
	};
	render() {
		const { files, sanksi, pt } = this.state;
		const thumbs = files.map((file, index) => (
			
				 ));
		return (
			
		));
		return (
			
				
				
					
						Permohonan Keberatan
						
							
								
							
						
					 
					
						
							
								
									
										{sanksi.data && sanksi.data.length && }
									
									
										{sanksi.data && sanksi.data.length && }
									
									
										
											Jawaban
											
										
									
								
							
						
						{pt?.data && }
					
					
						{sanksi.data && sanksi.data.length && }
					
				 
			
		);
	}
}
export default DetailKeberatan;