var jQuery_1_8_2 = jQuery_1_8_2 || $.noConflict();

(function ($, undefined) {
  "use strict";
  $(function () {
    var dialog = $.fn.dialog !== undefined,
      datagrid = $.fn.datagrid !== undefined,
      validate = $.fn.validate !== undefined,
      $dialogDeleteLogo = $("#dialogDeleteLogo"),
      $dialogSendInvoice = $("#dialogSendInvoice"),
      $frmInvoiceConfig = $("#frmInvoiceConfig"),
      tabs = $.fn.tabs !== undefined,
      $tabs = $("#tabs"),
      tOpt = {
        activate: function (event, ui) {
          $(":input[name='tab_id']").val($(ui.newPanel).prop("id"));
        },
      };

    if ($tabs.length > 0 && tabs) {
      $tabs.tabs(tOpt);
    }

    if ($frmInvoiceConfig.length > 0) {
      $frmInvoiceConfig.validate({
        rules: {
          y_email: { email: true },
          y_url:   { url:   true },
        },
        errorPlacement: function (error, element) {
          error.insertAfter(element.parent());
        },
        onkeyup: false,
        errorClass: "err",
        wrapper: "em",
        ignore: "",
        // The global submitHandler in pjBaseCore.js only starts Ladda, so this
        // local handler also submits the form.
        submitHandler: function (form) {
          var laddaButtons = $(form).find('.ladda-button');
          if (laddaButtons.length > 0 && typeof laddaButtons.ladda === 'function') {
            try { laddaButtons.ladda().ladda('start'); } catch (e) {}
          }
          form.submit();
          return true;
        },
        invalidHandler: function (event, validator) {
          if (validator.numberOfInvalids()) {
            var index = $(validator.errorList[0].element, this)
              .closest("div[id^='tabs-']")
              .index();
            if ($tabs.length > 0 && tabs && index !== -1) {
              $tabs.tabs(tOpt).tabs("option", "active", index - 1);
            }
          }
        },
      });
    }

    $(document)
      .on("search", ".frm-filter", function (e) {
        var $this = $(this),
          content = $grid.datagrid("option", "content"),
          cache = $grid.datagrid("option", "cache");

        $.extend(cache, { q: $this.find("input[name='q']").val() });

        $grid.datagrid("option", "cache", cache);
        updateInvoiceFilterState(cache);

        $grid.datagrid(
          "load",
          "index.php?controller=pjInvoice&action=pjActionGetInvoices",
          "created",
          "DESC",
          content.page,
          content.rowCount
        );
      })
      .on("submit", ".frm-filter", function (e) {
        if (e && e.preventDefault) { e.preventDefault(); }
        var $form = $(this),
          $q = $form.find("input[name='q']");
        $q.val($q.val().replace(/^\s+|\s+$/g, ""));
        $form.trigger("search");
        return false;
      })
      .on("change", "select[name='foreign_id']", function (e) {
        var $this = $(this),
          content = $grid.datagrid("option", "content"),
          cache = $grid.datagrid("option", "cache");

        $.extend(cache, { foreign_id: $this.find("option:selected").val() });

        $grid.datagrid("option", "cache", cache);

        $grid.datagrid(
          "load",
          "index.php?controller=pjInvoice&action=pjActionGetInvoices",
          "created",
          "DESC",
          content.page,
          content.rowCount
        );
      })
      .on("change", "select[name='status_filter']", function () {
        if (typeof $grid === "undefined" || !$grid) { return; }
        var $this = $(this),
          content = $grid.datagrid("option", "content"),
          cache = $grid.datagrid("option", "cache");
        $.extend(cache, { status: $this.val() });
        $grid.datagrid("option", "cache", cache);
        updateInvoiceFilterState(cache);
        $grid.datagrid(
          "load",
          "index.php?controller=pjInvoice&action=pjActionGetInvoices",
          "created",
          "DESC",
          1,
          content.rowCount
        );
      })
      .on("shown.bs.collapse hidden.bs.collapse", "#collapseInvoiceFilters", function () {
        $(".pj-invoice-search-group .btn-advance-search").attr("aria-expanded", $(this).hasClass("in") ? "true" : "false");
      })
      .on("click", ".btn-clear-filters", function () {
        if (typeof $grid === "undefined" || !$grid) { return; }
        var content = $grid.datagrid("option", "content"),
          cache = $grid.datagrid("option", "cache") || {};
        cache.q = "";
        cache.status = "";
        cache.foreign_id = "";
        $(".frm-filter input[name='q']").val("");
        $("select[name='status_filter'], select[name='foreign_id']").val("");
        $("#collapseInvoiceFilters").collapse("hide");
        $grid.datagrid("option", "cache", cache);
        updateInvoiceFilterState(cache);
        $grid.datagrid(
          "load",
          "index.php?controller=pjInvoice&action=pjActionGetInvoices",
          "created",
          "DESC",
          1,
          content.rowCount
        );
      })
      .on("change", ".pj-table-cell-editable > select.pj-selector-editable", function () {
        $(this).trigger("save");
      });

    function updateInvoiceFilterState(cache) {
      var active = !!(cache && (cache.q || cache.status || cache.foreign_id));
      $(".pj-invoice-search-group").toggleClass("has-active-filter", active);
      $(".btn-clear-filters").toggle(active);
    }

    function formatInvoiceNumber(str, obj) {
      return '<a href="index.php?controller=pjInvoice&action=generatePdf&id=' + encodeURIComponent(obj.uuid) + '&uuid=' + encodeURIComponent(obj.order_id) + '" target="_blank">' + str + '</a>';
    }

    function formatOrderId(str, obj) {
      if (obj.order_db_id) {
        return '<a href="index.php?controller=pjAdminOrders&action=pjActionUpdate&id='
          + encodeURIComponent(obj.order_db_id)
          + '">'
          + escapeHtml(str)
          + '</a>';
      }

      return escapeHtml(str);
    }

    function formatTotal(str, obj) {
      return obj.total_formated;
    }

    function escapeHtml(str) {
      if (str === null || str === undefined) { return ""; }
      return String(str)
        .replace(/&/g, "&amp;")
        .replace(/</g, "&lt;")
        .replace(/>/g, "&gt;")
        .replace(/"/g, "&quot;")
        .replace(/'/g, "&#039;");
    }

    function formatCustomer(str, obj) {
      var label = obj.customer_label || "";
      if (!label) { return '<span class="text-muted">—</span>'; }
      return '<div class="pj-invoice-customer">' + escapeHtml(label) + "</div>";
    }

    function formatCreated(str) {
      if (str === null || str.length === 0) {
        return myLabel.empty_datetime;
      }
      if (str === "0000-00-00 00:00:00") {
        return myLabel.invalid_datetime;
      }
      if (str.match(/\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}/) !== null) {
        var x = str.split(" "),
          date = x[0],
          time = x[1],
          dx = date.split("-"),
          tx = time.split(":"),
          y = dx[0],
          m = parseInt(dx[1], 10) - 1,
          d = dx[2],
          hh = tx[0],
          mm = tx[1],
          ss = tx[2];
        return $.datagrid.formatDate(
          new Date(y, m, d, hh, mm, ss),
          pjGrid.jsDateFormat + ", hh:mm:ss"
        );
      }
    }

    $(document).ajaxSuccess(function (event, xhr, settings, response) {
      if (
        settings.url.indexOf(
          "controller=pjInvoice&action=pjActionGetInvoices"
        ) !== -1 &&
        response.hide_loader
      ) {
        $(".sk-spinner").hide();
      }
    });

    if ($("#grid").length > 0 && datagrid) {
      var $grid = $("#grid").datagrid({
        buttons: [
          // Inline icon buttons. The datagrid creates <a class="pj-table-icon-{type}"><i class="fa fa-{type}"></i></a>.
          // PDF opens directly. Envelope opens the Deepak-style send confirmation flow.
          // Delete: deletes invoice.
          {
            type: "file-pdf-o",
            url: "index.php?controller=pjInvoice&action=generatePdf&id={:uuid}&uuid={:order_id}",
            title: myLabel.download_pdf_invoice,
            target: "_blank",
          },
          // {
          //   type: "envelope",
          //   url: "index.php?controller=pjInvoice&action=pjActionSend&id={:uuid}&uuid={:order_id}",
          //   title: myLabel.email_invoice,
          //   linkClass: "plugin_invoice_email",
          // },
          {
            type: "delete",
            url: "index.php?controller=pjInvoice&action=pjActionDelete&id={:id}",
            title: "Delete",
          },
        ],

        columns: [
          {
            text: myLabel.num,
            type: "text",
            sortable: true,
            editable: false,
            renderer: formatInvoiceNumber,
          },
          {
            text: myLabel.order_id,
            type: "text",
            sortable: true,
            editable: false,
            renderer: formatOrderId,
          },
          {
            text: myLabel.customer,
            type: "text",
            sortable: true,
            editable: false,
            renderer: formatCustomer,
          },
          {
            text: myLabel.issue_date,
            type: "date",
            sortable: true,
            editable: false,
            renderer: $.datagrid._formatDate,
            dateFormat: pjGrid.jsDateFormat,
          },
          {
            text: myLabel.status,
            type: "select",
            sortable: true,
            editable: true,
            applyClass: "pj-status",
            options: [
              { label: myLabel.paid, value: "paid" },
              { label: myLabel.not_paid, value: "not_paid" },
              { label: myLabel.cancelled, value: "cancelled" },
            ],
          },
          {
            text: myLabel.total,
            type: "text",
            sortable: true,
            editable: false,
            align: "right",
            renderer: formatTotal,
          },
        ],

        dataUrl: "index.php?controller=pjInvoice&action=pjActionGetInvoices",
        dataType: "json",
        fields: ["uuid", "order_id", "b_name", "issue_date", "status", "total"],

        paginator: {
          actions: [
            {
              text: myLabel.download_pdfs_bulk,
              url: "index.php?controller=pjInvoice&action=pjActionDownloadBulk",
              render: true,
              ajax: false,
            },
            {
              text: myLabel.delete_title,
              url: "index.php?controller=pjInvoice&action=pjActionDeleteBulk",
              render: true,
              confirmation: myLabel.delete_body,
            },
          ],
          gotoPage: true,
          paginate: true,
          total: true,
          rowCount: true,
        },

        saveUrl: "index.php?controller=pjInvoice&action=pjActionSaveInvoice&id={:id}",

        select: { field: "id", name: "record[]" },
      });

      var m = window.location.href.match(/&q=(.*)/);
	    if (m !== null) {
	      $(".frm-filter").trigger("search");
	    }

      m = window.location.href.match(/&(foreign_id=)(\d+)?/);
      if (m !== null) {
        var content = $grid.datagrid("option", "content"),
          cache = $grid.datagrid("option", "cache");

        $.extend(cache, { foreign_id: m[2] !== undefined ? m[2] : "" });
	        $grid.datagrid("option", "cache", cache);
	        updateInvoiceFilterState(cache);

	        $grid.datagrid(
          "load",
          "index.php?controller=pjInvoice&action=pjActionGetInvoices",
          "created",
          "DESC",
          content.page,
          content.rowCount
        );
      }
    }

    $(document)
      .on("click", ".plugin_invoice_delete_logo", function (e) {
        e.preventDefault();
        swal(
          {
            title: myLabel.btn_delete,
            text: myLabel.confirm_delete_msg,
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: myLabel.btn_delete,
            cancelButtonText: myLabel.btn_cancel,
            closeOnConfirm: false,
            showLoaderOnConfirm: true,
          },
          function () {
            $.post("index.php?controller=pjInvoice&action=pjActionDeleteLogo")
              .done(function () {
                $("#plugin_invoice_box_logo").html(
                  '<input type="file" name="y_logo" id="y_logo" />'
                );
                swal.close();
              })
              .fail(function () {
                swal("Error", "Something went wrong while deleting the logo.", "error");
              });
          }
        );
      })
      .on("click", ".plugin_invoice_email, .pj-table-icon-envelope", function (e) {
        if (e && e.preventDefault) { e.preventDefault(); }

        var href = $(this).attr("href") || "";
        var uuidMatch = href.match(/[?&]uuid=([^&]+)/);
        var orderUuid = uuidMatch ? decodeURIComponent(uuidMatch[1]) : null;

        if (!orderUuid) {
          return false;
        }

        var rowData = $(this).data("id") || {};
        var clientMail = rowData.b_email || "";
        var clientName = rowData.b_name || rowData.client_name || "";

        if (typeof swal === "function") {
          var bodyText = "Send the payment confirmation with the invoice attached to:\n\n"
                       + (clientName ? clientName + "\n" : "")
                       + (clientMail || "the customer");
          swal({
            title: "Send invoice?",
            text: bodyText,
            type: "info",
            showCancelButton: true,
            confirmButtonText: myLabel.btn_send || "Send",
            cancelButtonText: myLabel.btn_cancel || "Cancel",
            closeOnConfirm: false,
            showLoaderOnConfirm: true
          }, function (confirm) {
            if (confirm === false) { return false; }
            $.post("index.php?controller=pjAdminOrders&action=pjActionSendStatusEmail", {
              uuid: orderUuid,
              variant: "payment"
            }).done(function (r) {
              if (r && r.status === "OK") {
                swal("Sent!", "The invoice email has been sent" + (clientMail ? " to " + clientMail : "") + ".", "success");
              } else {
                swal("Error", (r && r.text) ? r.text : "Sending failed.", "error");
              }
            }).fail(function () {
              swal("Error", "Sending failed because of a server error.", "error");
            });
          });
        }

        return false;
      });

    $("li.plugin_view_invoice a").unbind("click");

    if ($dialogSendInvoice.length > 0 && dialog) {
      var buttons = {};
      var btnSend = myLabel.btn_send || "Send";
      var btnCancel = myLabel.btn_cancel || "Cancel";

      buttons[btnSend] = function () {
        var $this = $(this);
        $.post(
          "index.php?controller=pjInvoice&action=pjActionSend",
          $dialogSendInvoice.find("form").serialize()
        ).done(function (r) {
            if (r && r.status === "OK") {
                if (typeof swal === "function") {
                    swal("Sent!", "The invoice has been successfully sent.", "success");
                } else {
                    alert("Sent!");
                }
                $this.dialog("close");
            } else {
                var errTxt = (r && r.text) ? r.text : "Sending failed.";
                if (typeof swal === "function") {
                    swal("Error", errTxt, "error");
                } else {
                    alert(errTxt);
                }
            }
        }).fail(function () {
            if (typeof swal === "function") {
                swal("Error", "Server error.", "error");
            } else {
                alert("Server error.");
            }
        });
      };
      buttons[btnCancel] = function () { $(this).dialog("close"); };

      $dialogSendInvoice.dialog({
        modal: true,
        autoOpen: false,
        draggable: false,
        resizable: false,
        width: 770,
        open: function () {
          var $this = $(this);
          $dialogSendInvoice.html('<div style="padding:40px; text-align:center;"><i class="fa fa-spinner fa-spin fa-2x"></i></div>');
          $.get("index.php?controller=pjInvoice&action=pjActionSend", {
            id: $this.data("id"),
            uuid: $this.data("uuid"),
          })
            .done(function (data) { 
                $dialogSendInvoice.html(data); 
                $dialogSendInvoice.dialog("option", "position", "center");
            });
        },
        buttons: buttons,
      });
    }

    if ($dialogDeleteLogo.length > 0 && dialog) {
      var dlButtons = {};
      dlButtons[myLabel.btn_delete] = function () {
        var $this = $(this);
        $.post("index.php?controller=pjInvoice&action=pjActionDeleteLogo")
          .done(function () {
            $("#plugin_invoice_box_logo").html('<input type="file" name="y_logo" id="y_logo" />');
          })
          .always(function () { $this.dialog("close"); });
      };
      dlButtons[myLabel.btn_cancel] = function () { $(this).dialog("close"); };

      $dialogDeleteLogo.dialog({
        modal: true,
        autoOpen: false,
        draggable: false,
        resizable: false,
        buttons: dlButtons,
      });
    }

    if (window.tinymce !== undefined) {
      tinymce.init({
        selector: "textarea.mceEditor",
        theme: "modern",
        height: 700,
        plugins: [
          "advlist autolink link image lists charmap print preview hr anchor pagebreak",
          "searchreplace visualblocks visualchars code fullscreen insertdatetime media nonbreaking",
          "save table contextmenu directionality emoticons template paste textcolor",
        ],
        toolbar:
          "insertfile undo redo | styleselect fontselect | fontsizeselect | bold italic | alignleft aligncenter alignright alignjustify | bullist numlist outdent indent | link image | print preview media fullpage | forecolor backcolor emoticons",
      });
    }
  });
})(jQuery_1_8_2);
