/**
 * jiw
 * 2019.08.23
 * 标签补打
 */

 import { UPCLOUD_RDC_TMS_MAINDATA_SERVICE,UPCLOUD_RDC_TMS_BUSINESS_SERVICE,UPCLOUD_RDC_TMS_INTERFACE_SERVICE} from '@/config/serviceSettings.js';
 import { getSelectGJ, querySysPicture } from '@/utils/prolog.ajax.js';
 export default () => {


    var rsp_billtype = [];
    var grid = null;
    var param = { "pageNum": 1, "pageSize": 10, "startDate": Prolog.formatDate(new Date()), "endDate": Prolog.formatDate(new Date()) };

    Prolog.ajax({
        url: "/sys/refdata/dtl/dd-list/get",
        type: "post",
        contentType: "application/x-www-form-urlencoded",
        data: { "objectcode": "billtype", "groupcode1": "Do" },
        async: false,
        success: function(data) {
            if (data.success) {
                rsp_billtype.push({ text: "", value: "" });
                for (var i = 0; i < data.data.length; i++) {
                    rsp_billtype.push({ text: data.data[i].text, value: data.data[i].value });
                }
            }
        },
        error: function() { PlgDialog.msg("接口调用失败"); }
    });

    //滑屏
    var carousel = layui.carousel;
    var car = carousel.render({
        elem: "#labelPrint_grid",
        autoplay: false,
        index: 0,
        arrow: "none",
        indicator: "none",
        anim: "fade",
        width: "100%", //设置容器宽度
        height: "100%"
    });

    function changePanel(name) {
        car.slide(name);
    }

    //返回主页面
    $("#labelPrint_btn_back").click(function() {
        changePanel("labelPrint_grid_01");
        return false;
    });

    let shop_Table = {
        columns: [
            { id: "companyid", name: "id", type: "ro", width: 100, hidden: true },
            { id: "companycode", name: "门店编码", type: "ro", width: 120 },
            { id: "locname", name: "门店名称", type: "ro", width: "*" },
            {
                id: "status",
                name: "状态",
                type: "ro",
                width: 80,
                render: function(v) {
                    if (v == "0") return Prolog.getCircle("yellow") + "锁定";
                    else if (v == "1") return Prolog.getCircle("green") + "启用";
                }
            }
        ],
        toolbar: [
            { type: "settings", labelAlign: "left", labelWidth: 65 },
            { type: "input", name: "locname", label: "查询条件:", offsetLeft: 5, placeholder: "请输入编码/名称", width: 300 },
            { type: "newcolumn" },
            { type: "button", name: "btn_search", value: "查询" }
        ],
        multiselect: false,
        url: "/api/company/getStore",
        type: "post",
        params: JSON.stringify({ "pageNum": 1, "pageSize": 10 }),
        contentType: "application/json",
        pagebar: { type: "simple" },
        page: true,
        totalCount: "totalCount"
    };

    gridInit();

    //主页面Grid
    function gridInit() {
        var numOpts = {
            skin: "terrace",
            columns: [
                { id: "id", name: "ID", type: "ro", hidden: true, width: 100 },
                { id: "allocaNo", name: "分播单号", type: "ro", width: 150 },
                { id: "createDate", name: "单据日期", align: "left", type: "ro", width: 130 },
                { id: "consignor", name: "货主编码", align: "left", type: "ro", width: 150 },
                { id: "consignorName", name: "货主名称", align: "center", type: "ro", width: 150 },
                { id: "customerCode", name: "门店编码", align: "left", type: "ro", width: 150 },
                { id: "customerName", name: "门店名称", align: "left", type: "ro", width: 150 },
                {
                    id: "billType",
                    name: "单据类型",
                    align: "left",
                    type: "ro",
                    width: 100,
                    render: function(v) {
                        return Prolog.getNameByID(rsp_billtype, v);
                    }
                },
                { id: "custPo", name: "入库单号", type: "ro", width: 150 },
                { id: "dobillNo", name: "出库单号", align: "center", type: "ro", width: 150 },
                { id: "containerNo", name: "托盘条码", type: "ro", width: 150 },
                { id: "labelNo", name: "箱码", type: "ro", width: 150 },
                {
                    id: "op",
                    name: "操作",
                    type: "button",
                    align: "center",
                    width: 100,
                    render: function() {
                        return "打印^res_labelPrint_printClick";
                    }
                }
            ],
            multiselect: true,
            fixedLast: true,
            toolbar: [{
                    type: "block",
                    list: [
                        { type: "settings", inputWidth: 180, width: "*", labelWidth: 60, position: "label-left", labelAlign: "right" },
                        { type: "calendar", name: "startDate", dateFormat: "%Y-%m-%d", label: "单据日期:", placeholder: "请输入或选择", width: 110, value: Prolog.formatDate(new Date()) },
                        { type: "newcolumn" },
                        { type: "calendar", name: "endDate", dateFormat: "%Y-%m-%d", label: "", placeholder: "请输入或选择", width: 110, labelWidth: 8, value: Prolog.formatDate(new Date()) },
                        { type: "newcolumn", offset: 20 },
                        {
                            type: "selectTable",
                            name: "stores",
                            width: 180,
                            label: "门店：",
                            placeholder: "请选择门店",
                            readonly: true,
                            table: shop_Table,
                            tableWidth: 550,
                            tableHeight: 500,
                            fieldText: "locname",
                            fieldValue: ["companyid", "companycode", "locname"],
                            listener: {
                                "init": function(sgrid) {
                                    sgrid.attachToolBarEvent("onButtonClick", function(name) {
                                        if (name === "btn_search") {
                                            var p = sgrid.getToolBarForm().getFormData();
                                            sgrid.setParams(JSON.stringify(p));
                                            sgrid.reload();
                                        }
                                    });
                                }
                            }
                        },
                        { type: "newcolumn", offset: 20 },
                        { type: "input", name: "order", label: "订单信息:", placeholder: "请输入入库单号/出库单号" },
                        { type: "newcolumn", offset: 20 },
                        { type: "combo", name: "orderType", label: "单据类型:", placeholder: "请选择", options: rsp_billtype, width: 180, readonly: true },
                        { type: "newcolumn", offset: 10 },
                        { type: "button", name: "btn_search", value: "查询" },
                        { type: "newcolumn" },
                        { type: "button", name: "btn_reset", className: " normal", value: "重置" }
                    ]
                },
                {
                    type: "block",
                    list: [
                        { type: "settings", inputWidth: 180, width: "*", labelWidth: 60, position: "label-right", labelAlign: "let" },
                        { type: "label", name: "tmp", label: "补印类型:" },
                        { type: "newcolumn" },
                        { type: "radio", name: "ptype", value: "2", label: "按箱码补打", checked: true },
                        { type: "newcolumn" },
                        { type: "radio", name: "ptype", value: "1", label: "按单补打", checked: false, disabled: true }
                    ]
                }
            ],
            pagebar: {
                buttons: [
                    { text: "批量打印", className: "", name: "btn_print" }
                ],
                type: "full"
            },

            url: "/straight/straightpobill/getPrintList/v1.0",
            type: "post",
            params: JSON.stringify(param),
            contentType: "application/json",
            page: true,
            totalCount: "totalCount"
        };
        //分页参数 pageSize pageNum
        grid = new PlgGrid(numOpts);
        grid.renderTo("labelPrint_grid_01");
        grid.loadData();

        //工具栏按钮事件
        grid.attachToolBarEvent("onButtonClick", function(name) {
            // 查询
            if (name === "btn_search") {
                let p = grid.getToolBarForm().getFormData();
                p.startDate = Prolog.formatDate(p.startDate);
                p.endDate = Prolog.formatDate(p.endDate);
                if (p.stores != "" && p.stores != null) {
                    let arr = JSON.parse(p.stores);
                    if (arr.length > 0)
                        p.store = arr[0].companycode;
                    else
                        p.store = "";
                }
                grid.setParams(JSON.stringify(p));
                grid.reload();
            }
 else if (name == "btn_reset") {
                let frm = grid.getToolBarForm();
                frm.clear();
                frm.setFormData({ "btype": "2", "startDate": Prolog.formatDate(new Date()), "endDate": Prolog.formatDate(new Date()) });
            }
        });

        //分页栏按钮事件
        grid.attachPageBarEvent(function(name) {
            if (name == "btn_print") {
                print();
            }
        });
    }

    //打印采
    function print() {
        //加载等待
        let pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });

        let rid = grid.getCheckedIds();
        if (rid == "") rid = grid.getSelectedRowId();
        if (rid == null || rid == "") {
            PlgDialog.msg("请选择要打印的数据");
            PlgDialog.close(pload);
            return;
        }
        let ids = rid.split(",");
        let par = [];
        ids.forEach(t => {
            let rd = grid.getRowData(t);
            par.push({
                "rcvContainerno": rd.rcvContainer,
                "customercode": rd.customerCode,
                "customername": rd.customerName,
                "consignorname": rd.consignorName,
                "billtype": rd.billType,
                "itemid": rd.itemid,
                "barList": [rd.dobillNo]
            });
        });

        Prolog.ajax({
            url: "/straight/straightpobill/printCaseCodeList/v1.0",
            type: "post",
            contentType: "application/json",
            data: JSON.stringify(par),
            success: function(data) {
                if (data.success) {
                    PrintService("", data.data, function(data) {
                        PlgDialog.close(pload);
                        if (data == null) {
                            PlgDialog.alert("调用打印服务失败");
                            return;
                        }
                        let rsp;
                        for (let i in data.data[0]) {
                            rsp = data.data[0][i];
                        }
                        setTimeout(() => { PlgDialog.msg(rsp.message == null ? "接口调用失败" : rsp.message); }, 300);
                        return;
                    });
                }
 else {
                    setTimeout(() => { PlgDialog.alert(data.message == null ? "接口调用失败" : data.message); }, 300);
                    PlgDialog.close(pload);
                }
            },
            error: function() {
                PlgDialog.close(pload);
                PlgDialog.msg("接口调用失败");
            }
        });
    }

    window.res_labelPrint_printClick = function(rid) {
        grid.selectRowById(rid);
        print();
        return false;
    };
}