<?php
include_once("../include/main.inc.php");
check_session('MyCpanel','index.php');
################ Paging Starts From Here ##################
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

$heading="Manage Orders";
$record_type="Orders";
$script_name="manage-orders.php";
$table_name="orders_tbl1";
#################### Add Record ##################

####################################################

if(isset($_REQUEST['action']) && $_REQUEST['action'] =="Update") {
	while (list($key,$val)=each($_POST[])) {
		if (substr($key,0,6)=="order_") {
			$key1 = substr($key,6,strlen($key)-6);
			$qry1=db_query("update $table_name set catOrder='$val' where catID='$key1'");
		}
	}
	$_SESSION['session_message']='<div class="btn btn-success">' . $record_type . ' has been updated successfully  </div>';
    
	header("Location: ".SITE_ADMIN_URL."/".$script_name."?start=".$start."&parentID=".$parentID."&catID=".$catID."&pagesize=".$pagesize);
	exit;
}


if (isset($_POST['e-kyc'])) {
    if (!empty($_POST['arr_ids'])) {
        $orderIDs = $_POST['arr_ids'];
        foreach ($orderIDs as $orderID) {
            mysqli_query($dbconn, "UPDATE $table_name SET ekyc = 1 WHERE order_id = '$orderID'");
        }
        $_SESSION['session_message'] = '<div class="btn btn-success">e-KYC has been updated successfully</div>';
    } else {
        $_SESSION['session_message'] = '<div class="btn btn-danger">No orders selected</div>';
    }
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;
}
if (isset($_POST['video'])) {
    if (!empty($_POST['arr_ids'])) {
        $orderIDs = $_POST['arr_ids'];
        foreach ($orderIDs as $orderID) {
            mysqli_query($dbconn, "UPDATE $table_name SET video = 1 WHERE order_id = '$orderID'");
        }
        $_SESSION['session_message'] = '<div class="btn btn-success">Video has been updated successfully</div>';
    } else {
        $_SESSION['session_message'] = '<div class="btn btn-danger">No orders selected</div>';
    }
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;
}

if (isset($_POST['delete'])) {
    if (!empty($_POST['arr_ids'])) {
        $orderIDs = $_POST['arr_ids'];
        foreach ($orderIDs as $orderID) {
            mysqli_query($dbconn, "UPDATE $table_name SET status = 2 WHERE order_id = '$orderID'");
        }
        $_SESSION['session_message'] = '<div class="btn btn-success">Video has been updated successfully</div>';
    } else {
        $_SESSION['session_message'] = '<div class="btn btn-danger">No orders selected</div>';
    }
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;
}

if (isset($_POST['download'])) {
    if (!empty($_POST['arr_ids'])) {
        $orderIDs = $_POST['arr_ids'];
        foreach ($orderIDs as $orderID) {
            mysqli_query($dbconn, "UPDATE $table_name SET download = 1 WHERE order_id = '$orderID'");
        }
        $_SESSION['session_message'] = '<div class="btn btn-success">Order has been updated successfully</div>';
    } else {
        $_SESSION['session_message'] = '<div class="btn btn-danger">No orders selected</div>';
    }
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;
}
if (isset($_POST['status_active'])) {
    if (!empty($_POST['arr_ids'])) {
        $orderIDs = $_POST['arr_ids'];
        foreach ($orderIDs as $orderID) {
            mysqli_query($dbconn, "UPDATE $table_name SET status = 1 WHERE order_id = '$orderID'");
        }
        $_SESSION['session_message'] = '<div class="btn btn-success">Order has been updated successfully</div>';
    } else {
        $_SESSION['session_message'] = '<div class="btn btn-danger">No orders selected</div>';
    }
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;
}
if (isset($_POST['status_inactive'])) {
    if (!empty($_POST['arr_ids'])) {
        $orderIDs = $_POST['arr_ids'];
        foreach ($orderIDs as $orderID) {
            mysqli_query($dbconn, "UPDATE $table_name SET status = 0 WHERE order_id = '$orderID'");
        }
        $_SESSION['session_message'] = '<div class="btn btn-success">Order has been updated successfully</div>';
    } else {
        $_SESSION['session_message'] = '<div class="btn btn-danger">No orders selected</div>';
    }
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;
}
if (isset($_POST['status_inactive'])) {
    if (!empty($_POST['arr_ids'])) {
        $orderIDs = $_POST['arr_ids'];
        foreach ($orderIDs as $orderID) {
            mysqli_query($dbconn, "UPDATE $table_name SET status = 0 WHERE order_id = '$orderID'");
        }
        $_SESSION['session_message'] = '<div class="btn btn-success">Order has been updated successfully</div>';
    } else {
        $_SESSION['session_message'] = '<div class="btn btn-danger">No orders selected</div>';
    }
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;
}
if (isset($_POST['payment_paid'])) {
    if (!empty($_POST['arr_ids'])) {
        $orderIDs = $_POST['arr_ids'];
        foreach ($orderIDs as $orderID) {
            mysqli_query($dbconn, "UPDATE $table_name SET payment = 'Paid' WHERE order_id = '$orderID'");
        }
        $_SESSION['session_message'] = '<div class="btn btn-success">Order has been updated successfully</div>';
    } else {
        $_SESSION['session_message'] = '<div class="btn btn-danger">No orders selected</div>';
    }
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;
}
if (isset($_POST['delivered'])) {
    if (!empty($_POST['arr_ids'])) {
        $orderIDs = $_POST['arr_ids'];
        foreach ($orderIDs as $orderID) {
            mysqli_query($dbconn, "UPDATE $table_name SET delivered = 1 WHERE order_id = '$orderID'");
        }
        $_SESSION['session_message'] = '<div class="btn btn-success">Order has been updated successfully</div>';
    } else {
        $_SESSION['session_message'] = '<div class="btn btn-danger">No orders selected</div>';
    }
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;
}
if (isset($_POST['dongle_dispatch'])) {
    if (!empty($_POST['arr_ids'])) {
        $orderIDs = $_POST['arr_ids'];
        foreach ($orderIDs as $orderID) {
            mysqli_query($dbconn, "UPDATE $table_name SET dongle_dispatch = 1 WHERE order_id = '$orderID'");
        }
        $_SESSION['session_message'] = '<div class="btn btn-success">Order has been updated successfully</div>';
    } else {
        $_SESSION['session_message'] = '<div class="btn btn-danger">No orders selected</div>';
    }
    header("Location: " . SITE_ADMIN_URL . "/" . $script_name . "?start=" . $start . "&parentID=" . $parentID . "&catID=" . $catID . "&pagesize=" . $pagesize);
    exit;
}

$order= mysqli_fetch_assoc(mysqli_query($dbconn,"select * from orders_tbl1 where order_id=".$_REQUEST['orderid']));
if($order){
    // echo "select cname,cmobile, cemail from customer_tbl where cid=".$order['customer_id'];
    // exit;
    $user_sql=mysqli_query($dbconn,"select cname,cmobile, cemail from customer_tbl where cid=".$order['customer_id']);
    $user = mysqli_fetch_assoc($user_sql);

    $paid_amount = 0;
    $invoice = [];
    $payid = '';
    $paid = mysqli_query($dbconn, "SELECT * FROM order_payment_history WHERE invoice_id='".$order['invoice_id']."'");
    while($pay = mysqli_fetch_array($paid)){
        $invoice[] = $pay;
        $paid_amount += $pay['requested_amount'];
        //$invoice .= '<a class="ml-2 pl-1 pr-1" href="'.SITE_ADMIN_URL.'/manage-order-invoice.php?orderid='.$pay['payment_id'].'">Download Invoice-'.$insn.'</a> <a href="#" class="editPaymnt" data-id="'.$pay['payment_id'].'" data-amount="'.$pay['requested_amount'].'" data-tid="'.$pay['tracking_id'].'" data-toggle="modal" data-target="#editPayment">Edit</a>';
    }
}else{
    header('Location: manage-order.php');
    exit();
}

