<div class="breadcrumb">
    <h1>Template</h1>
    <ul>
        <li><a href="<?php echo SITE_URL.ADMIN_DIR ?>">Dashboard</a></li>
        <li>Add New Template</li>
    </ul>
</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>
<div class="separator-breadcrumb border-top"></div>
  <div id="results" >
      <div  id="msg_error_name"></div>
  </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 class="row">
    <div class="col-md-12">
        <div class="card mb-4">
            <div class="card-body">
                <div class="card-title mb-3">Add New Template</div>
                <form action="" id='frm-template' method="post" enctype="multipart/form-data">
                    <?php 
                    if(isset($templateDetail->admin_notificationtemplate_id) && $templateDetail->admin_notificationtemplate_id !=0) { ?>
                       <input type="hidden" id="template_id" name="template_id" value="<?php echo $templateDetail->admin_notificationtemplate_id; ?>" />
                       
                    <?php } ?>
                    <div class="row">
                        
                        <div class="col-md-6 form-group mb-3" id="div_template_name">
                            <label for="template_name">Template Name <span class="asterisk">*</span></label>
                            <select class="form-control" id="template_name" name="template_name">
                                
                                <?php 
                                if(isset($templateDetail->admin_notificationtemplate_id) && $templateDetail->admin_notificationtemplate_id !=0) { ?>
                                <?php if($templateDetail->template_type == 'digitaldoc'){ ?>
                                    <option value="digitaldoc">Digital Doc Upload From Patient</option>
                                    <?php } ?>
                                <?php if($templateDetail->template_type == 'referral_sender'){ ?>
                                    <option value="referral_sender">Referral Sender</option>
                                    <?php } ?>
                                <?php if($templateDetail->template_type == 'referral_receiver'){ ?>
                                    <option value="referral_receiver">Referral Receiver</option>
                                    <?php } ?>
                                <?php if($templateDetail->template_type == 'referral_success_sender'){ ?>
                                    <option value="referral_success_sender">Referral Success Sender</option>
                                    <?php } ?>
                                <?php if($templateDetail->template_type == 'referral_success_receiver'){ ?>
                                    <option value="referral_success_receiver">Referral Success Receiver</option>
                                    <?php } ?>
                                <?php }else{ ?>
                                    <option value="">Select Template</option>
                                    <?php
                                        if(isset($tempdata) && count($tempdata) > 0)
                                        {    
                                            if(!in_array('digitaldoc',$tempdata))
                                            {?>
                                                <option value="digitaldoc">Digital Doc Upload From Patient</option>
                                           <?php } 
                                           if(!in_array('referral_sender',$tempdata))
                                            {?>
                                                <option value="referral_sender">Referral Sender</option>
                                             <?php } 
                                             if(!in_array('referral_receiver',$tempdata))
                                            {?>
                                                <option value="referral_receiver">Referral Receiver</option>
                                             <?php }    
                                             if(!in_array('referral_success_sender',$tempdata))
                                            {?>
                                                <option value="referral_success_sender">Referral Success Sender</option>
                                             <?php } 
                                             if(!in_array('referral_success_receiver',$tempdata))
                                            {?>
                                                <option value="referral_success_receiver">Referral Success Receiver</option>
                                             <?php } ?> 
                                            
                                        <?php }else
                                        { ?>
                                            <option value="digitaldoc">Digital Doc Upload From Patient</option>
                                            <option value="referral_sender">Referral Sender</option>
                                            <option value="referral_receiver">Referral Receiver</option>
                                            <option value="referral_success_sender">Referral Success Sender</option>
                                            <option value="referral_success_receiver">Referral Success Receiver</option>
                                       <?php }
                                    ?>
                                    
                                <?php } ?>    
                            </select>
                            <div class="invalid-tooltip" id="msg_template_name"></div>
                       </div>

                    	<div class="col-md-6 form-group mb-3">
                            
                        </div>

                        <div class="col-md-12 form-group mb-3" id="short_codes_dis">
                            <label for="short_codes"><h5>Short codes</h5> </label>
                            <br/>

                            <span >Patient Name </span>
                            <a  title="Use {patientname} in <br> body to attach patient name"><img src="<?php echo SITE_URL; ?>x-admin/assets/images/clipboard-icon-popup.598131d8.svg" alt="Patient Name" style="width:20px;" name="patient_name" id="patient_name" class="copyboard"  data-text="{patientname}" data-val = "patient name"></img></a>


                            
                            <span>&nbsp;&nbsp; Patient Email </span><img src="<?php echo SITE_URL; ?>x-admin/assets/images/clipboard-icon-popup.598131d8.svg" alt="Patient Email" style="width:20px;" name="patient_email" id="patient_email" class="copyboard"  data-text="{patientemail}" data-val = "patient email" ></img>

                            <span>&nbsp;&nbsp; Patient Phone </span><img src="<?php echo SITE_URL; ?>x-admin/assets/images/clipboard-icon-popup.598131d8.svg" alt="Patient Phone" style="width:20px;" name="patient_phone" id="patient_phone" class="copyboard"  data-text="{patientphone}" data-val = "patient phone"></img>

                            <span id="clinic_label">&nbsp;&nbsp; Clinic Name </span><img id="clinic_img" src="<?php echo SITE_URL; ?>x-admin/assets/images/clipboard-icon-popup.598131d8.svg" alt="Clinic Name" style="width:20px;" name="clinic_name" id="clinic_name" class="copyboard"  data-text="{clinicname}" data-val = "clinic name"></img>

                            <span id="referral_link_label">&nbsp;&nbsp; Referral Link </span><img id="referral_link_img" src="<?php echo SITE_URL; ?>x-admin/assets/images/clipboard-icon-popup.598131d8.svg" alt="Referral Link" style="width:20px;" name="referral_link" id="referral_link" class="copyboard"  data-text="{referrallink}" data-val = "referral link"></img>

                            <span id="referral_send_point_label">&nbsp;&nbsp; Referral Send Point </span><img id="referral_send_point_img" src="<?php echo SITE_URL; ?>x-admin/assets/images/clipboard-icon-popup.598131d8.svg" alt="Referral Sender Point" style="width:20px;" name="referralsenderpoint" id="referralsenderpoint" class="copyboard"  data-text="{referralsenderpoint}" data-val = "referralsenderpoint"></img>


                            <span id="referral_receive_point_label">&nbsp;&nbsp; Referral Receive Point </span><img id="referral_receive_point_img" src="<?php echo SITE_URL; ?>x-admin/assets/images/clipboard-icon-popup.598131d8.svg" alt="Referral Receiver Point" style="width:20px;" name="referralreceiverpoint" id="referralreceiverpoint" class="copyboard"  data-text="{referralreceiverpoint}" data-val = "referralreceiverpoint"></img>
                            
                            <div class="invalid-tooltip" id="msg_short_codes"></div>
                        </div>

                        <div class="col-md-12 form-group mb-3"  id="div_template_subject_line">
                            <label for="template_subject_line">Subject line </label>
                            <input class="form-control" type="text" name="template_subject_line" id="template_subject_line" placeholder="Enter email subject line" value="<?php echo $templateDetail->subject;?>">

                            <div class="invalid-tooltip" id="msg_template_subject_line"></div>
                        </div>
                        
                        <div class="col-md-12 form-group mb-3"  id="div_template_content_data">
                            <label for="template_content">Email Content</label>
                            <textarea class="ckeditor"  id="template_content_data" name="template_content_data" ><?php echo $templateDetail->body; ?></textarea>

                            <div class="invalid-tooltip" id="msg_template_content_data"></div>
                        </div>

                        <div class="col-md-12 form-group mb-3"  id="div_template_sms_content_data">
                            <label for="template_sms_content">SMS Content</label>
                            <textarea class="form-control"  id="template_sms_content_data" name="template_sms_content_data" rows="10"><?php echo $templateDetail->smstext; ?></textarea>
                            <div class="invalid-tooltip" id="msg_template_sms_content_data"></div>
                        </div>

                        <div class="col-md-12">
                            <input type="submit" class="btn btn-primary" id="submit" name="submit" value="Submit">
                        </div>

                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<div class='loader-bubble loader-bubble-secondary m-5' align="center" style="display: none;"></div>
