{% load i18n %}


<script type="text/javascript">
    $(document).ready(function () {
        var $dates = $('.datepicker2').datepicker();

        $('#clear-dates').on('click', function () {
            $dates.datepicker('setDate', null);
        });
        $('.datepicker2').datepicker({dateFormat: 'yy-mm-dd', showButtonPanel: true, clearBtn: true});
        setDatePickerZh();

        $('#company_old_name').autocomplete({
            source: {{company|safe}},
            select: function (event, ui) {
                $("#company_old_id").val(ui.item.value);
                $("#company_old_name").val(ui.item.label);
                return false;
            }
        });


        $('#company_old_id').val('0');
        $('#company_new_id').val('0');
        $('#product_id').val('0');
        $('#order_number').val('');

        $('#product_name').autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: '/obj/ajax/autocomplete/product/',
                    dataType: "json",
                    data: {
                        term: $("#product_name").val(),//搜索栏里的内容
                        company_id:{{request.user.company_id}} //额外参数
                    },
                    success: function (data) {
                        response(data);
                    }
                });
            },
            select: function (event, ui) {
                $("#product_id").val(ui.item.value);
                $("#product_name").val(ui.item.label);
                return false;
            }
        });


        $('#company_new_name').autocomplete({
            source: {{company|safe}},
            select: function (event, ui) {
                $("#company_new_id").val(ui.item.value);
                $("#company_new_name").val(ui.item.label);
                return false;
            }
        });

        $('.btn-transfer').click(function () {
            var id_n = $('#company_new_id').val();
            var id_o = $('#company_old_id').val();
            if (id_n == '0' || id_o == '0') {
                toastr.error(
                    "请先选择要转出和转入库存的经销商！",
                    "操作错误：",
                    {timeOut: 1000, positionClass: "toast-center-center"}
                );

            } else {
                var title = "{% trans  '转移经销商库存' %}";
                var content = "您确定要将 <span style='color:red;font-weight:bold;'>" +
                    $('#company_old_name').val() + "</span>" +
                    " 下的库存转移到 " + "<span style='color:green;font-weight:bold;'>" +
                    $('#company_new_name').val() + "</span> 吗?";
                $.OpenCfmDlg(title, content);

            }
        });
    });
    $('#confirm-modal .btn-confirm').click(function () {
        var id_n = $('#company_new_id').val();
        var id_o = $('#company_old_id').val();
        var order_number = $('#order_number_name').val();
        var product_id = $('#product_id').val();
        var date_fr = $('#dt_fr2').val();
        var date_to = $('#dt_to2').val();
        $('#data-content .btn').hide();

        $.ajax({
            type: 'POST',
            data: {
                order_number: order_number,
                product_id: product_id,
                company_id_old: id_o,
                company_id_new: id_n,
                dt_fr: date_fr,
                dt_to: date_to,
                csrfmiddlewaretoken: '{{ csrf_token }}'
            },
            url: '{{request.path}}',
            success: function (res) {
                $("#confirm-modal").modal('hide');
                $('#data-content .btn').show();
                toastr.success(
                    "经销商库存转移完成(" + res.cnt + ")！",
                    "操作成功：",
                    {timeOut: 1200, positionClass: "toast-center-center"}
                );
                return;
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $("#confirm-modal").modal('hide');
                $('#data-content .btn').show();
                toastr.error(
                    "连接服务器失败！",
                    "网络错误：",
                    {timeOut: 1000, positionClass: "toast-center-center"}
                );
                return;
            }
        });
    });
</script>


<ol class="breadcrumb">
    <li>{% trans "物流工具" %}</li>
    <li>{% trans "经销商库存转移" %}</li>
</ol>


<div class='x_panel'>
    <br/>
    <div class="x_content">
        <div id="obj_form" class="">
            <div> {% csrf_token %}

                <div class="row">

                    <div class="col-md-8 ">
                        <div class="input-group">

                            <span class="input-group-addon">{% trans "开始日期" %}</span>
                            <input class="datepicker2 form-control" id="dt_fr2"/>

                            <span class="input-group-addon">{% trans "结束日期" %}</span>
                            <input class="datepicker2 form-control" id="dt_to2"/>

                        </div>

                    </div>
                    <div class="col-md-1">
                        <input type="button" value="清空日期" class="form-control btn btn-primary" id="clear-dates" width="60">
                    </div>
                </div>

                <div class="col-md-12 form-group has-feedback">
                    <input id='order_number' type='hidden' name='order_number'/>
                    <input type="text" class="form-control has-feedback-left" id="order_number_name"
                           placeholder='{% trans "订单号" %}'/>
                    <span class="fa fa-plus-circle form-control-feedback left" aria-hidden="true"></span>
                </div>
                <div class="col-md-12 form-group has-feedback">
                    <input id='product_id' type='hidden' name='product_id'/>
                    <input type="text" class="form-control has-feedback-left" id="product_name"
                           placeholder='{% trans "产品名称" %}'/>
                    <span class="fa fa-plus-circle form-control-feedback left" aria-hidden="true"></span>
                </div>
                <div class="col-md-12 form-group has-feedback">
                    <input id='company_old_id' type='hidden' name='company_old_id'/>
                    <input type="text" class="form-control has-feedback-left" id="company_old_name"
                           placeholder='{% trans "要转出库存的经销商名称" %}'/>
                    <span class="fa fa-minus-circle form-control-feedback left" aria-hidden="true"></span>
                </div>

                <div class="col-md-12 form-group has-feedback">
                    <input id='company_new_id' type='hidden' name='company_new_id'/>
                    <input type="text" class="form-control has-feedback-left" id="company_new_name"
                           placeholder='{% trans "要转入库存的经销商名称" %}'/>
                    <span class="fa fa-plus-circle form-control-feedback left" aria-hidden="true"></span>
                </div>


                <br/>
                <div class="col-md-12 form-group ">
                    <span class="btn btn-primary btn-transfer">{% trans "转移经销商库存" %}</span>
                </div>
                </form>

            </div>
        </div>
    </div>











