<form action="" method="post" class="form" id="editPositionForm">
    <div class="panel panel-fcs-border">
        <div class="panel-heading" style="padding-top: 0px;">
            <div class="row">
                <div class="col-md-10">
                    <div class="fcs-panel-title h2">
                        [% $user->gettext('Artikel-Position bearbeiten') %]
                    </div>
                </div>
                <div class="col-md-2">
                    <span id="submitPositionForm" class="btn btn-default">[% $user->gettext('Speichern') %]</span>
                </div>
            </div>
            <div class="panel-body">
                <input type="hidden" name="tid" value="[% $TicketID %]" />
                <input type="hidden" name="CallID" value="[% $CallID %]" />
                <input type="hidden" name="P[PositionsNr]" value="[% $Position.PositionNumber %].0" />
                <input type="hidden" name="save" value="1" />
                <div class="row">
                    <div class="col-md-12 form-group">
                        <label class="col-lg-2 control-label">[% $user->gettext('Artikel') %]</label>
                        <div class="col-lg-10">
                            <input type="text" class="form-control" value="[% $Position.Article %]" readonly/>
                        </div>
                    </div>
                    <div class="col-md-12 form-group">
                        <label class="col-lg-2 control-label">[% $user->gettext('Bezeichnung') %]</label>
                        <div class="col-lg-10">
                            <input type="text" class="form-control" value="[% $Position.Description %]"  readonly/>
                        </div>
                    </div>
                    <div class="col-md-12 form-group">
                        <label class="col-lg-2 control-label">[% $user->gettext('Anzahl') %]</label>
                        <div class="col-lg-10">
                            <input type="number" name="P[Amount]" class="form-control" value="[% $Position.Amount %]"/>
                        </div>
                    </div>

                    <div class="col-md-12 form-group">
                        <label class="col-lg-2 control-label">[% $user->gettext('Beschreibung') %]</label>
                        <div class="col-lg-10">
                            <input type="text" name="P[Kundenbezeichnung2]"  maxlength="20" class="form-control" value="[% $Position.Kundenbezeichnung2 %]"/>
                        </div>
                    </div>

                    <div class="col-md-12 form-group">
                        <label class="col-lg-2 control-label">[% $user->gettext('Zusätzliche Info') %]</label>
                        <div class="col-lg-10">
                            <input type="text" name="P[Kundenbezeichnung1]"  maxlength="20" class="form-control" value="[% $Position.Kundenbezeichnung1 %]"/>
                        </div>
                    </div>

                    <div class="col-md-12 form-group">
                        <label class="col-lg-2 control-label">[% $user->gettext('Kundenbez3') %]</label>
                        <div class="col-lg-10">
                            <input type="text" name="P[Kundenbezeichnung3]"  maxlength="30" class="form-control" value="[% $Position.Kundenbezeichnung3 %]"/>
                        </div>
                    </div>
                    <div class="col-md-12 form-group">
                        <label class="col-lg-2 control-label">[% $user->gettext('Kundenbez4') %]</label>
                        <div class="col-lg-10">
                            <input type="text" name="P[Kundenbezeichnung4]"  maxlength="30" class="form-control" value="[% $Position.Kundenbezeichnung4 %]"/>
                        </div>
                    </div>

                    <div class="col-md-12 form-group">
                        <label class="col-lg-2 control-label">[% $user->gettext('Klassifikation') %]</label>
                        <div class="col-lg-10">
                            <select name="P[Classification]" id="WPRClassificationID" class="form-control" data-width="100%">
                                <option value="0">-- [% $user->gettext('Klassifikation wählen') %] --</option>
                            </select>
                            <p id="WPRClassificationID_ol"></p>
                        </div>
                    </div>

                    <div class="col-md-12 form-group">
                        <label class="col-lg-2 control-label">[% $user->gettext('Fehlerbild') %]</label>
                        <div class="col-lg-10">
                            <select name="P[ErrorPattern]" id="WPRErrorPatterns" class="form-control" data-width="100%">
                                <option value="0" disabled>-- [% $user->gettext('Erst Fehlerbild wählen') %] --</option>
                            </select>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>

