/**
 * zzx
 * 2020.12.09
 * 晚点率报表
 */
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 details_grid;

  // 获取默认时间
  var startTime = Prolog.formatDate(new Date(), "%YY-%MM") + "-01";
  var endTime = Prolog.formatDate(new Date());

  // 获取下拉框数据
  var dataSelect;
  getSelectGJ([
    { objectcode: "TMS_SHIP_BILLSTATUS" },
    { objectcode: "TMS_SHIP_BILLTYPE" },
    { objectcode: "URG_FLG" }], function (data) {
      dataSelect = data.data;
    }
  );

  // 货主table
  var consignorTable = {
    // terrace显示全部，card分页显示
    skin: "card",
    // renderer:"grid",
    columns: [
      { id: "companyid", name: "id", type: "ro", width: "130", hidden: true },
      { id: "companycode", name: "货主编号", type: "ro" },
      { id: "companyname", name: "货主名称", type: "ro" }
    ],
    multiselect: false,
    toolbar: [
      { type: "settings", position: "label-left", width: 200, labelWidth: 100 },
      {
        type: "block", id: "bk", blockOffset: 0, list: [
          { type: "input", name: "companycode", label: "货主编号:", width: 120 },
          { type: "newcolumn" },
          { type: "button", name: "querycarr", value: "查询" },
          { type: "newcolumn" },
          { type: "button", name: "car_Reset", value: "重置" }
        ]
      }
    ],
    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/pubCompany/queryCompanyConsignor",
    pagebar: { type: "simple" },
    type: "post", // 数据提交方式，默认为get
    params: JSON.stringify({ companytype: "H" }), // 查询提交参数，分页参数默认为pageNum,pageSize
    contentType: "application/json",
    page: true, // 是否启用分页
    totalCount: "totalCount" // 总记录数字段，默认totalCount
  };
  // 配送中心/站点table
  var branchTable = {
    // terrace显示全部，card分页显示
    skin: "card",
    // renderer:"grid",
    columns: [
      { id: "companyid", name: "id", type: "ro", width: "130", hidden: true },
      { id: "companycode", name: "配送中心/站点编号", type: "ro" },
      { id: "companyname", name: "配送中心/站点名称", type: "ro" }
    ],
    multiselect: false,
    toolbar: [
      { type: "settings", position: "label-left", width: 200, labelWidth: 100 },
      {
        type: "block", id: "bk", blockOffset: 0, list: [
          { type: "input", name: "companycode", label: "配送中心/站点编号:", width: 120 },
          { type: "newcolumn" },
          { type: "button", name: "querycarr", value: "查询" },
          { type: "newcolumn" },
          { type: "button", name: "car_Reset", value: "重置" }
        ]
      }
    ],
    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/pubCompany/queryCompanyBranch",
    pagebar: { type: "simple" },
    type: "post", // 数据提交方式，默认为get
    params: JSON.stringify({ companytype: "S" }), // 查询提交参数，分页参数默认为pageNum,pageSize
    contentType: "application/json",
    page: true, // 是否启用分页
    totalCount: "totalCount" // 总记录数字段，默认totalCount
  };
  // 承运商table
  var carrierTable = {
    // terrace显示全部，card分页显示
    skin: "card",
    // renderer:"grid",
    columns: [
      { id: "companyid", name: "id", type: "ro", width: "130", hidden: true },
      { id: "companycode", name: "承运商编号", type: "ro" },
      { id: "companyname", name: "承运商名称", type: "ro" }
    ],
    multiselect: false,
    toolbar: [
      { type: "settings", position: "label-left", width: 200, labelWidth: 70 },
      {
        type: "block", id: "bk", blockOffset: 0, list: [
          { type: "input", name: "companycode", label: "承运商编号:", width: 120 },
          { type: "newcolumn" },
          { type: "button", name: "querycarr", value: "查询" },
          { type: "newcolumn" },
          { type: "button", name: "car_Reset", value: "重置" }
        ]
      }
    ],
    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/pubCompany/queryCompanyCarrier",
    pagebar: { type: "simple" },
    type: "post", // 数据提交方式，默认为get
    params: JSON.stringify({ companytype: "Y" }), // 查询提交参数，分页参数默认为pageNum,pageSize
    contentType: "application/json",
    page: true, // 是否启用分页
    totalCount: "totalCount" // 总记录数字段，默认totalCount
  };

  // 投诉率列表
  var master_grid_opts = {
    skin: "card",
    columns: [
      { id: "INDEX", name: "id", type: "ro", width: "130", hidden: true },
      { id: "branchname", name: "配送中心/站点名称", type: "ro", width: 120 },
      { id: "consignorname", name: "货主", type: "ro", width: 120 },
      { id: "billdate", name: "单据日期", type: "ro", width: 120 },
      { id: "orderNums", name: "运单数", type: "ro", width: 120 },
      { id: "shipperDelayNums", name: "发货超时运单数", type: "ro", width: 120 },
      { id: "arriverDelayNums", name: "到货超时运单数", type: "ro", width: 120 },
      { id: "shipperDelayRate", name: "发货超时率(%)", type: "ro", width: 120 },
      { id: "arriverDelayRate", name: "到货超时率(%)", type: "ro", width: 120 }
    ],
    toolbar: [
      { type: "settings", labelWidth: 90, labelAlign: "right" },
      {
        type: "row",
        list: [
          {
            type: "selectTable",
            className: "layui-col-md3",
            placeholder: "请选择配送中心/站点",
            readonly: false,
            table: branchTable,
            tableWidth: 600,
            tableHeight: 500,
            fieldText: "companyname",
            fieldValue: "companyid",
            name: "branchidList",
            label: "配送中心/站点:",
            listener: function (Grid) {
              setMethod(Grid, branchTable, function () { });
            }
          },
          {
            type: "selectTable",
            className: "layui-col-md3",
            placeholder: "请选择货主",
            readonly: false,
            table: consignorTable,
            tableWidth: 600,
            tableHeight: 500,
            fieldText: "companyname",
            fieldValue: "companyid",
            name: "consignorList",
            label: "货主:",
            listener: function (Grid) {
              setMethod(Grid, consignorTable, function () { });
            }
          },
          { type: "input", className: "layui-col-md3", name: "", label: "发货方:" },
          { type: "input", className: "layui-col-md3", name: "", label: "收货方:" },
          { type: "calendar", className: "layui-col-md3", name: "sdate", label: "开始时间:", value: startTime, readonly: true },
          { type: "calendar", className: "layui-col-md3", name: "edate", label: "结束时间:", value: endTime, readonly: true },
          {
            type: "selectTable",
            className: "layui-col-md3",
            placeholder: "请选择承运商",
            readonly: false,
            table: carrierTable,
            tableWidth: 600,
            tableHeight: 500,
            fieldText: "companyname",
            fieldValue: "companyid",
            name: "carrieridList",
            label: "承运商:",
            listener: function (Grid) {
              setMethod(Grid, carrierTable, function () { });
            }
          },
          {
            type: "block", className: "layui-col-md2 form_btn_left",
            list: [
              { type: "button", className: "btn-normal", name: "master_search", value: "查询" },
              { type: "newcolumn" },
              { type: "button", name: "master_reset", value: "重置" }
            ]
          },
          {
            type: "block", className: "layui-col-md1 right",
            list: [
              { type: "button", className: "btn-normal", name: "master_export", value: "导 出" }
            ]
          }
        ]
      }
    ],
    beforeLoadData: function (res) {
      res.data.list.forEach((item, index) => {
        item.INDEX = index + "";
      });
      return res;
    },
    fixedLast: false, // 开启固定列，固定最后一行，倒数第二列不能隐藏
    multiselect: false, // 是否支持多选
    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/delayRateReport/queryDelayRateReport", // 数据接口
    type: "post", // 数据提交方式，默认为get
    params: JSON.stringify({ sdate: startTime, edate: endTime }), // 查询提交参数，分页参数默认为pageNum,pageSize
    page: true, // 是否启用分页
    contentType: "application/json",
    totalCount: "totalCount" // 总记录数字段，默认totalCount
  };
  var master_grid = new PlgGrid(master_grid_opts);
  master_grid.renderTo("lateRate_master");
  master_grid.loadData();
  master_grid.attachToolBarEvent("onButtonClick", function (name) {
    switch (name) {
      case "master_search":
        var request = getRequest();
        master_grid.setParams(JSON.stringify(request));
        master_grid.reload();
        details_grid.clearAll();
        break;
      case "master_reset":
        master_grid.getToolBarForm().clear();
        master_grid.getToolBarForm().setText("branchidList", "");
        master_grid.getToolBarForm().setText("consignorList", "");
        master_grid.getToolBarForm().setText("carrieridList", "");
        master_grid.getToolBarForm().setFormData({ sdate: startTime, edate: endTime });
        break;
      case "master_export":
        request = getRequest();
        exportDelayRateReport(request);
        break;
    }
  });

  // 获取查询条件
  function getRequest() {
    var request = master_grid.getToolBarForm().getFormData();
    request.sdate = request.sdate ? Prolog.formatDate(request.sdate) : "";
    request.edate = request.edate ? Prolog.formatDate(request.edate) : "";
    request.branchid = request.branchidList ? JSON.parse(request.branchidList)[0] : "";
    request.consignor = request.consignorList ? JSON.parse(request.consignorList)[0] : "";
    request.carrierid = request.carrieridList ? JSON.parse(request.carrieridList)[0] : "";
    dealNullQueryCondition(request);
    delete request.branchidList;
    delete request.consignorList;
    delete request.carrieridList;
    return request;
  }

  // 导出
  function exportDelayRateReport(params) {
    let url = UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/delayRateReport/exportDelayRateReport";
    let name = "晚点率报表" + Prolog.formatDate(new Date(), "%YY%MM%DD") + ".xls";
    Prolog.exportFile(url, name, "application/json", "POST", params);
  }

  // 单击一行显示明细
  master_grid.on("onRowSelect", function (id) {
    let { branchid, consignor, billdate } = master_grid.getRowData(id);
    details_grid.setParams(JSON.stringify({ branchid, consignor, billdate }));
    details_grid.reload();
  });

  // 投诉信息列表
  var details_grid_opts = {
    skin: "card",
    columns: [
      { id: "shipbillid", name: "id", type: "ro", width: "130", hidden: true },
      {
        id: "status", name: "单据状态", type: "ro", width: 120, render: function (v) {
          return getSelectValue(dataSelect.TMS_SHIP_BILLSTATUS, v);
        }
      },
      { id: "shipbillno", name: "运单号", type: "ro", width: 120 },
      {
        id: "billtype", name: "运单类型", type: "ro", width: 120, render: function (v) {
          return getSelectValue(dataSelect.TMS_SHIP_BILLTYPE, v);
        }
      },
      { id: "billdate", name: "单据日期", type: "ro", width: 120 },
      {
        id: "urgentIdentify", name: "是否急单", type: "ro", width: 120, render: (v) => {
          return getSelectValue(dataSelect.URG_FLG, v);
        }
      },
      {
        id: "refbilltype", name: "业务类型", type: "ro", width: 120, render: function (v) {
          return getSelectValue(dataSelect.TMS_SHIP_BILLTYPE, v);
        }
      },
      { id: "arriverName", name: "客户名称", type: "ro", width: 120 },
      { id: "arriveAddress", name: "客户地址", type: "ro", width: 120 },
      { id: "rsumcases", name: "件数", type: "ro", width: 120 },
      { id: "rsumgoodsweight", name: "重量", type: "ro", width: 120 },
      { id: "rsumgoodsvol", name: "体积", type: "ro", width: 120 },
      { id: "carriername", name: "承运商", type: "ro", width: 120 },
      { id: "driver1stname", name: "司机", type: "ro", width: 120 },
      { id: "voucherno", name: "车辆", type: "ro", width: 120 },
      { id: "reqshipmentdate", name: "预约时间", type: "ro", width: 120 },
      { id: "startloadingtime", name: "装车时间", type: "ro", width: 120 },
      { id: "reqshipmentdate", name: "发货时间", type: "ro", width: 120 },
      { id: "", name: "中转时间", type: "ro", width: 120, hidden: true },
      { id: "signdate", name: "签收时间", type: "ro", width: 120 },
      { id: "loadingTime", name: "装车时长", type: "ro", width: 120 },
      { id: "runningTime", name: "在途时长", type: "ro", width: 120 },
      { id: "totalTime", name: "总时长", type: "ro", width: 120 },
      { id: "memo", name: "备注", type: "ro", width: 120 }
    ],
    fixedLast: false, // 开启固定列，固定最后一行，倒数第二列不能隐藏
    multiselect: false, // 是否支持多选
    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/delayRateReport/queryDetails", // 数据接口
    type: "post", // 数据提交方式，默认为get
    params: "{}", // 查询提交参数，分页参数默认为pageNum,pageSize
    page: true, // 是否启用分页
    contentType: "application/json",
    totalCount: "totalCount" // 总记录数字段，默认totalCount
  };
  details_grid = new PlgGrid(details_grid_opts);
  details_grid.renderTo("lateRate_details");
};