import React, { Component } from "react";
import ContentWrapper from "@/components/Layout/ContentWrapper";
import DetailPT from "@/components/Main/DetailPT";
import Header from "@/components/Main/Header";
import DetailLaporan from "@/components/Main/DetailLaporan";
import InputEvaluasi from "@/components/Pemeriksaan/InputEvaluasi";
import TableRiwayat from "@/components/Pemeriksaan/TableRiwayat";
import { getOneLaporan } from "@/actions/pelaporan";
import Link from "next/link";
import { Row, Col, Card, CardBody, CardHeader, CardTitle } from "reactstrap";
import Loader from "@/components/Common/Loader";
import { connect } from "react-redux";

class PemeriksaanNew extends Component {
	constructor(props) {
		super(props);
		this.state = {
			pelaporan: {},
		};
	}
	static getInitialProps = async ({ query }) => {
		return { query };
	};

	componentDidMount = async () => {
		const { token, query } = this.props;
		const pelaporan = await getOneLaporan(token, query.id);
		this.setState({ pelaporan });
	};

	componentShouldUpdate(nextProps, nextState) {
		return nextState.pelaporan !== this.state.pelaporan;
	}

	changePelaporan = (data) => {
		this.setState({ pelaporan: data });
	};

	render() {
		const { query, token } = this.props;
		const { pelaporan } = this.state;
		return (
			<ContentWrapper unwrap>
				{/* <Header /> */}
				<div className="p-3">
					<div className="content-heading">
						<div>Evaluasi Pemeriksaan</div>
						<div className="ml-auto">
							<Link href="/app/pemeriksaan">
								<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} />
												<InputEvaluasi query={query} token={token} changePelaporan={this.changePelaporan} />
											</Col>
										</Row>
									</CardBody>
								</Card>
							) : (
								<Loader />
							)}
						</Col>
						<Col xl="3">{pelaporan.data ? <DetailPT data={pelaporan.data.pt} /> : <Loader />}</Col>
					</Row>
					{pelaporan.data && (
						<Row>
							<Col>
								<Card className="card-default">
									<CardHeader>
										<CardTitle>Riwayat Evaluasi</CardTitle>
									</CardHeader>
									<CardBody>
										<TableRiwayat data={pelaporan.data} />
									</CardBody>
								</Card>
							</Col>
						</Row>
					)}
				</div>
			</ContentWrapper>
		);
	}
}

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