var ptyAdminInputPopupSubmitUrl = "";
var ptyAdminInputPopupInput2 = false;
var ptyAdminInputPopupInput3 = false;

$(function () {
    $('.ptyAdminInputPopup').click(function () {
        var title = $(this).attr("title");
        var content = $(this).attr("content");
        var content2 = $(this).attr("content2");
        var content3 = $(this).attr("content3");
        var value = $(this).attr("value");
        var value2 = $(this).attr("value2");
        var value3 = $(this).attr("value3");
        // var yesButtonTitle = $(this).attr("yesButtonTitle");
        ptyAdminInputPopupSubmitUrl = $(this).attr("href");

        if (typeof title !== typeof undefined && title !== false) {
            $("#ptyAdminInputPopupDlg_title").text(title);
        }

        if (typeof content !== typeof undefined && content !== false) {
            $("#ptyAdminInputPopupDlg_content").text(content);
        }

        $("div#ptyAdminInputPopupDlg_content2").hide();
        if (typeof content2 !== typeof undefined && content2 !== false) {
            ptyAdminInputPopupInput2 = true;
            $("div#ptyAdminInputPopupDlg_content2").show();
            $("span#ptyAdminInputPopupDlg_content2").text(content2);
        }

        $("div#ptyAdminInputPopupDlg_content3").hide();
        if (typeof content3 !== typeof undefined && content3 !== false) {
            ptyAdminInputPopupInput3 = true;
            $("div#ptyAdminInputPopupDlg_content3").show();
            $("span#ptyAdminInputPopupDlg_content3").text(content3);
        }


        /*
        if (typeof yesButtonTitle !== typeof undefined && yesButtonTitle !== false) {
            $("#ptyConfirm_yesButtonTitle").text(yesButtonTitle);
        }
        */

        $("input[name=ptyAdminInputPopupDlg_input]").val(value);
        $("input[name=ptyAdminInputPopupDlg_input2]").val(value2);
        $("input[name=ptyAdminInputPopupDlg_input3]").val(value3);
        $("#ptyAdminInputPopupDlg").modal();
        $("input[name=ptyAdminInputPopupDlg_input]").focus();
        return false;
    });

    $('#ptyAdminInputPopupDlg_ok').click(function () {
        $("#ptyAdminInputPopupDlg").modal('hide');

        if (ptyAdminInputPopupInput3 === true)
            location.href = ptyAdminInputPopupSubmitUrl + "&value=" + $("input[name=ptyAdminInputPopupDlg_input]").val() + "&value2="+ $("input[name=ptyAdminInputPopupDlg_input2]").val() + "&value3="+ $("input[name=ptyAdminInputPopupDlg_input3]").val();
        else if (ptyAdminInputPopupInput2 === true)
            location.href = ptyAdminInputPopupSubmitUrl + "&value=" + $("input[name=ptyAdminInputPopupDlg_input]").val() + "&value2="+ $("input[name=ptyAdminInputPopupDlg_input2]").val();
        else
            location.href = ptyAdminInputPopupSubmitUrl + "&value=" + $("input[name=ptyAdminInputPopupDlg_input]").val();

        return false;
    });
});


