<?php
ob_start();
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');

$formname = 'Patient / Meeting';
$maindb = 'accmast';
$printfile = "patient_print.php";
$file = 'patient.php';
$trandbexist = 0;
$keyfld = "accode";
$focusfield = "name";
$mgrpcode = '010';
$keycond1 = "AND (`grpcode`='$mgrpcode' or grpcode ='022')";

////////////////// FB ////////////////////
$adduser = $_SESSION['username_' . $dbname];

date_default_timezone_set("Asia/Kolkata");
$adddate = date("Y-m-d");
$addtime = date("h:i:sa");
//$usertype = $_SESSION["usertype_" . $dbname];
$usertype = $_SESSION["usertype"];
$ajpara = $_REQUEST["ajpara"];
// browse($ajpara);

$admin = $_SESSION["admin_" . $dbname];
$zdb = $_SESSION["zdbyear_" . $dbname];
mysqli_select_db($conn, $zdb);
include("includes/code_generate.php");
include("includes/prev&next.php");


if (isset($_POST["submit"])) {

    $a = 1;
    $name = mysqli_real_escape_string($conn, $_POST["name"]);
    $custcode = mysqli_real_escape_string($conn, $_POST["custcode"]);
    $area = mysqli_real_escape_string($conn, $_POST["area"]);
    $add1 = mysqli_real_escape_string($conn, $_POST["add1"]);
    $add2 = mysqli_real_escape_string($conn, $_POST["add2"]);
    $add3 = mysqli_real_escape_string($conn, $_POST["add3"]);
    $refby = mysqli_real_escape_string($conn, $_POST["refby"]);
    $purpvisit = mysqli_real_escape_string($conn, $_POST["purpvisit"]);
    $address = trim($add1) . ' ' . trim($add2) . ' ' . trim($add3);
    $apttime = $_POST["apttime"];
    $age = $_POST["age"];
    $dob = $_POST["dob"];
    $mob = $_POST["mobile"];
    $mob2 = $_POST["mobile2"];
    $email = $_POST["email"];
    $regndt = $_POST["regndt"];
    $gender = $_POST["gender"];
    $acttype = $_POST["acttype"];

    $mvouch = $qvouch;

    if ($name == "") {
        $a = 0;
        browse("Name Cannot Be Empty");
        if ($addflag == 1) {
            header("location:javascript://history.go(-1)");
        } else {
            header("refresh: ; $file?mode=last");
        }
    }

    if (isset($_POST["name"])) {
        $result = mysqli_query($conn, "SELECT * FROM `$maindb` WHERE `name`= '$name' AND accode != '$qvouch'") or die(mysqli_error($conn));
        if (mysqli_num_rows($result) > 0) {
            $a = 0;
            browse("Customer Name Already Exist");
            header("refresh: 1; $file?mode=last");
        }
    }

    if ($a == 1) {
        if ($acttype == "P") {
            $mgrpcode = "010";
        } else {
            $mgrpcode = "022";
        }

        mysqli_query($conn, "LOCK TABLE `$maindb` WRITE, consultmain WRITE, schedtreat WRITE");
        $tdate = date("Y-m-d");
        if ($addflag != 1) {

            if ($ajpara != "C") {
                mysqli_query($conn, "UPDATE `schedtreat` SET remarks='$rem1' WHERE scheddate ='$tdate' and accode = '$mvouch' and `consult` ='1'") or die(mysqli_error($conn));
            }
        }

        if ($addflag == 1) {
            $gen1 = mysqli_query($conn, "SELECT MAX($keyfld) AS maxcode FROM `$maindb` WHERE $keyfld !='' LIMIT 0,1 ") or die(mysqli_error($conn));

            if (mysqli_num_rows($gen1) > 0) {
                $vvouch = mysqli_fetch_assoc($gen1);
                $qvouch = $vvouch["maxcode"] + 1;
            } else {
                $qvouch = '1';
            }
            

            $gen1c = mysqli_query($conn, "SELECT MAX(vouchno) AS maxcode FROM `consultmain` WHERE vouchno !='' LIMIT 0,1 ") or die(mysqli_error($conn));

            if (mysqli_num_rows($gen1c) > 0) {
                $vvouchc = mysqli_fetch_assoc($gen1c);
                $qvouchc = $vvouchc["maxcode"] + 1;
            } else {
                $qvouchc = '1';
            }
            $date = date("Y-m-d");
            $time = date("h:i:sa");

            mysqli_query($conn, "INSERT INTO `$maindb`(accode,`name`, `custcode`,`address`, `add1`, `add2`, `add3`, `age`, `dob`, `regndt`,`grpcode`,`acttype`,
                                                mobile , mobile2 , `email`,`gender`, purpvisit, refby, apttime, `adduser`, `adddate`, `addtime`)
                                        VALUES('$qvouch','$name', '$custcode','$address', '$add1', '$add2', '$add3', '$age', '$dob', '$regndt','$mgrpcode','$acttype',
                                        '$mob' , '$mob2' , '$email', '$gender',  '$purpvisit', '$refby', '$apttime', '$adduser', '$adddate', '$addtime')") or die(mysqli_error($conn));

            if ($acttype == "P") {
                mysqli_query($conn, "INSERT INTO `consultmain`(`vouchno`, `vouchdt`, `vouchtime`, `accode`, purpvisit, refby, apttime)
                                        VALUES('$qvouchc','$date', '$time','$qvouch',  '$purpvisit', '$refby', '$apttime')") or die(mysqli_error($conn));
            }
            mysqli_query($conn, "INSERT INTO `schedtreat`(vouchno, `vouchdt`, scheddate,  `accode`, apttime, purpvisit, refby)
                                        VALUES('$qvouchc','$date', '$regndt','$qvouch', '$apttime', '$purpvisit', '$refby')") or die(mysqli_error($conn));
            insmess();
            header("refresh: 1; $file?mode=edit&$keyfld=$mvouch");
        } else {
            $mvouch = $qvouch;
            $editdate = date("Y-m-d");
            $edittime = date("h:i:sa");
            $oldregndt = $_POST["oldregndt"];
            if ($regndt != $oldregndt) {
                $gen1c = mysqli_query($conn, "SELECT vouchno, vouchdt FROM `consultmain` WHERE accode ='$mvouch' order by vouchdt DESC limit 1") or die(mysqli_error($conn));
                $fetvc = mysqli_fetch_array($gen1c);
                $qvouchc = $fetvc['vouchno'];
                $date = $fetvc['vouchdt'];

                mysqli_query($conn, "INSERT INTO `schedtreat`(vouchno, `vouchdt`, scheddate,  `accode`, apttime, purpvisit, refby)
                                        VALUES('$qvouchc','$date', '$regndt','$mvouch', '$apttime', '$purpvisit', '$refby')") or die(mysqli_error($conn));
//                mysqli_query($conn, "UPDATE `consultmain` set vouchdt = '$regndt'  WHERE $keyfld = '$qvouchc' ") or die(mysqli_error($conn));
            }



            mysqli_query($conn, "UPDATE `$maindb` SET `name`='$name',`custcode`='$custcode', `address` = '$address', `add1`='$add1', `add2`='$add2', `add3`='$add3', `age`='$age', 
                        `dob`='$dob', `mobile`='$mob', `mobile2`='$mob2',`email`='$email', `gender`='$gender',`regndt`='$regndt', `grpcode`='$mgrpcode',`acttype`='$acttype',
                         `purpvisit`='$purpvisit', `refby`='$refby', `edituser`='$adduser',`edittime`='$edittime',`editdate`='$editdate' WHERE $keyfld = '$mvouch' ") or die(mysqli_error($conn));
            //            updmess();
            //            header("refresh: 1; $file?$keyfld=$mvouch&mode=edit");
            header("refresh: 1; $file?mode=edit&$keyfld=$mvouch");
        }
        mysqli_query($conn, "UNLOCK TABLES");
    }
}

if ($addflag != 1) {
    $getvouch = '';
    if ($addflag == 0) {
        $getvouch = $_GET[$keyfld];
    } else {
        $getvouch = $qvouch;
    }



    $result = mysqli_query($conn, "SELECT * ,DATE_FORMAT(`adddate`, '%d-%m-%Y') as addt, DATE_FORMAT(`editdate`, '%d-%m-%Y') as eddt FROM `$maindb` WHERE $keyfld='$getvouch' $keycond1") or die(mysqli_error($conn));
    $get = mysqli_fetch_array($result);


    $getsched = mysqli_query($conn, "SELECT * FROM `schedtreat` WHERE scheddate='$get[regndt]' and accode= '$getvouch'") or die(mysqli_error($conn));
    if (!empty($getsched)) {
        $fsched = mysqli_fetch_array($getsched);
    }

    if ($get['acttype'] == 'P') {
        $gtype = "Patient";
    } else {
        $gtype = "Meeting";
    }
    if ($get['gender'] == 'M') {
        $gval = "Male";
    } else if ($get['gender'] == 'F') {
        $gval = "Female";
    } else {
        $gval = "Other";
    }
}

$senable = "";
$cenable = "";


if (isset($_GET["delete"])) {
    $getcustdtl = mysqli_query($conn, "SELECT * FROM `actran` WHERE `accode` = '$getvouch'") or die(mysqli_error($conn));
    if (mysqli_num_rows($getcustdtl) > 0) {
        browse("Patient Exist in Transaction");
        header("refresh: 1; $file?mode=last");
    } else {
        delete($zdb, "$maindb", "$keyfld", $_GET[$keyfld], "$file?mode=last");
        delete($zdb, "schedtreat", "$keyfld", $_GET[$keyfld], "$file?mode=last");
        delete($zdb, "consultmain", "$keyfld", $_GET[$keyfld], "$file?mode=last");
    }
}

include("includes/linkshead.php");
?>
<script src="MyScript/openbal.js" type="text/javascript"></script>
<script src="MyScript/patient.js" type="text/javascript"></script>
<script src="MyScript/patient_search.js" type="text/javascript"></script>
<style>
    select:focus {
        border: 1px solid #007bff;
        /* Change border color on focus */
        /*box-shadow: 0 0 5px rgba(0, 123, 255, 0.5);  Optional shadow effect */
    }

    select {
        padding: 10px;
        border: 2px solid #ccc;
        /* Default border */
        border-radius: 4px;
        outline: none;
        /* Remove default outline */
    }

    .disc {
        width: 80px;
    }

    .disc1 {
        width: 50px;
    }

    .form-control {
        text-transform: uppercase;
    }
</style>

</head>

<body>
    <form action='' method='POST'>
        <!-- Default box -->
        <div class="row">
            <div class="col-lg-6 pad">
                <div class="panel panel-flat" style="margin-bottom: -11px;">
                    <div class="panel-heading">
                        <?php include("includes/usermention.php");
                        ?>
                        <div class="heading-elements">

                        </div>
                        <a class="heading-elements-toggle"><i class="icon-menu"></i></a>
                    </div>
                    <div class="panel-body">

                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Type</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-8 col-xs-8 pad">
                                <select class="form-control form acttype select2 focufield" name="acttype" style="width: 100%" autofocus>
                                    <!-- <option value="<?php echo ($addflag != 1) ? $get["acttype"] : ""; ?>"><?php echo ($addflag != 1) ? $gtype : ""; ?></option>
                                    <?php if ($addflag == 1) { ?>
                                                                <option value='P'>Patient</option>
                                                                <option value='M'>Meeting</option>
                                        <?php
                                    }
                                    ?> -->
                                    <option <?php
                                    if ($get['acttype'] == 'P') {
                                        echo "selected='selected'";
                                    }
                                    ?> value="P">Patient</option>
                                    <option <?php
                                    if ($get['acttype'] == 'M') {
                                        echo "selected='selected'";
                                    }
                                    ?> value="M">Meeting</option>
                                </select>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Name</label>
                            </div>
                            <div class="col-lg-8 col-md-8 col-sm-8 col-xs-8 pad">
                                <input type="hidden" class="form-control form  vouchno" required name="vouchno" value="<?php echo ($addflag != 1) ? $qvouch : $qvouch; ?>">
                                <input type="text" class="form-control form " required name="name" value="<?php echo ($addflag != 1) ? $get["name"] : ""; ?>" required>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Address</label>
                            </div>
                            <div class="col-lg-8 col-md-6 col-sm-8 col-xs-8 pad">
                                <input type="text" class="form-control form add1" name="add1" value="<?php echo ($addflag != 1) ? $get["add1"] : ""; ?>">
                                <input type="text" class="form-control form add2" name="add2" value="<?php echo ($addflag != 1) ? $get["add2"] : ""; ?>">
                                <input type="text" class="form-control form add3" name="add3" value="<?php echo ($addflag != 1) ? $get["add3"] : ""; ?>">
                            </div>

                        </div>

                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Gender</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 pad">
                                <select class="form-control form gender select2" name="gender" style="width: 100%">
                                    <option value="<?php echo ($addflag != 1) ? $get["gender"] : ""; ?>"><?php echo ($addflag != 1) ? $gval : ""; ?></option>
                                    <option value='M'>Male</option>
                                    <option value='F'>Female</option>
                                    <option value='O'>Other</option>
                                </select>
                            </div>
                            <div class="col-lg-1 col-md-2 col-sm-1 col-xs-1 text-right pad">
                                <label class="label-control marg">DOB</label>
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 pad">
                                <input type="date" class="form-control form dob" name="dob" value="<?php echo ($addflag != 1) ? $get["dob"] : ""; ?>">
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 text-right pad">
                                <label class="label-control marg">Age</label>
                            </div>
                            <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 pad">
                                <input type="text" class="form-control form age" name="age" value="<?php echo ($addflag != 1) ? $get["age"] : ""; ?>" required>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Mobile </label>
                            </div>
                            <div class="col-lg-4 col-md-4 col-sm-6 col-xs-6 pad">
                                <input type="text" class="form-control form  number" name="mobile" value="<?php echo ($addflag != 1) ? $get["mobile"] : ""; ?>">
                            </div>
                            <!-- <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2 text-right pad">
                                <label class="label-control marg">Mobile 2</label>
                            </div>
                            <div class="col-lg-2 col-md-3 col-sm-3 col-xs-3 pad">
                                <input type="text" class="form-control form number" name="mobile2" value="<?php echo ($addflag != 1) ? $get["mobile2"] : ""; ?>">
                            </div> -->
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Email</label>
                            </div>
                            <div class="col-lg-4 col-md-6 col-sm-6 col-xs-6 pad">
                                <input type="text" class="form-control form email" name="email" value="<?php echo ($addflag != 1) ? $get["email"] : ""; ?>">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Purpose of Visit</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pad">
                                <input list='purpvisit' name='purpvisit' value='<?php echo $get['purpvisit'] ?>' class='purpvisit form-control form ' autocomplete="off">
                                <datalist id='purpvisit'>
                                    <option value=''>
                                        <?php
                                        $gparty = mysqli_query($conn, "SELECT `purpvisit` FROM `$maindb` group by `purpvisit` order by `purpvisit`") or die(mysqli_error($conn));
                                        while ($fparty = mysqli_fetch_array($gparty)) {
                                            echo "<option value='$fparty[purpvisit]'>";
                                        }
                                        ?>
                                </datalist>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Referred By</label>
                            </div>
                            <div class="col-lg-6 col-md-6 col-sm-6 col-xs-6 pad">
                                <input list='refby' name='refby' value='<?php echo $get['refby'] ?>' class='refby form-control form ' autocomplete="off">
                                <datalist id='refby'>
                                    <option value=''>
                                        <?php
                                        $grefby = mysqli_query($conn, "SELECT `refby` FROM `$maindb` group by `refby` order by refby") or die(mysqli_error($conn));
                                        while ($frefby = mysqli_fetch_array($grefby)) {
                                            echo "<option value='$frefby[refby]'>";
                                        }
                                        ?>
                                </datalist>
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Appointment Date</label>
                            </div>
                            <div class="col-lg-2 col-md-6 col-sm-4 col-xs-4 pad">
                                <input type="date" class="form-control form regndt" name="regndt" value="<?php echo ($addflag != 1) ? $get["regndt"] : date("Y-m-d"); ?>">
                                <input type="date" class="form-control form oldregndt" name="oldregndt" value="<?php echo ($addflag != 1) ? $get["regndt"] : ""; ?>" style="display:none">
                            </div>
                            <div class="col-lg-2 col-md-2 col-sm-1 col-xs-1 text-right pad">
                                <label class="label-control marg">Time</label>
                            </div>
                            <div class="col-lg-2 col-md-6 col-sm-3 col-xs-3 pad">
                                <input type="time" class="form-control form apttime" name="apttime" value="<?php echo ($addflag != 1) ? $get["apttime"] : ""; ?>">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-lg-2 col-md-2 col-sm-3 col-xs-3 text-right pad">
                                <label class="label-control marg">Remarks</label>
                            </div>
                            <div class="col-lg-8 col-md-6 col-sm-6 col-xs-6 pad">
                                <input type="text" class="form-control form rem1" name="rem1" value="<?php echo ($addflag != 1) ? $fsched["remarks"] : ""; ?>">

                            </div>

                        </div>

                        <div class="row">

                        </div>
                    </div>
                    <div class="panel-footer">
                        <?php include("includes/buttons.php"); ?>
                    </div>
                </div>
            </div>
        </div>
    </form>
    <div class="modal fade bs-example-modal-lg searchModal" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel">
        <div class="modal-dialog modal-lg">
            <div class="modal-content">
                <div class="modal-header">
                    <button class="close" data-dismiss="modal"><span>&times;</span></button>
                    <h4 class="modal-title" id="myLargeModalLabel">Search <? echo $formname; ?></h4>
                </div>
                <div class="modal-body">
                    <div class="row">
                        <div class="col-lg-2">
                            <select class="form-control sfield">
                                <option value="1">Mobile</option>
                                <option value="6">Accode</option>
                                <option value="5">Name</option>
                                <option value="2">Address</option>
                                <option value="3">Age</option>
                                <option value="4">DOB</option>

                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control soperator">
                                <option value="">Type of Search</option>
                                <option value="%">Like</option>
                                <option value="=">Exactly</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control sortBy">
                                <option value="">Sort By</option>
                                <option value="1">Mobile</option>
                                <option value="6">Accode</option>
                                <option value="5">Name</option>
                                <option value="2">Address</option>
                                <option value="3">Age</option>
                                <option value="4">DOB</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <select class="form-control orderAs">
                                <option value="asc">Asc</option>
                                <option value="desc">Desc</option>
                            </select>
                        </div>
                        <div class="col-lg-2">
                            <input type="text" value="" class="form-control ssearch" placeholder="Search Here">
                        </div>
                        <div class="col-lg-2">
                            <input type="button" class="btn btn-primary sload" value="Search">
                        </div>
                    </div>
                    <div class="col-lg-12 sloader text-center">
                        <!--<img src="images/search.gif" alt="loading">-->
                    </div>
                    <div class="col-lg-12 snoRecord text-center">
                        <h3>NO RECORDS FOUND</h3>
                    </div>
                    <div class="table-responsive" style="max-height: 400px;overflow: auto;">
                        <table id="" class="table table-striped table-condensed table-bordered" width="100%">
                            <thead>
                                <tr class="mySearchTr">
                                    <th width="10%">Accode</th>
                                    <th width="25%">Name</th>
                                    <th width="20%">Address</th>
                                    <th width="10%">Age</th>
                                    <th width="10%">DOB</th>
                                    <th width="10%">Mobile</th>
                                    <th width="10%">Email</th>
                                    <th width="10%">Gender</th>
                                </tr>
                            </thead>
                            <tbody class="sTbody">
                            </tbody>
                        </table>
                    </div>
                    <div class="row">
                        <div class="col-xs-12 text-right search-action-bar">
                            <input type="button" class="btn btn-primary sprev" value="Previous">
                            <input type="button" class="btn btn-primary snext" value="Next">
                        </div>
                    </div>
                </div>
                <div class="modal-footer"></div>
            </div>
        </div>
    </div>
    <?php include("includes/linksfoot.php"); ?>
</body>

</html>