import React, { Component } from "react";
import Link from "next/link";
import { getSanksi } 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: {},
		};
	}

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

	componentDidMount = async () => {
		const { user } = this.props;
		const { noSanksi } = this.props.query;
		const sanksi = await getSanksi({ noSanksi, ptId: user.peran[0].organisasi.id });
		console.log(sanksi);
		this.setState({ sanksi });
	};

	render() {
		const { sanksi } = this.state;
		const { jawaban } = (sanksi.data && sanksi.data[0].sanksi.cabut_sanksi) || {};
		return (
			<ContentWrapper unwrap>
				<Header data={this.props.pt[0]} />
				<div className="p-3">
					<div className="content-heading">
						<div>Jawaban Permohonan Pencabutan Sanksi</div>
						<div className="ml-auto">
							<Link href="/app/pt/jawaban-pencabutan-sanksi">
								<button className="btn btn-sm btn-secondary text-sm">&lt; kembali</button>
							</Link>
						</div>
					</div>
					<Row>
						{sanksi?.data ? (
							<Col xl="9">
								<Card className="card-default">
									<CardBody>
										<Row>
											<Col lg={12}>
												<DetailSanksi data={sanksi.data[0]} />
												<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>
													<FormGroup>
														<label md="4">Keterangan:</label>
														<div md="8">
															<p>{jawaban && jawaban.description}</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.files.map((e) => (
																				<tr>
																					<td>
																						<em className="fa-lg far fa-file-code"></em>
																					</td>
																					<td>
																						<a className="text-muted" href={API_URL + e.path} target="_blank" download={e.name}>
																							{e.name}
																						</a>
																					</td>
																				</tr>
																			))}
																	</tbody>
																</table>
															</Scrollable>
														</div>
													</FormGroup>
												</form>
												{jawaban && jawaban.status === "Rekomendasi Perbaikan" && (
													<Link
														href={{
															pathname: "/app/pt/dokumen-perbaikan/detail",
															query: { noSanksi: sanksi.data[0].sanksi.no_sanksi },
														}}
													>
														<Button color="primary">Perbaiki Dokumen</Button>
													</Link>
												)}
											</Col>
										</Row>
									</CardBody>
								</Card>
							</Col>
						) : (
							<Loader />
						)}
						<Col xl="3">{this.props.pt && <DetailPT data={this.props.pt[0]} />}</Col>
					</Row>
				</div>
			</ContentWrapper>
		);
	}
}

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