/**
 * guoj
 * 2020.05.25
 * 站点司机关系
    */
    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 dataSelect;
    //获取下拉框
    getSelectGJ([{ "objectcode": "SCHE_STATUS" }], function (data) {
        dataSelect = data.data;
    });
    let fromDataJson;
    var grid;
    //配送中心table
    var userTable = {
        columns: [
            { id: "companyid", name: "id", type: "ro", width: "130", hidden: true },
            { id: "companycode", name: "组织编号", type: "ro" },
            { id: "companyname", name: "组织名称", type: "ro" }
        ],
        multiselect: false,
        pagebar: { type: "simple" },

        url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/pubCompany/queryCompanyBranch", //数据接口
        type: "post", //数据提交方式，默认为get
        contentType: "application/json",
        params: JSON.stringify({"companytype": "S"}), //查询提交参数，分页参数默认为pageNum,pageSize
        page: true, //是否启用分页
        totalCount: "totalCount" //总记录数字段，默认totalCount
    };
    //司机table
    var userTable2 = {
        columns: [
            { id: "id", name: "id", type: "ro", width: "130", hidden: true },
            { id: "userId", name: "司机编号", type: "ro" },
            { id: "userName", name: "司机名称", type: "ro" },
            { id: "tel", name: "手机号", type: "ro" },
            { id: "card", name: "身份证号", type: "ro" }
        ],
        multiselect: false,

        url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/sysUserDuty/querySysUserDuty", //数据接口
        type: "post", //数据提交方式，默认为get
        contentType: "application/json",
        pagebar: { type: "simple" },
        params: "{}", //查询提交参数，分页参数默认为pageNum,pageSize
        page: true, //是否启用分页
        totalCount: "totalCount" //总记录数字段，默认totalCount
    };
    //车牌号table
    var userTable3 = {
        columns: [
            { id: "vehicleId", name: "id", type: "ro", width: "130", hidden: true },
            { id: "vehicleNo", name: "车辆编号", type: "ro" },
            { id: "vehicleName", name: "车辆名称", type: "ro" },
            { id: "vehicleSpecs", name: "规格编号", type: "ro" },
            { id: "vehicleSpecsName", name: "规格名称", type: "ro" }
        ],
        multiselect: false,

        url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/tBaseVehicle/queryTBaseVehicle", //数据接口
        type: "post", //数据提交方式，默认为get
        pagebar: { type: "simple" },
        contentType: "application/json",
        params: "{}", //查询提交参数，分页参数默认为pageNum,pageSize
        page: true, //是否启用分页
        totalCount: "totalCount" //总记录数字段，默认totalCount
    };
    //承运商table
    var company_Table = {
        columns: [
            { id: "carrierId", name: "id", type: "ro", width: "130", hidden: true },
            { id: "carrierCode", name: "承运商编号", type: "ro" },
            { id: "carrierName", name: "承运商名称", type: "ro" }
        ],
        multiselect: false,

        url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/tmsBaseCarrier/queryTmsBaseCarrier", //数据接口
        type: "post", //数据提交方式，默认为get
        pagebar: { type: "simple" },
        contentType: "application/json",
        params: JSON.stringify({pageNum: 1,pageSize: 500}), //查询提交参数，分页参数默认为pageNum,pageSize
        page: true, //是否启用分页
        totalCount: "totalCount" //总记录数字段，默认totalCount
    };
    var ddfrom = null;
    var opts = {
        //terrace显示全部，card分页显示
        skin: "card",
        //renderer:"grid",
        columns: [
            { id: "id", name: "id", type: "ro", width: "100", hidden: true },
            { id: "branchId", name: "配送中心/站点ID", type: "ro", width: 150 },
            { id: "branchName", name: "配送中心/站点名称", type: "ro", width: 150 },
            {
                id: "companyName", name: "承运商", type: "ro", width: 100
            },
            { id: "driverName", name: "司机", type: "ro", width: 100 },
            { id: "vehicleNo", name: "绑定车牌号", type: "ro", width: 100 },
            {
                id: "status", name: "状态", type: "ro", width: 100,render: (v)=>{
                    return getSelectValue(dataSelect.SCHE_STATUS, v);
                }
            },
            {
                id: "memo", name: "备注", type: "ro", width: 100
            },
            {
                id: "op", name: "操作", type: "button", align: "center", fixed: "right", width: 200, render: function () {
                    return "删除^driverRelationship_deleteClick,修改^driverRelationship_updataClick,复制^driverRelationship_copy,详情^driverRelationship_details";
                }
            }
        ],
        multiselect: true,
        fixedLast: true, //开启固定列，固定最后一行，倒数第二列不能隐藏
        toolbar: [
            { type: "settings", labelWidth: 90, labelAlign: "right" },
            {
                type: "row",
                list: [
                    {
                        className: "layui-col-md3",
                        type: "selectTable",
                        placeholder: "请选择配送中心/站点",
                        readonly: false,
                        table: userTable,
                        tableWidth: 500,
                        tableHeight: 400,
                        fieldText: "companyname",
                        fieldValue: "companyid",
                        name: "buyername1",
                        label: "配送中心/站点:",
                        listener: function (consignorGrid) {
                            setMethod(consignorGrid, userTable, function (data) {
                                var fromdata = grid.getToolBarForm().getFormData();
                                data = fromdata.buyername1.length == 2 ? "" : data;
                                var datad = {};
                                datad.branchId = data.companyid ? data.companyid : "";
                                grid.getToolBarForm().setFormData(datad);
                            });
                        }
                    },
                    { type: "hidden", name: "branchId", label: "配送中心/站点:", className: "layui-col-md3" },
                    {
                        className: "layui-col-md3",
                        type: "selectTable",
                        placeholder: "请选择承运商",
                        readonly: false,
                        table: company_Table,
                        tableWidth: 500,
                        tableHeight: 400,
                        fieldText: "carrierName",
                        fieldValue: "carrierId",
                        name: "vendorcode1",
                        label: "承运商:",
                        listener: function (consignorGrid) {
                            setMethod(consignorGrid, company_Table, function (data) {
                                var fromdata = grid.getToolBarForm().getFormData();
                                data = fromdata.vendorcode1.length == 2 ? "" : data;
                                var datad = {};
                                datad.vehicleNo = "";
                                datad.companyId = data.carrierId ? data.carrierId : "";
                                grid.getToolBarForm().setFormData(datad);
                                userTable3.params = JSON.stringify( { carrierId: data.carrierId });
                                grid.getToolBarForm().getDForm().setTableConfig("vendorcode2", userTable3);
                                grid.getToolBarForm().getDForm().setText("vendorcode2", "");
                            });
                        }
                    },
                    { type: "hidden", name: "companyId", label: "承运商:", className: "layui-col-md3" },
                    { type: "input",className: "layui-col-md3", name: "vehicleNo", label: "车辆:" },
                    {
                        type: "block", className: "layui-col-md2 form_btn_left",
                        list: [
                            { type: "button", className: "btn-normal", name: "querywork", value: "查询" },
                            { type: "newcolumn" },
                            { type: "button", name: "work_Reset", className: "normal", value: "重置" }
                        ]
                    },
                    {
                        type: "block", className: "layui-col-md1 right",
                        list: [
                            { type: "button", className: "btn-normal", name: "addBtn", offsetLeft: 10, value: "<i class='layui-icon layui-icon-add-1'></i>新增" }
                        ]
                    }
                ]
            }],

        url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/tBaseBranchDriver/queryTBaseBranchDriver",//数据接口
        type: "post",//数据提交方式，默认为get
        pagebar: {
            buttons: [
                { text: "删除", className: "layui-btn-danger", name: "delete" }
            ]
        },
        contentType: "application/json",
        params: "{}",//查询提交参数，分页参数默认为pageNum,pageSize
        page: true, //是否启用分页
        totalCount: "totalCount" //总记录数字段，默认totalCount
    };
    //分页参数 pageSize pageNum
     grid = new PlgGrid(opts);
    grid.renderTo("driverRelationship_guojzhuans2");
    $("#driverRelationship_guojzhuans2").addClass("layui-form");
    grid.attachEvent("onDataReady", function () {
        setTimeout(function () {
            layui.form.render();
        }, 200);
    });
    grid.loadData();

    //按钮添加方法
    grid.attachToolBarEvent("onButtonClick", function (name) {
        if (name == "querywork") {
            var fdata = grid.getToolBarForm().getFormData();
            grid.setParams(JSON.stringify(fdata));
            grid.reload();
        }
        if (name == "addBtn") {
            setView("#driverRelationship_div1");
            $("#driverRelationship_div4").html("");
            setDdfromDisabled({});
            $("#driverRelationship_title").html("新增");
        }
        if (name == "work_Reset") {
            grid.getToolBarForm().getDForm().setText("buyername1", "");
            grid.getToolBarForm().getDForm().setText("vendorcode2", "");
            grid.getToolBarForm().getDForm().setText("vendorcode1", "");
            grid.getToolBarForm().clear();
        }
    });
    //按钮添加方法
    grid.attachPageBarEvent(function (name) {
        //删除
        if (name == "delete") {
            let data = [];
            let datas = getDeleteAll(grid);
            datas.forEach(v => {
                data.push(grid.getRowData( v ));
            });
            data = JSON.stringify(data);
            utilDelete(UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/tBaseBranchDriver/deleteTBaseBranchDriver", "post", data, function (data) {
                        grid.reload();
            }, "application/json");
        }
    });
    //删除
    window.driverRelationship_deleteClick = function (rid,rdata) {
        grid.selectRowById(rid);
        rdata = JSON.stringify([rdata]);
        utilDelete(UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/tBaseBranchDriver/deleteTBaseBranchDriver", "post", rdata, function (data) {
                grid.reload();
        }, "application/json");
    };
    //修改
    window.driverRelationship_updataClick = function (rid, data) {
        $("#driverRelationship_div4").html("");
        grid.selectRowById(rid);
        setDdfromDisabled(data);
        ddfrom.getDForm().setText("buyername1", data.branchName);
        ddfrom.getDForm().setText("vendorcode1", data.companyName);
        ddfrom.getDForm().setText("buyername4", data.driverName);
        ddfrom.getDForm().setText("buyername5", data.vehicleNo);
        setView("#driverRelationship_div1");
        $("#driverRelationship_title").html("编辑");
    };
    //复制
    window.driverRelationship_copy = function (rid, data) {
        $("#driverRelationship_div4").html("");
        grid.selectRowById(rid);
        data.id = "";
        setDdfromDisabled(data);
        ddfrom.getDForm().setText("buyername1", data.branchName);
        ddfrom.getDForm().setText("vendorcode1", data.companyName);
        ddfrom.getDForm().setText("buyername4", data.driverName);
        ddfrom.getDForm().setText("buyername5", data.vehicleNo);
        setView("#driverRelationship_div1");
        $("#driverRelationship_title").html("新增");
    };
    function setDdfromDisabled(data){
        ddfrom = new PlgForm(fromDataJson);
        ddfrom.disableItem("buyername5");
        ddfrom.disableItem("buyername4");
        ddfrom.setFormData(data);
    }
    //详情
    window.driverRelationship_details = function (rid, rdata) {
        grid.selectRowById(rid);
        setView("#driverRelationship_div9");
        new PlgPanel({
            renderer: "driverRelationship_div422",
            skin: 2,
            header: {
                isShow: true,
                title: "基本信息"
            },
            defaultBody: {
                layoutCol: 3,
                valueBj: true,
                cols: [
                    [{
                        label: "配送中心/站点ID",
                        value: rdata.branchId
                    },
                    {
                        label: "配送中心/站点名称",
                        value: rdata.branchName
                    },
                    {
                        label: "承运商",
                        value: rdata.companyName
                    },
                    {
                        label: "司机",
                        value: rdata.driverName
                    }],
                    [
                        {
                            label: "绑定车牌号",
                            value: rdata.vehicleNo
                        },
                        {
                            label: "状态",
                            value: getSelectValue(dataSelect.SCHE_STATUS, rdata.status)
                        },
                        {
                            label: "备注",
                            value: rdata.memo
                        }
                    ]
                ]
            }
        });
    };
    //跳转页面
    var carousel = layui.carousel;
    carousel.render({
        elem: "#driverRelationship_div2",
        autoplay: false,
        index: 0,
        arrow: "none",
        indicator: "none",
        anim: "fade",
        width: "100%", //设置容器宽度
        height: "100%"
    });
    let viewList = ["#driverRelationship_div1","#driverRelationship_div3","#driverRelationship_div9"];
    function setView(name){
        for(let i = 0;i < viewList.length;i++){
            if(name == viewList[i]){
                $(name).css("display", "block");
            }
            else{
                $(viewList[i]).css("display", "none");
            }
        }
    }
    fromDataJson = {
        renderer: "driverRelationship_div4",
        items: [
            { type: "settings", position: "label-left", width: 300, labelWidth: 90 },
            { type: "hidden", name: "id", label: "id:", width: 230 },
            {
                width: 230,
                type: "selectTable",
                placeholder: "请选择配送中心/站点",
                readonly: false,
                table: userTable,
                tableWidth: 500,
                tableHeight: 400,
                fieldText: "companyname",
                fieldValue: "companyid",
                name: "buyername1",
                label: "配送中心/站点:<span class='dhxform_item_required'>*<span>",
                listener: function (consignorGrid) {
                    setMethod(consignorGrid, userTable, function (data) {
                        var fromdata = ddfrom.getFormData();
                        data = fromdata.buyername1.length == 2 ? "" : data;
                        var datad = {};
                        datad.branchId = data.companyid ? data.companyid : "";
                        datad.branchName = data.companyname ? data.companyname : "";
                        ddfrom.setFormData(datad);
                    });
                }
            },
            { type: "hidden", name: "branchId", label: "配送中心/站点:", width: 230},
            { type: "hidden", name: "branchName", label: "配送中心/站点:", width: 230},
            {
                width: 230,
                type: "selectTable",
                placeholder: "请选择绑定车牌号",
                readonly: false,
                table: userTable3,
                tableWidth: 500,
                tableHeight: 400,
                fieldText: "vehicleNo",
                fieldValue: "vehicleId",
                name: "buyername5",
                label: "绑定车牌号:<span class='dhxform_item_required'>*<span>",
                listener: function (consignorGrid) {
                    setMethod(consignorGrid, userTable3, function (data) {
                        var fromdata = ddfrom.getFormData();
                        data = fromdata.buyername5.length == 2 ? "" : data;
                        var datad = {};
                        datad.vehicleNo = data.vehicleNo ? data.vehicleNo : "";
                        datad.vehicleName = data.vehicleName ? data.vehicleName : "";
                        datad.vehicleSpecsNo = data.vehicleSpecs ? data.vehicleSpecs : "";
                        datad.vehicleSpecsName = data.vehicleSpecsName ? data.vehicleSpecsName : "";
                        datad.maxVol = data.maxVol ? data.maxVol : "0";
                        datad.maxWeight = data.maxWeight ? data.maxWeight : "0";
                        datad.maxPkg = data.maxPkg ? data.maxPkg : "0";
                        ddfrom.setFormData(datad);
                    });
                }
            },
            {
                type: "hidden", name: "vehicleNo", label: "绑定车牌号:", width: 230
            },
            {
                type: "hidden", name: "vehicleName", label: "车辆:", width: 230
            },
            { type: "newcolumn", offset: 10 },
            {
                width: 230,
                type: "selectTable",
                placeholder: "请选择承运商",
                readonly: false,
                table: company_Table,
                tableWidth: 500,
                tableHeight: 400,
                fieldText: "carrierName",
                fieldValue: "carrierId",
                name: "vendorcode1",
                label: "承运商:<span class='dhxform_item_required'>*<span>",
                listener: function (consignorGrid) {
                    setMethod(consignorGrid, company_Table, function (data) {
                        var fromdata = ddfrom.getFormData();
                        data = fromdata.vendorcode1.length == 2 ? "" : data;
                        var datad = {};
                        //数据赋值
                        datad.companyId = data.carrierId ? data.carrierId : "";
                        datad.companyName = data.carrierName ? data.carrierName : "";
                        datad.vehicleNo = "";
                        datad.vehicleName = "";
                        datad.driverId = "";
                        datad.driverName = "";
                        datad.card = "";
                        datad.tel = "";
                        ddfrom.setFormData(datad);
                        //司机和车辆弹出框重置
                        userTable3.params = JSON.stringify( { carrierId: data.carrierId });
                        userTable2.params = JSON.stringify( { companyId: data.carrierId });
                        ddfrom.getDForm().setTableConfig("buyername5", userTable3);
                        ddfrom.getDForm().setTableConfig("buyername4", userTable2);
                        ddfrom.getDForm().setText("buyername5", "");
                        ddfrom.getDForm().setText("buyername4", "");
                        ddfrom.enableItem("buyername5");
                        ddfrom.enableItem("buyername4");
                    });
                }
            },
            {
                type: "hidden", name: "companyId", label: "承运商:", width: 230
            },
            {
                type: "hidden", name: "companyName", label: "承运商名称:", width: 230
            },
            {
                type: "combo", name: "status", label: "状态:", width: 230, options: dataSelect.SCHE_STATUS, required: true
            },
            { type: "newcolumn", offset: 10 },
            {
                width: 230,
                type: "selectTable",
                placeholder: "请选择司机",
                readonly: false,
                table: userTable2,
                tableWidth: 500,
                tableHeight: 400,
                fieldText: "userName",
                fieldValue: "userId",
                name: "buyername4",
                label: "司机:<span class='dhxform_item_required'>*<span>",
                listener: function (consignorGrid) {
                    setMethod(consignorGrid, userTable2, function (data) {
                        var fromdata = ddfrom.getFormData();
                        data = fromdata.buyername4.length == 2 ? "" : data;
                        var datad = {};
                        datad.driverId = data.userId ? data.userId : "";
                        datad.driverName = data.userName ? data.userName : "";
                        datad.card = data.card ? data.card : "";
                        datad.tel = data.tel ? data.tel : "";
                        ddfrom.setFormData(datad);
                    });
                }
            },
            {
                type: "hidden", name: "driverId", label: "司机ID:", width: 230
            },
            {
                type: "hidden", name: "driverName", label: "司机名称:", width: 230
            },
            {
                type: "hidden", name: "card", label: "身份证:", width: 230
            },
            {
                type: "hidden", name: "tel", label: "手机号:", width: 230
            },
            {
                type: "hidden", name: "vehicleSpecsNo", label: "规格编号:", width: 230
            },
            {
                type: "hidden", name: "vehicleSpecsName", label: "规格名称:", width: 230
            },
            {
                type: "input", name: "memo", label: "备注:", width: 230
            }]
    };
    //返回
    $(document).on("click", "#driverRelationship_return", function () {
        setView("#driverRelationship_div3");
    });
    //保存
    document.querySelector("#driverRelationship_save").onclick = function () {
        //数据data
        var data = ddfrom.getFormData();
        data.branchid = userGJ.p_companyid;
        var feik = ddfrom.validate();
        if (!feik) {
            return;
        }
        //请求地址和类型
        var url = UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/tBaseBranchDriver/updateTBaseBranchDriver";
        var type = "post";
        var contentType = "application/json";
        //message新增为0，修改为1
        if (data.id == null || data.id == "") {
            url = UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/tBaseBranchDriver/addTBaseBranchDriver";
        }
        //调用ajax方法
        utilAjaxGJ(url, type, JSON.stringify(data), function (data) {
                setView("#driverRelationship_div3");
            grid.reload();
        }, contentType);
    };
};
