import React, { Component } from "react";
import { Card, Table } from "reactstrap";
import { getPelanggaran } from "@/actions/pelanggaran";
import { connect } from "react-redux";

export class TablePenetapanSanksi extends Component {
	checkedData = [];

	constructor(props) {
		super(props);
		this.state = {
			pelanggaran: null,
			checkedData: [],
		};
	}

	componentDidMount = async () => {
		const { token } = this.props;
		const pelanggaran = await getPelanggaran(token);
		this.setState({ pelanggaran });
	};

	onHandleChange = (evt) => {
		const checked = evt.target.checked;
		const item = evt.target.value;
		if (checked) this.checkedData.push(evt.target.value);
		else this.checkedData = this.checkedData.filter((e) => e != item);
		this.props.setCheckedData(this.checkedData);
	};

	render() {
		const { pelanggaran } = this.state;
		return (
			<Card className="card-default">
				<Table bordered hover responsive>
					<thead>
						<tr>
							<th>No</th>
							<th>Jenis Pelanggaran</th>
							<th>Sanksi</th>
							<th></th>
						</tr>
					</thead>
					<tbody>
						{pelanggaran
							? pelanggaran.data.map((jp, index) => (
								<tr key={jp._id}>
									<td>
										<label>{index + 1}</label>
									</td>
									<td>
										<div className="media align-items-center">
											<div className="media-body d-flex">
												<div>
													<p>{jp.pelanggaran}</p>
													{/* <p>TMT : {jp.tmt_bulan} Bulan</p> */}
													<p>Jenis Sanksi Administratif : {jp.label_sanksi}</p>
												</div>
											</div>
										</div>
									</td>
									<td>
										<div className="media align-items-center">
											<div className="media-body d-flex">
												<div>
													<p>{jp.sanksi}</p>
													<p>Keterangan : {jp.keterangan_sanksi}</p>
												</div>
											</div>
										</div>
									</td>
									<td>
										<div className="checkbox c-checkbox">
											<label>
												<input type="checkbox" value={jp._id} onChange={this.onHandleChange} />
												<span className="fa fa-check"></span>
											</label>
										</div>
									</td>
								</tr>
							))
							: ""}
					</tbody>
				</Table>
			</Card>
		);
	}
}

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