import React, { Component } from "react";
import Header from "@/components/Main/Header";
import DetailPT from "@/components/Main/DetailPT";
import { getOneLaporan } from "@/actions/pelaporan";
import { getLog } from "@/actions/log";
import DetailLaporan from "@/components/Main/DetailLaporan";
import Link from "next/link";
import ContentWrapper from "@/components/Layout/ContentWrapper";
import { Row, Col, Card, CardBody, FormGroup } from "reactstrap";
import Loader from "@/components/Common/Loader";
import { connect } from "react-redux";
import Scrollable from "@/components/Common/Scrollable";
import Timeline from "@/components/Main/Timeline";

class DetailPelaporan extends Component {
	constructor(props) {
		super(props);
		this.state = {
			pelaporan: {},
			pemantauan: {},
		};
	}

	static getInitialProps = async ({ query }) => {
		return { query };
	};

	componentDidMount = async () => {
		const { query } = this.props;
		const pelaporan = await getOneLaporan(this.props.token, query.id + "?delegasi=true");
		const pemantauan = await getLog(this.props.token, query.id + "?delegasi=true&asc=true");
		pemantauan.data = pemantauan.data.filter((e) => e.action != "CREATE LAPORAN");
		this.setState({ pelaporan, pemantauan });
	};

	render() {
		const { pelaporan, pemantauan } = this.state;
		return (
			<ContentWrapper unwrap>
				{/* <Header /> */}
				<div className="p-3">
					<div className="content-heading">
						<div>Detail Laporan Delegasi</div>
						<div className="ml-auto">
							<Link href="/app/laporan-delegasi">
								<button className="btn btn-sm btn-secondary text-sm">&lt; back</button>
							</Link>
						</div>
					</div>
					<Row>
						<Col xl="9">
							{pelaporan.data ? (
								<Card className="card-default">
									<CardBody>
										<Row>
											<Col lg={12}>
												{<DetailLaporan data={pelaporan.data} />}
												<p className="lead bb">Keterangan Delegasi</p>
												<form className="form-horizontal">
													<FormGroup row>
														<Col md="4">Alasan Delegasi:</Col>
														<Col md="8">
															<Scrollable height="100px" className="list-group">
																<p>{pelaporan.data.alasan_delegasi}</p>
															</Scrollable>
														</Col>
													</FormGroup>
												</form>
											</Col>
										</Row>
									</CardBody>
								</Card>
							) : (
								<Loader />
							)}
						</Col>
						<Col xl="3">{pelaporan.data ? <DetailPT data={pelaporan.data.pt} /> : <Loader />}</Col>
					</Row>
					<Row>
						{pemantauan.data && (
							<Col xl={12}>
								<Timeline data={pemantauan.data} />
							</Col>
						)}
					</Row>
				</div>
			</ContentWrapper>
		);
	}
}

const mapStateToProps = (state) => ({ user: state.user, token: state.token });
export default connect(mapStateToProps)(DetailPelaporan);
