/**
 * 仓库预约看板
 * view/delivery/appointment_record/show_index.ftl
 */

layui.use(["laydate", "layer", 'element', 'table'], function () {
    let $ = layui.jquery, element = layui.element, table = layui.table;
    let el = {
            tb: '#tb',
            supplierCode: "#supplierCode",
            searchForm: '#searchForm',
            appointmentDate: '#appointmentDate',
            supplierName: "#supplierName",
            refreshMinute: "#refreshMinute",
            dockerCode: "#dockerCode",
            dockerId: "#dockerId",
            factoryCode: "#factoryCode",
            bodyHeight: document.documentElement.clientHeight,
        },
        defaultDate = moment(new Date()).format('YYYY-MM-DD'),
        scanTime = $(el.refreshMinute).val(),
        oldTabs = [];
    /**
     * 初始化日期控件
     */
    layui.laydate.render({
        elem: el.appointmentDate,
        // min: 0,
        value: defaultDate,
        calendar: true,
        showBottom: false,
        done: function (value, date, endDate) {
            defaultDate = value;
            getData();
        }
    });


    $(el.dockerId).combogrid({
        panelWidth: 450,
        idField: 'id',
        textField: 'dockerName',
        prompt: '-- 请选择 --',
        editable: false,
        url: g.ctx+'pc/order/appointmentDocker/rest/listAll',
        columns: [[
            {field:'dockerId',hidden:true},
            {field: 'dockerCode', title: '卸货点编码', width: 60},
            {field: 'dockerName', title: '卸货点名称', width: 100},
            {field: 'factoryCode', title: '工厂编码', width: 100},
            {field: 'factoryName', title: '工厂名称', width: 200}
        ]],
        onSelect: function (value) {
            var g = $(el.dockerId).combogrid('grid');
            var r = g.datagrid('getSelected');
            $(el.dockerCode).val(r.dockerCode);
            $(el.factoryCode).val(r.factoryCode);
            getData();
        }
    });

    /**
     * 页面初始加载
     */
    if (scanTime) {
        getData();
        window.setInterval(function () {
            getData();
        }, scanTime * 1000 * 60);//5*1000*60 分钟为单位
    } else {
        getData();
    }

    /**
     * 搜索
     */
    $('#search_btn').on('click', function () {
        getData();
    });

    /**
     * 重置
     */
    $('#reset_btn').on('click', function () {
        $(el.supplierCode).val('');
        defaultDate = moment(new Date()).format('YYYY-MM-DD');
        $(el.appointmentDate).val(defaultDate);
        getData();
    });
    /**
     * 获取数据
     */
    function getData() {
        $.ajax({
            url: g.ctx+'pc/order/appointmentRecord/rest/appointmentShow',
            type: "POST",
            data: {
                appointmentDate: defaultDate,
                supplierCode: $(el.supplierCode).val(),
                dockerCode: $(el.dockerCode).val(),
                factoryCode: $(el.factoryCode).val()
            },
            success: function (data) {
                generateView(data);
            },
            error: function (data) {
                layer.alert(data.responseJSON.message, {icon: 2});
            }
        });
    }

    /**
     * 动态生成tab页面
     * @param data
     */
    function generateView(data) {
        if (oldTabs.length > 0) {
            layui.each(oldTabs, function (index, id) {
                element.tabDelete('test', id); //删除 lay-id="xxx" 的这一项
            });
        }

        $('#dateTotalCount').text(data.appointmentCount);
        $('#dateSupplieCount').text(data.supplierCount);

        oldTabs = [];
        data = data.dtos;
        layui.each(data, function (index, obj) {
                oldTabs.push(obj.appointTimeId);
                var content =
                    // '<div style="height: 40px;line-height: 40px">\n' +
                    // '        <div class="head_title">\n' +
                    // '            <span style="font-weight: bold;">预约合计数:<span id="bt_appoint' + obj.appointTimeId + '"></span> </span>\n' +
                    // '            <span style="margin-left: 20px;font-weight: bold;">剩余预约数:<span id="bt_appoint_over' + obj.appointTimeId + '"></span></span>\n' +
                    // '        </div>\n' +
                    // '    </div>' +
                    '<table  id="bt_' + obj.appointTimeId + '"></table>';
                element.tabAdd('test', {
                    title: obj.startTime + "-" + obj.endTime,
                    content: content,
                    id: obj.appointTimeId,
                });

                table.render({
                    elem: '#bt_' + obj.appointTimeId
                    , height: 'full-200'
                    , even: true //开启隔行背景
                    , toolbar: false
                    , page: false
                    , cellMinWidth: 80
                    , limit: obj.items.length //显示的数量
                    , data: obj.items //数据接口
                    , cols: [[ //表头
                        {field: 'id', title: 'ID', sort: true, fixed: 'left', type: 'numbers'}
                        , {field: 'supplierCode',width:130, title: '供应商编码',}
                        , {field: 'supplierName',width:220, title: '供应商名称',}
                        // , {field: 'appointNum', title: '预约数量',}
                        , {field: 'appointCreateTime', title: '创建时间',}
                        , {
                            field: 'urgency', title: '处理状态', templet: function (d) {
                                return '<span style="">' + (Number(d['urgency']) === 1 ? '已收货' : '未收货') + '</span>'
                            }
                        }
                        , {field: 'factoryName', title: '工厂名称',}
                        , {field: 'factoryCode', title: '工厂编码',}
                    ]]
                });

                $('#bt_appoint' + obj.appointTimeId).text(obj.allUseCount);
                $('#bt_appoint_over' + obj.appointTimeId).text(obj.canUse);

            }
        );
        layui.each(data, function (index, obj) {
            if (obj.selectOrder === 1) {
                element.tabChange('test', obj.appointTimeId);
            }
        });
    }

    element.init('tab', 'test');
});