<script src="./assets/ckeditor/ckeditor.js"></script>
<!-- <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> -->
<!-- <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/toastr.js/latest/css/toastr.min.css"> -->
<link rel="stylesheet" href="./assets/styles/css/jquery.toast.css">
<script src="./assets/js/jquery.toast.js"></script>


<script type="text/javascript">
    
    $("#referral_link_label").hide();
    $("#referral_link_img").hide();
    $("#referral_send_point_label").hide();
    $("#referral_send_point_img").hide();
    $("#referral_receive_point_label").hide();
    $("#referral_receive_point_img").hide();

    $("#div_template_sms_content_data").hide();
    


    $('#template_name').on('change',function(e){
        var template_name = $("#template_name").val()
        if(template_name == 'referral_sender' || template_name == 'referral_receiver' || template_name == 'referral_success_sender' || template_name == 'referral_success_receiver')
        {
            $("#referral_link_label").show();
            $("#referral_link_img").show();
            $("#div_template_sms_content_data").show();
            $("#clinic_label").hide();
            $("#clinic_img").hide();

            $("#referral_send_point_label").show();
            $("#referral_send_point_img").show();

            $("#referral_receive_point_label").show();
            $("#referral_receive_point_img").show();

        }else{
            $("#referral_link_label").hide();
            $("#referral_link_img").hide();
            $("#div_template_sms_content_data").hide();
            $("#clinic_label").show();
            $("#clinic_img").show();

            $("#referral_send_point_label").hide();
            $("#referral_send_point_img").hide();

            $("#referral_receive_point_label").hide();
            $("#referral_receive_point_img").hide();
        }
    });

    if($("#template_name").val() == 'referral_sender')
    {
        $("#template_name").val("referral_sender").change();    
    }
    if($("#template_name").val() == 'referral_receiver')
    {
        $("#template_name").val("referral_receiver").change();    
    }
    if($("#template_name").val() == 'referral_success_sender')
    {
        $("#template_name").val("referral_success_sender").change();    
    }
    if($("#template_name").val() == 'referral_success_receiver')
    {
        $("#template_name").val("referral_success_receiver").change();    
    }
    

    $('.copyboard').on('click', function(e) 
    {
      e.preventDefault();

      var copyText = $(this).attr('data-text');
      var el = $('<input style="position: absolute; bottom: -120%" type="text" value="'+copyText+'"/>').appendTo('body'); 
      el[0].select();
      document.execCommand("copy");
      el.remove();
      
      /*var copyVal = $(this).attr('data-val');

      $.toast({
            //heading: 'Positioning',
            heading: '</em>',
            text: copyVal+' copied.<br/>',
            position: 'top-right',
            stack: false,
            showHideTransition: 'slide',
            icon: 'success'
        })*/
    })


    $( "form" ).on( "submit", function(e) {
      e.preventDefault();
      var template_name = $("#template_name").val();
      var template_subject_line = $("#template_subject_line").val();
      var template_content_data = CKEDITOR.instances['template_content_data'].getData();
      var template_sms_content_data = $("#template_sms_content_data").val();
      
      //$("#template_content_data").val();
      
      if($("#template_name").val()=='')
       {
          $("#template_name").addClass('error');
          $("#template_name").focus();
          $("#msg_template_name").html('Please select template').show();
          return false;
        }else if($("#template_subject_line").val()=='')
        {
            if($("#template_name").val()!='')
            {
              $("#template_name").removeClass('error');
              $("#msg_template_name").hide();
            }

            $("#template_subject_line").addClass('error');
            $("#template_subject_line").focus();
            $("#msg_template_subject_line").html('Please input subject line').show();
            return false;
        }else if(template_content_data=='')
        {
            if($("#template_subject_line").val()!='')
            {
              $("#template_subject_line").removeClass('error');
              $("#msg_template_subject_line").hide();
            }

            $("#template_content_data").addClass('error');
            $("#template_content_data").focus();
            $("#msg_template_content_data").html('Please input content').show();
            return false;
        }
        else
        {
          if(template_content_data !='')
            {
              $("#template_content_data").removeClass('error');
              $("#msg_template_content_data").hide();
            }

          <?php if(!isset($_REQUEST['template_id']))
          { ?>
            
              $.ajax({
                      url: "templateadd.php",
                      data: { act:"load",
                            template_name:template_name,template_subject_line:template_subject_line,template_content_data:template_content_data,template_sms_content_data:template_sms_content_data },
                      method: 'POST',
                      success: function(msg){
                          if(msg!=''){
                              window.location='templatelist.php';
                              //return false;
                          }
                      }
                  });
          <?php 
          }else
          {  ?>
           
            $.ajax({      
                    url: "templateadd.php",
                    data: { act:"load",
                          template_name:template_name,template_subject_line:template_subject_line,template_content_data:template_content_data,template_id:<?php echo $_REQUEST['template_id']; ?>,template_sms_content_data:template_sms_content_data },
                    method: 'POST',
                    success: function(msg){
                        if(msg!=''){
                            window.location='templatelist.php';
                        }
                    }
                });
         <?php }
          ?>
      }

  });
      

</script>