var ptyConfirm_yesUrl = "";
var ptyConfirm_selectUrl = null;
var ptyConfirm_selectArray = null;
var ptyConfirm_selectArray2 = null;
var ptyConfirm_postUrl = null;
$(function () {
    /**
     * ptyConfirmDialog
     *
     * <button type="button" class="ptyConfirmDialog" content='삭제하시겠습니까?' title='삭제' yesButtonTitle='삭제' href='<?=$this->request->classUrl?>/remove/?id=<?=$item->id?>'">삭제</button>
     */
    $('.ptyShowConfirmDialog').click(function (e) {
        e.preventDefault();
        var title = $(this).attr("title");
        var content = $(this).attr("content");
        var yesButtonTitle = $(this).attr("yesButtonTitle");
        var selectUrl = $(this).attr("selectUrl");
        var selectArray = $(this).attr("selectArray");
        var selectArray2 = $(this).attr("selectArray2");
        var postUrl = $(this).attr("postUrl");
        ptyConfirm_yesUrl = $(this).attr("href");
        ptyConfirm_selectUrl = selectUrl;
        ptyConfirm_selectArray = selectArray;
        ptyConfirm_selectArray2 = selectArray2;
        ptyConfirm_postUrl = postUrl;

        if (typeof title !== typeof undefined && title !== false) {
            $("#ptyConfirm_title").html(title);
        }

        if (typeof content !== typeof undefined && content !== false) {
            $("#ptyConfirm_content").html(content);
        }

        if (typeof selectUrl !== typeof undefined && selectUrl !== false) {
            var $el = $('#ptyConfirm_select');
            ptyPost(selectUrl, {}, function (ret, result) {
                $el.html(' ');
                // console.log(result.items);
                $.each(result.items, function (key, value) {
                    // console.log('key = ' + key + ', value = ' + value);
                    $el.append($("<option></option>").attr("value", value).text(key));
                });
                $el.addClass('animated flash');
                $("#ptyConfirm_select").show();
            });
        }


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

        if (typeof selectArray !== typeof undefined && selectArray !== false) {
            var $el = $('#ptyConfirm_select');
            ptyConfirm_selectArray = JSON.parse(ptyConfirm_selectArray);
            $el.html(' ');
            $.each(ptyConfirm_selectArray, function (key, value) {
                // console.log('key = ' + key + ', value = ' + value);
                $el.append($("<option></option>").attr("value", value).text(key));
            });
            $el.addClass('animated flash');
            $("#ptyConfirm_select").show();
        }

        if (typeof selectArray2 !== typeof undefined && selectArray2 !== false) {
            var $el = $('#ptyConfirm_select2');
            ptyConfirm_selectArray2 = JSON.parse(ptyConfirm_selectArray2);
            $el.html(' ');
            $.each(ptyConfirm_selectArray2, function (key, value) {
                $el.append("<div class='checkbox'><label><input type='checkbox' name='ptyConfirm_selectArray2_value' value=" + value + ">" + key + "</label></div>");
            });
            $el.addClass('animated flash');
            $("#ptyConfirm_select").hide();
            $("#ptyConfirm_select2").show();
        }

        if (typeof yesButtonTitle !== typeof undefined && yesButtonTitle !== false) {
            $("#ptyConfirm_yesButtonTitle").html(yesButtonTitle);
        }

        $("#ptyConfirmDlg").modal();
        return false;
    });

    $('#ptyConfirm_yesButtonTitle').click(function () {
        $("#ptyConfirmDlg").modal('hide');

        if (typeof ptyConfirm_yesUrl !== typeof undefined && ptyConfirm_yesUrl !== false) {
            ptyConfirm_yesUrl = ptyConfirm_yesUrl.replace("%40value", $("#ptyConfirm_select").val());
        }

        if (typeof ptyConfirm_postUrl !== typeof undefined && ptyConfirm_postUrl !== false) {

            if (typeof ptyConfirm_selectUrl !== typeof undefined && ptyConfirm_selectUrl !== false) {
                ptyConfirm_postUrl = ptyConfirm_postUrl.replace("%40value", $("#ptyConfirm_select").val());
            }
            else if (typeof ptyConfirm_selectArray2 !== typeof undefined && ptyConfirm_selectArray2 !== false) {

                // 콤마로 구분하여 가져옴
                var valuesArray = $('input[name="ptyConfirm_selectArray2_value"]:checked').map(function () {
                    return this.value;
                }).get().join(",");
                ptyConfirm_postUrl = ptyConfirm_postUrl.replace("%40value", valuesArray);
            }
        }

        if (typeof ptyConfirm_postUrl !== typeof undefined && ptyConfirm_postUrl !== false) {
            ptyPost(ptyConfirm_postUrl, {}, function (ret, result) {
                if (typeof ptyConfirm_yesUrl !== typeof undefined && ptyConfirm_yesUrl !== false) {
                    location.href = ptyConfirm_yesUrl;
                }
                else
                    window.location.reload();
            });
            return false;
        }

        if (typeof ptyConfirm_yesUrl !== typeof undefined && ptyConfirm_yesUrl !== false) {
            location.href = ptyConfirm_yesUrl;
        }
        else
            window.location.reload();
        return false;
    });
});