<?php require_once('jdf.php');
date_default_timezone_set('Asia/Tehran');
function jalaliTotsmp($date, $glue = '/'){
    $input = explode(' ',urldecode($date)); 
    $arr = explode('/', $input[0]);  
    $date = implode($glue,jalali_to_gregorian($arr[0], $arr[1], $arr[2]));
    return strtotime($date);
}
$db = new PDO("mysql:host=localhost;dbname=bookshoo_db;charset=utf8mb4",'bookshoo_db','OEKoG-T}G]Eu');
$msg = "";
if(isset($_GET['startdate'], $_GET['enddate'], $_GET['userid'])){
    
    $userid = intval($_GET['userid']);
    $user_detail = $db->query("select * from fl_user where userid='$userid'")->fetch(2);
    if(!$user_detail) {
        $msg = "کاربر مورد نظر یافت نشد";
    }else{
        $start = jalaliTotsmp($_GET['startdate']);
        $end = jalaliTotsmp($_GET['enddate']);  
        $order_amount = $db->query("SELECT sum(amount) as total FROM fl_order WHERE userid='$userid' and status=1 and date BETWEEN $start and $end")->fetch(2)['total'];
    
        $wallet_amount = $db->query("SELECT sum(amount) as sum FROM fl_wallet WHERE status=1 and userid='$userid'")->fetch(2)['sum'];
        //$wallet_amount = $db->query("SELECT sum(amount) as sum FROM fl_wallet WHERE status=1 and userid='$userid' and date BETWEEN $start and $end")->fetch(2)['sum'];
        
        $amount = intval($order_amount) + intval($wallet_amount);
    
        $uname = $user_detail['name'];
        $msg = "👤اطلاعات کاربر : $userid ($uname) \n💰مبلغ کل : $amount تومان \n\n";

    }
    
}
?>
<!doctype html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">

    <title>Stat</title>

    <!-- Bootstrap core CSS -->
    <link href="css/bootstrap.rtl.min.css" rel="stylesheet">
    <link type="text/css" rel="stylesheet" href="css/persianDatepicker-default.css" />

  </head>

  <body dir="rtl">
    <div class="container">
        <div class="row">
            <div class="card-deck mb-3 mt-3 text-center col-6" style=" margin: 0 auto; ">
                <div class="card mb-4 shadow-sm">
                  <div class="card-header">
                    <h4 class="my-0 font-weight-normal">آمار</h4>
                  </div>
                  <div class="card-body">
                      <form method="GET" action="">
                          <input class="form-control mb-2" type="text" name="userid" placeholder="آیدی عددی کاربر" required />
                          <input class="form-control mb-2" type="text" id="startdate" name="startdate" placeholder="شروع" required />
                          <input class="form-control" type="text" id="enddate" name="enddate" placeholder="پایان" required />
                          <button type="submit" class="btn btn-block btn-primary mt-3 mb-4">دریافت آمار</button>
                      </form>
                    <?php if($msg != '') echo '<div class="alert alert-success" style="text-align: right" role="alert">'.nl2br($msg).'</div>'; ?>
                  </div>
                </div>
            </div>
        </div>
    </div> <!-- container -->

    <script type="text/javascript" src="js/jquery-1.10.1.min.js"></script>
    <script type="text/javascript" src="js/persianDatepicker.min.js"></script>
    <script>
      $(function() {
        $("#startdate, #enddate").persianDatepicker();       
    });
    </script>
  </body>
</html>
