import React, { Component } from 'react';
import { TextField, Divider, Typography, Checkbox, withStyles } from '@material-ui/core';
import api from '../../../api';
import Autocomplete from '@material-ui/lab/Autocomplete';
import { titleCase } from '../../../library/Utils';
import localeID from "date-fns/locale/id"
import format from "date-fns/format";

const CustomCheckbox = withStyles({
    root: {
      color: '#51c6ea',
      '&$checked': {
        color: '#51c6ea',
      },
    },
    checked: {},
  })((props) => <Checkbox color="default" {...props} />);

export default class AddUser extends Component {
    
    constructor(props) {
        super(props)
        this.state = {
            paramsId: this.props.data,
            tempData: null,
            menuData: null,
            checked: false,
            fullname: '',
            email: '',
            role: null,
            startDate: null,
            endDate: null,
            company: [],
            listRole: null,
            date: new Date(),
        }
    }

    handleChecked() {
        this.setState({checked: !this.state.checked})
    }
    
    componentDidMount() {
        this.getRole()
    }

    handleChange(e) {
        let data = this.state
        this.setState({...data, tempData: {...this.state.tempData, [e.target.name] : e.target.value}})
    }

    crateUser() {
        let payload = {
            "role_id": this.state.role.role_id,
            "email": this.state.email,
            "fullname": this.state.fullname,
            "password": this.state.fullname,
            "company": this.state.company,
            "start_date": this.state.startDate,
            "end_date": this.state.endDate
        }
        api.create().createUser(payload).then((response) => {
            console.log(response)
        })
    }

    getRole() {
        api.create().getRole().then((response) => {
            if(response.data.status == 'success') {
                let data = response.data.data
                let roleData = data.map((item) => {
                    return {
                        role_id: item.role_id,
                        role_name: item.role_name
                    }
                })
                let defaultProps = {
                    options: roleData,
                    getOptionLabel: (option) => titleCase(option.role_name),
                  };
                this.setState({listRole: defaultProps})
            } else {
                alert(response.data.message)
            }
        })
    }


