<script type="text/javascript">
var emailFilter=/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,4}))$/;
$(document).ready(function(){
    $("#doctor_speciality").change(function(){
        var val = $("#doctor_speciality").val();
        $.ajax({
        url: "doctoradd.php",
        data: 'act=service&speciality='+val,
        method: 'POST',
        success: function(res){
             $("#spe_service").html(res);
            }
        });
    });
    $("#frm-doctor").submit(function(e){
        if(!validate_doctor()){
            e.preventDefault();
        }
    });
    $("#doctor_email").blur(function(){
        var email = $("#doctor_email");
        var emailmsg = $("#msg_doctor_email");
        if(email.val()==""){
            email.addClass('error');email.focus();emailmsg.html('Please enter email address').show();
            return false;
        }
        if($("#doctor_id").length > 0){var doctor_id = $("#doctor_id").val();
        }else{var doctor_id = '';}
        $("#submit").prop('disabled', true);
        $.ajax({
            url: "doctoradd.php",
            data: 'act=validate_email&email='+email.val()+'&doctor_id='+doctor_id,
            method: 'POST',
            success: function(msg){
                if(msg!=''){
                    email.addClass('error');email.focus();emailmsg.html(msg).show();
                    return false;
                }else{
                    $("#submit").prop('disabled', false);
                    email.removeClass('error');emailmsg.hide();
                }
            }
        });
    });
});

function validate_doctor(){
    if($("#doctor_fullname").val()==''){
        $("#doctor_fullname").addClass('error');
        $("#doctor_fullname").focus();
        $("#msg_doctor_fullname").html('Please enter name').show();
        return false;
    }else{
        $("#doctor_fullname").removeClass('error');
        $("#msg_doctor_fullname").hide();
    }
    if($("#doctor_speciality").val()==''){
        $("#doctor_speciality").addClass('error');
        $("#doctor_speciality").focus();
        $("#msg_doctor_speciality").html('Please select speciality').show();
        return false;
    }else{
        $("#doctor_speciality").removeClass('error');
        $("#msg_doctor_speciality").hide();
    }
    if($("#doctor_email").val()==''){
        $("#doctor_email").addClass('error');
        $("#doctor_email").focus();
        $("#msg_doctor_email").html('Please enter email address').show();
        return false;
    }else if($("#doctor_email").val()!='' && !(emailFilter.test($("#doctor_email").val())) ){
        $("#doctor_email").addClass('error');
        $("#doctor_email").focus();
        $("#msg_doctor_email").html('Please enter valid email address').show();
        return false;
    }else{
        $("#doctor_email").removeClass('error');
        $("#msg_doctor_email").hide();
    }
    if($("#doctor_photo").val()=='' && $("#hidden_logo").val()==''){
        $("#doctor_photo").addClass('error');
        $("#doctor_photo").focus();
        $("#msg_doctor_photo").html('Upload Logo').show();
        return false;
    }else{
        $("#doctor_photo").removeClass('error');
        $("#msg_doctor_photo").hide();
    }
    return true;
}

</script>
<div class="breadcrumb">
    <h1>Doctor</h1>
    <ul>
        <li><a href="<?php echo SITE_URL.ADMIN_DIR ?>">Dashboard</a></li>
        <li>Add New Doctor</li>
    </ul>
</div>
<div class="separator-breadcrumb border-top"></div>
<div class="widget-body">
    <div class="portlet-body">
                        
    <?php echo (isset($successMsg) && $successMsg!='') ? "<div class='alert alert-success'><button data-dismiss='alert' class='close' type='button'>×</button>".base64_decode($successMsg)."</div>" : '' ; ?>
    <?php echo (isset($failedMsg) && $failedMsg!='') ? "<div class='alert alert-danger'><button data-dismiss='alert' class='close' type='button'>×</button>".base64_decode($failedMsg)."</div>" : '' ?>
   
    <div id="status_msg" style="display: none;"><div class="alert alert-success"><button data-dismiss='alert' class='close' type='button'>×</button>&nbsp;</div></div>
    <div id="alert_msg" style="display: none;"><div class="alert alert-danger"><button data-dismiss='alert' class='close' type='button'>×</button>&nbsp;</div></div>

    </div>
