import React from 'react';
import axios from 'axios';
import { useState,useEffect } from "react";
import { useParams,Link, useNavigate} from 'react-router-dom';
import { Helmet } from 'react-helmet';

export default function Bank(props){
    
    const [inputField , setInputField] = useState({bank_name:'',acc_no:'',ifsc:'',acc_holder:''});

    const handleChange = (e) =>{
        const value = e.target.value;
        setInputField({
            ...inputField,
            [e.target.name]: value
        });
    }
    
    const fetchData =()=>{
        fetch('https://capitalsgrowth.in/Rest/user_details_api/'+localStorage.getItem('userid'))
        .then((response) =>{
            return response.json();
        }).then((data)=>{
            console.log(data)
            setInputField({ ...inputField,bank_name: data.bank_name,acc_holder:data.acc_holder,ifsc:data.ifsc,acc_no:data.acc_no})
        });
       
    }
    useEffect(()=>{
        fetchData();
    },[])

    const formSubmit = param => e => {e.preventDefault();
        //console.log('param '+param);return false;
        //if(inputField.lot==''){alert("Please Enter Lot");return false;}
        var formData = new FormData();
        formData.append("bank_name", inputField.bank_name);
        formData.append("acc_no", inputField.acc_no);
        formData.append("ifsc", inputField.ifsc);
        formData.append("acc_holder", inputField.acc_holder);
        formData.append("id", localStorage.getItem('userid'));
        const config = {
            headers: {
              'content-type': 'multipart/form-data',
            },
          };
      
        axios.post('https://capitalsgrowth.in/Rest/bank_update_api', formData,config)
            .then((res) => {
            //console.log(res.data);return false;
            alert(res.data.msg);            
        }).catch((err) => alert("Couldnot update bank"));    
    }

        return (     
            <div>
            <Helmet>
            <title>MCX Trading</title>
            <link rel="stylesheet" href="/site/css/style.css"/>
            <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css"/> 
            <script src="/site/js/lib/bootstrap.bundle.min.js"></script>
            <script src="/site/js/plugins/splide/splide.min.js"></script>
            
            </Helmet>
            
            <div className="appHeader no-border">
        <div className="left">
            <Link to="/settings" className="headerButton goBack">
                <i className="fa fa-arrow-left"></i> 
            </Link>
        </div>
        <div className="pageTitle">Bank Details</div>
        <div className="right"> </div>
    </div>
    <div id="appCapsule">
    
        <div className="section">
            <div className="splash-page  pt-1 pb-1">
                
                        <div className="form-group basic">
                            <div className="input-wrapper">
                                <label className="label" for="email1">Bank Name</label>
                                <input type="text" name="bank_name" value={inputField.bank_name} onChange={handleChange} 
                                 className="form-control" placeholder=""/>
                            </div>
                        </div>

                        <div className="form-group basic">
                            <div className="input-wrapper">
                                <label className="label" for="email1">Account Holder</label>
                                <input type="text" name="acc_holder" value={inputField.acc_holder} onChange={handleChange} 
                                 className="form-control" placeholder=""/>
                            </div>
                        </div>

                        <div className="form-group basic">
                            <div className="input-wrapper">
                                <label className="label" for="email1">Account Number</label>
                                <input type="text" name="acc_no" value={inputField.acc_no} onChange={handleChange} 
                                 className="form-control" placeholder=""/>
                            </div>
                        </div>

                        <div className="form-group basic">
                            <div className="input-wrapper">
                                <label className="label" for="email1">IFSC Code</label>
                                <input type="text" name="ifsc" value={inputField.ifsc} onChange={handleChange} 
                                 className="form-control" placeholder=""/>
                            </div>
                        </div>
                    

            
        </div>
        {/* <div className="section full">
                    <div className="wide-block pt-1 pb-1">
                        <div className="form-check form-check-inline">
                            <input className="form-check-input" onChange={handleChange} value='normal' type="radio" name="otype"
                            checked={inputField.otype === "normal"}/>
                            <label className="form-check-label" for="normal"> Normal Order </label>
                        </div> &nbsp;&nbsp;&nbsp;
                        <div className="form-check form-check-inline">
                            <input className="form-check-input" onChange={handleChange} value='stoploss' type="radio" name="otype" 
                            checked={inputField.otype === "stoploss"}/>
                            <label className="form-check-label" for="stoploss"> Stoploss </label>
                        </div> 
                    </div>
                </div> */}
            </div>

        <div className="fixed-bar">
        <button type='button' onClick={formSubmit()} className="btn btn-lg btn-danger btn-block">UPDATE</button>
        </div>

    </div>
            </div>
        )
    }


