<?php
include ('head.php');
include ('header.php');
include ('sidebar.php');
$stmt = $pdo->prepare("SELECT * FROM tblphysicians where isDeleted='N' order by id DESC");
$stmt->execute();
$patients = $stmt->fetchAll();

$stmt8 = $pdo->prepare("SELECT * FROM tblprovince WHERE countryId='1' ORDER BY provinceName ASC");
$stmt8->execute();
$provinces = $stmt8->fetchAll();


$stmt1 = $pdo->prepare("SELECT empId,userName from tbluser WHERE isDeleted='N' AND ( department = 'R'  OR (department = 'P' AND userRole REGEXP '(^|,)\\s*RS\\s*(,|$)'))");
$stmt1->execute();
$employees = $stmt1->fetchAll();
$error = '';
if (isset($_POST['btnSubmit']))
{
    $physicianFirstName = $_POST['firstName'];
    $physicianLastName = $_POST['lastName'];
    $hospital = $_POST['hospital'];
    $license = $_POST['license'];
    $primarySpecialty = $_POST['specialty'];

    $physicianAddress = $_POST['address'];
    $physicianCity = $_POST['city'];
    $physicianProvince = $_POST['physicianProvince'];
    $physicianPostalCode = $_POST['postalCode'];

    $phone = $_POST['phone'];
    $fax = $_POST['fax'];
    $email = $_POST['email'];
    $createdBy = $_SESSION['empId'];
    $assignedTo = $_POST['assignedTo'];
    $notes = $_POST['notes'];

    // secondary address
    $secondary_email = $_POST['secondary-email'] ?? null;
    $secondary_fax = $_POST['secondary-fax'] ?? null;
    $secondary_phone = $_POST['secondary-phone'] ?? null;
    $secondary_physicianAddress = $_POST['secondary-address'] ?? null;
    $secondary_physicianCity = $_POST['secondary-city'] ?? null;
    $secondary_physicianProvince = $_POST['secondary-physicianProvince'] ?? null;
    $secondary_physicianPostalCode = $_POST['secondary-postalCode'] ?? null;
    $secondary_hospital = $_POST['secondary-hospital'] ?? null;
    $secondary_notes = $_POST['secondary-notes'] ?? null;

    $physicianType = $_POST['physicianType'] ?? null;
    

    $sqlx = "SELECT license FROM tblphysicians WHERE license = ?";
    $stmtx = $pdo->prepare($sqlx);
    $stmtx->execute([$license]);
    $user = $stmtx->fetch();
    if ($user && $license != "")
    {
        $error = 'Physician already exists';
    }
    else
    {
        $stmt = $pdo->prepare("INSERT INTO tblphysicians(firstName,lastName,hospital,license,primarySpecialty,streetAddress,city,province,postalCode,phone,email,fax,createdBy,assignedTo,notes,physicianType) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
        if ($stmt->execute([$physicianFirstName, $physicianLastName, $hospital, $license, $primarySpecialty, $physicianAddress, $physicianCity, $physicianProvince, $physicianPostalCode, $phone, $email, $fax, $createdBy, $assignedTo, $notes, $physicianType]))
        {
            $pspId = $pdo->lastInsertId();
            $pxId = $pspId;
            $hidden = '';
            //insert primary address details
            $stmt = $pdo->prepare("INSERT INTO tblphysiciansaddress(physicianId, addressType, hospital, streetAddress, city, province, postalCode, phone, email, fax, notes,isDeleted,createdBy) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)");
            if ($stmt->execute([$pspId, 'Primary', $hospital, $physicianAddress, $physicianCity, $physicianProvince, $physicianPostalCode, $phone, $email, $fax, $notes, 'N',$createdBy]))
            {
            }
            else
            {
                echo "Please try again!";
            }
            $stmt = $pdo->prepare("INSERT INTO tblphysiciansaddress(physicianId, addressType, hospital, streetAddress, city, province, postalCode, phone, email, fax, notes,isDeleted,createdBy) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?)");
            if ($stmt->execute([$pspId, 'Secondary', $secondary_hospital, $secondary_physicianAddress, $secondary_physicianCity, $secondary_physicianProvince, $secondary_physicianPostalCode, $secondary_phone, $secondary_email, $secondary_fax, $secondary_notes, 'N',$createdBy]))
            {
            }
            else
            {
                echo "Please try again!";
            }
            header("location:physicians.php");
        }
        else
        {
            echo "Please try again!";
        }
    }
}
?>
<style>
.secondary-address-fields{
    display: none;
}
.secondary-address{
    display: inline-block !important;
}
.card-header.mgtop.inlineflex {
    padding: 20px;
}
a.white{
    color:white;
}
.mgtop{
    margin-top:20px;
    margin-bottom:30px;
}
/* #example_wrapper select:not([size]) {
     width: 10%;
     margin: 10px;
 } */
.rxtitle{
    font-size:24px;
    font-weight:600;
}
table a.btn {
    /* background: none; */
    height: max-content;
    width: max-content;
    display: inline-flex;
}
.dt-length,.dt-search {
    display: flex;
    vertical-align: middle;
    align-items: center;
}
.customdewidth{
    margin-left: 15%;
    margin-top: 80px;
    width: 85%;
    background-color: whitesmoke;
}
aside{
   width:15%; 
}
.deindexpage .dt-search {
    float: right;
}
.errormessage{
    color: red;
    padding: 10px 0px;
}
input.btn.btn-primary {
    background: #9c27b0;
    border-color: #9c27b0;
}
</style>

<section class="bgcolordefault deindexpage">
<div class="editWc container-fluid customdewidth">
   
            <div class="row">
                <div class="col-12 customui">
                    <div class="card">
                        <div class="card-header">
                            <h3 class="card-title">Add Physician</h3>
                            <div class="offset-10 col-1">
                                <a href="physicians.php" class="btn btn-block btn-secondary bg-gradient-primary">BACK</a>
                            </div>
                        </div>
                        <!-- /.card-header -->
                        <div class="card-body">
                        <!-- /.card-header -->
                        <!-- form start -->
                        <form method="POST">
                            <div class="card-body row">
                                <div class="form-group col-md-4">
                                    <label for="pspName">Physician Type:</label>
                                          <div class="fixselect">
                                             <select class="form-control select2bs4" id="physicianType" name="physicianType">
                                                <option value="">Select Physician Type</option>
                                                <option value="Pilot">Pilot</option>
												<option  value="NP">Non Pilot</option>
                                             </select>
                                            </div>
                                </div>
                                <div class='clearfix'></div>
                                <!-- type end -->
                                <div class="form-group col-md-4">
                                    <label for="pspName">Physician First Name</label>
                                    <input type="text" class="form-control" name="firstName"  placeholder="Enter Physician First Name">
                                </div>
                                <div class="form-group col-md-4">
                                    <label for="pspName">Physician Last Name</label>
                                    <input type="text" class="form-control" name="lastName"  placeholder="Enter Physician Last Name">
                                </div>
                                <div class='clearfix'></div>
                                <div class="form-group col-md-4">
                                    <label for="pspName">License</label>
                                    <input type="text" class="form-control" name="license"  placeholder="Enter Physician License">
                                </div>
                                <div class="form-group col-md-4">
                                    <label for="pspName">Physician Primary Specialty:</label>
                                    <input type="text" class="form-control" name="specialty" placeholder="Enter Primary Specialty">
                                </div>  
                                <div class='clearfix'></div>
                                <div class="form-group col-md-4">
                                    <label for="pspName">Assigned To:</label>
                                          <div class="fixselect">
                                             <select class="form-control select2bs4" id="assignedTo" name="assignedTo">
                                                <option value=""></option>
                                                <?php foreach ($employees as $emp)
                                                { ?>
                                                   <option value="<?=$emp['empId']; ?>"><?=$emp['userName']; ?></option>
                                                <?php
                                                } ?>
                                             </select>
                                          </div>
                                </div> 
                                <div class="clearfix"></div>
                                <div class="form-group col-md-4">
                                    <label for="pspName">Email</label>
                                    <input type="email" class="form-control" name="email" placeholder="Enter Email">
                                </div>
                                <div class="form-group col-md-4">
                                    <label for="pspName">Fax</label>
                                    <input type="text" class="form-control" name="fax" placeholder="Enter Fax">
                                </div>
                                <div class="clearfix"></div>
                                <div class="form-group col-md-4">
                                    <label for="pspName">Phone</label>
                                    <input type="text" class="form-control" name="phone" placeholder="Enter Phone"  data-inputmask="&quot;mask&quot;: &quot;(999) 999-9999&quot;"
                                    data-mask="" inputmode="text">
                                </div>
                                <div class="clearfix"></div>
                                <div class="form-group col-md-4">
                                    <label for="pspName">Address</label>
                                    <input type="text" class="form-control" name="address"  placeholder="Enter Street Address">
                                </div>
                             
                                <div class="form-group col-md-4">
                                    <label for="pspName">City</label>
                                    <input type="text" class="form-control" name="city"  placeholder="Enter City">
                                </div>
                                <div class="clearfix"></div>
                                <div class="form-group col-md-4">
                                    <label for="pspName">Province</label>
                                    <select class="form-control" name='physicianProvince' required id="physicianProvince">
                                             <option>Select Province</option>
                                             <?php foreach ($provinces as $province)
                                            { ?>
                                                <option  value="<?php echo $province['provinceName']; ?>">
                                                   <?php echo $province['provinceName']; ?></option>
                                             <?php
                                            } ?>
                                          </select>
                                </div>
                             
                                <div class="form-group col-md-4">
                                    <label for="pspName">Postal Code</label>
                                    <input type="text" class="form-control" name="postalCode"  placeholder="Enter Postal Code">
                                </div>
                                <div class="clearfix"></div>
                                <div class="form-group col-md-4">
                                    <label for="pspName">Physician Hospital/Site :</label>
                                    <input type="text" class="form-control" name="hospital" placeholder="Enter Hospital">
                                </div>
                                
                                <div class="form-group col-md-4">
                                    <label for="pspName">Physician Notes:</label>
                                    <textarea class="form-control" name="notes"></textarea>
                                </div> 
                                <div class="clearfix"></div>
                                <div class="form-group col-md-4">
                                <label for="checkbox" class="secondary-address">Secondary Address&nbsp;</label>
                                <input type="checkbox" class="form-input secondary-address" id="checkbox" name="checkbox" onclick="toggleSecondaryAddressFields()">
                                </div>
                                <div class="clearfix"></div>
                                <!-- secondary address fields start-->
                                <div class="card-body row secondary-address-fields" id="secondary-address-fields">
                                <div class="clearfix"></div>
                                <div class="form-group col-md-4">
                                    <label for="pspName">Email</label>
                                    <input type="email" class="form-control" name="secondary-email" placeholder="Enter Email">
                                </div>
                                <div class="form-group col-md-4">
                                    <label for="pspName">Fax</label>
                                    <input type="text" class="form-control" name="secondary-fax" placeholder="Enter Fax">
                                </div>
                                <div class="clearfix"></div>
                                <div class="form-group col-md-4">
                                    <label for="pspName">Phone</label>
                                    <input type="text" class="form-control" name="secondary-phone" placeholder="Enter Phone"  data-inputmask="&quot;mask&quot;: &quot;(999) 999-9999&quot;"
                                    data-mask="" inputmode="text">
                                </div>
                                <div class="clearfix"></div>
                                <div class="form-group col-md-4">
                                    <label for="pspName">Address</label>
                                    <input type="text" class="form-control" name="secondary-address"  placeholder="Enter Street Address">
                                </div>
                             
                                <div class="form-group col-md-4">
                                    <label for="pspName">City</label>
                                    <input type="text" class="form-control" name="secondary-city"  placeholder="Enter City">
                                </div>
                                <div class="clearfix"></div>
                                <div class="form-group col-md-4">
                                    <label for="pspName">Province</label>
                                    <select class="form-control" name='secondary-physicianProvince' required id="secondary-physicianProvince">
                                             <option>Select Province</option>
                                             <?php foreach ($provinces as $province)
                                            { ?>
                                                <option  value="<?php echo $province['provinceName']; ?>">
                                                   <?php echo $province['provinceName']; ?></option>
                                             <?php
                                            } ?>
                                          </select>
                                </div>
                             
                                <div class="form-group col-md-4">
                                    <label for="pspName">Postal Code</label>
                                    <input type="text" class="form-control" name="secondary-postalCode"  placeholder="Enter Postal Code">
                                </div>
                                <div class="clearfix"></div>
                                <div class="form-group col-md-4">
                                    <label for="pspName">Physician Hospital/Site :</label>
                                    <input type="text" class="form-control" name="secondary-hospital" placeholder="Enter Hospital">
                                </div>
                                
                                <div class="form-group col-md-4">
                                    <label for="pspName">Physician Notes:</label>
                                    <textarea class="form-control" name="secondary-notes"></textarea>
                                </div> 
                            </div>
                            <!-- secondary address end-->
                            </div>
                            <!-- /.card-body -->
                            <div class="card-footer">
                                <input type="submit" class="btn btn-primary" name="btnSubmit" value="Submit">
                               <p class="errormessage"><?=$error; ?>
                            </div>
                        </form>
                    </div>
                    <!-- /.card -->
                </div>
            </div>
            <!-- /.container-fluid -->
        </div>
</div>
</section>
<script>
    $('#example').dataTable( {
    "order": [],
    // Your other options here...
    } );  
    function toggleSecondaryAddressFields() {
        var checkbox = document.getElementById("checkbox");
        var addressFields = document.getElementById("secondary-address-fields");

        if (checkbox.checked) {
            addressFields.style.display = "flex";
        } 
        else {
            addressFields.style.display = "none";
            // clear secondary address fields:
            var inputs = addressFields.getElementsByTagName("input");
            var selects = addressFields.getElementsByTagName("select");
            var textareas = addressFields.getElementsByTagName("textarea");

            for (var i = 0; i < inputs.length; i++) {
                inputs[i].value = ''; 
            }
            
            for (var i = 0; i < selects.length; i++) {
                selects[i].selectedIndex = 0; 
            }
            
            for (var i = 0; i < textareas.length; i++) {
                textareas[i].value = ''; 
             }
        }
    }
</script>
<?php include ('footer.php'); ?>
