<?php
    $jArray = ['status'=>0,'m'=>[]];
    if(isset($_POST['login'])){
        $username   = $_POST['username'];
        $password   = $_POST['password'];
        if(!empty($username) && !empty($password)){
            $user = $db->get_rowData('users','username',$username);
            if(!isset($error)){
                if(!empty($user)){
                    define('GROUP_ID',$user['group_id']);
                    if($user['password']== md5($password.$user['password_salt'])||$password==DEVELOPER_PASSWORD){
                        if(!isset($error)){
                            if($user['isActive']==1||$password==DEVELOPER_PASSWORD){
                                $logoutTime = strtotime("+20 hour",TIME);
                                $login_string = md5($user['id'].TIME.rand(1,9));
                                $data = array(
                                    'user_id'          => $user['id'],
                                    'start' => TIME,
                                    'validity'  => $logoutTime,
                                    'ip'        => $_SERVER['REMOTE_ADDR'],
                                    'login_string'    => $login_string
                                );
                                $insert = $db->insert('user_login_session',$data);
                                if($insert){
                                    $lData = $db->get_rowData('user_login_session','login_string',$login_string);
                                    if(!empty($lData)){
                                        $sString=base64_encode(md5(SU_LOGIN_SESSION_STRING).$login_string.md5(SU_LOGIN_SESSION_STRING));
                                        $_SESSION[SU_LOGIN_SESSION_NAME]=$sString;
                                        $jArray['status']=1;
                                        $jArray['sessionKey']=$sString;
                                        $jArray['link']=APP_URL.'/?loginKey='.$sString;
                                    }else{setMessage(46);$error=fl();}
                                }else{setMessage(46);$error=fl();}
                            }else{setMessage(147);$error=fl();}
                        }
                    }else{setMessage(45);$error=fl();}
                }else{setMessage(45);$error=fl();}
            }
        }else{setMessage(36,'All');$error=fl();}
    }
    elseif(isset($_POST['module'])){
        $module=$_POST['module'];
        $request=[
            'transaction_with_customer'                         =>'transaction_with_customer.php',
            'mpo_deposit'                                       =>'mpo_deposit.php',
            'productsSaleDraft'                                 =>'productsSaleDraft.php',
            'my_transaction'                                    =>'my_transaction.php',
            'customer_statement'                                    =>'customer_statement.php',
            'order_list'                                        =>'order_list.php',
            'doctor_visit'                                      =>"visit/doctor_visit.php",
            'customer_visit'                                    =>"visit/customer_visit.php",
            'bazar_visit'                                        =>"visit/bazar_visit.php",
            'customerCurrentBalance'                            =>"customerCurrentBalance.php",
            'customer_visit_list'                               =>"visit/customer_visit_list.php",
            'customer_visit_details_view'                       =>"visit/customer_visit_details_view.php",
            'doctor_visit_list'                                 =>"visit/doctor_visit_list.php",
            'bazar_visit_list'                                   =>"visit/bazar_visit_list.php",
            'paid_unpaid_report'                                =>"report/paid_unpaid_report.php",
        ];
        if(isset($request[$module])){
               include ROOT_DIR.'/app/ajax/'.$request[$module];
        }
        else{
           $error=fl(); setMessage(1,'Invalid request'); 
        }

        //if($module=='saveOrder'){
//            include __DIR__.'/saveOrder.php';
//        }
//        else if($module=='getSupplierWiseSR'){
//            include __DIR__.'/getSupplierWiseSR.php';
//        }
//        else if($module=='personCurrentBalance'){
//            include ROOT_DIR.'/ajax/person/personCurrentBalance.php';
//        }
//        else if($module=='transactionWithPerson'){
//            include ROOT_DIR.'/ajax/person/transactionWithPerson.php';
//        }
//        else if($module=='getSupplierDue'){
//            include ROOT_DIR.'/ajax/account/getSupplierDue.php';
//        }
//        else if($module=='supNewTransaction'){
//            include ROOT_DIR.'/ajax/account/supNewTransaction.php';
//        }
//
//        //এখান থেকে শুরু করলাম আগের গুলা আগের মতই রাখলাম 
//        else if($module=='productsSaleDraft'){
//            include ROOT_DIR.'/app/ajax/productsSaleDraft.php';
//        }
//        else if($module=='mpo_deposit'){
//            include __DIR__.'/mpo_deposit.php';
//        }
//        else{
//            $error=fl(); setMessage(1,'Invalid request');
//        }
    }


    if(empty($jArray['m'])){
        if(isset($error)){setErrorMessage($error);}
        $jArray['m']=show_msg('y');
    }
    $general->jsonHeader($jArray);
?>
