import React, { Component } from "react";
import Router from "next/router";
import ContentWrapper from "@/components/Layout/ContentWrapper";
import { Row, Col, Button, Table, Pagination, PaginationItem, PaginationLink } from "reactstrap";
// React Slider
import Slider from "rc-slider";
import "rc-slider/assets/index.css";
// React Select
import Select from "react-select";
// DateTimePicker
import Datetime from "react-datetime";
import "react-datetime/css/react-datetime.css";

import https from "https";

import CardTool from "@/components/Common/CardTool";

import dummyData from "../PT-ID.json";

var pembina = [];

class Search extends Component {
	static async getInitialProps(ctx) {
		pembina = [];
		const httpsAgent = new https.Agent({
			rejectUnauthorized: false,
		});
	}

	state = {
		selectedOptionMulti: [],
		data: [],
	};

	handleChangeSelectMulti = (selectedOptionMulti) => {
		this.setState({ selectedOptionMulti });
	};

	renderInputGroup = (props) => {
		return (
			<div className="input-group date">
				<input className="form-control" {...props} />
				<span className="input-group-append input-group-addon">
					<span className="input-group-text fas fa-calendar-alt"></span>
				</span>
			</div>
		);
	};

	handleClick = (e, PT_ID) => {
		const query = { ptId: PT_ID };
		e.preventDefault();
		Router.push({
			pathname: "/app/pemantauan/timeline",
			query: { ptId: PT_ID },
		});
	};

	fetchData = async () => {
		var qPembina = "";
		console.log("this.state.selectedOptionMulti.value : ", this.state.selectedOptionMulti.value);
		if (this.state.selectedOptionMulti.value !== undefined) {
			qPembina = "&pembina=" + this.state.selectedOptionMulti.value;
		}

		const searchValue = "q=" + document.getElementById("searchInput").value;

		const httpsAgent = new https.Agent({
			rejectUnauthorized: false,
		});

		const jsonData = dummyData;

		this.setState({ data: jsonData });
	};

	handleApplyClick = () => {
		console.log("selectedOptionMulti : ", this.state.selectedOptionMulti);
		this.fetchData();
		if (this.state.data.length > 0) {
			this.renderTableData();
		}
	};

	handleSearchClick = () => {
		this.fetchData();
		if (this.state.data.length > 0) {
			this.renderTableData();
		}
	};

	renderTableData() {

		return this.state.data.map((pt, index) => {
			return (
				<tr>
					<td>
						<label>{index + 1}</label>
					</td>
					<td>
						<div className="media align-items-center">
							<a className="mr-3" href="">
								<img className="img-fluid rounded thumb64" src="/static/img/dummy-search.png" alt="Dummy" />
							</a>
							<div className="media-body d-flex">
								<div>
									<h4 className="m-0">{pt.nama}</h4>
									<small className="text-muted">
										{pt.sk_pendirian} - {pt.website} - {pt.email}
									</small>
									<p>{pt.alamat.jalan}</p>
								</div>
								<div className="ml-auto">
									<Button color="info" size="sm" onClick={(e) => this.handleClick(e, pt.id)}>
										View
									</Button>
								</div>
							</div>
						</div>
					</td>
				</tr>
			);
		});
		//}
	}

	render() {
		// used for react select
		const { selectedOptionMulti } = this.state;

		return (
			<ContentWrapper>
				<div className="content-heading">
					<div>
						Search
						<small>Search and filter results</small>
					</div>
				</div>
				<Row>
					<Col lg="9">
						<div className="form-group mb-4">
							<input className="form-control mb-2" type="text" id="searchInput" placeholder="Pencarian Nama Perguruan Tinggi" />
							<div className="d-flex">
								<button className="btn btn-secondary" type="button" onClick={(e) => this.handleSearchClick()}>
									Search
								</button>
								<div className="ml-auto">
								</div>
							</div>
						</div>
						{/* START card */}
						<div className="card card-default">
							<div className="card-header">
								Search Results
							</div>
							{/* START table-responsive */}
							<Table striped bordered hover>
								<thead>
									<tr>
										<th>No. </th>
										<th>Description</th>
									</tr>
								</thead>
								<tbody>{this.renderTableData()}</tbody>
							</Table>
							{/* END table-responsive */}
							<div className="card-footer">
								<div className="d-flex">
								</div>
							</div>
						</div>
						{/* END card */}
					</Col>
					<Col lg="3">
						<h3 className="m-0 pb-3">Filters</h3>
						<div className="form-group mb-4">
							<label className="col-form-label mb-2">by Pembina</label>
							<br />
							<Select name="multi-select-name" multi simpleValue value={selectedOptionMulti} onChange={this.handleChangeSelectMulti} options={pembina} />
						</div>
						<Button color="secondary" size="lg" onClick={(e) => this.handleApplyClick()}>
							Apply
						</Button>
					</Col>
				</Row>
			</ContentWrapper>
		);
	}
}

export default Search;
