<link href="includes/js/fancytree/skin-bootstrap/ui.fancytree.css" rel="stylesheet" class="skinswitcher">
<script src="includes/js/fancytree/jquery.fancytree-all-deps.min.js"></script>
<script src="includes/js/fancytree/fcs_glyphoptions.js" ></script>
<input type='hidden' name='TicketType' id="TicketType" value='[% $type.type_id %]' />

<div id="CategoryItem" class="card panel-fcs-border shadow-sm rounded" style="min-height: 400px;">
    <div class="card-header" role="tab" id="headingCategory">
        <div class="row">
            <div class="col-lg-12">
                <div class="row">
                    <div class="col-8">
                        <h1 class="card-title">
                            [% $user->gettext('Kategorien auswählen') %]
                        </h1>
                    </div>
                        <div class="col-4">
                             <a style="width:100%;" href="javascript: parent.closeIFrame();" class='btn btn-outline-primary'>[% $user->gettext('Ticket neu laden') %]</a>
                        </div>
                </div>
            </div>
            
        </div>
    </div>
    <div id="CategoryPanel" class="panel" role="tabpanel" aria-labelledby="headingCategory">
        <div class="card-body">
            <div class="row">
                <div class="col-sm-6">
                    <div  class="form-group row mb-3" >
                        <label class="control-label col-sm-4" >[% $user->gettext('Kategorie') %] 1</label>
                        <div class="col-sm-8">
                            <div class="input-group">
                                <div name="category1_message" id="category1_message" ></div>
                                <input type="hidden" id="CategoryID1" value="[% $ticket.categoryid1 %]" />
                                [% if $ServiceTicket || (!(($ticket.ticketreferenzid < 1 && $ticket.ticketstatusid != 3 && $ticket.ticketstatusid != 7 && $user->hasRight(5) && ($ticket.ticketstatusid != 9 || $user->hasRight(112))) && !($NoCatChangeWhenForms && $categoryforms.formularid1 > 0))) %]
                                    <input type="text" class="form-control" [% if !$ServiceTicket %] id="CategoryDescr1"[% /if %]
                                           [% if $ServiceTicket || (!(($ticket.ticketreferenzid < 1 && $ticket.ticketstatusid != 3 && $ticket.ticketstatusid != 7 && $user->hasRight(5) && ($ticket.ticketstatusid != 9 || $user->hasRight(112))) && !($NoCatChangeWhenForms && $categoryforms.formularid1 > 0))) %] readonly="readonly" [% /if %]
                                           value="[% $ticket.category1 %]" onchange="emptyCat2();" />
                                [% else %]
                                    <select name='CategoryDescr1' id='CategoryDescr1'>
                                        <option id='[% $ticket.categoryid1 %]' selected='selected'>[% $ticket.category1 %]</option>
                                    </select>
                                [% /if %]
                                <span class="input-group-text">
                                    [% if !$ServiceTicket && ($ticket.ticketreferenzid < 1 && $ticket.ticketstatusid != 3 && $ticket.ticketstatusid != 7 && $user->hasRight(5) && ($ticket.ticketstatusid != 9 || $user->hasRight(112))) && !($NoCatChangeWhenForms && ($categoryforms.formularid1 > 0 || $categoryforms.formularid2 > 0) ) %]
                                        <a href="JavaScript:void(0);" onclick="openCategory(1);" title="[% $user->gettext('Kategorie ausw&auml;hlen') %]">
                                            <span class="fa fa-pencil" aria-hidden="true"></span>
                                        </a>
                                    [% /if %]
                                </span>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="col-sm-6">
                    [% if !$user->hasRight(51) %]
                        <div  class="form-group row mb-3" >
                            <label class="control-label col-sm-4" >[% $user->gettext('Kategorie') %] 2</label>
                            <div class="col-sm-8">
                                <div class="input-group">
                                    <input type="hidden" id="CategoryID2" value="[% $ticket.categoryid2 %]" />
                                    <div name="category2_message" id="category2_message" ></div>
                                    [% if $ServiceTicket || (!(($ticket.ticketreferenzid < 1 && $ticket.ticketstatusid != 3 && $ticket.ticketstatusid != 7 && $user->hasRight(5) && ($ticket.ticketstatusid != 9 || $user->hasRight(112))) && !($NoCatChangeWhenForms && $categoryforms.formularid2 > 0))) %]
                                        <input type="text" class="form-control" [% if !$ServiceTicket %] id="CategoryDescr2"[% /if %]
                                               [% if $ServiceTicket || (!(($ticket.ticketreferenzid < 1 && $ticket.ticketstatusid != 3 && $ticket.ticketstatusid != 7 && $user->hasRight(5) && ($ticket.ticketstatusid != 9 || $user->hasRight(112))) && !($NoCatChangeWhenForms && $categoryforms.formularid2 > 0))) %] readonly="readonly" [% /if %]
                                               value="[% $ticket.category2 %]" />
                                    [% else %]
                                        <select name='CategoryDescr2' id='CategoryDescr2'>
                                            <option id='[% $ticket.categoryid2 %]' selected='selected'>[% $ticket.category2 %]</option>
                                        </select>
                                    [% /if %]
                                    <span class="input-group-text">
                                        [% if !$ServiceTicket && ($ticket.ticketreferenzid < 1 && $ticket.ticketstatusid != 3 && $ticket.ticketstatusid != 7 && $user->hasRight(5) && ($ticket.ticketstatusid != 9 || $user->hasRight(112))) && !($NoCatChangeWhenForms && $categoryforms.formularid2 > 0) %]
                                            <a href="JavaScript:void(0);" onclick="openCategory(2);" title="[% $user->gettext('Kategorie ausw&auml;hlen') %]">
                                                <span class="fa fa-pencil" aria-hidden="true"></span>
                                            </a>
                                        [% /if %]
                                    </span>
                                </div>
                            </div>
                        </div>
                    [% /if %]
                </div>
            </div>
                [%*
                <div class="row">
                    <div class="col-sm-12">
                        <a style="width:100%;" href="javascript: parent.closeIFrame();" class='btn btn-outline-primary'>[% $user->gettext('Ticket neu laden') %]</a>
                    </div>
                </div>
                *%]
        </div>
    </div>
