import React, { Component } from "react";
import BasePage from "@/components/Layout/BasePage";
import { Row, Col, FormGroup, Input, Card, CardBody, Button, Navbar, NavItem, NavLink, NavbarBrand, NavbarToggler, Nav, Collapse } from "reactstrap";
import Link from "next/link";
import ContentWrapper from "@/components/Layout/ContentWrapper";
import { getPelaporanPublic } from "@/actions/pelaporan";
import DetailLaporan from "@/components/Public/DetailLaporan";
import { getLogPublic } from "@/actions/log";
import Timeline from "@/components/Main/Timeline";

const menu = [
	{
		title: "Home",
		path: "/app",
	},
	{
		title: "Membuat Laporan",
		path: "/laporan/new",
	},
	{
		title: "Pemantauan",
		path: "/pemantauan",
	},
	{
		title: "Login",
		path: "/login",
	},
];
class App extends Component {
	constructor(props) {
		super(props);
		this.state = {
			isOpen: false,
			no_laporan: "",
			no_hp: "",
			msgError: [],
			laporan: {},
			log: {},
		};
	}

	static getInitialProps = ({ pathname }) => ({ pathname });

	toggleCollapse = () => {
		this.setState({
			isOpen: !this.state.isOpen,
		});
	};

	handleLihatPemantaun = async (e) => {
		e.preventDefault();
		const { no_hp, no_laporan } = this.state;
		const laporan = await getPelaporanPublic({ number: no_laporan, noHp: no_hp });
		const log = await getLogPublic({ ptId: laporan.data[0].pt_id, laporanId: laporan.data[0]._id });
		this.setState({ laporan });
		this.setState({ log });
	};

	render() {
		const { laporan, log } = this.state;
		return (
			<div>
				<Navbar color="info" expand="md" dark>
					<NavbarBrand href="/">
						<img className="img-fluid" src="/static/img/logo-single.png" alt="App Logo" /> Aldila Dikti
					</NavbarBrand>
					<NavbarToggler onClick={this.toggleCollapse} />
					<Collapse isOpen={this.state.isOpen} navbar>
						<Nav className="ml-auto" navbar>
							{menu.map((e) => (
								<NavItem active={e.path === this.props.pathname ? true : false}>
									<Link href={e.path}>
										<NavLink style={{ cursor: "pointer" }}>{e.title}</NavLink>
									</Link>
								</NavItem>
							))}
						</Nav>
					</Collapse>
				</Navbar>
				<ContentWrapper>
					<Row>
						<Col lg={8} className="block-center d-block ">
							<Card className="card-default">
								<CardBody>
									<form className="form-horizontal" method="post" onSubmit={this.onSubmit}>
										<p className="lead bb">Pemantauan</p>
										<FormGroup row>
											<label className="col-md-2 col-form-label">Nomor Laporan</label>
											<div className="col-md-10">
												<Input type="text" value={this.state.no_laporan} onChange={(e) => this.setState({ no_laporan: e.target.value })} required />
											</div>
										</FormGroup>
										<FormGroup row>
											<label className="col-md-2 col-form-label">Nomor yang dapat dihubungi</label>
											<div className="col-md-10">
												<Input type="text" value={this.state.no_hp} onChange={(e) => this.setState({ no_hp: e.target.value })} required />
											</div>
										</FormGroup>
										<FormGroup row>
											<div className="col-12 col-lg-2">
												<Button color="info" block type="submit" onClick={this.handleLihatPemantaun}>
													Lihat Pemantauan
												</Button>
											</div>
										</FormGroup>
									</form>
								</CardBody>
							</Card>
							<Card className="card-default">
								<CardBody>
									{laporan.data && log.data ? (
										<>
											<DetailLaporan data={laporan.data[0]} />
											<p className="lead bb">Pemantauan</p>
											<Timeline data={log.data} noFile />{" "}
										</>
									) : (
										""
									)}
								</CardBody>
							</Card>
							{/* <Card className="card-default">
								<CardBody></CardBody>
							</Card> */}
						</Col>
					</Row>
				</ContentWrapper>
			</div>
		);
	}
}

App.Layout = BasePage;

export default App;
