<?php $__env->startSection('modals'); ?>
    <!-- New product modal -->
    <div class="ui tiny modal" id="newProductModal">
        <div class="header"><?php echo e(trans('products.modal.header.new')); ?></div>
        <div class="scrolling content">
            <p><?php echo e(trans('products.modal.instruction.new')); ?></p>
            <form action="<?php echo e(action('ProductController@add')); ?>" method="POST">
                <?php echo e(csrf_field()); ?>

                <div class="ui form">
                    <div class="field required">
                        <label><?php echo e(trans('products.input.label.name')); ?></label>
                        <input type="text" maxlength="64" minlength="3" name="name" placeholder="<?php echo e(trans('products.input.placeholder.name')); ?>" required value="<?php echo e(old('name')); ?>">
                    </div>
                    <div class="field required">
                        <label><?php echo e(trans('products.input.label.insurer')); ?></label>
                        <input type="text" maxlength="64" minlength="3" name="insurer" placeholder="<?php echo e(trans('products.input.placeholder.insurer')); ?>" required value="<?php echo e(old('insurer')); ?>">
                    </div>
                    <div class="field required">
                        <label><?php echo e(trans('products.input.label.company')); ?></label>
                        <select class="ui fluid search dropdown" name="company_id">
                            <?php foreach($companies as $company): ?>
                            <option<?php echo e(!empty(old('company_id')) && old('company_id') === $company->id || empty(old('company_id')) && $company === $user->company ? ' selected' : ''); ?> value="<?php echo e($company->id); ?>"><?php echo e($company->name); ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                </div>
            </form>
            <?php foreach($companies as $company): ?>
            <div id="company<?php echo e($company->id); ?>ProductCategoryTemplate" style="display:none;">
                <div class="company company-<?php echo e($company->id); ?> field required">
                    <label><?php echo e(trans('products.input.label.category')); ?></label>
                    <select class="dropdown fluid search ui" name="category">
                        <?php $__empty_1 = true; foreach($company->product_categories as $category): $__empty_1 = false; ?>
                        <option<?php echo e(!empty(old('category')) && old('category') === $category || empty(old('category')) && $category === $company->product_categories->first() ? ' selected' : ''); ?> value="<?php echo e($category); ?>"><?php echo e($category); ?></option>
                        <?php endforeach; if ($__empty_1): ?>
                        <option disabled value=""><?php echo e(trans('products.input.option.category')); ?></option>
                        <?php endif; ?>
                    </select>
                </div>
                <div class="company company-<?php echo e($company->id); ?> field required">
                    <label><?php echo e(trans('products.input.label.sub_category')); ?></label>
                    <select class="dropdown fluid search ui" name="sub_category">
                        <?php $__empty_1 = true; foreach($company->product_sub_categories as $sub_category): $__empty_1 = false; ?>
                        <option<?php echo e(!empty(old('sub_category')) && old('sub_category') === $sub_category || empty(old('sub_category')) && $sub_category === $company->product_sub_categories->first() ? ' selected' : ''); ?> value="<?php echo e($sub_category); ?>"><?php echo e($sub_category); ?></option>
                        <?php endforeach; if ($__empty_1): ?>
                        <option disabled value=""><?php echo e(trans('products.input.option.sub_category')); ?></option>
                        <?php endif; ?>
                    </select>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
        <div class="actions">
            <div class="ui buttons">
                <button class="ui cancel button"><?php echo e(trans('products.modal.button.cancel.new')); ?></button>
                <div class="or" data-text="<?php echo e(trans('products.modal.button.or')); ?>"></div>
                <button class="ui positive primary button"><?php echo e(trans('products.modal.button.confirm.new')); ?></button>
            </div>
        </div>
    </div>
    <!-- Edit product modals -->
    <?php foreach($products as $product): ?>

    <div class="ui tiny modal" id="editProduct<?php echo e($product->id); ?>Modal">
        <div class="header"><?php echo e(trans('products.modal.header.edit')); ?></div>
        <div class="scrolling content">
            <p><?php echo e(trans('products.modal.instruction.edit')); ?></p>
            <form action="<?php echo e(action('ProductController@edit', $product->id)); ?>" method="POST">
                <?php echo e(csrf_field()); ?>

                <div class="ui form">
                    <div class="field required">
                        <label><?php echo e(trans('products.input.label.name')); ?></label>
                        <input type="text" maxlength="64" minlength="3" name="name" placeholder="<?php echo e(trans('products.input.placeholder.name')); ?>" required value="<?php echo e($product->name); ?>">
                    </div>
                    <div class="field required">
                        <label><?php echo e(trans('products.input.label.insurer')); ?></label>
                        <input type="text" maxlength="64" minlength="3" name="insurer" placeholder="<?php echo e(trans('products.input.placeholder.insurer')); ?>" required value="<?php echo e($product->insurer); ?>">
                    </div>
                    <div class="field required">
                        <label><?php echo e(trans('products.input.label.company')); ?></label>
                        <select class="ui fluid search dropdown" name="company_id" required>
                            <?php foreach($companies as $company): ?>
                            <option<?php echo e($company->id === $product->company_id ? ' selected' : ''); ?> value="<?php echo e($company->id); ?>"><?php echo e($company->name); ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                </div>
            </form>
            <?php foreach($companies as $company): ?>
            <div id="company<?php echo e($company->id); ?>ProductCategoryTemplate" style="display:none;">
                <div class="company company-<?php echo e($company->id); ?> field required">
                    <label><?php echo e(trans('products.input.label.category')); ?></label>
                    <select class="dropdown fluid search ui" name="category">
                        <?php $__empty_1 = true; foreach($company->product_categories as $category): $__empty_1 = false; ?>
                        <option<?php echo e($category === $product->category || (!$company->product_categories->contains($product->category) && $category === $company->product_categories->first()) ? ' selected' : ''); ?> value="<?php echo e($category); ?>"><?php echo e($category); ?></option>
                        <?php endforeach; if ($__empty_1): ?>
                        <option disabled value=""><?php echo e(trans('products.input.option.category')); ?></option>
                        <?php endif; ?>
                    </select>
                </div>
                <div class="company company-<?php echo e($company->id); ?> field required">
                    <label><?php echo e(trans('products.input.label.sub_category')); ?></label>
                    <select class="dropdown fluid search ui" name="sub_category">
                        <?php $__empty_1 = true; foreach($company->product_sub_categories as $sub_category): $__empty_1 = false; ?>
                        <option<?php echo e($sub_category === $product->sub_category || (!$company->product_sub_categories->contains($product->sub_category) && $sub_category === $company->product_sub_categories->first()) ? ' selected' : ''); ?> value="<?php echo e($sub_category); ?>"><?php echo e($sub_category); ?></option>
                        <?php endforeach; if ($__empty_1): ?>
                        <option disabled value=""><?php echo e(trans('products.input.option.sub_category')); ?></option>
                        <?php endif; ?>
                    </select>
                </div>
            </div>
            <?php endforeach; ?>
        </div>
        <div class="actions">
            <div class="ui buttons">
                <button class="ui cancel button"><?php echo e(trans('products.modal.button.cancel.edit')); ?></button>
                <div class="or" data-text="<?php echo e(trans('products.modal.button.or')); ?>"></div>
                <button class="ui positive primary button"><?php echo e(trans('products.modal.button.confirm.edit')); ?></button>
            </div>
        </div>
    </div>
    <?php endforeach; ?>
<?php $__env->stopSection(); ?>

<?php $__env->startSection('extra_scripts'); ?>
    <script type="text/javascript">
        (function($insura, $) {
            $(document).ready(function() {
                $('div.modal select[name="company_id"]').change(function() {
                    var element = $(this);
                    var parentModal = element.parents('div.modal:first');
                    var companyElementsHTML = parentModal.find('div#company' + element.val() + 'ProductCategoryTemplate').html();
                    parentModal.find('form div.company').remove();
                    parentModal.find('form div.field').last().after(companyElementsHTML);
                    $insura.helpers.initDropdown(parentModal.find('form div.company div.dropdown'));
                    $insura.helpers.requireDropdownFields(parentModal.find('form div.required div.dropdown input[type="hidden"]'));
                }).change();
            });
        })(window.insura, window.jQuery);
    </script>
<?php $__env->stopSection(); ?>
<?php echo $__env->make('templates.products', array_except(get_defined_vars(), array('__data', '__path')))->render(); ?>