</div>
<form action="" id='frm-doctor' method="post" enctype="multipart/form-data">
<div class="row">
    <div class="col-md-6">
        <div class="card mb-4">
            <div class="card-body">
                
                <?php if(isset($doctorDetail->doctor_id) && $doctorDetail->doctor_id!=0) { ?>
                    <input type="hidden" id="doctor_id" name="doctor_id" value="<?php echo $doctorDetail->doctor_id ?>" />
                 <?php } ?>
                    <div class="card-title mb-3">Doctor Information</div>
                    <div class="row">
                       <div class="col-md-5 form-group mb-3">
                            <label for="doctor_fullname">Full Name <span class="asterisk">*</span></label>
                            <input type="text" class="form-control" id="doctor_fullname" name="doctor_fullname" value="<?php echo $doctorDetail->doctor_fullname; ?>" placeholder="Enter first name">
                            <div class="invalid-tooltip" id="msg_doctor_fullname"></div>
                        </div>
                        <div class="col-md-4 form-group mb-3">
                            <label for="doctor_speciality">Speciality <span class="asterisk">*</span></label>
                            <select id="doctor_speciality" multiple="multiple" name="doctor_speciality[]" class="form-control">
                                <option value="">Select Speciality</option>
                                <?php if($allspecialities && count($allspecialities)){foreach($allspecialities as $key =>$speciality){?>
                                <option value="<?php echo $speciality->speciality_id ?>" <?php echo (isset($doctorDetail) && in_array($speciality->speciality_id,explode(',', $doctorDetail->doctor_speciality)))?"selected='selected'":""  ?>><?php echo $speciality->speciality_name ?></option>
                                <?php }} ?>
                            </select>    
                            <div class="invalid-tooltip" id="msg_doctor_speciality"></div>
                        </div>
                        <div class="col-md-3 form-group mb-3">
                            <label for="doctor_gender">Gender<span class="asterisk">*</span></label>
                            <select id="doctor_gender" name="doctor_gender" class="form-control">
                                <option value="Male">Male</option>
                                <option value="Female">Female</option>
                            </select>
                            <div class="invalid-tooltip" id="msg_doctor_gender"></div>
                        </div>
                        <div class="col-md-6 form-group mb-3">
                            <label for="doctor_email">Email <span class="asterisk">*</span></label>
                            <input type="text" class="form-control" id="doctor_email" name="doctor_email" value="<?php echo $doctorDetail->doctor_email; ?>" placeholder="Enter your email address">
                            <div class="invalid-tooltip" id="msg_doctor_email"></div>
                        </div>
                        
                        <div class="col-md-6 form-group mb-3">
                            <label for="doctor_languages">Languages<span class="asterisk">*</span></label>
                            <select id="doctor_languages" name="doctor_languages[]" class="form-control" multiple="multiple">
                                <?php if($alllanguage && count($alllanguage)>0){foreach($alllanguage as $language){ ?>
                                    <option value="<?php echo $language->language_id ?>" <?php echo (isset($doctorDetail) && in_array($language->language_id,explode(',', $doctorDetail->doctor_languages)))?"selected='selected'":""  ?>><?php echo $language->language_name ?></option>
                                <?php }} ?>
                            </select>
                            <div class="invalid-tooltip" id="msg_doctor_languages"></div>
                        </div>
                        
                        <div class="col-md-12 form-group mb-3">
                            <label for="doctor_photo">Photo <span class="asterisk">*</span></label>
                            <input type="file" class="form-control" id="doctor_photo" name="doctor_photo">
                            <?php 
                            $logo = json_decode($doctorDetail->doctor_photo,1);
                            if($logo['name']!='' && file_exists(DIR_UPLOAD_DOCTOR.$logo['name']."-200x200.".$logo['type'])){ ?>
                                </br>
                                <input type="hidden" name="hidden_logo" id="hidden_logo" value="<?php echo $doctorDetail->doctor_photo ?>">
                                <input type="hidden" name="hidden_logo_ext" id="hidden_logo_ext" value="<?php echo $logo['type'] ?>">
                                <img src="<?php echo DIR_UPLOAD_DOCTOR_SHOW.$logo['name']."-200x200.".$logo['type'] ?>" width="100px" />
                            <?php } ?>
                            <div class="invalid-tooltip" id="msg_doctor_photo"></div>
                        </div>
                        
                    </div>
            </div>
        </div>
    </div>
    <div class="col-md-6">
        <div class="card mb-4">
            <div class="card-body">
                <div class="card-title mb-3">Profile Description</div>
                <div class="row">
                    <div class="col-md-12 form-group mb-3">
                        <textarea class="form-control" id="doctor_desc" rows="10" name="doctor_desc"  placeholder="Enter profile description."><?php echo $doctorDetail->doctor_desc; ?></textarea>
                        <div class="invalid-tooltip" id="msg_doctor_desc"></div>
                    </div>
                </div>
            </div>
        </div>
        <div class="card mb-4">
        <div class="card-body">
            <div class="card-title mb-3">Profile Questions</div>
                <div class="row">
                    <?php if($allquestions && count($allquestions)>0){foreach($allquestions as $qkey =>$question){ ?>
                    <div class="col-md-12 form-group mb-3">
                        <label for="question_answer_<?php echo $qkey ?>"><?php echo $question->question_title ?></label>
                        <input type="hidden" id="hdn_que_<?php echo $qkey ?>" name="question_id[]" value="<?php echo $question->question_id ?>" />
                        <textarea class="form-control" id="question_answer_<?php echo $qkey ?>" rows="5" name="question_answer[<?php echo $qkey ?>]"  placeholder="Enter answer."><?php echo $quesData[$question->question_id]; ?></textarea>
                        <div class="invalid-tooltip" id="msg_question_answer_<?php echo $qkey ?>"></div>
                    </div>
                    <?php }}?> 
                </div>
            </div>
        </div>
        
    </div>
    <div class="col-md-12">
        <input type="submit" class="btn btn-primary" id="submit" name="submit" value="Submit">
    </div>
    </div>
</form>