if(isset($_REQUEST['download_zip'])){
    $files = [];
    $docss = mysqli_query($dbconn,"SELECT docFile FROM orders_documents WHERE invoice_id='".$order['invoice_id']."'");
    while($d = mysqli_fetch_array($docss)){
        if(!empty($d['docFile'])){
            $files[] = $d['docFile'];
        }
    }
    
    $zipname = uniqid().'_file.zip';
    $zip = new ZipArchive();
    $zip->open($zipname, ZIPARCHIVE::CREATE);
    foreach($files as $file) {
        $zip->addFile('images/auth/'.$file, $file);
       // echo '<a href="images/auth/'.$file.'" >'.$file.'</a>';
    }
    $zip->close();
    header('Content-Type: application/zip');
    header('Content-disposition: attachment; filename="'.$zipname.'"');
    header('Content-Length: ' . filesize($zipname));
    readfile($zipname);
    unlink($zipname);
    
}

########################################################
admin_header();
    
?>
<style>
    .frm-error{
        color: red;
        font-size: 10px;
    }
    a.editPaymnt {
        color: red;
        font-size: 10px;
    }
</style>
<div class="content-wrapper">
    <div class="page-header">
        <h3 class="page-title">Manage Order</h3>
        <nav aria-label="breadcrumb">
            <ol class="breadcrumb">
                <li class="breadcrumb-item"><a href="welcome.php">Admin</a></li>
                <li class="breadcrumb-item"><a href="manage-orders.php">Orders</a></li>
                <li class="breadcrumb-item active" aria-current="page">Orders Details</li>
            </ol>
        </nav>
    </div>
    <style>
        #order-details td label{
            margin-bottom: 3px;
        }
        #order-details th h5{
            margin-bottom: 0px;
        }
        #order-details td p{
            border: 1px solid #ccc;
            background: #ededed;
            color: #000;
            padding: 8px;
            margin: 0px;
            border-radius: 3px;
            min-height: 33px;
        }
        #saveRemark{
            display: none;
        }
    </style>
    <table class="table table-bordered bg-white" id="order-details">
        <tbody>
            <tr>
                <th colspan="4"><h5>User details</h5></th>
            </tr>
            <?php if(mysqli_num_rows($user_sql)>0){ ?>
            <tr>
                <td style="width:25%">
                    <label>Name</label>
                    <p><?= $user['cname']?></p>
                </td>
                <td style="width:25%">
                    <label>Mobile</label>
                    <p><?= $user['cmobile']?></p>
                </td>
                <td style="width:25%">
                    <label>Email</label>
                    <p><?= $user['cemail']?></p>
                </td>
                <td style="width:25%"></td>
            </tr>
            <?php }else{  ?>
                <tr>
                <td style="width:25%">
                    <label>Name</label>
                    <p><?= $order['applicants_name']?></p>
                </td>
                <td style="width:25%">
                    <label>Mobile</label>
                    <p><?= $order['applicants_mobile']?></p>
                </td>
                <td style="width:25%">
                    <label>Email</label>
                    <p><?= $order['applicants_email']?></p>
                </td>
                <td style="width:25%"></td>
            </tr>
                <?php } ?>

            <tr>
                <th colspan="4"><h5>Applicants details <a class="float-right" href="#" data-toggle="modal" data-target="#editApplicants"><i class="fa fa-edit"></i></a></h5></th>
            </tr>
            <tr>
                <td style="width:25%">
                    <label>Name</label>
                    <p><?= $order['applicants_name']?></p>
                </td>
                <td style="width:25%">
                    <label>Mobile</label>
                    <p><?= $order['applicants_mobile']?></p>
                </td>
                <td style="width:25%">
                    <label>Email</label>
                    <p><?= $order['applicants_email']?></p>
                </td>
                <td style="width:25%">
                    <label>Organization</label>
                    <p><?= $order['applicants_org']?></p>
                </td>
            </tr>
            <tr>
                <td style="width:25%">
                    <label>Applicant PAN</label>
                    <p><?= $order['ApplicantPAN']?></p>
                </td>
                <td style="width:25%">
                    <label>Name as per PAN</label>
                    <p><?= $order['NameasperPAN']?></p>
                </td>
                <td style="width:25%">
                    <label>DOB</label>
                    <p><?= empty($order['dob'])?'':date('d M Y', strtotime($order['dob'])); ?></p>
                </td>
                <td style="width:25%">
                    <label>Aadhaar No</label>
                    <p><?= $order['AadhaarNo']?></p>
                </td>
            </tr>

            <tr>
                <td style="width:25%">
                    <label>Org. GST No</label>
                    <p><?= $order['OrgGSTNo']?></p>
                </td>
                <td style="width:25%">
                    <label>Org. Legal Name</label>
                    <p><?= $order['OrgLegalName']?></p>
                </td>
                <td style="width:25%">
                    <label>Org. Trade Name</label>
                    <p><?= $order['OrgTradeName']?></p>
                </td>
                <td style="width:25%"></td>
            </tr>
            
            <tr>
                <th colspan="4"><h5>Order details</h5></th>
            </tr>
            <tr>
                <td>
                    <label>Invoice no</label>
                    <p><?= $order['success_order_id']?></p>
                </td>
                <td>
                    <label>Order no</label>
                    <p><?= $order['invoice_id']?></p>
                </td>
                
                <td>
                    <label>Date</label>
                    <p><?= date('h:i:a d M Y', strtotime($order['order_date'])) ?></p>
                </td>
                
                <td>
                    <label>Payment Status</label>
                    <p><?= $order['payment_status']?></p>
                </td>                
            </tr>

            <tr>
                <th colspan="4"><h5>Payment details <?php if(((($order['total_amount']-$order['discount'])+$order['gst_amount'])-$paid_amount) > 0){echo '<a href="3" data-toggle="modal" data-target="#addPayment" class="float-right small">+ Add Payment</a>';}?></h5></th>
            </tr>
            
            <tr>
                <td>
                    <label>Amount</label>
                    <p>₹ <?= $t = $order['total_amount']?></p>
                </td>
                <td>
                    <label>Discount</label>
                    <p>₹ <?= $d = $order['discount']; ?></p>
                </td>
                <td>
                    <label>GST</label>
                    <p>₹ <?= $g = $order['gst_amount']?></p>
                </td>
                <td>
                    <label>Recieved / Balance</label>
                    <p>₹ <?= $paid_amount?> / ₹ <?= (($t-$d)+$g)-$paid_amount?></p>
                </td>                
            </tr>
            <tr>
                <td colspan="4">
                <table class="table table-sm">
                        <thead>
                            <tr>
                                <td>Sn.</td>
                                <td>Date</th>
                                <td>Payment ID</td>
                                <td>Gateway TID</td>
                                <td>Amount</td>
                                <td>Pay Mode</td>
                                <td>Remark</td>
                                <td>Action</td>
                            </tr>
                        </thead>
                        <tbody>
                            <?php $tsn =1; foreach($invoice as $invc){?>
                            <tr>
                                <td><?= $tsn++; ?></td>
                                <td><?= date('d m Y h:ia', strtotime($invc['created_at'])); ?></td>
                                <td><?= $invc['payment_invoice_id']; ?></td>
                                <td><?= $invc['tracking_id']; ?></td>
                                <td><?= $invc['requested_amount']; ?></td>
                                <td><?= $invc['payment_mode']; ?></td>
                                <td><?= $invc['payment_remark']; ?></td>
                                <td>
                                    <a class="ml-2 pl-1 pr-1" href="<?= SITE_ADMIN_URL ?>/manage-order-invoice.php?orderid=<?= $invc['payment_id'] ?>">Download Invoice</a> 
                                    <a href="#" class="editPaymnt" data-id="<?= $invc['payment_id'] ?>" data-amount="<?= $invc['requested_amount'] ?>" data-tid="<?= $invc['tracking_id'] ?>" data-toggle="modal" data-target="#editPayment">Edit</a>    
                                </td>
                            </tr>
                            <?php } ?>
                        </tbody>

                    </table>
                </td>
            </tr>
            
            <tr>
                <th colspan="4">
                    <h5>Product Description <a class="float-right" href="#" data-toggle="modal" data-target="#editProduct"><i class="fa fa-edit"></i></a></h5>
                </th>
            </tr>
            <tr>
                <?php
                    $sn = 1;
                    $ekyc_link = '#';
                    $oldProduct = '';
                    $items = mysqli_query($dbconn,"SELECT o.*, catName FROM order_items AS o LEFT JOIN category_tbl AS c ON o.category_id=c.catID WHERE invoice_id='".$order['invoice_id']."'");
                    while($p = mysqli_fetch_array($items)){
                        if(!empty($p['brand'])){
                            $elnk = mysqli_fetch_assoc($dbconn->query("SELECT ekyc_link FROM ekyclink_tbl WHERE catID='".$p['category_id']."' AND brand='".$p['brand']."' AND validity='".$p['validity']."' AND user_type='".$p['user_type']."' AND org_type='".$p['organizationType']."'"));
                            if($elnk){$ekyc_link = $elnk['ekyc_link']; }
                            $dsc_sign = $p;
                        }

                        if(!empty($p['token_type'])){$token_pack = $p;}
                ?>
                <td colspan="2">
                    <h6><?= $p_name = empty($p['brand'])?'Token: ':'Signature: '; ?><b><?= $p['catName']?></b><h6>
                    <div>
                        <?php if(!empty($p['brand'])){ echo 'Brand: <b>'.$p['brand'].'</b> |';}; ?>
                        <?php if(!empty($p['user_type'])){ echo 'User Type: <b>'.$p['user_type'].'</b> |';}; ?>
                        <?php if(!empty($p['organizationType'])){ echo 'Organization: <b>'.$p['organizationType'].'</b> |';}; ?>
                        <?php if(!empty($p['validity'])){ echo 'Validity: <b>'.$p['validity'].' Year</b>';}; ?>
                        <?php if(!empty($p['quantity']) && empty($p['brand'])){ echo 'Quanity: <b>'.$p['quantity'].'</b> |';}; ?>
                        <?php if(!empty($p['token_type'])){ echo 'Token Typpe: <b>'.$p['token_type'].'</b>';}; ?>
                        | Price: <b><?= $p['quantity']; ?> X ₹<?= $p['price_each'] ?> = ₹<?= $p['quantity']*$p['price_each'] ?></b>
                        
                    </div>
                </td>
                <?php 
                    $oldProduct .= $p_name.': '.$p['catName'];
                    $oldProduct .= !empty($p['brand'])?', Brand: '.$p['brand']:'';
                    $oldProduct .= !empty($p['user_type'])?', User Type: '.$p['user_type']:'';
                    $oldProduct .= !empty($p['organizationType'])?', Organization: '.$p['organizationType']:'';
                    $oldProduct .= !empty($p['validity'])?', Validity: '.$p['validity']:'';
                    $oldProduct .= !empty($p['token_type'])?', Token type: '.$p['token_type']:'';
                    $oldProduct .= !empty($p['quantity'])?', Quantity: '.$p['quantity']:'';
                    $oldProduct .= ', Price: '.($p['quantity']*$p['price_each']).' | ';
                    } 
                ?>
            </tr>
            <tr>
                <th colspan="4"><h5>Billing Address</h5></th>
            </tr>
            <tr>
                <td colspan="2">
                    <label>Organization</label>
                    <p><?= $order['bill_ogname']?></p>
                </td>
                <td colspan="2">
                    <label>GST Number</label>
                    <p><?= $order['bill_gst']?></p>
                </td>
            </tr>
            <tr>
                <td>
                    <label>Address</label>
                    <p><?= $order['bill_address']?></p>
                </td>
                <td>
                    <label>City</label>
                    <p><?= $order['bill_city']?></p>
                </td>
                <td>
                    <label>Zipcode</label>
                    <p><?= $order['bill_zip']?></p>
                </td>
                <td>
                    <label>State/Country</label>
                    <p><?= $order['bill_state']; ?> / <?= $order['bill_country']?></p>
                </td>                
            </tr>
            <?php if($order['delivery_status'] != 'NA'){?>
            <tr>
                <th colspan="4"><h5>Delivery Address</h5></th>
            </tr>
            <tr>
                <td>
                    <label>Address</label>
                    <p><?= $order['delivery_address']?></p>
                </td>
                <td>
                    <label>City</label>
                    <p><?= $order['delivery_city']?></p>
                </td>
                <td>
                    <label>Zipcode</label>
                    <p><?= $order['delivery_zip']?></p>
                </td>
                <td>
                    <label>State/Country</label>
                    <p><?= $order['delivery_state']?> / <?= $order['delivery_country']?></p>
                </td>                
            </tr>
            <?php } ?>
            
            <?php 
                if($order['ekyc'] != 'NA'){
                $document = mysqli_query($dbconn,"SELECT * FROM orders_documents WHERE invoice_id='".$order['invoice_id']."'");
            ?>
            <tr>
                <th colspan="4"><h5>Document Status <a href="3" data-toggle="modal" data-target="#addDocument" class="float-right small">+ Add Document</a></h5></th>
            </tr>
            <tr>
                <td colspan="4" id="allImages">
                <div class="row">
                <?php 
                    $maildoc = [];  $downbtn = 0;  $dsn = 1; 
                    while($dcs = mysqli_fetch_array($document)){ 
                        $maildoc[] =  $dcs['docName'];
                ?>
                    
                        <div class="col-6 mb-3 border-bottom pb-2">
                            <?= $dsn++; ?>) <?=  $dcs['docName']; ?>
                            <span class="float-right">
                                <?= empty($dcs['docFile'])?'<span class="text-danger">PENDING</span>':'<a target="_blank" class="docFilss" href="'.SITE_ADMIN_URL.'/images/auth/'.$dcs['docFile'].'" title="View Document"><i class="fa fa-eye"></i></a> | <a target="_blank" href="'.SITE_ADMIN_URL.'/images/auth/'.$dcs['docFile'].'" download title="Download Document"><i class="fa fa-download text-warning"></i></a> | <a href="#" class="text-danger removeDoc" data-id="'.$dcs['docID'].'" title="Delete Document"><i class="fa fa-times"></i></a>'; ?>

                                <a href="#" class="ml-3 DocFrm" data-toggle="modal" data-target="#DocForm" data-id="<?= $dcs['docID']; ?>" data-name="<?= $dcs['docName']; ?>">Upload</a>
                                <a href="#" class="ml-3 deleteDocs text-danger" data-id="<?= $dcs['docID']; ?>" title="Remove document from list"><i class="fa fa-trash"></i></a>
                                
                            </span>
                        </div>
                    
                <?php if(!empty($dcs['docFile'])){$downbtn++;}} ?>
                </div>
                <div class="row">
                    <div class="col-12 mb-3">
                        <?php if($downbtn > 0){ ?>
                        <!-- <button class="btn btn-sm btn-info" onclick="downloadAll()"> Download All Images</button> -->
                        <a class="badge bg-info text-white" href="<?= SITE_ADMIN_URL.'/manage-order-detail.php?orderid='.$order['order_id'].'&download_zip=yes'?>">Download All Documents</a>
                        <?php } ?>
                        <a href="#" class="badge bg-warning text-white" data-toggle="modal" data-target="#DocEmail" data-id="'.$dcs['docID'].'">Email For Pending Document</a>

                    </div>
                </div>
                <div>
                    <form action="">
                        <div>
                            <b>Remark for documents</b> 
                            <a class="ml-2" title="Edit remarks" id="editRemark" href="javascript:void(0)"><i class="fa fa-pen text-info"></i></a>
                            <a class="ml-2" title="Save remarks" id="saveRemark" href="javascript:void(0)"><i class="fa fa-save text-primary"></i></a>
                        </div>
                        <textarea readonly class="form-control mt-2 p-2" id="remarkBox"><?= $order['doc_remarks'];?></textarea>
                    </form>
                </div>
                    
                </td>       
            </tr>
           
            <?php } ?>

            <tr>
                <th colspan="4"><h5>Order Status <a class="float-right" href="#" data-toggle="modal" data-target="#updateStatus"><i class="fa fa-edit"></i></a></h5></th>
            </tr>
            <tr>
                <td colspan="2">
                    <div class="row">
                        <div class="col-12 mb-3 border-bottom pb-2">
                            Order Status:
                            <span class="float-right"><?= ($order['order_status'] == 'ACTIVE')?'<span class="text-success">ACTIVE</span>':'<span class="text-danger">INACTIVE</span>'; ?></span>
                        </div>
                        
                        <?php if($order['ekyc'] != 'NA'){ ?>
                        <div class="col-12 mb-3 border-bottom pb-2">
                            Application Number:
                            <span class="float-right"><?= $order['ApplicationNo']; ?></span>
                        </div>
                        <div class="col-12 mb-3 border-bottom pb-2">
                            eKYC:
                            <span class="float-right"><?= ($order['ekyc'] == 'COMPLETED')?'<span class="text-success">COMPLETED</span>':'<span class="text-danger">'.$order['ekyc'].'</span>'; ?></span>
                        </div>
                        <div class="col-12 mb-3 border-bottom pb-2">
                            eKYC Link:
                            <span class="float-right"><a href="<?= $ekyc_link; ?>" target="_blank">Click Here</a></span>
                        </div>
                        
                        <div class="col-12 mb-3 border-bottom pb-2">
                            eKYC Pin:
                            <span class="float-right"><?= $order['eKYCPin']; ?></span>
                        </div>
                        <div class="col-12 mb-3 border-bottom pb-2">
                            Challenge Code:
                            <span class="float-right"><?= $order['ChallengeCode']; ?></span>
                        </div>

                        <?php } ?>

                        <?php if($order['video'] != 'NA'){ ?>
                        <div class="col-12 mb-3 border-bottom pb-2">
                            Video Verification:
                            <span class="float-right"><?= ($order['video'] == 'COMPLETED')?'<span class="text-success">COMPLETED</span>':'<span class="text-danger">'.$order['video'].'</span>'; ?></span>
                        </div>
                        <?php } ?>

                        <?php if($order['caApproval'] != 'NA'){ ?>
                        <div class="col-12 mb-3 border-bottom pb-2">
                            CA Approval:
                            <span class="float-right"><?= ($order['caApproval'] == 'COMPLETED')?'<span class="text-success">COMPLETED</span>':'<span class="text-danger">'.$order['caApproval'].'</span>'; ?></span>
                        </div>
                        
                        <?php } ?>

                        

                        <?php if($order['download_status'] != 'NA'){ ?>
                        <div class="col-12 mb-3 border-bottom pb-2">
                            Download Status:
                            <span class="float-right"><?= ($order['download_status'] == 'COMPLETED')?'<span class="text-success">COMPLETED</span>':'<span class="text-danger">'.$order['download_status'].'</span>'; ?></span>
                        </div>
                        <?php } ?>

                    </div>
                    
                </td>
                <td colspan="2" style="vertical-align: baseline;">
                    <?php if($order['delivery_status'] != 'NA'){ ?>
                    <div class="row">
                        <div class="col-12 mb-3 border-bottom pb-2">
                            Delivery Status:
                            <span class="float-right"><?= (in_array($order['delivery_status'], ['DELIVERED', 'DISPATCHED']))?'<span class="text-success">'.$order['delivery_status'].'</span>':'<span class="text-danger">'.$order['delivery_status'].'</span>'; ?></span>
                        </div>
                        <div class="col-12 mb-3 border-bottom pb-2">
                            Courier Receipt:
                            <span class="float-right"><?= !empty($order['delievry_reciept'])?'<a href="'.SITE_ADMIN_URL.'/images/auth/'.$order['delievry_reciept'].'" target="_blank">View Receipt</a>':''; ?></span>
                        </div>
                        <div class="col-12 mb-3 border-bottom pb-2">
                            Courier Partner:
                            <span class="float-right"><?= $order['courierPartner']; ?></span>
                        </div> 
                        <div class="col-12 mb-3 border-bottom pb-2">
                            Docket Number:
                            <span class="float-right"><?= $order['docketNumber']; ?></span>
                        </div> 
                        <div class="col-12 mb-3 border-bottom pb-2">
                            Tracking Link:
                            <span class="float-right"><?= $order['trackingLink']; ?></span>
                        </div>
                        <div class="col-12 mb-3 border-bottom pb-2">
                            Token Password / Pin:
                            <span class="float-right"><?= $order['tokenPassword']; ?></span>
                        </div>
                    </div>
                    <?php } ?>
                </td>
            </tr>
            <tr>
                <td colspan="4">
                    <div>
                        <b>Remarks</b>
                    </div>
                    <textarea readonly class="form-control mt-2 p-2"><?= $order['remarks'];?></textarea>
                </td>
            </tr>
            <tr>
                <th colspan="4"><h5>Agent Details <a class="float-right" href="#" data-toggle="modal" data-target="#agentForm"><i class="fa fa-edit"></i></a></h5></th>
            </tr>
            <tr>
                <?php 
                    $agntmob = $agntName= 'Not Assigned';
                    if(!empty($order['agentId'])){
                        $agent = mysqli_fetch_assoc(mysqli_query($dbconn, "SELECT slno, mobile,name FROM admin_details WHERE slno=".$order['agentId']));
                        if($agent){
                            $agntName = $agent['name'];
                            $agntmob = $agent['mobile'];
                        }
                    }
                    
                ?>
                <td colspan="2">
                    <label>Agent Name</label>
                    <p><?= $agntName; ?></p>
                </td>
                <td colspan="2">
                    <label>Agent Mobile</label>
                    <p><?= $agntmob; ?></p>
                </td>
            </tr>
        </tbody>
    </table>

   
    <div class="alert alert-light bg-white mt-2">
        <h6>Order History: </h6>
        <?php 
            $hstry = mysqli_query($dbconn, "SELECT * FROM order_item_history WHERE invoice_id='".$order['invoice_id']."' ORDER BY history_id desc");
            while($hst = mysqli_fetch_array($hstry)){
        ?>
        <span class="border-right pr-2 mr-2"><b><?= date('d M Y h:ia', strtotime($hst['created_at'])).'</b>: '.$hst['remarks'].' ('.strtoupper($hst['label']).')'; ?> </span>
        <?php } ?>
    </div>
