/**
* guoj
* 2020.10.13
* 容器绑定
*/
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 () => {
    let saveItem = "";
    let dataSelect;
    //获取下拉框
    getSelectGJ([{ "objectcode": "TMS_SHIP_BILLTYPE" }], function (data) {
        dataSelect = data.data;
    });
    var opts = {
        //terrace显示全部，card分页显示
        skin: "card",
        //renderer:"grid",
        columns: [
            { id: "lineid", name: "id", type: "ro", width: "100", hidden: true },
            { id: "branchName", name: "配送中心", type: "ro", width: 100 },
            { id: "containerCode", name: "容器编号", type: "ro", width: 130 },
            {
                id: "containerName", name: "容器名称", type: "ro", width: 100
            },
            { id: "containerno", name: "包裹号", type: "ro", width: 100 },
            { id: "shipbillno", name: "运单号", type: "ro", width: 100 },
            {
                id: "shipbilltype", name: "运单类型", type: "ro", width: 100, render: (v) => {
                    return getSelectValue(dataSelect.TMS_SHIP_BILLTYPE, v);
                }
            },
            { id: "billdate", name: "单据时间", type: "ro", width: 130 },
            { id: "refbillno", name: "业务单号", type: "ro", width: 110 },
            { id: "shiptoCustomerCode", name: "客户编号", type: "ro", width: 110 },
            {
                id: "shiptoCustomerName", name: "客户名称", type: "ro", width: 110
            }
        ],
        multiselect: true,
            url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/containerBind/queryContainerRecord",//数据接口
        type: "post",//数据提交方式，默认为get
        contentType: "application/json",
        params: "{}",//查询提交参数，分页参数默认为pageNum,pageSize
        page: true, //是否启用分页
        totalCount: "totalCount" //总记录数字段，默认totalCount
    };
    //分页参数 pageSize pageNum
    var grid = new PlgGrid(opts);
    grid.renderTo("containerBinding_bottom");
    grid.loadData();
    //重置
    $("#contain_clear").on("click", () => {
        $("#contain_conner").val("");
        $("#contain_scanning").val("");
        $("#contain_conner").focus();
    });
    //删除
    $("#contain_delete").on("click", () => {
        let data = getDeleteAll(grid);
        if (data == "" || data == null) {
            return;
        }
        let dataList = [];
        data.forEach(v => {
            dataList.push(grid.getRowData(v));
        });
        utilDelete(UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/containerBind/containerNotBind", "post", JSON.stringify(dataList), ()=>{
            layer.msg("成功");
            grid.reload();
        }, "application/json");
    });
    //扫描容器号
    $("#contain_conner")[0].onkeydown = (e) => {
        if (e.key == "Enter") {
            saveItem = "";
            let conter = $("#contain_conner").val();
            if(conter == "" || conter == null){
                $("#contain_conner").focus();
                layer.msg("请输入容器号");
                return;
            }
            utilAjaxGJ(UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/tBaseTransportContainer/queryUsableContainer", "post", JSON.stringify({ "containerCode": conter, pageNum: 1, pageSize: 10 }), (data) => {
                if(data.data.length > 0){
                    saveItem = data.data[0];
                    if(saveItem.status != "01" && saveItem.status != "02"){
                        $("#contain_conner").val("");
                        $("#contain_conner").focus();
                        $("#textAlien_contain").text("当前容器无法绑定");
                        return;
                    }
                    $("#textAlien_contain").text("扫描容器号成功");
                    $("#contain_scanning").focus();
                }
                else{
                    $("#contain_conner").val("");
                    $("#contain_conner").focus();
                    $("#textAlien_contain").text("容器号不存在");
                }
            }, "application/json");
        }
    };
    //扫描扫描号
    $("#contain_scanning")[0].onkeydown = (e) => {
        if (e.key == "Enter") {
            let conter = $("#contain_scanning").val();
            if(saveItem == "" || saveItem == null){
                $("#contain_conner").val("");
                $("#contain_scanning").val("");
                $("#contain_conner").focus();
                layer.msg("请扫描容器号");
                return;
            }
            if(conter == "" || conter == null){
                $("#contain_scanning").val("");
                $("#contain_scanning").focus();
                layer.msg("请输入扫描号");
                return;
            }

            saveItem.containerno = conter;
            utilAjaxGJ(UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/containerBind/containerBind", "post", JSON.stringify(saveItem), ()=>{
                saveItem = "";
                $("#contain_scanning").val("");
                $("#contain_conner").val("");
                $("#textAlien_contain").text("绑定成功");
                grid.reload();
                $("#contain_conner").focus();
            }, "application/json");

        }
    };
    setTimeout(() => {
        $("#contain_conner").focus();
    }, 100);
};