</div>
                    <div id="dialog-category" style="display:none;" width="500" hight="500">

<script>
    [% if $setCat %]
    $(document).ready(function(){
        parent.changeCat1Text("[% $ticket.category1 %]");
        parent.changeCat2Text("[% $ticket.category2 %]");
    });
    [% /if %]
    var TicketID = [% $ticket.id %];
     var tid = [% $ticket.id %];
    function openCategory(nr) {
        var url = "index.php?module=category&class=select&showInPopup=1&id=" + nr + "&tid=" + TicketID + "&tickettype=" + $("#TicketType").val();
        $("#dialog-category").fancybox({
            title: "Kategorie auswählen",
            href: url,
            type: 'ajax',
            fitToView: false,
            autoSize: false,
            width: 500, // or whatever
            height: 500
        }).trigger("click");
    }

    function openCategoryRequired(nr) {
        var url = "index.php?module=category&class=select&required&showInPopup=1&id=" + nr + "&tid=" + TicketID + "&tickettype=" + $("#TicketType").val();
        $("#dialog-category").fancybox({
            title: "Kategorie auswählen",
            href: url,
            type: 'ajax',
            fitToView: false,
            autoSize: false,
            width: 500, // or whatever
            height: 500,
            closeBtn: false,
            closeClick: false
        }).trigger("click");
    }
    
     [% if ($ticket.ticketstatusid != 3 && $ticket.ticketstatusid != 7 && $user->hasRight(5) && ($ticket.ticketstatusid != 9 || $user->hasRight(112))) && !($NoCatChangeWhenForms && $categoryforms.formularid2 > 0)%]
                            $("#CategoryDescr1").select2({theme: 'bootstrap-5',
                                                language: "[% $user->language_type %]",
                                                ajax: {
                                                url: "index.php?module=category&event=autocompleteJson",
                                                        dataType: 'json',
                                                        delay: 250,
                                                        data: function (params) {
                                                        return {
                                                        q: params.term, // search term
                                                                id: 0,
                                                                type: 'ticket',
        [% if $type.type_id %]
                                                        typeid: [% $type.type_id %],
        [% /if %]
        [% if $catdep %]
                                                        cid: 1,
        [% /if %]
        [% if $hmcompany|strpos:"AVAG HOLDING" !== false %]
                                                        view:'avag',
                                                                tid: 0,
        [% /if %]
                                                        ttt:0
                                            };
                                                },
                                                processResults: function (data) {
                                                                return {
                                                                results: data.items
                                                };
                                                }
                                                },
                                                    escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
                                                    }).on("select2:select", function(e) {
                                                                        var data = e.params.data;
                                                                if (data && data.id != $("#CategoryID1").val()) {
                                                                $("#CategoryID1").val(data.id).trigger('change');
                                                                $.post("index.php?module=category&class=select&showInPopup=1&type=ticket&tid=" + tid + "&id=1&cid=" + $("#CategoryID1").val() + "&save=1").done(function (json) {
                                                                //location.reload();
                                                                 parent.changeCat1Text(data.text);
                                                    })
                                                    
                                                    }
                                                        });
                                                        $("#CategoryDescr2").select2({theme: 'bootstrap-5',
                                                                        language: "[% $user->language_type %]",
                                                                        ajax: {
                                                                        url: "index.php?module=category&event=autocompleteJson",
                                                                                dataType: 'json',
                                                                                delay: 250,
                                                                                data: function (params) {
                                                                                return {
                                                                                q: params.term, // search term
                                                                                        cid: 2,
                                                                                        type: 'ticket',
        [% if $type.type_id %]
                                                                                typeid: [% $type.type_id %],
        [% /if %]
        [% if $catdep %]
                                                                                id: $('#CategoryID1').val(),
        [% else %]
                                                                                id: "0",
        [% /if %]

        [% if $hmcompany|strpos:"AVAG HOLDING" !== false %]
                                                                                view: "eds",
        [% /if %]
                                                                                ttt:0
                                                    };
                                                    },
                                                        processResults: function (data) {
                                                                                        return {
                                                                                        results: data.items
                                                            };
                                                            }
                                                            },
                                                            escapeMarkup: function (markup) { return markup; }, // let our custom formatter work
                                                }).on("select2:select", function(e) {
                                                                                                var data = e.params.data;
                                                                                        if (data && data.id != $("#CategoryID2").val()) {
                                                                                        $("#CategoryID2").val(data.id).trigger('change');
                                                                                        $.post("index.php?module=category&showInPopup=1&class=select&type=ticket&tid=" + tid + "&id=2&cid=" + $("#CategoryID2").val() + "&save=1").done(function (json) {
                                                                                       // location.reload();
                                                                                        parent.changeCat2Text(data.text);
                                                    })
                                                }
                                                });
                                                [% /if %]
</script>
