/**
 * guoj
 * 2020.06.28
 * 中转到货
 */
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": "TMS_URGENTIDENTIFY" },{ "objectcode": "TMS_REF_BILLTYPE" },
    { "objectcode": "TMS_SHIP_BILLTYPE" ,"groupcode1": "APPOINT"}], function (data) {
        data.data.TMS_URGENTIDENTIFY.unshift({ "text": "", "value": "" });
        dataSelect = data.data;
    });
    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,
      contentType: "application/json",
        url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/pubCompany/queryCompanyBranch", //数据接口
      type: "post", //数据提交方式，默认为get
      pagebar: { type: "simple" },
      params: JSON.stringify({ "companytype": "S" }), //查询提交参数，分页参数默认为pageNum,pageSize
      page: true, //是否启用分页
      totalCount: "totalCount" //总记录数字段，默认totalCount
    };
    var ddfrom = null;
    var opts = {
        //terrace显示全部，card分页显示
        skin: "card",
        columns: [
            { id: "seqno", name: "id", type: "ro", width: "100", hidden: true },
            { id: "branchname", name: "配送中心", type: "ro", width: 150 },
            { id: "shipbillno", name: "运单号", type: "ro", width: 150 },
            {
                id: "consignorname", name: "货主", type: "ro", width: 100
            },
            { id: "billtype", name: "运单类型", type: "ro", width: 100 ,render: (v)=>{
                return getSelectValue(dataSelect.TMS_SHIP_BILLTYPE,v);
            }},
            { id: "linenane", name: "线路名称", type: "ro", width: 100 },
            {
                id: "billdate", name: "单据日期", type: "ro", width: 130
            },
            {
                id: "urgentIdentify", name: "是否急单", type: "ro", width: 100,render: (v)=>{
                    return getSelectValue(dataSelect.TMS_URGENTIDENTIFY,v);
                }
            },
            {
                id: "refbillno", name: "业务单号", type: "ro", width: 100
            },
            {
                id: "refbilltype", name: "业务类型", type: "ro", width: 120,render: (v)=>{
                    return getSelectValue(dataSelect.TMS_REF_BILLTYPE,v);
                }
            },
            { id: "customerName", name: "承运商", type: "ro", width: 100 },
            { id: "shiptoCustomerName", name: "客户名称", type: "ro", width: 130 },
            { id: "shiptoAddress", name: "客户地址", type: "ro", width: 130 },
            { id: "appointVehicle", name: "车牌号", type: "ro", width: 100 },
            { id: "appointDriver", name: "司机", type: "ro", width: 100 },
            { id: "rsumcases", name: "件数", type: "ro", width: 100 },
            { id: "rsumgoodsweight", name: "重量", type: "ro", width: 100 },
            { id: "rsumgoodsvol", name: "体积", type: "ro", width: 100 },
            { id: "memo", name: "备注", type: "ro", width: 100 }
        ],
        multiselect: true,
        toolbar: [
            { type: "settings", labelWidth: 90, labelAlign: "right" },
            {
              type: "row",
              list: [
                { type: "calendar", name: "sdate", label: "开始时间:",className: "layui-col-md3" },
                { type: "calendar", name: "edate", label: "结束时间:",className: "layui-col-md3" },
                {
                    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 datad = {};
                            datad.branchid = data.companyid;
                            grid.getToolBarForm().setFormData(datad);
                        });
                    }
                },
                { type: "hidden", name: "branchid", label: "配送中心:",className: "layui-col-md3" },
                { type: "input", name: "shipbillno", label: "运单号:",className: "layui-col-md3" },
                { type: "input", name: "refbillno", label: "业务单号:",className: "layui-col-md3" },
                { type: "combo", name: "urgentIdentify", label: "是否急单:",className: "layui-col-md3",options: dataSelect.TMS_URGENTIDENTIFY },
                { type: "input", name: "appointDriver", label: "司机:",className: "layui-col-md3" },
                { type: "input", name: "appointVehicle", label: "车牌号:",className: "layui-col-md3" },
                {
                  type: "block", className: "layui-col-md6 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-md6 right",
                  list: [
                    { type: "button", className: "btn-addBtn btn-normal", name: "addBtn", value: "中转到货" }
                  ]
                }
              ]
            }],
            url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/onTheWay/trainsArrivalQuery",//数据接口
        type: "post",//数据提交方式，默认为get
        contentType: "application/json",
        params: JSON.stringify({ sdate: ship_FormatDate(new Date()) , edate: FormatDate(new Date()) }),//查询提交参数，分页参数默认为pageNum,pageSize
        page: true, //是否启用分页
        totalCount: "totalCount" //总记录数字段，默认totalCount
    };
    //分页参数 pageSize pageNum
     grid = new PlgGrid(opts);
    grid.renderTo("transitArrival_guojzhuans2");
    grid.loadData();
    grid.getToolBarForm().setFormData({ sdate: ship_FormatDate(new Date()),edate: FormatDate(new Date())});

    //按钮添加方法
    grid.attachToolBarEvent("onButtonClick", function (name) {
        //查询
        if (name == "querywork") {
            var jsonData = grid.getToolBarForm().getFormData();
            if (jsonData.sdate == null) {
              jsonData.sdate = ship_FormatDate(new Date()) ;
            }
            else {
              jsonData.sdate = Prolog.formatDate(jsonData.sdate) ;
            }
            if (jsonData.edate == null) {
              jsonData.edate = FormatDate(new Date()) ;
            }
            else {
              jsonData.edate = Prolog.formatDate(jsonData.edate) ;
            }
            grid.setParams(JSON.stringify(jsonData));
            grid.reload();
        }
        //中转到货
        if (name == "addBtn") {
            let listData = getDeleteAll(grid) ;
            if(listData == null || listData == ""){
                return;
            }
            let dataJsom = {
                consorcode: 1,
                orderpcs: 0,
                orderwight: 0,
                tiji: 0,
                orderqty: listData.length,
                appointVehicle: ""
            };
            for(var z = 0;z < listData.length;z++){
                listData[z] = grid.getRowData(listData[z]);
            }
            for(var i = 0;i < listData.length;i++){
                for(var y = 0;y < listData.length;y++){
                    if(i != 0 && listData[i].appointVehicle != listData[y].appointVehicle){
                        layer.msg("不同车牌号不能够一起装车");
                        return;
                    }
                    if(i != y && listData[i].shiptoCustomerName != listData[y].shiptoCustomerName){
                        dataJsom.consorcode++;
                    }
                }
                listData[i].rsumcases = listData[i].rsumcases == null ? 0 : listData[i].rsumcases * 1;
                dataJsom.orderpcs = Prolog.floatOper(dataJsom.orderpcs, listData[i].rsumcases, "+");
                listData[i].rsumgoodsweight = listData[i].rsumgoodsweight == null ? 0 : listData[i].rsumgoodsweight * 1;
                dataJsom.orderwight = Prolog.floatOper(dataJsom.orderwight, listData[i].rsumgoodsweight, "+");
                listData[i].rsumgoodsvol = listData[i].rsumgoodsvol == null ? 0 : listData[i].rsumgoodsvol * 1;
                dataJsom.tiji = Prolog.floatOper(dataJsom.tiji, listData[i].rsumgoodsvol, "+");
                dataJsom.appointVehicle = listData[0].appointVehicle;
            }
            setViewName("#transitArrival_div1");
            setdataItem(dataJsom);
            ddfrom.clear();
            $("#transitArrival_title").html("中转到货");
        }
        //重置
        if (name == "work_Reset") {
            grid.getToolBarForm().getDForm().setText("buyername1", "");
            grid.getToolBarForm().clear();
            grid.getToolBarForm().setFormData({ sdate: ship_FormatDate(new Date()),edate: FormatDate(new Date())});
        }
    });
    //跳转页面
    var carousel = layui.carousel;
    carousel.render({
        elem: "#transitArrival_div2",
        autoplay: false,
        index: 0,
        arrow: "none",
        indicator: "none",
        anim: "fade",
        width: "100%", //设置容器宽度
        height: "100%"
    });
    let dataView = ["#transitArrival_div1","#transitArrival_div3"];
    function setViewName(name){
        for(var i = 0;i < dataView.length;i++){
            if(name == dataView[i]){
                $(name).css("display", "block");
            }
            else{
                $(dataView[i]).css("display", "none");
            }
        }
    }
    ddfrom = new PlgForm({
        renderer: "transitArrival_div4B",
        items: [
            { type: "settings", position: "label-left", width: 300, labelWidth: 90 },
            { type: "hidden", name: "stationid", label: "id:", width: 230 },
            { type: "input", name: "shipcrossby", label: "中转站:", width: 230, validate: "NotEmpty", required: true },
            { type: "newcolumn", offset: 10 },
            { type: "input", name: "singer", label: "交接人:", width: 230, validate: "NotEmpty", required: true },
            { type: "newcolumn", offset: 10 },
            { type: "input", name: "singRemark", label: "交接备注:", width: 230 }
        ]
    });
    //返回
    $(document).on("click", "#transitArrival_return", function () {
        setViewName("#transitArrival_div3");
    });
    //中转确认
    document.querySelector("#transitArrival_save").onclick = function () {
        //数据data
        var data = ddfrom.getFormData();
        var feik = ddfrom.validate();
        if (!feik) {
            return;
        }
        let listData = getDeleteAll(grid) ;
        for(var i = 0;i < listData.length;i++){
            listData[i] = grid.getRowData(listData[i]);
        }
        data.volist = listData;
        //调用ajax方法
        utilAjaxGJ(UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/onTheWay/trainsArrivalConfirm", "post", JSON.stringify(data), function (data) {
            if(data.success){
                        setViewName("#transitArrival_div3");
                grid.reload();
            }
        }, "application/json");
    };
    function setdataItem(rdata){
        new PlgPanel({
            renderer: "transitArrival_div4A",
            skin: 2,
            header: {
                isShow: true,
                title: "中转货物信息"
            },
            defaultBody: {
                layoutCol: 3,
                valueBj: true,
                cols: [
                    [{
                        label: "客户数",
                        value: rdata.consorcode
                    },
                    {
                        label: "总件数",
                        value: rdata.orderpcs
                    },
                    {
                        label: "总重量",
                        value: rdata.orderwight
                    },
                    {
                        label: "总体积",
                        value: rdata.tiji
                    }],
                    [
                        {
                            label: "订单数",
                            value: rdata.orderqty
                        },
                        {
                            label: "车牌号",
                            value: rdata.appointVehicle
                        }
                    ]
                ]
            }
        });
    }
};
