
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 AdminCenters extends Component{
    constructor(props){
        super(props);
         this.state={assetsLoaded:false,cname:'',cid:'',cimg:'',ctreats:'',cphone:'',centers:[],oldimg:''}
         this.handleChange = this.handleChange.bind(this)
      }

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

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

      handleFile(e){
        console.log(e.target.files[0]);
        this.setState({cimg:e.target.files[0]})
      }

      editAss(id){
        axios.get('https://drkushwahindore.com/admin/Rest/edit_api/centers/cid/'+id).then(res => {
            //console.log(res.data);
            this.setState({cname:res.data.cname,cid:res.data.cid,oldimg:res.data.cimg,
                ctreats:res.data.ctreats,cphone:res.data.cphone});
        });
      }


    submitForm(event){event.preventDefault();
        var formData = new FormData();
        formData.append("cname", this.state.cname);
        formData.append("cid", this.state.cid);
        formData.append("oldimg", this.state.oldimg);
        formData.append("cimg", this.state.cimg);
        formData.append("ctreats", this.state.ctreats);
        formData.append("cphone", this.state.cphone);
        const config = {
            headers: {
              'content-type': 'multipart/form-data',
            },
          };
      
        axios.post('https://drkushwahindore.com/admin/Rest/add_center_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: "centers",
                        col: "cid",
                        val: id
                    })
                    .then(response => {
                        console.log(response);
                    })
                }
              },
              {
                label: 'No',
              }
            ]
          });        
    }

    render(){
        return (
            <div className="container-fluid">       
                <div className='row'>
                    <div className='col-md-4'>
                    <div class="widget-area-2 proclinic-box-shadow"><h3 class="widget-title">Add/Edit Center</h3>
                        <form onSubmit={(e)=>this.submitForm(e)}>
                            <input type="hidden" name='cid' value={this.state.cid}/>
                            <div className='form-group'>
                                <label>Center Title</label>
                                <input type="text" onChange={this.handleChange} 
                                className="form-control" value={this.state.cname} required name='cname'/>
                            </div>
                            <div className='form-group'>
                                <label>Center Image</label>
                                <input type="file"  onChange={(e)=>this.handleFile(e)} className="form-control" name='cimg'/>
                                {this.state.oldimg!='' &&
                                <img width="100px" className='img' src={'https://drkushwahindore.com/admin/adminassets/images/'+this.state.oldimg}/>}
                            </div>
                            <div className='form-group'>
                                <label>Phone Numbers</label>
                                <input type="text" onChange={this.handleChange}  className="form-control" value={this.state.cphone}
                                placeholder='' required name='cphone'/>
                            </div>
                            <div className='form-group'>
                            <label> Treatments </label>
                            <textarea className='form-control' name="ctreats" rows="2" 
                                onChange={this.handleChange} value={this.state.ctreats}></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 Store</h3>
                                <div className='table-responsive mb-3'>
                        <table className='table table-bordered table-striped table-condensed'>
                                        <thead><tr><th>Image</th><th>Name</th><th>Phone</th><th>Treatments</th><th>Action</th></tr></thead>
                                        <tbody>
                                        {this.state.centers.map(p=>
                                        <tr  key={p.cid}>
                                        <td><img width="50px" className='img' 
                                        src={'https://drkushwahindore.com/admin/adminassets/images//'+p.cimg}/></td>
                                        <td>{p.cname}</td>
                                        <td>{p.cphone}</td>
                                        <td>{p.ctreats}</td>
                                        <td>
                                            
                            <button className='btn btn-warning btn-sm' onClick={()=>this.editAss(p.cid)}>Edit</button>
                            <button className='btn btn-danger btn-sm' 
                                        onClick={()=>this.deleteAss(p.cid)}>Delete</button></td></tr>)}
                                        </tbody>
                                    </table>
                        </div>  
                    </div>
                    </div>
                </div>
            </div>
        );
    }
}


