
import React,{Component} from 'react';
import axios from 'axios';
import { confirmAlert } from 'react-confirm-alert';
import 'react-confirm-alert/src/react-confirm-alert.css'; // Import css

export default class AdminStores extends Component{
    constructor(props){
        super(props);
         this.state={assetsLoaded:false,sstate:'',store_id:'',city:'',name:'',address:'',phone:'',states:[],stores:[]}
         this.handleChange = this.handleChange.bind(this)
      }

      componentDidMount(){
        axios.get('https://drkushwahindore.com/admin/Rest/states_api').then(res => {
            this.setState({states:res.data});
        });
        axios.get('https://drkushwahindore.com/admin/Rest/stores_api').then(res => {
            //console.log(res.data);
            this.setState({stores:res.data});
        });
      }

      handleChange(event){
        this.setState({
          [event.target.name] : event.target.value
        })
      }

      editAss(id){
        axios.get('https://drkushwahindore.com/admin/Rest/edit_api/stores/id/'+id).then(res => {
            //console.log(res.data);
            this.setState({sstate:res.data.state,store_id:res.data.id,city:res.data.city,name:res.data.name,
            address:res.data.address,phone:res.data.phone});
        });
      }


    submitForm(event){event.preventDefault();
        var formData = new FormData();
        formData.append("sstate", this.state.sstate);
        formData.append("store_id", this.state.store_id);
        formData.append("city", this.state.city);
        formData.append("name", this.state.name);
        formData.append("address", this.state.address);
        formData.append("phone", this.state.phone);
        const config = {
            headers: {
              'content-type': 'multipart/form-data',
            },
          };
      
        axios.post('https://drkushwahindore.com/admin/Rest/add_store_api', formData,config)
            .then((res) => {
            console.log(res.data);
            //if(res.status==1){this.componentDidMount();}
        }).catch((err) => alert("File Upload Error"));    
    }

    deleteAss(id){
        confirmAlert({
            title: 'Confirm Delete',
            message: 'Are you sure to do delete?.',
            buttons: [
              {
                label: 'Yes',
                onClick: () => {
                    axios.post("https://drkushwahindore.com/admin/Rest/delete_record", {
                        tbl: "stores",
                        col: "id",
                        val: id
                    })
                    .then(response => {
                        console.log(response);
                    })
                }
              },
              {
                label: 'No',
              }
            ]
          });        
    }

    render(){
        return (
            <div className="container-fluid">       
                <div className='row'>
                    <div className='col-md-4'>
                    <div className='widget-area-2 proclinic-box-shadow'>
                        <h3 className="widget-title">Add/Edit Store</h3>
                        <form onSubmit={(e)=>this.submitForm(e)}>
                            <input type="hidden" name='store_id' value={this.state.store_id}/>
                            <div className='form-group'>
                                <label>Store Name</label>
                                <input type="text" onChange={this.handleChange} 
                                className="form-control" value={this.state.name} required name='name'/>
                            </div>
                            <div className='form-group'>
                                <label>State</label>
                                <select  onChange={this.handleChange} value={this.state.sstate}
                                 className='form-control' name='sstate' required>
                                    <option value="">Select</option>
                                    {this.state.states.map(dep=>
                                    <option key={dep.StateID} value={dep.StateID}>{dep.StateName}</option>)}
                                </select>
                            </div>
                            <div className='form-group'>
                                <label>City</label>
                                <input type="text" onChange={this.handleChange} 
                                className="form-control" value={this.state.city} required name='city'/>
                            </div>
                            <div className='form-group'>
                                <label>Phone Numbers</label>
                                <input type="text" onChange={this.handleChange}  className="form-control" value={this.state.phone}
                                placeholder='' required name='phone'/>
                            </div>
                            <div className='form-group'>
                            <label> Store Address </label>
                            <textarea className='form-control' name="address" rows="2" 
                                onChange={this.handleChange} value={this.state.address}></textarea>
                            </div>
                            <button className='btn btn-primary' type='submit'>Add Store</button>
                        </form></div>
                    </div>   
                    <div className='col-md-8'>
                    <div className='widget-area-2 proclinic-box-shadow'>
                        <h3 className="widget-title">Listed Stores</h3>
                        <table className='table table-bordered table-striped table-condensed'>
                                        <thead><tr><th>Name</th><th>Phone</th><th>City/State</th>
                                                <th>Address</th><th>Action</th></tr></thead>
                                        <tbody>
                                        {this.state.stores.map(p=>
                                        <tr  key={p.id}>
                                        <td>{p.name}</td>
                                        <td>{p.phone}</td>
                                        <td>{p.city} {p.StateName}</td>
                                        <td>{p.address}</td>
                                        <td>
                                            
                            <button className='btn btn-warning btn-sm' onClick={()=>this.editAss(p.id)}>Edit</button>
                            <button className='btn btn-danger btn-sm' 
                                        onClick={()=>this.deleteAss(p.id)}>Delete</button></td></tr>)}
                                        </tbody>
                                    </table>
                        </div>  
                    </div>
                </div>
            </div>
        );
    }
}