<script>

    LoadWPRClassificationSelection([% $Position.WarrantyRequestPositionID %], "[% $Position.ClassificationKey %]", "[% $Position.ErrorPatternKey %]");


    function LoadWPRClassificationSelection(warrantyprocessingrequestpositionid, classicode, SelectedErrorPattern) {
        $("#WPRClassificationID_ol").html("");

        if (typeof (TicketID) !== 'undefined')
        {
            useTicketID = TicketID;
        } else
        {
            useTicketID = 0;
        }

        var url = 'index.php?module=errorpatterns&event=loadClassificationsForPosition';

        $.post(url, {ticketid: useTicketID, WarrantyProcessingRequestPositionID: warrantyprocessingrequestpositionid, selectedValue: classicode}, function (Response)
        {
            if (Response.error) {
                return false;
            } else {
                Klassi = Response;
                $('#WPRClassificationID').empty();
                var PlaceholderOption = new Option('-- [% $user->gettext('Klassifikation wählen') %] --', "0", true, true);
                $('#WPRClassificationID').append(PlaceholderOption);

                $.each(Response, function (i, item)
                {
                    if (item.selected === true) {
                        $("#WPRClassificationID_ol").html(" " + item.string_ol + "");
                    }
                    var newOption = new Option(item.string, item.code, true, item.selected);
                    $('#WPRClassificationID').append(newOption);
                });
            }

            $('#WPRClassificationID').select2().on('change', function (e)
            {
                ClassiCode = $('#WPRClassificationID').val();
                var url = 'index.php?module=wprequest&event=UpdateWPRPositionClassification';

                $.post(url, {WarrantyProcessingRequestPositionID: warrantyprocessingrequestpositionid, ClassificationCode: ClassiCode}, function (Response) {});

                LoadWPRErrorPatterns(warrantyprocessingrequestpositionid, SelectedErrorPattern);

            });

            LoadWPRErrorPatterns(warrantyprocessingrequestpositionid, SelectedErrorPattern);
        });
    }

    function LoadWPRErrorPatterns(warrantyprocessingrequestpositionid, SelectedPattern) {
        $("#WPRErrorPatternDescr_ol").html("");

        var url = 'index.php?module=errorpatterns&event=LoadErrorpatternsForPositon';

        $.post(url, {ticketid: useTicketID, ClassificationID: $('#WPRClassificationID').val(), WarrantyProcessingRequestPositionID: warrantyprocessingrequestpositionid, selectedValue: SelectedPattern}, function (Response)
        {
            if (Response.error) {
                return false;
            } else {
                EP = Response;
                EPCode = $('#WPRErrorPatterns').val();
                if (typeof (EP[EPCode]) === 'undefined') {
                    $("#ErrorPatternDescr_ol").html("");
                }
                $('#WPRErrorPatterns').empty();
                var PlaceholderOption = new Option('-- [% $user->gettext('Fehlerbild wählen') %] --', "0", true, true);
                $('#WPRErrorPatterns').append(PlaceholderOption);

                $.each(Response, function (i, item)
                {
                    if (item.selected === true) {
                        $("#WPRErrorPatternDescr_ol").html(" " + item.string_ol + "");
                    }
                    var newOption = new Option(item.string, item.code, true, item.selected);
                    $('#WPRErrorPatterns').append(newOption);
                });
            }

            $('#WPRErrorPatterns').select2();

            $('#WPRErrorPatterns').select2().on('change', function (e)
            {
                ErrorPattern = $('#WPRErrorPatterns').val();
                var url = 'index.php?module=wprequest&event=UpdateWPRPositionErrorPattern';
                $.post(url, {WarrantyProcessingRequestPositionID: warrantyprocessingrequestpositionid, ErrorPattern: ErrorPattern}, function (Response) {});
            });
        });
    }


       $('#submitPositionForm').click(function() {
          $.ajax({
             type: "POST",
             url: "index.php?module=ticket&event=editPosition",
             data: $("#editPositionForm").serialize(),
             success: function(json) {
                if(json.error){
                 alert(json.message);

                } else {
                    noty({
                        text: json.message,
                        type: json.error ? 'error' : 'success',
                        timeout: notifyTimeout
                    });
                    $("#LargeModal").modal("hide");
                }
             }
          });

       });

</script>
