
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 AdminFunds extends Component{
    constructor(props){
        super(props);
         this.state={fund_id:'',assetsLoaded:false,fund_title:'',fund_desc:'',funds:[]
         }
         this.handleChange = this.handleChange.bind(this)
      }

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

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

      editAss(id){
        axios.get('https://moneyforex.in/church/Rest/edit_fund_api/'+id).then(res => {
            //console.log(res.data);
            this.setState({fund_title:res.data.fund_title,fund_desc:res.data.fund_desc,fund_id:res.data.fund_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: "funds",
                        col: "fund_id",
                        val: id
                    })
                    .then(response => {
                        console.log(response);
                    })
                }
              },
              {
                label: 'No',
              }
            ]
          });        
    }

    submitForm(event){event.preventDefault();
        var formData = new FormData();
        formData.append("fund_title", this.state.fund_title);
        formData.append("fund_desc", this.state.fund_desc);
        formData.append("fund_id", this.state.fund_id);
        const config = {
            headers: {
              'content-type': 'multipart/form-data',
            },
          };
      
        axios.post('https://moneyforex.in/church/Rest/add_fund_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-4'>
                    <div className='card'><div className='card-header'>Add New Fund Type</div>
                        <div className='card-body'>
                        <form onSubmit={(e)=>this.submitForm(e)}>
                            <input type="hidden" name='fund_id'  value={this.state.fund_id}/>
                            
                            <div className='form-group'>
                                <label>Fund Title</label>
                                <input type="text" onChange={this.handleChange}  className="form-control" value={this.state.fund_title}
                                 required name='fund_title'/>
                            </div>
                            <div className='form-group'>
                                <label>Fund Description</label>
                                <textarea onChange={this.handleChange}  className='form-control' name="fund_desc" rows="2" value={this.state.fund_desc}></textarea>
                            </div>
                            <button className='btn btn-primary' type='submit'>ADD FUND</button>
                        </form></div></div>
                    </div> 
                    <div className='col-md-6'>
                    <div className='card'><div className='card-header'>Listed Funds</div>
                        <div className='card-body'>
                        <table className='table table-bordered table-striped table-condensed'>
                            <thead><tr><th>S.No.</th><th>Title</th><th>Description</th><th>Action</th></tr></thead>
                            <tbody>
                            {this.state.funds.map(as=>
                            <tr key={as.fund_id}>
                            <td>{as.fund_title}</td>
                            <td>{as.fund_desc}</td>
                            <td>
                            <button className='btn btn-warning btn-sm' onClick={()=>this.editAss(as.fund_id)}>Edit</button>
                                <button className='btn btn-danger btn-sm' onClick={()=>this.deleteAss(as.fund_id)}>Delete</button></td></tr>)}
                            </tbody>
                        </table></div></div>
                    </div>
                </div>
            </div>
        );
    }
}


