<form method="post" action="{{ request()->url() }}" id="input_form">
	@csrf
    <div class="row">
        @if ($target->id == null)
       <div class="form-group col-lg-12">
            <label>Pengguna <text class="text-danger">*</text></label>
            <select class="form-control" name="user_id" data-toggle="select2">
                <option value="" selected>Pilih...</option>
                @foreach($users as $item)
                    <option value="{{ $item->id }}" {{ old('user_id') ? (old('user_id') == $item->id ? 'selected' : '') : ($target->user_id == $item->id ? 'selected' : '')  }}>{{ $item->username.' ('.$item->full_name.')' }}</option>
                @endforeach
            </select>
            <small class="text-danger user_id_error"></small>
        </div>
        @else
        <div class="form-group col-lg-12">
            <label>Pengguna <text class="text-danger">*</text></label>
            <select class="form-control" name="user_id" data-toggle="select2">
                <option value="" selected>Pilih...</option>
                @foreach($users as $item)
                    <option value="{{ $item->id }}" {{ old('user_id') ? (old('user_id') == $item->id ? 'selected' : '') : ($target->user_id == $item->id ? 'selected' : '')  }}>{{ $item->username.' ('.$item->full_name.')' }}</option>
                @endforeach
            </select>
            <small class="text-danger user_id_error"></small>
        </div>
        @endif
        <div class="form-group col-lg-6">
            <label>Kategori <text class="text-danger">*</text></label>
            <select class="form-control" name="category_id" id="category_id" data-toggle="select2">
                <option value="" selected>Pilih...</option>
                @foreach($categories as $item)
                    @if ($item->category == true)
                    <option value="{{ $item->category->id }}" {{ $target->id <> null ? ($target->service->category->id == $item->category->id ? 'selected' : '') : ''  }}>{{ $item->category->name }}</option>
                    @endif
                @endforeach
            </select>
            <small class="text-danger category_id_error"></small>
        </div>
        <div class="form-group col-lg-6">
            <label>Layanan <text class="text-danger">*</text></label>
            <select class="form-control" name="service_id" id="service_id" data-toggle="select2">
                <option value="" selected>Pilih Kategori...</option>
                @foreach($services as $item)
                    <option value="{{ $item->id }}" {{ $target->service_id <> null ? ($target->service_id == $item->id ? 'selected' : '') : ''  }}>{{ $item->name.' (Rp '.number_format($item->price,0,',','.').'/K)' }}</option>
                @endforeach
            </select>
            <small class="text-danger service_id_error"></small>
        </div>
    </div>
    <hr />
    <div class="row">
        <div class="form-group col-lg-6">
            <label>Harga/K <text class="text-danger">*</text></label>
            <input type="text" class="form-control autonumeric-currency" name="price" value="{{ old('price') ?? $target->price }}">
            <small class="text-danger price_error"></small>
        </div>
        <div class="form-group col-lg-6">
            <label>Keuntungan/K <text class="text-danger">*</text></label>
            <input type="text" class="form-control autonumeric-currency" name="profit" value="{{ old('profit') ?? $target->profit }}">
            <small class="text-danger profit_error"></small>
        </div>
    </div>
    <hr />
    <div class="text-right">
        <button type="reset" class="btn btn-danger"><i class="fa fa-redo"></i> Reset</button>
        <button type="submit" class="btn btn-success"><i class="fa fa-check"></i> Submit</button>
    </div>
</form>
<script src="{{ asset('assets/js/modal-form.js') }}"></script>
<script src="{{ asset('assets/js/app.js') }}"></script>
<script>
    // $('[data-toggle="select2-multiple"]').select2();
    $(document).ready(function() {
        $('#category_id').on('change', function() {
			var category_id = $('#category_id').val();
			$.ajax({
				type: "GET",
				url: "{{ url('ajax/service/list') }}",
				data: {
					type    : 'general',
					category: $('#category_id').val(),
				},
				dataType: "json",
				success: function(result) {
					$('#service_id').html(result.data);
				}, error: function() {
					swal.fire("Gagal!", "Terjadi kesalahan.", "error");
				}
			});
		});
        $('.autonumeric-currency').autoNumeric('init', {
            mDec  : '0',
            aSep  : '.',
            aDec  : ',',
            aSign : 'Rp ',
        });
        $('.autonumeric-quantity').autoNumeric('init', {
            mDec  : '0',
            aSep  : '.',
            aDec  : ',',
            aSign : '# ',
        });
    });
</script>