<?php
    if (file_exists(__DIR__."/../../pp-config.php")) {
        if (file_exists(__DIR__.'/../../maintenance.lock')) {
            if (!file_exists(__DIR__.'/../../pp-include/pp-maintenance.php')) {
                die('System is under maintenance. Please try again later.');
            }
            exit();
        } else {
            if (!file_exists(__DIR__.'/../../pp-include/pp-controller.php') || !file_exists(__DIR__.'/../../pp-include/pp-view.php')) {
                echo 'System is under maintenance. Please try again later.';
                exit();
            }

            if (file_exists(__DIR__.'/../../pp-include/pp-model.php')) {
                include(__DIR__."/../../pp-include/pp-model.php");
            } else {
                echo 'System is under maintenance. Please try again later.';
                exit();
            }
        }
    } else {
        echo 'System is under maintenance. Please try again later.';
        exit();
    }

    if (!defined('pp_allowed_access')) {
        die('Direct access not allowed');
    }

    if(isset($global_user_login) && $global_user_login == true){
?>
          <div class="page-header">
            <div class="row align-items-center">
              <div class="col">
                <h1 class="page-header-title">Add SMS</h1>
              </div>
              <div class="col-auto">
                <a class="btn btn-light" onclick="load_content('All SMS','sms-data?sms_status=all&sms_title=All%20SMS','nav-btn-sms')">
                  <i class="bi-list-ul me-1"></i> All SMS
                </a>
              </div>
            </div>
          </div>

          <div class="card">
            <div class="card-header">
              <h4 class="card-header-title">Create SMS Data</h4>
            </div>
            <div class="card-body">
              <div class="row g-3">
                <div class="col-md-6">
                  <label class="form-label">Device</label>
                  <select id="new-sms-data-device" class="form-select">
                    <option value="--" selected>Select Device</option>
                    <?php
                      $devices = json_decode(getData($db_prefix.'devices', 'ORDER BY 1 DESC'), true);
                      if($devices['status'] == true){
                          foreach($devices['response'] as $device){
                              if($device['d_status'] == "Disconnected"){
                                  $device_status = "Disconnected";
                              }else{
                                  $device_status = getConnectionStatus($device["created_at"], 3) ? "Connected" : "Disconnected";
                              }
                    ?>
                            <option value="<?php echo $device['id']?>"><?php echo htmlspecialchars($device['d_model'].' - '.$device['d_brand'].' ('.$device_status.')')?></option>
                    <?php
                          }
                      }
                    ?>
                  </select>
                </div>

                <div class="col-md-6">
                  <label class="form-label">Sim Slot</label>
                  <select id="new-sms-data-sim" class="form-select">
                    <option value="sim1" selected>SIM 1</option>
                    <option value="sim2">SIM 2</option>
                  </select>
                </div>

                <div class="col-md-6">
                  <label class="form-label">Status</label>
                  <select id="new-sms-data-status" class="form-select">
                    <option value="approved" selected>Approved</option>
                    <option value="review">Awaiting review</option>
                    <option value="used">Used</option>
                  </select>
                </div>

                <div class="col-md-6">
                  <label class="form-label">Payment Method</label>
                  <select id="new-sms-data-payment-method" class="form-select">
                    <option value="bKash" selected>bKash</option>
                    <option value="Nagad">Nagad</option>
                    <option value="Rocket">Rocket</option>
                    <option value="Upay">Upay</option>
                    <option value="Tap">Tap</option>
                    <option value="OkWallet">OkWallet</option>
                    <option value="Cellfin">Cellfin</option>
                    <option value="Ipay">Ipay</option>
                  </select>
                </div>

                <div class="col-md-6">
                  <label class="form-label">Amount</label>
                  <input type="number" class="form-control" id="new-sms-data-amount" placeholder="Amount" required>
                </div>

                <div class="col-md-6">
                  <label class="form-label">Phone Number</label>
                  <input type="text" class="form-control" id="new-sms-data-phone-number" placeholder="Phone Number" required>
                </div>

                <div class="col-md-6">
                  <label class="form-label">Transaction ID</label>
                  <input type="text" class="form-control" id="new-sms-data-transaction" placeholder="Transaction ID" required>
                </div>
              </div>

              <span class="response-model-new-message"></span>

              <div class="mt-4">
                <button class="btn btn-primary btn-insert-new-message" onclick="insert_newmessage()">
                  <i class="bi-plus-circle me-1"></i> Create
                </button>
              </div>
            </div>
          </div>

          <script>
            function insert_newmessage(){
              var method = document.querySelector("#new-sms-data-payment-method").value;
              var amount = document.querySelector("#new-sms-data-amount").value;
              var phone_number = document.querySelector("#new-sms-data-phone-number").value;
              var transaction_id = document.querySelector("#new-sms-data-transaction").value;
              var sim_slot = document.querySelector("#new-sms-data-sim").value;
              var status = document.querySelector("#new-sms-data-status").value;
              var device_id = document.querySelector("#new-sms-data-device").value;

              document.querySelector(".btn-insert-new-message").innerHTML = '<div class="spinner-border text-light spinner-border-sm" role="status"> <span class="visually-hidden">Loading...</span> </div>';

              $.ajax({
                type: "POST",
                url: "<?php echo pp_base_url()?>/admin/sms-data",
                data: { "action": "pp_sms-new-message", "method": method, "amount": amount, "phone_number": phone_number, "transaction_id": transaction_id, "sim_slot": sim_slot, "status": status, "device_id": device_id},
                success: function (data) {
                  document.querySelector(".btn-insert-new-message").innerHTML = '<i class="bi-plus-circle me-1"></i> Create';

                  var dedata = JSON.parse(data);

                  if(dedata.status == "false"){
                    document.querySelector(".response-model-new-message").innerHTML = '<div class="alert alert-danger mt-3 mb-0"> <i class="fa fa-info-circle me-2"></i> '+dedata.message+'</div>';
                  }else{
                    document.querySelector("#new-sms-data-amount").value = '';
                    document.querySelector("#new-sms-data-phone-number").value = '';
                    document.querySelector("#new-sms-data-transaction").value = '';
                    document.querySelector(".response-model-new-message").innerHTML = '<div class="alert alert-primary mt-3 mb-0"> <i class="fa fa-info-circle me-2"></i> '+dedata.message+'</div>';
                  }
                }
              });
            }
          </script>
<?php
    }
?>
