{% set userData = app.session.get('userData') %}
<form method="POST" id="form-faxingOption">
    <h5>{{ page_title }}</h5>
    <div id="flash-messages-content-modal"></div>
    <div class="row max-width-initial">
        <div class="large-12 columns">
            <div class="row">
                {#<div class="large-6 columns">#}
                    {#{{ form_label(form.company_desc) }}#}
                    {#{{ form_widget(form.company_desc) }}#}
                    {#{{ form_widget(form.company) }}#}
                {#</div>#}
                <div class="large-6 columns">
                    {{ form_label(form.faxTo) }}
                    {{ form_widget(form.faxTo) }}
                </div>
                <div class="large-6 columns">
                    {{ form_label(form.insurance_desc) }}
                    {{ form_widget(form.insurance_desc) }}
                    {{ form_widget(form.insurance) }}
                </div>
                <div class="large-6 columns">
                    {{ form_label(form.referringProvider_fullName) }}
                    {{ form_widget(form.referringProvider_fullName) }}
                    {{ form_widget(form.referringProvider) }}
                </div>
                <div class="large-6 columns">
                    {{ form_label(form.referringHealthFacility_desc) }}
                    {{ form_widget(form.referringHealthFacility_desc) }}
                    {{ form_widget(form.referringHealthFacility) }}
                </div>
            </div>
            <div class="row">
                <div class="large-6 columns">
                    {{ form_label(form.faxNo) }}
                    {{ form_widget(form.faxNo) }}
                </div>
                <div class="large-6 columns">
                    {{ form_label(form.birth_date) }}
                    {{ form_widget(form.birth_date) }}
                </div>
            </div>
            <div class="row">
                <div class="large-6 columns">
                    {{ form_label(form.patientName) }}
                    {{ form_widget(form.patientName) }}
                    {{ form_widget(form.patient) }}
                </div>
                <div class="large-6 columns">
                    {{ form_label(form.page) }}
                    {{ form_widget(form.page) }}
                </div>
            </div>
            <div class="row">
                <div class="large-6 columns">
                    <label for="faxing_option_template">Template</label>
                    <select name="faxingOptionForm[template]" id="">
                        <option value="">--- SELECT TEMPLATE ---</option>
                        {% for company in companies %}
                            <option value="{{ company.id|base64Encode }}" {{ faxingOption.template|default('') == company.description ? 'selected' : '' }}>{{ company.description }}</option>
                        {% endfor %}
                    </select>
                </div>
                <div class="large-6 columns"></div>
            </div>
            <div class="row">
                <div class="large-12 columns" style="margin-top: 5%">
                    {{ form_label(form.faxCoverSelection) }}
                    {{ form_widget(form.faxCoverSelection) }}
                </div>
            </div>
            <div class="row">
                <div class="large-12 columns">
                    {{ form_label(form.customBody) }}
                    {{ form_widget(form.customBody) }}
                </div>
            </div>
            <div class="row">
                <div class="large-12 columns" style="margin-top: 2%">
                    <h5>Attachments(s) <a href="javascript:void(0)" id="href-new-file-field"><i class="fa fa-plus"></i></a></h5>
                    <p class="form-hint-text">Upload file(s) below</p>
                    <div id="file-field-container">
                        {% if faxingOption %}
                            {% for key,fof in faxingOption.faxingOptionFiles %}
                                {% if fof.isDeleted != true %}
                                    <div class="input-group no-margin-bottom">
                                        <input type="hidden" name="otherForm[files][{{ key }}]" value="{{ fof.idencoded }}" />
                                        <a href="{{ path('faxing_option_file_download', {id: fof.idencoded}) }}">{{ fof.filedesc }}</a>
                                        <div class="input-group-button">
                                            <button type="button" class="button secondary file-field-remove"><i class="fa fa-remove"></i></button>
                                        </div>
                                    </div>
                                {% endif %}
                            {% endfor %}
                        {% endif %}
                    </div>
                </div>
            </div>
        </div>
    </div>
    <hr>
    <div class="row-max-width-initial">
        <div class="small-12 columns no-padding-right">
            <div class="button-container clearfix text-right">
                <a href="javascript:void(0)" class="button just-continue href-faxingOption-action float-right" data-axn="{{ action }}"
                   data-message="Are you sure you want to save this information?"
                   style="margin-left: 10px; margin-right: 0;"
                >Save</a>
                {% if action == 'u' and (userData.type == 'Super Admin' or 'Admin Fax Option Delete' in userData.accesses) %}
                        <a href="javascript:void(0)" class="ask-continue button alert href-faxingOption-action float-right" data-axn="d"
                        data-message="Are you sure you want to delete this record?"
                        style="margin-left: 10px; margin-right: 0;"
                    >Delete</a>
                {% endif %}
                <a href="javascript:void(0)" class="button secondary" data-close style="margin-left: 10px; margin-right: 0;">Close</a>
            </div>
        </div>
    </div>
    {{ form_widget(form.action) }}
    {{ form_widget(form.id) }}
    {{ form_widget(form._token) }}

    <script>
        jQ(document).ready(function(){
            var axn = jQ('#faxingOptions_action').val();

            let faxingOptionForm = {
                settings: {},
                init: function() {
                    faxingOptionForm.bindElements();
                    faxingOptionForm.fileField_arrangeIndexes();
                    faxingOptionForm.fileField_bindRemove();
                    faxingOptionForm.bindFaxTo();
                },
                bindElements: function() {
                    jQ('#href-new-file-field').unbind('click').bind('click', this.newFileField);
                },
                newFileField: function() {
                    var div_ig = jQ('<div/>'),
                        div_igb = jQ('<div/>'),
                        btn = jQ('<button/>'),
                        input_file = jQ('<input/>');

                    div_ig.addClass('input-group').addClass('no-margin-bottom');
                    div_igb.addClass('input-group-button');
                    btn.attr('type', 'button').addClass('button secondary').addClass('file-field-remove').html('<i class="fa fa-remove"></i>');
                    input_file.attr({
                        type:'file',
                        accept : 'application/pdf'
                    }).addClass('input-group-field');

                    div_igb.append(btn);
                    div_ig.append(input_file).append(div_igb);

                    jQ('#file-field-container').append(div_ig);

                    faxingOptionForm.fileField_arrangeIndexes();
                    faxingOptionForm.fileField_bindRemove();
                },
                fileField_arrangeIndexes: function() {
                    jQ('#file-field-container .input-group').each(function(key){
                        jQ(this).attr('id', 'file-field-input-group-' + key).data('key', key);
                        jQ(this).find('input[type=file]').attr('name', 'files[' + key + ']');
                        jQ(this).find('button').data('key', key);
                    });
                },
                fileField_bindRemove: function() {
                    jQ('.file-field-remove').unbind('click').bind('click', function(){
                        jQ('#file-field-input-group-' + jQ(this).data('key')).remove();
                        faxingOptionForm.fileField_arrangeIndexes();
                    });
                },
                bindFaxTo: function () {
                    jQ("#faxingOptions_faxTo").unbind('change').bind('change',function () {
                        if(jQ(this).val() === 'Insurance'){
                            jQ('.insurance').removeClass('hide');
                            jQ('.referring-hf, .referring-provider').addClass('hide');
                            jQ('#faxingOptions_referringHealthFacility_desc, #faxingOptions_referringHealthFacility, #faxingOptions_referringProvider_fullName, #faxingOptions_referringProvider').val(null);
                        }else if(jQ(this).val() === 'Referring Affiliate'){
                            jQ('.referring-hf').removeClass('hide');
                            jQ('.referring-provider, .insurance').addClass('hide');
                            jQ('#faxingOptions_insurance_desc, #faxingOptions_insurance, #faxingOptions_referringProvider_fullName, #faxingOptions_referringProvider').val(null);
                        }else{
                            jQ('.referring-provider').removeClass('hide');
                            jQ('.referring-hf, .insurance').addClass('hide');
                            jQ('#faxingOptions_insurance_desc, #faxingOptions_insurance, #faxingOptions_referringHealthFacility_desc, #faxingOptions_referringHealthFacility').val(null);
                        }
                    });

                    jQ('.insurance').addClass('hide');
                    if(axn === 'n'){
                        jQ('.insurance').removeClass('hide');
                    }else{
                        if(jQ('#faxingOptions_referringProvider').val() !== '' && jQ('#faxingOptions_referringProvider').val() !== null){
                            jQ('.referring-provider').removeClass('hide');
                            jQ('#faxingOptions_faxTo').val('Referring Physician');
                        }else if(jQ('#faxingOptions_referringHealthFacility').val() !== '' && jQ('#faxingOptions_referringHealthFacility').val() !== null){
                            jQ('.referring-hf').removeClass('hide');
                            jQ('#faxingOptions_faxTo').val('Referring Affiliate');
                        }else {
                            jQ('.insurance').removeClass('hide');
                            jQ('#faxingOptions_faxTo').val('Insurance');
                        }
                    }
                }
            };

            faxingOptionForm.init();

        });
    </script>
</form>