/**
 * guoj
 * 2020.12.10
 * 派车及时率
 */
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 data_select;
  getSelectGJ([{ "objectcode": "TMS_SHIP_BILLSTATUS" }, { "objectcode": "URG_FLG" },
  { "objectcode": "TMS_SHIP_BILLTYPE" }], function (data) {
    data_select = data.data;
  });
  // 获取默认时间
  var sdate = ship_FormatDate(new Date()) + " 00:00:00";
  var edate = FormatDate(new Date()) + " 23:59:59";
  let gridBottom;
  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 consignor_Table = {
    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/queryCompanyConsignor", //数据接口
    type: "post", //数据提交方式，默认为get
    contentType: "application/json",
    params: JSON.stringify({ "companytype": "H" }), //查询提交参数，分页参数默认为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 opts = {
    //terrace显示全部，card分页显示
    skin: "card",
    //renderer:"grid",
    columns: [
      { id: "INDEX", name: "id", type: "ro", width: "100", hidden: true },
      { id: "branchname", name: "配送中心/站点", type: "ro", width: 150 },
      { id: "consignorname", name: "货主", type: "ro", width: 150 },
      { id: "billdate", name: "单据日期", type: "ro", width: 100 },
      { id: "orderNums", name: "运单数", type: "ro", width: 100 },
      { id: "sendVehicleNums24", name: "24小时内派车运单数", type: "ro", width: 150 },
      { id: "sendVehicleRate24", name: "24小时内派车率(%)", type: "ro", width: 130 },
      { id: "sendVehicleNums48", name: "48小时内派车运单数", type: "ro", width: 150 },
      { id: "sendVehicleRate48", name: "48小时内派车率(%)", type: "ro", width: 130 },
      { id: "sendVehicleNums48M", name: "超48小时派车运单数", type: "ro", width: 150 },
      { id: "sendVehicleRate48M", name: "超48小时派车率(%)", type: "ro", width: 130 }
    ],
    multiselect: true,
    toolbar: [
      { type: "settings", labelWidth: 90, labelAlign: "right" },
      {
        type: "row",
        list: [
          { type: "calendar", className: "layui-col-md3", name: "sdate", label: "单据开始时间:", value: sdate, readonly: true },
          { type: "calendar", className: "layui-col-md3", name: "edate", label: "单据结束时间:", value: edate, readonly: true },
          {
            className: "layui-col-md3",
            type: "selectTable",
            placeholder: "请选择配送中心/站点",
            readonly: false,
            table: userTable,
            tableWidth: 500,
            tableHeight: 400,
            fieldText: "companyname",
            fieldValue: "companyid",
            name: "branchid",
            label: "配送中心/站点:"
          },
          {
            className: "layui-col-md3",
            type: "selectTable",
            placeholder: "请选择货主",
            readonly: false,
            table: consignor_Table,
            tableWidth: 500,
            tableHeight: 400,
            fieldText: "companyname",
            fieldValue: "companyid",
            name: "consignor",
            label: "货主:"
          },
          {
            className: "layui-col-md3",
            type: "selectTable",
            placeholder: "请选择承运商",
            readonly: false,
            table: company_Table,
            tableWidth: 500,
            tableHeight: 400,
            fieldText: "carrierName",
            fieldValue: "carrierId",
            name: "carrierid",
            label: "承运商:"
          },
          { type: "input", name: "shipperName", label: "发货方:", className: "layui-col-md3" },
          { type: "input", name: "arriverName", label: "收货方:", className: "layui-col-md3" },
          {
            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", name: "export", className: "btn-normal", value: "导出" }
            ]
          }
        ]
      }],
    beforeLoadData: function (res) {
      res.data.list.forEach((item, index) => {
        item.INDEX = index + "";
      });
      return res;
    },
    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/sendVehicleOntimeRateReport/querySendVehicleOntimeRate",//数据接口
    type: "post",//数据提交方式，默认为get
    contentType: "application/json",
    params: JSON.stringify({ sdate, edate }),//查询提交参数，分页参数默认为pageNum,pageSize
    page: true, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  //分页参数 pageSize pageNum
  grid = new PlgGrid(opts);
  grid.renderTo("timelyDeliveryRate_guojzhuans2");
  grid.loadData();

  //单击事件
  grid.on("onRowSelect", function (id) {
    let item = grid.getRowData(id);
    gridBottom.setParams(JSON.stringify({ "consignor": item.consignor, "startTime": item.billdate+" 00:00:00", "endTime": item.billdate+" 23:59:59","branchid": item.branchid }));
    gridBottom.reload();
  });
  //按钮添加方法
  grid.attachToolBarEvent("onButtonClick", function (name) {
    var fdata = grid.getToolBarForm().getFormData();
    fdata.sdate = FormatDate(fdata.sdate) + " 00:00:00";
    fdata.edate = FormatDate(fdata.edate) + " 23:59:59";
    fdata.branchid = fdata.branchid ? JSON.parse(fdata.branchid)[0] : "";
    fdata.consignor = fdata.consignor ? JSON.parse(fdata.consignor)[0] : "";
    fdata.carrierid = fdata.carrierid ? JSON.parse(fdata.carrierid)[0] : "";
    //查询
    if (name == "querywork") {
      grid.setParams(JSON.stringify(fdata));
      grid.reload();
      gridBottom.clearAll();
    }
    //重置
    if (name == "work_Reset") {
      grid.getToolBarForm().getDForm().setText("branchid", "");
      grid.getToolBarForm().getDForm().setText("consignor", "");
      grid.getToolBarForm().getDForm().setText("carrierid", "");
      grid.getToolBarForm().clear();
      grid.getToolBarForm().setFormData({ sdate: sdate, edate: edate });
    }
    //导出
    if (name == "export") {
      if (grid.getRowsNum() <= 0) {
        PlgDialog.msg("没有要导出的数据");
        return;
      }
      let url = UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/sendVehicleOntimeRateReport/exportSendVehicleOntimeRate";
      let name = "派车及时率" + FormatDateTime(new Date()) + ".xls";
      let pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
      Prolog.exportFile(url, name, "application/json", "post", fdata);
      PlgDialog.close(pload);
    }

  });

  var optsBottom = {
    //terrace显示全部，card分页显示
    skin: "card",
    //renderer:"grid",
    columns: [
      { id: "shipbillid", name: "id", type: "ro", width: "100", hidden: true },
      { id: "shipbillno", name: "运单号", width: "100", type: "ro" },
      {
        id: "billtype", name: "运单类型", width: "100", type: "ro", render: (v) => {
          return getSelectValue(data_select.TMS_SHIP_BILLTYPE, v);
        }
      },
      { id: "billdate", name: "单据日期", width: "100", type: "ro" },
      {
        id: "urgentIdentify", name: "是否急单", width: "100", type: "ro", render: (v) => {
          return getSelectValue(data_select.URG_FLG, v);
        }
      },
      { id: "arriverName", name: "客户名称", width: "100", type: "ro" },
      { id: "arriveAddress", name: "客户地址", width: "100", type: "ro" },
      { id: "rsumcases", name: "件数", width: "100", type: "ro" },
      { id: "rsumgoodsweight", name: "重量", width: "100", type: "ro" },
      { id: "rsumgoodsvol", name: "体积", width: "100", type: "ro" },
      { id: "carriername", name: "承运商", width: "100", type: "ro" },
      { id: "driver1stname", name: "司机", width: "100", type: "ro" },
      { id: "voucherno", name: "车辆", width: "100", type: "ro" },
      { id: "yydate", name: "预约时间", width: "130", type: "ro" },
      { id: "zcdate", name: "装车时间", width: "130", type: "ro" },
      { id: "departuretime", name: "发货时间", width: "130", type: "ro" },
       { id: "signdate", name: "签收时间", width: "130", type: "ro" },
      { id: "loadingTime", name: "装车时长", width: "130", type: "ro" },
      { id: "runningTime", name: "在途时长", width: "130", type: "ro" },
      { id: "totalTime", name: "总时长", width: "130", type: "ro" },
      { id: "memo", name: "备注", width: "100", type: "ro" }
    ],
    multiselect: false,
    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/sendVehicleOntimeRateReport/querySendVehicleDetail",//数据接口
    type: "post",//数据提交方式，默认为get
    contentType: "application/json",
    params: "{}",//查询提交参数，分页参数默认为pageNum,pageSize
    page: false, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  //分页参数 pageSize pageNum
  gridBottom = new PlgGrid(optsBottom);
  gridBottom.renderTo("timelyDeliveryRate_guojzhuans2CP");
};
