<?php include_once 'header.php';?>

<?php if ($errors) { ?>
  <div class="alert alert-danger">
    <?php if (isset($errors["e"])) echo $errors["e"];
    if (isset($errors["e1"])) echo $errors["e1"];?>
  </div>
<?php } ?>

<?php if (in_array($JAK_FORM_DATA["status"], [3,4]) && $JAK_FORM_DATA["ended"] != 0) { ?>
<div class="row">
  <div class="col-md-12">
    <div class="card card-stats">
      <div class="card-header card-header-warning card-header-icon">
        <div class="card-icon">
          <i class="material-icons">av_timer</i>
        </div>
        <p class="card-category"><?php echo $jkl["stat_s71"];?></p>
        <h3 class="card-title"><?php echo secondsToTime(round($JAK_FORM_DATA["ended"] - $JAK_FORM_DATA["initiated"]), $jkl['g230']);?></h3>
      </div>
      <div class="card-footer">
      </div>
    </div>
  </div>
</div>

<?php } ?>

<div class="header text-center">
  <h3 class="title"><?php echo $JAK_FORM_DATA["subject"];?></h3>
  <p class="category">
    <?php echo sprintf($jkl["hd178"], JAK_base::jakTimesince($JAK_FORM_DATA["initiated"], JAK_DATEFORMAT, JAK_TIMEFORMAT));?> / <?php echo sprintf($jkl["hd179"], JAK_base::jakTimesince($JAK_FORM_DATA["updated"], JAK_DATEFORMAT, JAK_TIMEFORMAT));?><br><?php echo sprintf($jkl["hd350"], $total_responses ? secondsToTime(round($total_responses_time / $total_responses), $jkl['g230']) : '-');?>
  </p>
</div>

<div class="row">
  <div class="col-6">
    <a href="<?php echo JAK_rewrite::jakParseurl('support');?>" class="btn btn-default"><?php echo $jkl["g103"];?></a>
    <a class="btn btn-primary" href="<?php echo JAK_rewrite::jakParseurl('support', 'new');?>"><?php echo $jkl["hd166"];?></a>
    <?php if ($JAK_FORM_DATA["status"] != 4) { ?>
      <a class="btn btn-info" href="<?php echo JAK_rewrite::jakParseurl('support', 'merge', $page2, $JAK_FORM_DATA["clientid"]);?>" data-toggle="modal" data-target="#jakModal"><?php echo $jkl["hd251"];?></a>
    <?php } ?>
  </div>
</div>

