import React, { Component } from "react";
import { Row, Col, Card, CardBody, FormGroup, Input, Button, Progress } from "reactstrap";
import { toast } from "react-toastify";
import { Formik, Form, Field, ErrorMessage } from "formik";
import * as Yup from "yup";
import { connect } from "react-redux";
import { getOneSanksi, editTmt } from "@/actions/sanksi";
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
import { addDays, addMonths } from 'date-fns';
import id from 'date-fns/locale/id';
import moment from "moment";
import 'moment/min/locales';
moment.locale('id');
import Router from "next/router";
const selectInstanceId = 1;
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 rekomendasiSchema = Yup.object().shape({
    from_date: Yup.date().required("Wajib diisi"),
    to_date: Yup.date().required("Wajib diisi"),
    dokumen: Yup.array().required("Wajib diisi").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 InputTanggal extends Component {
    constructor(props) {
        super(props);
        const tmt_awal = new Date();
        this.state = {
            dropdownOpen: false,
            splitButtonOpen: false,
            files: [],
            sanksi: {},
            data: {},
            from_date: "",
            to_date: "",
            startDay: tmt_awal,
            sanksi: {},
        };
    }
    async componentDidMount() {
        const { token, query } = this.props;
        const { id } = query;
        const sanksi = await getOneSanksi(token, id);
        this.setState({ sanksi })
        console.log(this.state.sanksi)
    }
    toggleSplit = () => {
        this.setState({
            splitButtonOpen: !this.state.splitButtonOpen,
        });
    };
    toggleDropDown = () => {
        this.setState({
            dropdownOpen: !this.state.dropdownOpen,
        });
    };
    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: [],
        });
    };
    static getInitialProps = async ({ query }) => {
        return { query };
    };
    handelSimpan = async (data) => {
        const { token, query } = this.props;
        const { id } = query;
        const formdata = new FormData();
        formdata.append("from_date", this.state.from_date);
        formdata.append("to_date", this.state.to_date);
        this.state.files.forEach((e) => {
            formdata.append("dokumen", e);
        });
        const toastid = toast.loading("Please wait...");
        const added = await editTmt(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("/app/perpanjangan-sanksi");
        }
    };
    render() {
        const { files, sanksi } = this.state;
        const removeFile = file => () => {
            const newFiles = [...files]
            newFiles.splice(newFiles.indexOf(file), 1)
            this.setState({
                files: newFiles,
            });
        }
        const thumbs = files.map((file, index) => (
            
                  {file.name}
                
            
        ));
        return (
            
                
                    Perpanjangan Sanksi
                     {
                            this.setState({ data });
                            await this.handelSimpan();
                        }}
                    >
                        {({ isSubmitting }) => (
                            
                        )}
                    
                
            
        );
    }
}
const mapStateToProps = (state) => ({ user: state.user, token: state.token });
export default connect(mapStateToProps)(InputTanggal);