    render() {
        return (
            <div className="test app-popup-show">
                <div className="popup-content background-white border-radius" style={{ borderRadius: 8 }}>
                    <div className="popup-panel grid grid-2x" style={{ backgroundColor: '#51c6ea', height: 64, borderTopRightRadius: 8, borderTopLeftRadius: 8 }}>
                        <div className="col-1" style={{ maxWidth: "inherit", display: 'flex', alignItems: 'center' }}>
                            <div className="popup-title">
                                <span style={{ color: '#fff', fontSize: 16, fontWeight: 'bold' }}>Ubah Data</span>
                            </div>
                        </div>
                        <div className="col-2 content-right" style={{ maxWidth: "inherit", alignSelf: 'center' }}>
                            <button
                                type="button"
                                className="btn btn-circle btn-white"
                                onClick={() => this.props.onClickClose()}
                            >
                                <i className="fa fa-lg fa-times" style={{ color: 'white' }} />
                            </button>
                        </div>
                    </div>

                    <div className="grid grid-2x grid-mobile-none gap-15px" style={{ padding: 20 }}>
                        <div className="column-1">
                            <div className="">
                                <TextField
                                    style={{ width: '100%' }}
                                    id="id"
                                    label="ID"
                                    disabled
                                    // id="outlined-read-only-input"
                                    variant="filled"
                                    value={'-'}
                                    onChange={(e) => null}
                                >
                                    {/* {periode.map((option) => (
                                        <MenuItem key={option.value} value={option.value}>
                                            {option.label}
                                        </MenuItem>
                                    ))} */}
                                </TextField>
                            </div>
                        </div>

                        <div className="column-2">
                            <div className="">
                                <TextField
                                style={{ width: '100%' , marginTop: 7}}
                                id="fullname"
                                name="fullname"
                                label="Nama Lengkap"
                                value={this.state.tempData == null? '' : this.state.tempData.fullname}
                                onChange={(e) => this.handleChange(e)}
                                // defaultValue="Default Value"
                                // helperText="Some important text"
                                />
                            </div>
                        </div>
                    </div>

                    <div className="grid grid-2x grid-mobile-none gap-15px" style={{ paddingLeft: 20, paddingRight: 20 }}>
                        <div className="column-1">
                            <div className="margin-bottom-20px">
                                <TextField
                                style={{ width: '100%' , marginTop: 7}}
                                id="email"
                                name="email"
                                label="Email"
                                value={this.state.tempData == null? '' : this.state.tempData.email}
                                onChange={(e) => this.handleChange(e)}
                                // defaultValue="Default Value"
                                // helperText="Some important text"
                                />
                            </div>
                        </div>

                        <div className="column-2">
                            <div className="margin-bottom-20px">
                                <Autocomplete
                                    {...this.state.listRole}
                                    id="role"
                                    onChange={(event, newInputValue) => this.setState({role: newInputValue})}
                                    debug
                                    renderInput={(params) => <TextField {...params} label="Role" margin="normal" style={{marginTop: 7}}/>}
                                    value={this.state.role}
                                />
                            </div>
                        </div>
                    </div>

                    <div className="grid grid-2x grid-mobile-none gap-15px" style={{ paddingLeft: 20, paddingRight: 20 }}>
                        <div className="column-1">
                            <div className="margin-bottom-20px">
                                <TextField
                                style={{ width: '100%'}}
                                id="startDate"
                                label="Berlaku Mulai"
                                name="start_date"
                                value={this.state.tempData == null? '' : this.state.tempData.start_date}
                                defaultValue="Default Value"
                                onChange={(e) => this.handleChange(e)}
                                // helperText="Some important text"
                                />
                            </div>
                        </div>

                        <div className="column-2">
                            <div className="margin-bottom-20px">
                                <TextField
                                style={{ width: '100%'}}
                                id="endDate"
                                label="Berlaku Hingga"
                                name="end_date"
                                value={this.state.tempData == null? '' : this.state.tempData.end_date}
                                defaultValue="Default Value"
                                onChange={(e) => this.handleChange(e)}
                                // helperText="Some important text"
                                />
                            </div>
                        </div>
                    </div>

                    <div className="grid grid-2x grid-mobile-none gap-15px" style={{ paddingLeft: 20, paddingRight: 20 }}>
                        <div className="column-1">
                            <div className="margin-bottom-20px">
                                <TextField
                                    style={{ width: '100%' }}
                                    id="status"
                                    label="Status"
                                    disabled
                                    // id="outlined-read-only-input"
                                    variant="filled"
                                    value={'Aktif'}
                                >
                                    {/* {periode.map((option) => (
                                        <MenuItem key={option.value} value={option.value}>
                                            {option.label}
                                        </MenuItem>
                                    ))} */}
                                </TextField>
                            </div>
                        </div>

                        <div className="column-2">
                            <div className="margin-bottom-20px">
                                <TextField
                                    style={{ width: '100%' }}
                                    id="is_expired"
                                    name="is_expired"
                                    label="Expired"
                                    disabled
                                    // id="outlined-read-only-input"
                                    variant="filled"
                                    value={'T'}
                                >
                                    {/* {periode.map((option) => (
                                        <MenuItem key={option.value} value={option.value}>
                                            {option.label}
                                        </MenuItem>
                                    ))} */}
                                </TextField>
                            </div>
                        </div>
                    </div>
                    <div style={{flexDirection:'column', display: 'flex', paddingLeft: 20, paddingRight: 20 }}>
                        <Typography style={{fontSize: 12}}>{`Dibuat  : ${format(this.state.date, 'dd MMMM yyyy', {locale: localeID})}`}</Typography>
                        {/* <Typography style={{fontSize: 12}}>{`Diubah: ${this.state.tempData == null? '' : this.state.tempData.updated}`}</Typography> */}
                    </div>
                    <Divider style={{margin: 20}}/>
                    <div style={{paddingLeft: 20, paddingRight: 20}}>
                        <h5>Otorisasi Perusahaan</h5>
                        
                        
                    </div>
                    <div className="border-top grid grid-2x" style={{ height: 56, backgroundColor: '#f5f5f5', paddingLeft: 20, paddingRight: 20 }}>
                        <div className="column-1" style={{ alignSelf: 'center' }}>
                            <button onClick={() => this.props.onClickClose()}>
                                <div style={{ width: 102, height: 30, border: 'solid 1px #354960', borderRadius: 5, alignItems: 'center', display: 'flex', justifyContent: 'center' }}>
                                    <span style={{ color: '#354960', fontSize: 11 }}>Batal</span>
                                </div>
                            </button>
                        </div>
                        <div className="column-2" style={{display: 'flex', justifyContent: 'flex-end', alignItems: 'center'}}>
                            <button onClick={() => this.validasi()}>
                                <div style={{ width: 102, height: 30, backgroundColor: '#354960', borderRadius: 5, alignItems: 'center', display: 'flex', justifyContent: 'center' }}>
                                    <span style={{ color: '#fff', fontSize: 11 }}>Simpan</span>
                                </div>
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        );
    }
}