
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 AdminCampns extends Component{
    constructor(props){
        super(props);
         this.state={campns:[],
            assetsLoaded:false,camp_fund:'',funds:[],campg_title:'',campg_start:'',campg_end:'',campg_desc:'',campg_id:''
         }
         this.handleChange = this.handleChange.bind(this)
      }

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


      componentDidMount(){
        axios.get('https://moneyforex.in/church/Rest/funds_api').then(res => {
            //console.log(res.data);
            this.setState({funds:res.data});
        });
        axios.get('https://moneyforex.in/church/Rest/campaigns_api').then(res => {
            //console.log(res.data);
            this.setState({campns:res.data});
        });
      }

      editAss(id){
        axios.get('https://moneyforex.in/church/Rest/edit_campaigns_api/'+id).then(res => {
            //console.log(res.data);
            this.setState({camp_fund:res.data.camp_fund,campg_title:res.data.campg_title,campg_start:res.data.campg_start
                ,campg_end:res.data.campg_end,campg_desc:res.data.campg_desc,campg_id:res.data.campg_id});
        });
      }


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

    submitForm(event){event.preventDefault();
        var formData = new FormData();
        formData.append("campg_id", this.state.campg_id);
        formData.append("camp_fund", this.state.camp_fund);
        formData.append("campg_title", this.state.campg_title);
        formData.append("campg_start", this.state.campg_start);
        formData.append("campg_end", this.state.campg_end);
        formData.append("campg_desc", this.state.campg_desc);
        const config = {
            headers: {
              'content-type': 'multipart/form-data',
            },
          };
      
        axios.post('https://moneyforex.in/church/Rest/add_campaigns_api', formData,config)
            .then((res) => {
            console.log(res.data);
            if(res.status==1){this.componentDidMount();}
        }).catch((err) => alert("File Upload Error"));    
    }

    render(){
        return (
            <div className="container-fluid">       
                <div className='row'>
                    <div className='col-md-6'>
                    <div className='card'><div className='card-header'>Add New Campaing</div>
                        <div className='card-body'>
                        <form onSubmit={(e)=>this.submitForm(e)}>
                            <input type="hidden" name='campg_id'  value={this.state.campg_id}/>
                            <div className='form-group'>
                                <label>Fund Type</label>
                                <select  onChange={this.handleChange} value={this.state.camp_fund}
                                 className='form-control' name='camp_fund' required>
                                    <option value="">Select</option>
                                    {this.state.funds.map(as=>
                                    <option key={as.fund_id} value={as.fund_id}>{as.fund_title}</option>)}
                                </select>
                            </div>
                            <div className='form-group'>
                                <label>Campaign Title</label>
                                <input type="text" onChange={this.handleChange} className="form-control" value={this.state.campg_title}
                               required name='campg_title'/>
                            </div>
                            <div className='form-group'>
                                <label>Campaign Description</label>
                                <textarea className='form-control' name="campg_desc" rows="2" onChange={this.handleChange} value={this.state.campg_desc}></textarea>
                            </div>
                            <div className='row'>
                                <div className='form-group col-md-6'>
                                    <label>Start Date</label>
                                    <input type="date" onChange={this.handleChange} className="form-control" value={this.state.campg_start}
                                    required name='campg_start'/>
                                </div>
                                <div className='form-group col-md-6'>
                                    <label>End Date</label>
                                    <input type="date" onChange={this.handleChange} className="form-control" value={this.state.campg_end}
                                     required name='campg_end'/>
                                </div>
                            </div>
                            
                            <button className='btn btn-primary' type='submit'>ADD</button>
                        </form></div></div>
                    </div>                    
                    <div className='col-md-6'>
                    <div className='card'><div className='card-header'>Listed Campaings</div>
                        <div className='card-body'>
                        <table className='table table-bordered table-striped table-condensed'>
                            <thead><tr><th>S.No.</th><th>Title</th><th>Start</th><th>End</th><th>Action</th></tr></thead>
                            <tbody>
                            {this.state.campns.map(as=>
                            <tr key={as.campg_id}><td>{as.campg_title}<br/><span className='badge badge-info'>{as.fund_title}</span></td>
                                <td>{as.campg_start}</td>
                                <td>{as.campg_end}</td><td>
                            <button className='btn btn-warning btn-sm' onClick={()=>this.editAss(as.campg_id)}>Edit</button>
                                <button className='btn btn-danger btn-sm' onClick={()=>this.deleteAss(as.campg_id)}>Delete</button></td></tr>)}
                            </tbody>
                        </table></div></div>
                    </div>
                </div>
            </div>
        );
    }
}


