<input type="hidden" class="form-control" id="id_invoice_dropship" name="id_invoice"
    value="<?php echo encrypt_url($id); ?>">
<input type="hidden" class="form-control" id="id_dropship" name="id_dropship">
<div class="form-group">
    <label>Nama Toko/No.Telp/Pemilik Toko</label>
    <div class="input-group">
        <input class="form-control" id="cari_tlp_dropship" name="cari_tlp_dropship" autofocus="autofocus" required>
        <div class="input-group-append">
            <button class="btn btn-outline-secondary clear_button" id="clear_button" type="button" disabled>X</button>
        </div>
    </div>

    <p style="color:red" id="error_caritlp"></p>
</div>
<div class="form-group tampil_form_dropship">
    <label>Nama Toko</label>
    <input class="form-control" id="nama_cari_dropship" name="nama_cari_dropship" readonly>
</div>
<div class="form-group tampil_form_dropship">
    <label>Alamat Toko</label>
    <input class="form-control" id="alamat_cari_dropship" name="alamat_cari_dropship" readonly>
</div>

<script>
$(function() {
    $('#cari_tlp_dropship').keyup(function() {
        $('.tampil_form_dropship').hide();

        if (event.keyCode == 13) {
            $('.tampil_form_dropship').show();
        }
    })
    $("#cari_tlp_dropship").focus();
    $('.tampil_form_dropship').hide();
});
$("#clear_button").click(function() {
    $("#cari_tlp_dropship").focus();
    $('.tampil_form_dropship').hide();

    $('#cari_tlp_dropship').val('');
    $('#id_dropship').val('');

    $('#nama_cari_dropship').val('');
    $('#alamat_cari_dropship').val('');

    $('#cari_tlp_dropship').prop('readonly', false);
    $('#btn-simpan-dropship').prop('disabled', true);
});
$('#cari_tlp_dropship').autocomplete({
    source: function(request, response) {
        $.ajax({
            url: base_url + 'dropshiper/ajax_cari',
            dataType: "json",
            method: 'post',
            data: {
                name_startsWith: request.term,
                type: 'konsumen_cari',
                row_num: 1
            },
            success: function(data) {
                response($.map(data, function(item) {
                    var code = item.split("|");
                    return {
                        label: code[0],
                        value: code[0],
                        data: item
                    }
                }));
            }
        });
    },

    autoFocus: true,
    minLength: 1,
    select: function(event, ui) {
        if (ui.item.data == 'Data tidak ditemukan') {

            $('#cari_tlp_dropship').val('');
            $('.tampil_form_dropship').hide();
            console.log(ui.item.data)
            return false;
        }
        var names = ui.item.data.split("|");
        $('#cari_tlp_dropship').val(names[0]);
        $('#id_dropship').val(names[1]);
        $('#nama_cari_dropship').val(names[2]);
        $('#alamat_cari_dropship').val(names[3]);
        $('.tampil_form_dropship').show();
        $('#cari_tlp_dropship').prop('readonly', true);
        $('#clear_button').prop('disabled', false);
        $('#clear_button').addClass('btn-danger text-white');
        $('#btn-simpan-dropship').prop('disabled', false);
        $("#btn-simpan-dropship").focus();
    },
    change: function(event, ui) {
        //console.log(ui.item)
        //$('.tampil_form_dropship').hide();
    }
});
</script>