</div>

<div class="modal" id="editProduct" tabindex="-1" role="dialog">
    <div class="modal-dialog modal-dialog-lg" role="document">
        <div class="modal-content">
            <div class="modal-header p-3">
            <h5 class="modal-title">Update ordered product</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
            </div>
            <form class="forms-sample form_submit" method="POST" action="form_ajax.php" enctype="multipart/form-data">
                <div class="modal-body p-3">
                    <h5 id="productName" class="mb-2"></h5>
                    <div class="row">
                        <div class="col-md-12">
                            <div class="form-group mb-2">
                                <label>Signature Type</label>
                                <select name="order_signature" class="form-control" id="order_signature">
                                    <option value="">Select signature</option>
                                    <?php 
                                        $sign = mysqli_query($dbconn, "SELECT a.*, c.catName, brandName  FROM attribute_tbl AS a LEFT JOIN category_tbl AS c ON a.cat_id=c.catID LEFT JOIN brand_tbl AS b on b.slno=a.brand_id ORDER BY a.cat_id, a.user_type, a.brand_id, a.validity ASC");

                                        while($ds = mysqli_fetch_array($sign)){
                                            $dsc_sl = '';
                                            if(isset($dsc_sign)){
                                                if($dsc_sign['user_type'] == $ds['user_type'] && $dsc_sign['category_id'] == $ds['cat_id'] && $dsc_sign['brand'] == $ds['brandName'] && $dsc_sign['validity'] == $ds['validity'] && $dsc_sign['user_type'] == $ds['user_type']){$dsc_sl = 'selected';}
                                            }
                                            echo '<option '.$dsc_sl.' value="'.$ds['slno'].'" data-price="'.$ds['price'].'" data-type="'.$ds['user_type'].'">'.$ds['catName'].' | Brand: '.$ds['brandName'].' | Validty: '.$ds['validity'].' Year | User Type: '.$ds['user_type'].'</option>';
                                        }
                                    ?>

                                </select>
                            </div>
                            <div id="changed-product"></div>
                            <div class="form-group" <?= (isset($dsc_sign) && !empty($dsc_sign['organizationType']))?'':'style="display:none;"'; ?> id="bussinesstype">
                                <label for="">Organization type</label>
                                <select class="form-control" name="order_bussinesstype">
                                    <option value="Proprietorship" <?= (isset($dsc_sign) && $dsc_sign['organizationType'] == 'Proprietorship')?'selected':''?>>Proprietorship </option>
                                    <option value="Partnership" <?= (isset($dsc_sign) && $dsc_sign['organizationType'] == 'Partnership')?'selected':''?>>Partnership</option>
                                    <option value="LLP" <?= (isset($dsc_sign) && $dsc_sign['organizationType'] == 'LLP')?'selected':''?>>LLP</option>
                                    <option value="PrivateLimited" <?= (isset($dsc_sign) && $dsc_sign['organizationType'] == 'PrivateLimited')?'selected':''?>>Private Limited</option>
                                    <option value="PublicLimited" <?= (isset($dsc_sign) && $dsc_sign['organizationType'] == 'PublicLimited')?'selected':''?>>Public Limited</option>
                                    <option value="NGO / Trust" <?= (isset($dsc_sign) && $dsc_sign['organizationType'] == 'NGO / Trust')?'selected':''?>>NGO / Trust</option>
                                    <option value="HUF" <?= (isset($dsc_sign) && $dsc_sign['organizationType'] == 'HUF')?'selected':''?>>HUF</option>
                                    <option value="Government / PSU" <?= (isset($dsc_sign) && $dsc_sign['organizationType'] == 'Government / PSU')?'selected':''?>>Government / PSU </option>
                                    <option value="Bank" <?= (isset($dsc_sign) && $dsc_sign['organizationType'] == 'Bank')?'selected':''?>>Bank</option>
                                    <option value="AOP" <?= (isset($dsc_sign) && $dsc_sign['organizationType'] == 'AOP')?'selected':''?>>AOP</option>
                                    <option value="BOI" <?= (isset($dsc_sign) && $dsc_sign['organizationType'] == 'BOI')?'selected':''?>>BOI</option>
                                    <option value="AJP" <?= (isset($dsc_sign) && $dsc_sign['organizationType'] == 'AJP')?'selected':''?>>AJP</option>
                                    <option value="LocalAuthority" <?= (isset($dsc_sign) && $dsc_sign['organizationType'] == 'LocalAuthority')?'selected':''?>>Local Authority</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="mt-3 mb-3">
                                <span>Want to include USB Token? </span>
                                <span class="ml-3" id="buy-token-button">
                                    <label class="mr-3 border-right pr-3">
                                        <input type="radio" name="order_token" value="0" <?= isset($token_pack)?'':'checked'; ?>> No
                                    </label>
                                    <label for="">
                                        <input type="radio" name="order_token" value="1" <?= isset($token_pack)?'checked':''; ?>> Yes
                                    </label>
                                </span>
                            </div>
                        </div>
                    </div>
                    <div class="row" id="buy-token" <?= isset($token_pack)?'':'style="display:none;"'; ?>>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label>Token Type</label>
                                <select name="order_token_id" class="form-control" aria-label="Default select example">
                                    <?php
                                    $tokn_list = mysqli_query($dbconn, "select catName, catID from category_tbl where parentID=11 and status='Y'");
                                    while ($tkn = mysqli_fetch_array($tokn_list)) {
                                        $slct = (isset($token_pack) && $token_pack['category_id'] == $tkn['catID'])?'selected':'';
                                        echo '<option value="' . $tkn['catID'] . '" '.$slct.'>' . $tkn['catName'] . '</option>';
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>

                        <div class="col-md-4">
                            <div class="form-group">
                                <label>Token for</label>
                                <select name="order_token_type" class="form-control" aria-label="Default select example">
                                    <?php
                                    $sql_token = mysqli_query($dbconn, "select * from token_tbl");
                                    while ($res_token = mysqli_fetch_assoc($sql_token)) {
                                        $slct1 = (isset($token_pack) && $token_pack['token_type'] == $res_token['slink_title'])?'selected':'';
                                        echo '<option value="' . $res_token['slink_title'] . '" '.$slct1.'>' . $res_token['slink_title'] . '</option>';

                                        $tsn++;
                                    }
                                    ?>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label>Quantity</label>
                                <input type="text" class="form-control form-control-sm" name="order_token_qty" placeholder="Enter Quantity" value="<?= (isset($token_pack))?$token_pack['quantity']:0;?>">
                            </div>
                        </div>
                        <div class="col-md-12">
                            <div class="form-group">
                                <label>Delivery address</label>
                                <input type="text" class="form-control form-control-sm text-uppercase" name="delivery_address" placeholder="Enter Address" value="<?= $order['delivery_address'];?>">
                            </div>
                        </div>
                       
                        <div class="col-md-4">
                            <div class="form-group">
                                <label>City</label>
                                <input type="text" class="form-control form-control-sm text-uppercase" name="delivery_city" placeholder="Enter Quantity" value="<?= $order['delivery_city']?>">
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label>Zipcode</label>
                                <input type="text" class="form-control form-control-sm" name="delivery_zip" placeholder="Enter Zip code" value="<?=$order['delivery_zip'];?>">
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group">
                                <label>State</label>
                                <input type="text" class="form-control form-control-sm text-uppercase" name="delivery_state" placeholder="Enter State" value="<?=$order['delivery_state'];?>">
                            </div>
                        </div>
                    </div>

                    <div class="form-group">
                        <label>Remarks</label>
                        <textarea class="form-control"  name="order_remarks"><?= $order['remarks']; ?></textarea>
                    </div>
                    <div class="row">
                        <div class="col-6">
                            <div class="form-group">
                                <label>Discount Coupon</label>
                                <input type="text" class="form-control form-control-sm" name="discount_coupon" placeholder="Enter Coupon" value="<?= $order['coupon_code']; ; ?>">
                            </div>
                        </div>
                        <div class="col-6">
                            <div class="form-group">
                                <label>Discounted Amount</label>
                                <input type="text" class="form-control form-control-sm" name="discount_amount" placeholder="Enter Amount" value="<?= round($order['discount']); ?>" onkeyup="this.value=this.value.replace(/[^0-9]/g,'')" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');">
                            </div>
                        </div>
                    </div>
                    
                </div>
                <div class="modal-footer p-3">
                    <input type="hidden" name="order_invoice_id" value="<?= $order['invoice_id']?>">
                    <input type="hidden" name="old_product" value="<?= $oldProduct; ?>">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary">Submit</button>
                </div>
            </form>
        </div>
    </div>
</div>

<div class="modal" id="updateStatus" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header p-3">
            <h5 class="modal-title">Update DSC status</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
            </div>
            <form class="forms-sample form_submit" method="POST" action="form_ajax.php" enctype="multipart/form-data">
                <div class="modal-body p-3">
                    <h5 id="productName" class="mb-2"></h5>
                    <div class="row">
                        <input type="hidden" name="itmId" value="<?= $order['order_id']; ?>">
                        <input type="hidden" name="itmInvoiceId" value="<?= $order['invoice_id']; ?>">
                        <div class="col-md-4">
                            <div class="form-group mb-2">
                                <label>Order Status</label>
                                <select name="order_status" class="form-control form-control-sm">
                                    <option value="">Select Status</option>
                                    <option value="ACTIVE" <?= ($order['order_status'] == 'ACTIVE')?'selected':''; ?>>ACTIVE</option>
                                    <option value="INACTIVE" <?= ($order['order_status'] == 'INACTIVE')?'selected':''; ?>>INACTIVE</option>
                                </select>
                            </div>
                        </div>
                        
                        <?php if($order['ekyc'] != 'NA'){?>
                        <div class="col-md-4">
                            <div class="form-group mb-2">
                                <label>Application number</label>
                                <input type="text" class="form-control form-control-sm p-1" name="application_number" value="<?= $order['ApplicationNo']; ?>">
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group mb-2">
                                <label>eKYC Status</label>
                                <select name="kyc" class="form-control form-control-sm">
                                    <option value="">Select Status</option>
                                    <option value="PENDING" <?= ($order['ekyc'] == 'PENDING')?'selected':''; ?>>PENDING</option>
                                    <option value="COMPLETED" <?= ($order['ekyc'] == 'COMPLETED')?'selected':''; ?>>COMPLETED</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group mb-2">
                                <label>eKYC Pin</label>
                                <input type="text" class="form-control form-control-sm p-1" name="eKYCPin" value="<?= $order['eKYCPin']; ?>">
                            </div>
                        </div>
                        <div class="col-md-4">
                            <div class="form-group mb-2">
                                <label>Challenge Code</label>
                                <input type="text" class="form-control form-control-sm p-1" name="ChallengeCode" value="<?= $order['ChallengeCode']; ?>">
                            </div>
                        </div>
                        <?php } if($order['caApproval'] != 'NA'){?>
                        <div class="col-md-4">
                            <div class="form-group mb-2">
                                <label>CA Approval</label>
                                <select name="caApproval" class="form-control form-control-sm">
                                    <option value="">Select Status</option>
                                    <option value="PENDING" <?= ($order['caApproval'] == 'PENDING')?'selected':''; ?>>PENDING</option>
                                    <option value="COMPLETED" <?= ($order['caApproval'] == 'COMPLETED')?'selected':''; ?>>COMPLETED</option>
                                </select>
                            </div>
                        </div>
                        <?php } if($order['video'] != 'NA'){?>
                        <div class="col-md-4">
                            <div class="form-group mb-2">
                                <label>Video Status </label>
                                <select name="video" class="form-control form-control-sm">
                                    <option value="">Select Status</option>
                                    <option value="PENDING" <?= ($order['video'] == 'PENDING')?'selected':''; ?>>PENDING</option>
                                    <option value="COMPLETED" <?= ($order['video'] == 'COMPLETED')?'selected':''; ?>>COMPLETED</option>
                                </select>
                            </div>
                        </div>
                        <?php } if($order['download_status'] != 'NA'){?>
                        <div class="col-md-4">
                            <div class="form-group mb-2">
                                <label>Download Status </label>
                                <select name="download" class="form-control form-control-sm">
                                    <option value="">Select Status</option>
                                    <option value="PENDING" <?= ($order['download_status'] == 'PENDING')?'selected':''; ?>>PENDING</option>
                                    <option value="COMPLETED" <?= ($order['download_status'] == 'COMPLETED')?'selected':''; ?>>COMPLETED</option>
                                </select>
                            </div>
                        </div>

                        <?php  } if($order['delivery_status'] != 'NA'){?>
                        <div class="col-md-12"></div>
                        <div class="col-md-6">
                            <div class="form-group mb-2">
                                <label>Delivery Status</label>
                                <select name="delivery" class="form-control form-control-sm">
                                    <option value="PENDING" <?= ($order['delivery_status'] == 'PENDING')?'selected':''; ?>>PENDING</option>
                                    <option value="INPROCESS" <?= ($order['delivery_status'] == 'INPROCESS')?'selected':''; ?>>INPROCESS</option>
                                    <option value="DISPATCHED" <?= ($order['delivery_status'] == 'DISPATCHED')?'selected':''; ?>>DISPATCHED</option>
                                    <option value="DELIVERED" <?= ($order['delivery_status'] == 'DELIVERED')?'selected':''; ?>>DELIVERED</option>
                                    <option value="FAILED" <?= ($order['delivery_status'] == 'FAILED')?'selected':''; ?>>FAILED</option>
                                    <option value="ATTEMPTED" <?= ($order['delivery_status'] == 'ATTEMPTED')?'selected':''; ?>>ATTEMPTED</option>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group mb-2">
                                <label>Courier Partner</label>
                                <select name="courierpartner" class="form-control form-control-sm">
                                    <option value="">Select Courier Partner</option>
                                    <?php 
                                        $courier = mysqli_query($dbconn, "SELECT partner_name FROM courier_partners WHERE status=1");
                                        while($crr = mysqli_fetch_array($courier)){
                                    ?>
                                    <option value="<?= $crr['partner_name']; ?>" <?= ($order['courierPartner'] == $crr['partner_name'])?'selected':''; ?>><?= $crr['partner_name']; ?></option>
                                    <?php } ?>
                                </select>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group mb-2">
                                <label>Courier Receipt</label>
                                <input type="file" class="form-control form-control-sm" name="CourierReceipt" value="<?= $order['trackingLink']; ?>">
                            </div>
                        </div>

                        <div class="col-md-12">
                            <div class="form-group mb-2">
                                <label>Tracking Link</label>
                                <input type="text" class="form-control form-control-sm" name="trackinglink" value="<?= $order['trackingLink']; ?>">
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-group mb-2">
                                <label>Docket Number</label>
                                <input type="text" class="form-control form-control-sm" name="docketNumber" value="<?= $order['docketNumber']; ?>">
                            </div>
                        </div>
                        
                        <div class="col-md-6">
                            <div class="form-group mb-2">
                                <label>Token Password</label>
                                <input type="text" class="form-control form-control-sm" name="tokenPassword" value="<?= $order['tokenPassword']; ?>">
                            </div>
                        </div>
                        
                        <?php } ?>
                    </div>
                                        
                    <div class="form-group">
                        <label>Remarks</label>
                        <textarea class="form-control" type="text" name="remarks"><?= $order['remarks']; ?></textarea>
                    </div>
                    
                </div>
                <div class="modal-footer p-3">
                    <input type="hidden" name="update_order" value="update_order">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary">Submit</button>
                </div>
            </form>
        </div>
    </div>
</div>

<div class="modal" id="logs" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header p-3">
            <h5 class="modal-title">History</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
            </div>
            <div class="modal-body p-1">
                <h5>History Of <span id="histPrd"></span></h5>
                <div id="prdHist">
                </div>
            </div>
        </div>
    </div>
</div>

<div class="modal" id="DocForm" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header p-3">
            <h5 class="modal-title">Upload Document</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
            </button>
            </div>
            <div class="modal-body p-1">
                <form action="form_ajax.php" class="form_submit" enctype="multipart/form-data">
                    <div class="mb-3" style="font-size: 13px;">Upload <span id="Docstitle"></span></div>
                    <div class="row">
                        <div class="col-md-9">
                            <div class="form-group">
                                <input type="file" name="upFile" class="form-control mb-0">
                            </div>
                        </div>
                        
                        <div class="col-md-3">
                            <button class="btn btn-success">Upload</button>
                        </div>
                        <div class="col-md-12">
                            <div>
                                <input type="hidden" name="updocumentid">
                                <input type="hidden" name="updocumentname">
                                <input type="hidden" name="uporderId" value="<?= $order['invoice_id']; ?>">
                            </div>
                        </div>
                    </div>
                </form>                
            </div>
        </div>
    </div>
</div>

<div class="modal" id="agentForm" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header p-3">
                <h5 class="modal-title">Update Agent</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body p-1">
                <form action="form_ajax.php" class="form_submit" enctype="multipart/form-data">
                    <div class="container">
                        <div class="row">
                            <div class="col-md-12" style="font-size: 13px;">Update Agent</div>
                            <div class="col-md-9">
                                <div class="form-group">
                                    <select class="form-control" name="upagentid">
                                        <option value="">Select agent</option>
                                        <?php 
                                            $agents = mysqli_query($dbconn, "SELECT slno, name FROM admin_details WHERE status = 'Y'");
                                            while($a = mysqli_fetch_array($agents)){
                                            echo '<option value="'.$a['slno'].'"  '.(($a['slno'] == $order['agentId'])?'selected':'').'>'.$a['name'].'</option>';
                                            }
                                        ?>
                                    </select>
                                    
                                </div>
                            </div>
                            
                            <div class="col-md-3">
                                <button class="btn btn-success">Update</button>
                            </div>
                            <div class="col-md-12"><input type="hidden" name="update_agent_oid" value="<?= $order['order_id']; ?>"></div>
                            <div class="col-md-12"><input type="hidden" name="agent_invoice" value="<?= $order['invoice_id']; ?>"></div>
                        </div>
                    </div>
                </form>                
            </div>
        </div>
    </div>
</div>

<div class="modal" id="DocEmail" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header p-3">
                <h5 class="modal-title">Send Email for Pending Document</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <form action="form_ajax.php" class="form_submit" enctype="multipart/form-data">
                <div class="modal-body p-3">
                    <div class="form-group mb-2">
                        <label>Select Document</label>
                        <div>
                            <?php foreach($maildoc as $mdc){?>
                            <div class="form-check form-check-inline d-flex">
                                <label class="form-check-label" for="<?= str_replace('_', ' ', $mdc); ?>"><?= $mdc; ?>
                                    <input class="form-check-input" name="pendingdoc[]" type="checkbox" id="<?= str_replace('_', ' ', $mdc); ?>" value="<?= $mdc; ?>">
                                </label>
                            </div>
                            <?php } ?>
                        </div>
                    </div>
                    <div class="form-group mb-1">
                        <label>Remarks</label>
                        <textarea name="docmailremark" class="form-control" required placeholder="Enter Your Remarks"></textarea>
                        <div><input type="hidden" name="pendingdoc_oId" value="<?= $order['order_id']; ?>"></div>
                    </div>
                </div>
                <div class="modal-footer p-3">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary">Submit</button>
                </div>
            </form>                
        </div>
    </div>
</div>

<div class="modal" id="addPayment" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header p-3">
                <h5 class="modal-title">Add Payment manualy</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <form action="form_ajax.php" class="form_submit" enctype="multipart/form-data">
                <div class="modal-body p-3">
                    <div class="row">
                        <div class="col-6">
                            <div class="form-group">
                                <label>Amount</label>
                                <input type="text" name="pay_amount" placeholder="Enter amount" class="form-control form-control-sm" onkeyup="this.value=this.value.replace(/[^0-9]/g,'')" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');">
                            </div>
                        </div>
                        <div class="col-6">
                            <div class="form-group">
                                <label>Payment mode</label>
                                <input type="text" name="pay_mode" placeholder="Payment mode" class="form-control form-control-sm">
                            </div>
                        </div>
                        <div class="col-12">
                            <div class="form-group">
                                <label>Payment ID</label>
                                <input type="text" name="pay_id" placeholder="Payment id" class="form-control form-control-sm">
                            </div>
                        </div>
                        
                    </div>
                    <div class="form-group mb-1">
                        <label>Remarks</label>
                        <textarea name="payremark" class="form-control" required placeholder="Enter Your Remarks"></textarea>
                        <div><input type="hidden" name="pay_invoiceid" value="<?= $order['invoice_id']; ?>"></div>
                        <div><input type="hidden" name="success_order_id" value="<?= $order['success_order_id']; ?>"></div>
                    </div>
                </div>
                <div class="modal-footer p-3">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary">Submit</button>
                </div>
            </form>                
        </div>
    </div>
</div>

<div class="modal" id="addDocument" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header p-3">
                <h5 class="modal-title">Add document manualy</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <form action="form_ajax.php" class="form_submit" enctype="multipart/form-data">
                <div class="modal-body p-3">
                    <div class="row">
                        <div class="col-12">
                            <div class="form-group">
                                <label>Document name</label>
                                <input type="text" name="document_name" placeholder="Enter Document name" class="form-control form-control-sm">
                            </div>
                            <div><input type="hidden" name="dosc_invoiceid" value="<?= $order['invoice_id']; ?>"></div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer p-3">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary">Submit</button>
                </div>
            </form>                
        </div>
    </div>
</div>

<div class="modal" id="editPayment" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header p-3">
                <h5 class="modal-title">Update Payment</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <form action="form_ajax.php" class="form_submit" enctype="multipart/form-data">
                <div class="modal-body p-3">
                    <div class="row">
                        <div class="col-6">
                            <div class="form-group">
                                <label>Amount</label>
                                <input type="text" name="epay_amount" placeholder="Enter amount" class="form-control form-control-sm" onkeyup="this.value=this.value.replace(/[^0-9]/g,'')" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');">
                            </div>
                        </div>
                        
                        <div class="col-6">
                            <div class="form-group">
                                <label>Payment ID</label>
                                <input type="text" name="epay_id" placeholder="Payment id" class="form-control form-control-sm">
                            </div>
                            <div><input type="hidden" name="epay_invoiceid" value="<?= $order['invoice_id']; ?>"></div>
                            <div><input type="hidden" name="pay_editid" value=""></div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer p-3">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary">Submit</button>
                </div>
            </form>                
        </div>
    </div>
</div>


<div class="modal" id="editApplicants" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header p-3">
                <h5 class="modal-title">Update Applicant Details</h5>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <form action="form_ajax.php" class="form_submit" enctype="multipart/form-data">
                <div class="modal-body p-3">
                    <div class="row">
                        <div class="col-6">
                            <div class="form-group">
                                <label>Name</label>
                                <input type="text" name="applicants_name" value="<?= $order['applicants_name']?>" class="form-control form-control-sm p-1">
                            </div>
                        </div>
                        <div class="col-6">
                            <div class="form-group">
                                <label>Organization</label>
                                <input type="text" name="applicants_org" value="<?= $order['applicants_org']?>" class="form-control form-control-sm p-1">
                            </div>
                        </div>

                        <div class="col-6">
                            <div class="form-group">
                                <label>Mobile</label>
                                <input type="text" name="applicants_mobile" value="<?= $order['applicants_mobile']?>" class="form-control form-control-sm p-1" onkeyup="this.value=this.value.replace(/[^0-9]/g,'')" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');">
                            </div>
                        </div>
                        <div class="col-6">
                            <div class="form-group">
                                <label>Email</label>
                                <input type="text" name="applicants_email" value="<?= $order['applicants_email']?>" class="form-control form-control-sm p-1">
                            </div>
                        </div>
                        <div class="col-6">
                            <div class="form-group">
                                <label>Applicant PAN</label>
                                <input type="text" name="ApplicantPAN" value="<?= $order['ApplicantPAN']?>" class="form-control form-control-sm p-1">
                            </div>
                        </div>
                        <div class="col-6">
                            <div class="form-group">
                                <label>Name as per PAN</label>
                                <input type="text" name="NameasperPAN" value="<?= $order['NameasperPAN']?>" class="form-control form-control-sm p-1">
                            </div>
                        </div>
                        <div class="col-6">
                            <div class="form-group">
                                <label>DOB</label>
                                <input type="date" name="dob" value="<?= $order['dob']?>" class="form-control form-control-sm p-1">
                            </div>
                        </div>
                        <div class="col-6">
                            <div class="form-group">
                                <label>Aadhaar No</label>
                                <input type="text" name="AadhaarNo" value="<?= $order['AadhaarNo']?>" class="form-control form-control-sm p-1">
                            </div>
                        </div>
                        <div class="col-6">
                            <div class="form-group">
                                <label>Org. GST No</label>
                                <input type="text" name="OrgGSTNo" value="<?= $order['OrgGSTNo']?>" class="form-control form-control-sm p-1">
                            </div>
                        </div>
                        <div class="col-6">
                            <div class="form-group">
                                <label>Org. Legal Name</label>
                                <input type="text" name="OrgLegalName" value="<?= $order['OrgLegalName']?>" class="form-control form-control-sm p-1">
                            </div>
                        </div>
                        <div class="col-6">
                            <div class="form-group">
                                <label>Org. Trade Name</label>
                                <input type="text" name="OrgTradeName" value="<?= $order['OrgTradeName']?>" class="form-control form-control-sm p-1">
                            </div>
                        </div>
                        
                        <div class="col-6">
                            <div><input type="hidden" name="applicantsUpdate" value="<?= $order['order_id']; ?>"></div>
                        </div>
                    </div>
                </div>
                <div class="modal-footer p-3">
                    <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary">Submit</button>
                </div>
            </form>                
        </div>
    </div>
</div>

<?php admin_footer(); ?>
<script>
    $(document).ready(function(){

        $('.editPaymnt').click(function(){
            $('input[name=pay_editid]').val($(this).data('id'));
            $('input[name=epay_amount]').val($(this).data('amount'));
            $('input[name=epay_id]').val($(this).data('tid'));
        });

        $('#buy-token-button input[type=radio]').change(function(){
            if($(this).val() == 1){
                $('#buy-token').show();
            }else{
                $('#buy-token').hide();
            }
        });

        $('#order_signature').change(function(){            
            let price = $('#order_signature option:selected').attr('data-price');
            let utype = $('#order_signature option:selected').attr('data-type');
            if(utype == 'Organization'){
                $('#bussinesstype').show();
            }else{
                $('#bussinesstype').hide();
            }
        });

        $('.DocFrm').click(function(){
            $('#Docstitle').text($(this).data('name'));
            $('input[name=updocumentid]').val($(this).data('id'));
            $('input[name=updocumentname]').val($(this).data('name'));
        });

        $('.removeDoc').click(function(e){
            e.preventDefault();
            let elm = $(this);
            let id = elm.data('id');
            $.ajax({
                url: 'form_ajax.php',
                data: {removeDoc_id: id},
                type: 'post',
                dataType: 'json',
                success: function(r){
                    if(r.success == true){
                        location.reload();
                    }else{
                        alert('error ..');
                    }
                }
            });
        });


        $('.histBtn').click(function(){
            $('#histPrd').text($(this).data('name'));
            $('#prdHist').html('<div class="text-center">Loading...</div>');
            let id = $(this).data('id');
            $.ajax({
                url: 'form_ajax.php',
                data: {itmHistory: id},
                type: 'post',
                dataType: 'json',
                success: function(r){
                    if(r.success == true){
                        $('#prdHist').html(r.html);
                    }else{
                        alert('error ..');
                    }
                }
            });
        });

        $('.form_submit').submit(function(e){
            e.preventDefault();
            let frm = $(this);
            let lnk = frm.attr('action');
            frm.find('input, textarea, select, button').prop('readonly', true);
            $('.frm-error').remove();
            $.ajax({
            url: lnk,
            data: new FormData(this),
            type:'post',
            dataType: 'JSON',
            processData: false,
            cache: false,
            contentType: false,
            success: function(r){
                if(r.success == true){
                if(r.alert){alert(alert());}
                if(r.msg){frm.before(r.msg);}
                if(r.rlink){window.location.href = r.rlink;}
                if(r.reload){location.reload();}
                }else{
                frm.find('input, textarea, select, button').prop('readonly', false);
                $.each(r.message, function(k,v){
                    $('input[name='+k+'],select[name='+k+'],textarea[name='+k+']').parent().before(v);
                });
                }
            }
            });
        });

        $('#editRemark, #saveRemark').click(function(){
            $('#editRemark, #saveRemark').toggle();
            $('#remarkBox').prop('readonly', !($('#remarkBox').prop('readonly')));
        });

        $('#saveRemark').click(function(e){
            e.preventDefault();
            let txt = $('#remarkBox').val();
            $.ajax({
                url: 'form_ajax.php',
                data: {savedoc_remark: txt, docremrk_id: <?= $order['order_id']; ?>},
                type: 'post',
                dataType: 'json',
                success: function(r){
                    if(r.success == true){
                        location.reload();
                    }else{
                        alert('error ..');
                    }
                }
            });
        });

        $('.deleteDocs').click(function(e){
            e.preventDefault();
            if(confirm('Do you really want to delete the document? Press OK to proceed.') == true){
                let id = $(this).data('id');
                let elm = $(this);
                $.ajax({
                    url: 'form_ajax.php',
                    data: {delete_docs: id},
                    type: 'post',
                    dataType: 'json',
                    success: function(r){
                        if(r.success == true){
                            elm.parents('.col-6').remove();
                        }else{
                            alert('error ..');
                        }
                    }
                });
            }
            

            
        });
    });
</script>
