import React, { Component } from "react";
import { Container, Row, Col, Card, CardHeader, CardBody } from "reactstrap";
import FlotChart from "@/components/Charts/Flot.js";
import { connect } from "react-redux";
import { getPengunjung } from "@/actions/pengunjung";

//sudah live
class ChartData extends Component {
	constructor(props) {
		super(props);
		this.state = {
			dataPengunjung: [],
			dataChart: this.ChartSpline,
		};
	}

	async componentDidMount() {
		const { token } = this.props;
		const dataPengunjung = await getPengunjung(token);
		this.setState({ dataPengunjung: dataPengunjung.data.map((e) => [this.convertMonth(e._id.bulan), e.jumlah_pengunjung]) });
		this.setState((prevState) => ({ dataChart: { ...prevState.dataChart, data: [{ ...prevState.dataChart.data[0], data: prevState.dataPengunjung }] } }));
	}

	ChartSpline = {
		data: [
			{
				// label: "Uniques",
				color: "#3e3a8e",
				data: [],
			},
		],

		options: {
			series: {
				lines: {
					show: false,
				},
				points: {
					show: true,
					radius: 4,
				},
				splines: {
					show: true,
					tension: 0.4,
					lineWidth: 1,
					fill: 0.5,
				},
			},
			grid: {
				borderColor: "#eee",
				borderWidth: 1,
				hoverable: true,
				backgroundColor: "#fcfcfc",
			},
			tooltip: true,
			tooltipOpts: {
				content: (label, x, y) => x + " : " + y,
			},
			xaxis: {
				tickColor: "#fcfcfc",
				mode: "categories",
			},
			yaxis: {
				min: 0, // optional: use it for a clear represetation
				tickColor: "#eee",
				//position: 'right' or 'left',
				tickFormatter: (v) => v /* + ' visitors'*/,
			},
			shadowSize: 0,
		},
	};

	convertMonth = (int) => {
		switch (int) {
			case 1:
				return "Januari";
				break;
			case 2:
				return "Februari";
				break;
			case 3:
				return "Maret";
				break;
			case 4:
				return "April";
				break;
			case 5:
				return "Mei";
				break;
			case 6:
				return "Juni";
				break;
			case 7:
				return "Juli";
				break;
			case 8:
				return "Agustus";
				break;
			case 9:
				return "September";
				break;
			case 10:
				return "Oktober";
				break;
			case 11:
				return "November";
				break;
			case 12:
				return "Desember";
				break;
			default:
				break;
		}
	};

	render() {
		return (
			<Col lg={12}>
				<Card className="card-default">
					<CardBody>
						<FlotChart options={this.ChartSpline.options} data={this.state.dataChart.data} className="flot-chart" height="250px" />
					</CardBody>
					<div align="center">
						<span>Data Statistik Pengunjung Tahun {new Date().getFullYear()}</span>
					</div>
				</Card>
			</Col>
		);
	}
}

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