import React, { Component } from "react";
import Link from "next/link";
import { getOneSanksi } from "@/actions/sanksi";
import Header from "@/components/Main/Header";
import DetailPT from "@/components/Main/DetailPT";
import DetailSanksi from "@/components/Main/DetailSanksi";
import ContentWrapper from "@/components/Layout/ContentWrapper";
import Scrollable from "@/components/Common/Scrollable";
import { Row, Col, Card, CardBody, FormGroup, Button } from "reactstrap";
import { connect } from "react-redux";
import Loader from "@/components/Common/Loader";
import { API_URL } from "@/env";
class DetailJawabanPencabutanSanksi extends Component {
	constructor(props) {
		super(props);
		this.state = {
			sanksi: {},
			pt: null,
		};
	}

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

	componentDidMount = async () => {
		const { token, query } = this.props;
		const sanksi = await getOneSanksi(token, query.id);
		this.setState({ sanksi, pt: sanksi.data.laporan.pt });
	};

	render() {
		const { sanksi, pt } = this.state;
		const jawaban = sanksi.data?.jawaban.cabut_sanksi || null;
		return (
			<ContentWrapper unwrap>
				{pt && <Header data={pt} />}
				<div className="p-3">
					<div className="content-heading">
						<div>Jawaban Permohonan Pencabutan Sanksi</div>
						<div className="ml-auto">
							<Link href="/pt/jawaban-pencabutan-sanksi">
								<button className="btn btn-sm btn-secondary text-sm">&lt; back</button>
							</Link>
						</div>
					</div>
					<Row>
						{sanksi.data ? (
							<Col xl="9">
								<Card className="card-default">
									<CardBody>
										<Row>
											<Col lg={12}>
												<DetailSanksi data={sanksi.data} />
												<p className="lead bb">Jawaban Permohonan Pencabutan Sanksi</p>
												<form className="form-horizontal">
													<FormGroup>
														<label md="4">Jawaban:</label>
														<div md="8">
															<h3>{jawaban && jawaban.status}</h3>
														</div>
													</FormGroup>
													{jawaban && jawaban.keterangan && (
														<FormGroup>
															<label md="4">Keterangan:</label>
															<div md="8">
																<p>{jawaban.keterangan}</p>
															</div>
														</FormGroup>
													)}
													<FormGroup>
														<label md="4">Dokumen Jawaban:</label>
														<div md="8">
															<Scrollable height="120px" className="list-group">
																<table className="table table-bordered bg-transparent">
																	<tbody>
																		{jawaban &&
																			jawaban.dokumen.map((e) => (
																				<tr>
																					<td>
																						<em className="fa-lg far fa-file-code"></em>
																					</td>
																					<td>
																						<a className="text-muted" href={e.path} target="_blank" download={e.judul}>
																							{e.judul}
																						</a>
																					</td>
																				</tr>
																			))}
																	</tbody>
																</table>
															</Scrollable>
														</div>
													</FormGroup>
												</form>
												{jawaban && jawaban.status === "Rekomendasi Perbaikan" && (
													<Link
														href={{
															pathname: "/pt/dokumen-perbaikan/detail",
															query: { id: sanksi.data._id },
														}}
													>
														<Button color="primary">Perbaiki Dokumen</Button>
													</Link>
												)}
											</Col>
										</Row>
									</CardBody>
								</Card>
							</Col>
						) : (
							<Loader />
						)}
						<Col xl="3">{pt && <DetailPT data={pt} />}</Col>
					</Row>
				</div>
			</ContentWrapper>
		);
	}
}

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