<form method="post" class="jak_form" action="<?php echo $_SERVER['REQUEST_URI']; ?>">

  <div class="row">
    <div class="col-md-4">
      <div class="card" id="ticket_live_status_container" style="display: none">
        <div class="card-header card-header-icon card-header-rose">
          <div class="card-icon">
            <i class="material-icons">people</i>
          </div>
          <h4 class="card-title "><?php echo $jkl["g177"];?></h4>
        </div>
      <div class="card-body">
        <div id="ticket_live_status"></div>
      </div>
    </div>
      <div class="card">
        <div class="card-header card-header-icon card-header-rose">
          <div class="card-icon">
            <i class="material-icons">person</i>
          </div>
          <h4 class="card-title "><?php echo $jkl["hd180"];?></h4>
        </div>
        <div class="card-body">
          <dl class="row">

            <dt class="col-sm-4"><?php echo $jkl['hd191'];?></dt>
              <dd class="col-sm-8"><span class="badge badge-primary">#<?php echo $page2;?></span></dd>

              <dt class="col-sm-4"><?php echo $jkl['hd167'];?></dt>
              <dd class="col-sm-8"><span class="badge badge-<?php echo $ticket_status_class;?>"><?php echo $ticket_status_title;?></span></dd>

            <?php if (isset($OPERATOR_ALL) && !empty($OPERATOR_ALL)) { ?>
              <dt class="col-sm-4"><?php echo $jkl['g130'];?></dt>
              <dd class="col-sm-8">
                <select name="jak_opid" id="jak_opid" class="selectpicker col-12" data-live-search="true">
                  <?php foreach ($OPERATOR_ALL as $o) {
                    echo '<option value="'.$o["id"].'"'.($JAK_FORM_DATA["operatorid"] == $o["id"] ? ' selected' : '').'>'.$o["name"].' ('.$o["email"].')</option>';
                  } ?>
                </select>
                <input type="hidden" name="oldopid" value="<?php echo $JAK_FORM_DATA["operatorid"];?>">
              </dd>

              <dt class="col-sm-4"><?php echo $jkl['hd266'];?></dt>
              <dd class="col-sm-8">
                <select name="jak_opidcc[]" id="jak_opidcc" class="selectpicker col-12" data-live-search="true" multiple>
                  <?php foreach ($OPERATOR_ALL as $o) {
                    echo '<option value="'.$o["id"].'"'.(!empty($OPERATOR_CC) && in_array($o["id"], $OPERATOR_CC) ? ' selected' : '').'>'.$o["name"].' ('.$o["email"].')</option>';
                  } ?>
                </select>
              </dd>
            <?php } ?>

            <?php if (isset($DEPARTMENTS_ALL) && !empty($DEPARTMENTS_ALL)) { ?>
              <dt class="col-sm-4"><?php echo $jkl['g131'];?></dt>
              <dd class="col-sm-8">
                <select name="jak_depid" id="jak_depid" class="selectpicker col-12" data-live-search="true">
                  <?php foreach ($DEPARTMENTS_ALL as $d) {
                    echo '<option value="'.$d["id"].'"'.($JAK_FORM_DATA["depid"] == $d["id"] ? ' selected' : '').'>'.$d["title"].'</option>';
                  } ?>
                </select>
                <input type="hidden" name="olddep" value="<?php echo $JAK_FORM_DATA["depid"];?>">
              </dd>
            <?php } ?>

            <?php if (isset($PRIORITY_ALL) && !empty($PRIORITY_ALL)) { ?>
              <dt class="col-sm-4"><?php echo $jkl['hd149'];?></dt>
              <dd class="col-sm-8">
                <select name="jak_priority" id="jak_priority" class="selectpicker col-12">
                  <option valude="0"<?php if ($JAK_FORM_DATA["priorityid"] == 0) echo 'selected';?>>-</option>
                  <?php foreach ($PRIORITY_ALL as $p) {
                    echo '<option value="'.$p["id"].'"'.($JAK_FORM_DATA["priorityid"] == $p["id"] ? ' selected' : '').'>'.$p["title"].((JAK_BILLING_MODE == 1 && $p["credits"] != 0) ? ' ('.sprintf($jkl['hd232'], $p["credits"]).')' : '').'</option>';
                  } ?>
                </select>
                <input type="hidden" name="oldpriority" value="<?php echo $JAK_FORM_DATA["priorityid"];?>">
              </dd>
            <?php } ?>

            <?php if (isset($TOPTIONS_ALL) && !empty($TOPTIONS_ALL)) { ?>
              <dt class="col-sm-4"><?php echo $jkl['hd225'];?></dt>
              <dd class="col-sm-8">
                <select name="jak_toption" id="jak_toption" class="selectpicker col-12">
                  <option valude="0"<?php if ($JAK_FORM_DATA["toptionid"] == 0) echo ' selected';?>>-</option>
                  <?php foreach ($TOPTIONS_ALL as $t) {
                    echo '<option value="'.$t["id"].'"'.($JAK_FORM_DATA["toptionid"] == $t["id"] ? ' selected' : '').'>'.$t["title"].((JAK_BILLING_MODE == 1 && $t["credits"] != 0) ? ' ('.sprintf($jkl['hd232'], $t["credits"]).')' : '').'</option>';
                  } ?>
                </select>
                <input type="hidden" name="oldtoption" value="<?php echo $JAK_FORM_DATA["toptionid"];?>">
              </dd>
            <?php } ?>

            <dt class="col-sm-4"><?php echo $jkl['g22'];?></dt>
              <dd class="col-sm-8">
                <select name="jak_tlang" id="jak_tlang" class="selectpicker col-12" data-live-search="true">
                  <option valude=""<?php if ($JAK_FORM_DATA["lang"] == "") echo ' selected';?>>-</option>
                  <?php // The language list
                    foreach(jak_the_lang() as $k => $l) {  
                      echo '<option value="'.$k.'"'.(isset($JAK_FORM_DATA['lang']) && $JAK_FORM_DATA['lang'] != 0 && $JAK_FORM_DATA['lang'] == $k ? ' selected' : '').'>'.$l.'</option>';    
                    }
                  ?>
              </select>
              </dd>

            <dt class="col-sm-4"><?php echo $jkl['hd172'];?></dt>
            <dd class="col-sm-8">
              <select name="jak_private" id="jak_private" class="selectpicker col-12">
                <option value="0"<?php if ($JAK_FORM_DATA["private"] == 0) echo ' selected';?>><?php echo $jkl['g18'];?></option>
                <option value="1"<?php if ($JAK_FORM_DATA["private"] == 1) echo ' selected';?>><?php echo $jkl['g19'];?></option>
              </select>
            </dd>

            <dt class="col-sm-4"><?php echo $jkl['hd77'];?></dt>
            <dd class="col-sm-8"><?php echo (isset($JAK_FORM_DATA["clientid"]) && !empty($JAK_FORM_DATA["clientid"]) ? '<a href="'.JAK_rewrite::jakParseurl('users', 'clients', 'edit', $JAK_FORM_DATA["clientid"]).'">'.$JAK_FORM_DATA["name"].'</a>' : $JAK_FORM_DATA["guest_name"]);?></dd>

            <dt class="col-sm-4"><?php echo $jkl['u1'];?></dt>
            <dd class="col-sm-8 hyphenate"><?php echo (isset($JAK_FORM_DATA["clientid"]) && !empty($JAK_FORM_DATA["clientid"]) ? '<a href="'.JAK_rewrite::jakParseurl('users', 'clients', 'edit', $JAK_FORM_DATA["clientid"]).'">'.$JAK_FORM_DATA["email"].'</a>' : $JAK_FORM_DATA["guest_email"]);?></dd>

            <dt class="col-sm-4"><?php echo $jkl['g264'];?></dt>
            <dd class="col-sm-8"><?php echo JAK_base::jakTimesince($JAK_FORM_DATA["initiated"], JAK_DATEFORMAT, JAK_TIMEFORMAT);?></dd>

            <dt class="col-sm-4"><?php echo $jkl['hd181'];?></dt>
            <dd class="col-sm-8"><?php echo JAK_base::jakTimesince($JAK_FORM_DATA["updated"], JAK_DATEFORMAT, JAK_TIMEFORMAT);?></dd>

            <dt class="col-sm-4"><?php echo $jkl['hd182'];?></dt>
            <dd class="col-sm-8"><?php echo ($JAK_FORM_DATA["ended"] != 0 ? JAK_base::jakTimesince($JAK_FORM_DATA["ended"], JAK_DATEFORMAT, JAK_TIMEFORMAT) : '-');?></dd>

            <dt class="col-sm-4"><?php echo $jkl['hd168'];?></dt>
            <dd class="col-sm-8"><span id="upload-counter"><?php echo $JAK_FORM_DATA["attachments"];?></span></dd>

            <dt class="col-sm-4"><?php echo $jkl['g11'];?></dt>
            <dd class="col-sm-8"><?php echo $JAK_FORM_DATA["ip"];?></dd>

            <dt class="col-sm-4"><?php echo $jkl['g169'];?></dt>
            <dd class="col-sm-8 hyphenate"><?php echo $JAK_FORM_DATA["referrer"];?></dd>

            <dt class="col-sm-4"><?php echo $jkl['hd269'];?></dt>
            <dd class="col-sm-8"><input type="text" name="jak_duedate" class="form-control datepicker" value="<?php echo ($JAK_FORM_DATA["duedate"] != "1980-05-06" ? date($duedateformat[0], strtotime($JAK_FORM_DATA["duedate"])) : date($duedateformat[0], strtotime('+'.JAK_TICKET_DUEDATE_PRESET.'day')));?>" autocomplete="off"></dd>

          </dl>
          <?php if (isset($custom_fields) && !empty($custom_fields)) { ?>
            <hr>
            <?php echo $custom_fields;?>
          <?php } ?>
        </div>
        <div class="card-footer">
          <a href="<?php echo JAK_rewrite::jakParseurl('support');?>" class="btn btn-default"><?php echo $jkl["g103"];?></a>
          <?php if ($JAK_FORM_DATA["status"] != 4) { ?>
            <button type="submit" name="store-dep" value="1" class="btn btn-primary pull-right"><?php echo $jkl["g38"];?></button>
          <?php } else { ?>
            <a class="btn btn-warning" href="<?php echo JAK_rewrite::jakParseurl('support', 'status', $page2, '1');?>"><?php echo $jkl["hd258"];?></a>
          <?php } ?>
        </div>
      </div>

      <?php if ($jakuser->getVar("files") && $jakosub['files']) { ?>

      <div class="card">
        <div class="card-header card-header-icon card-header-warning">
          <div class="card-icon">
            <i class="material-icons">attachment</i>
          </div>
          <h4 class="card-title "><?php echo $jkl["g288"];?></h4>
        </div>
        <div class="card-body">

          <table class="table table-striped" id="attach-list">

            <?php if (isset($JAK_TICKET_FILES) && is_array($JAK_TICKET_FILES)) foreach($JAK_TICKET_FILES as $k) { ?>

              <tr><td>
                <?php if (getimagesize($k["path"])) { ?>
                  <a href="<?php echo BASE_URL_ORIG;?>_showfile.php?i=<?php echo jak_encrypt_decrypt($k["encrypt"]);?>" data-toggle="lightbox"><img src="<?php echo BASE_URL_ORIG;?>_showfile.php?i=<?php echo jak_encrypt_decrypt($k["encrypt"]);?>" alt="<?php echo $k["name"];?>" class="img-thumbnail" width="50px"></a>
                <?php } else { ?>
                  <?php echo $k["name"];?>
                <?php } ?>
                <a href="<?php echo BASE_URL_ORIG;?>_showfile.php?i=<?php echo jak_encrypt_decrypt($k["encrypt"]);?>" class="btn btn-info btn-sm"><i class="fa fa-file-download"></i></a> <a href="<?php echo JAK_rewrite::jakParseurl('support', 'deletef', $page2, $k["name"]);?>" class="btn btn-danger btn-sm btn-delfile" data-title="<?php echo addslashes($jkl["g48"]);?>" data-text="<?php echo addslashes($jkl["e32"]);?>" data-type="warning" data-okbtn="<?php echo addslashes($jkl["g279"]);?>" data-cbtn="<?php echo addslashes($jkl["g280"]);?>"><i class="fa fa-trash-alt"></i></a>
              </td></tr>
            <?php } ?>
          </table>

          <div id="share-attachments" class="dropzone upload-ticket"></div>

        </div>
        <div class="card-footer">
          <a href="<?php echo JAK_rewrite::jakParseurl('support');?>" class="btn btn-default"><?php echo $jkl["g103"];?></a>
        </div>
      </div>

      <?php } ?>

      <div class="card">
        <div class="card-header card-header-icon card-header-info">
          <div class="card-icon">
            <i class="material-icons">notes</i>
          </div>
          <h4 class="card-title "><?php echo $jkl["g181"];?></h4>
        </div>
        <div class="card-body">

          <div class="form-group">
            <label for="notes"><?php echo $jkl["g181"];?></label>
            <textarea name="notes" rows="3" class="form-control"<?php echo ($JAK_FORM_DATA["status"] == 4 ? " readonly" : "");?>><?php echo $JAK_FORM_DATA["notes"];?></textarea>
          </div>

        </div>
        <div class="card-footer">
          <a href="<?php echo JAK_rewrite::jakParseurl('support');?>" class="btn btn-default"><?php echo $jkl["g103"];?></a>
          <button type="submit" name="store-notes" value="1" class="btn btn-primary pull-right"><?php echo $jkl["g38"];?></button>
        </div>
      </div>
    </div>
    <div class="col-md-8">

      <ul class="timeline timeline-simple">
          <li class="timeline-inverted">
            <div class="timeline-badge primary">
              <i class="material-icons">create</i>
            </div>
            <div class="timeline-panel">
              <div class="timeline-heading">
                <span class="badge badge-pill badge-info"><?php echo $jkl["hd177"];?></span>
              </div>
              <div class="timeline-body">
                <?php if (isset($JAK_EDIT_ANSWER) && !empty($JAK_EDIT_ANSWER)) { ?>
                  <div class="alert alert-info"><?php echo sprintf($jkl["hd190"], $page3);?></div>
                <?php } ?>

                <?php if (isset($JAK_EDIT_CONTENT) && !empty($JAK_EDIT_CONTENT)) { ?>
                  <div class="alert alert-info"><?php echo $jkl["g47"];?></div>
                <?php } ?>

                <?php if (isset($JAK_RESPONSE_DATA) && !empty($JAK_RESPONSE_DATA)) { ?>
                  <div class="form-group">
                    <label class="control-label" for="supresp"><?php echo $jkl["u35"];?></label>
                    <select id="supresp" class="selectpicker col-12" data-live-search="true">
                      <?php echo $JAK_RESPONSE_DATA;?>
                    </select>
                  </div>
                <?php } ?>
                <?php if (isset($JAK_EDIT_CONTENT) && !empty($JAK_EDIT_CONTENT)) { ?>
                  <div class="form-group">
                    <label for="subject"><?php echo $jkl["g16"];?></label>
                    <input type="text" name="subject" id="subject" class="form-control" value="<?php echo $JAK_EDIT_CONTENT["subject"];?>">
                  </div>
                <?php } ?>
                <div class="form-group">
                  <p><label for="supresp"><?php echo $jkl["g321"];?></label></p>
                  <textarea name="content" id="content-editor" rows="5" class="form-control"><?php if (isset($_POST["content"]) && !empty($_POST["content"])) { echo $_POST["content"]; } elseif (isset($JAK_EDIT_ANSWER) && !empty($JAK_EDIT_ANSWER)) { echo $JAK_EDIT_ANSWER["content"];} elseif (isset($JAK_EDIT_CONTENT) && !empty($JAK_EDIT_CONTENT)) { echo $JAK_EDIT_CONTENT["content"];}?></textarea>
                </div>
                <div class="form-check form-check-inline">
                  <label class="form-check-label">
                    <input class="form-check-input" type="checkbox" name="private-note" value="1"<?php if (isset($JAK_EDIT_ANSWER["private"]) && $JAK_EDIT_ANSWER["private"]) echo ' checked';?>> <?php echo $jkl['hd248'];?>
                    <span class="form-check-sign">
                      <span class="check"></span>
                    </span>
                  </label>
                </div>
              </div>
              <h6>
                <?php if (!empty(JAK_OPENAI_APIKEY)) { ?>
                        <a href="javascript:" data-scan="grammar" data-ticketid="<?php echo $JAK_FORM_DATA["id"];?>" data-answerid="0" class="btn btn-success btn-translate"><i class="far fa-spell-check"></i> <i class="fa fa-spinner fa-pulse d-none tl-loader"></i></a>
                        <a href="javascript:" data-scan="translate" data-ticketid="<?php echo $JAK_FORM_DATA["id"];?>" data-answerid="0" class="btn btn-info btn-translate"><i class="fas fa-language"></i> <i class="fa fa-spinner fa-pulse d-none tl-loader"></i></a>
                    <?php } ?>
                <button type="submit" name="save_close" class="btn btn-default"><?php echo $jkl["hd281"];?></button>
                <button type="submit" name="save" class="btn btn-primary" id="sendTM"><?php echo $jkl["g4"];?> <i id="loader" class="fa fa-spinner fa-pulse"></i></button>
                <input type="hidden" name="edit-content" value="<?php echo $editcont;?>">
                <input type="hidden" name="edit-answer" value="<?php echo $editid;?>">
              </h6>
            </div>
          </li>

        <?php if (isset($JAK_ANSWER_DATA) && !empty($JAK_ANSWER_DATA)) foreach ($JAK_ANSWER_DATA as $a) { ?>
          <li class="timeline-inverted" id="answerid-<?php echo $a["id"];?>">
            <div class="timeline-badge <?php echo ($a["file"] ? 'warning' : ($a["private"] ? 'info' : 'success'));?>">
              <i class="fal fa-<?php echo ($a["file"] ? 'file-archive' : ($a["private"] ? 'comment-alt-exclamation' : 'reply'));?>"></i>
            </div>
            <div class="timeline-panel">
              <div class="timeline-body">
                <?php if ($a["file"] == 1) { ?>
                  <div id="file-delete<?php echo $a["id"];?>"><?php echo sprintf($jkl['hd333'], '<a href="'.BASE_URL_ORIG.'_showfile.php?i='.jak_single_file_encrypt($targetPathTA, $a["content"], $JAK_FORM_DATA["clientid"], $page2).'" target="_blank" class="text-warning">'.substr($a["content"], 17).'</a>');?></div>
                <?php } else { ?>
                  <?php echo $a["content"];?>
                  <div class="aitranslate<?php echo $a["id"];?> ticket_translation"></div>
                <?php  } ?>
              </div>
              <h6 class="small">
                <?php echo sprintf($jkl['hd188'], ($a["cname"] ? '<a href="'.JAK_rewrite::jakParseurl('users', 'clients', 'edit', $a["cid"]).'" class="text-dark">'.$a["cname"].'</a>' : $a["oname"]));?> <?php echo sprintf($jkl["hd179"], JAK_base::jakTimesince($a["sent"], JAK_DATEFORMAT, JAK_TIMEFORMAT)).' '.sprintf($jkl["hd189"], $a["id"]);?>
              </h6>
              <div class="timeline-buttons">
                <?php if (!empty(JAK_OPENAI_APIKEY) && $a["file"] != 1) { ?>
                            <a href="javascript:" data-scan="answer" data-ticketid="<?php echo $page2;?>" data-answerid="<?php echo $a["id"];?>" data-lang="<?php echo $BT_LANGUAGE;?>" class="btn btn-link btn-info btn-translate"><i class="fas fa-language"></i> <i class="fa fa-spinner fa-pulse d-none tl-loader"></i></a>
                            <?php } if ($JAK_FORM_DATA["status"] != 4) { ?><a href="<?php echo JAK_rewrite::jakParseurl('support', 'read', $page2, $a["id"]);?>" class="btn btn-link btn-default"><i class="fa fa-edit"></i></a><?php if ($a["file"] != 1) { ?> <a href="<?php echo JAK_rewrite::jakParseurl('support', 'split', $page2, $a["id"]);?>" class="btn btn-link btn-default"><i class="fa fa-cut"></i></a><?php } ?> <a href="<?php echo JAK_rewrite::jakParseurl('support', 'deletea', $page2, $a["id"]);?>" class="btn btn-link btn-danger"><i class="fa fa-trash"></i></a><?php } ?>
              </div>
            </div>
          </li>
        <?php } ?>

        <li class="timeline-inverted">
          <div class="timeline-badge<?php echo (!isset($JAK_FORM_DATA["picture"]) || empty($JAK_FORM_DATA["picture"]) ? ' primary' : '');?>">
            <?php echo (isset($JAK_FORM_DATA["picture"]) && !empty($JAK_FORM_DATA["picture"]) ? '<img src="'.BASE_URL_ORIG.basename(JAK_FILES_DIRECTORY).$JAK_FORM_DATA["picture"].'" class="rounded-circle img-fluid" alt="picture">' : '<i class="fal fa-user"></i>');?>
          </div>
          <div class="timeline-panel">
            <div class="timeline-body">
              <?php echo $JAK_FORM_DATA["content"];?>
              <div class="aitranslatem<?php echo $JAK_FORM_DATA["id"];?> ticket_translation"></div>
            </div>
            <h6 class="small">
                          <i class="fa fa-time"></i> <?php echo sprintf($jkl["hd178"], JAK_base::jakTimesince($JAK_FORM_DATA["initiated"], JAK_DATEFORMAT, JAK_TIMEFORMAT));?>
                        </h6>
            <div class="timeline-buttons">
              <div class="pull-right">
              <?php if (!empty(JAK_OPENAI_APIKEY)) { ?>
                          <a href="javascript:" data-scan="answer" data-ticketid="<?php echo $JAK_FORM_DATA["id"];?>" data-answerid="0" class="btn btn-link btn-info btn-translate"><i class="fas fa-language"></i> <i class="fa fa-spinner fa-pulse d-none tl-loader"></i></a>
                          <?php } if ($JAK_FORM_DATA["status"] != 4) { ?><a href="<?php echo JAK_rewrite::jakParseurl('support', 'read', $page2, 'edit');?>" class="btn btn-link"><i class="fa fa-edit"></i></a><?php } ?>
                          </div><div class="clearfix"></div>
            </div>
          </div>
        </li>
      </ul>
    </div>
  </div>

</form>

<?php include_once 'footer.php';?>
