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 ResetPassword(props){
    const navigate = useNavigate();
    const { userid } = useParams('user');
    const [inputField , setInputField] = useState({password:'',cpassword:''});

    const handleChange = (e) =>{
        const value = e.target.value;
        setInputField({
            ...inputField,
            [e.target.name]: value
        });
    }

    const formSubmit = param => e => {e.preventDefault();
        if(inputField.password!=inputField.cpassword){alert("Both Password must match");return false;}
        if(inputField.password.length<6){alert("Both must be atleast 6 char long");return false;}
        
        axios.post('https://capitalsgrowth.in/Rest/change_password_api/'+userid,{password:inputField.password})
            .then((res) => {
            console.log(res.data);return false;
            alert(res.data.msg);  
            if(res.data.status==0){
                navigate("/login");
            }          
        }).catch((err) => alert("Error start trading"));    
    }

        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">Reset Password</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">New Password</label>
                                <input type="password" name="password" value={inputField.password} onChange={handleChange} 
                                 className="form-control" placeholder=""/>
                            </div>
                        </div>

                        <div className="form-group basic">
                            <div className="input-wrapper">                            
                                <label className="label" for="email1">Confirm Password</label>
                                <input type="password" name="cpassword" value={inputField.cpassword} 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 PASSWORD</button>
        </div>

    </div>
            </div>
        )
    }



