/*
* zzx
* 2020.06.24
* 运单记录查询
* */
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 master_grid, details_grid;
  // 获取默认时间
  var startTime = Prolog.formatDate(new Date(), "%YY-%MM") + "-01 00:00:00";
  var endTime = Prolog.formatDate(new Date()) + " 23:59:59";
  // 获取下拉框
  var dataSelect;
  getSelectGJ([
    { "objectcode": "TMS_URGENTIDENTIFY" }
  ], function (data) {
    dataSelect = data.data;
    dataSelect.TMS_URGENTIDENTIFY.unshift({ "value": "", "text": "" });
  });

  // 配送中心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
  };
  // 主页主表grid
  var master_grid_opts = {
    skin: "card",
    columns: [
      { id: "shipbillid", name: "运单id", type: "ro", width: "130", hidden: true },
      { id: "shipbillno", name: "运单编号", type: "ro", width: 120 },
      { id: "branchid", name: "配送中心编号", type: "ro", width: 120 },
      { id: "branchname", name: "配送中心名称", type: "ro", width: 120 },
      { id: "consignor", name: "货主编号", type: "ro", width: 120 },
      { id: "consignorname", name: "货主名称", type: "ro", width: 120 },
      { id: "temperatureRequire", name: "运输温度要求", type: "ro", width: 120 },
      { id: "billdate", name: "单据时间", type: "ro", width: 120 },
      { id: "lineno", name: "线路编号", type: "ro", width: 120 },
      { id: "linenane", name: "线路名称", type: "ro", width: 120 },
      // { id: "shipfromAddress", name: "出发详细地址", type: "ro", width: 120 }, 2020.07.08 与产品沟通不显示
      { id: "arriverType", name: "目的客户类型", type: "ro", width: 120, render: function (v, rd) { return rd.arriverTypeName ? rd.arriverTypeName : v; } },
      { id: "arriverId", name: "目的客户/个人id", type: "ro", width: 120 },
      { id: "arriverName", name: "目的客户/个人名称", type: "ro", width: 120 },
      { id: "consignee", name: "目的联系人", type: "ro", width: 120 },
      { id: "consigneetel", name: "目的人联系电话", type: "ro", width: 120 },
      { id: "consigneemobile", name: "目的人联系手机", type: "ro", width: 120 },
      { id: "arriveAddress", name: "目的详细地址", type: "ro", width: 120 },
      { id: "reqshipmentdate", name: "预计发货日期", type: "ro", width: 120 },
      { id: "reqarrivvaldate", name: "要求到货日期", type: "ro", width: 120 },
      { id: "shipgoodsgroup", name: "货物类型", type: "ro", width: 120, render: function (v, rd) { return rd.shipgoodsgroupName ? rd.shipgoodsgroupName : v; } },
      { id: "urgentIdentify", name: "紧急标识", type: "ro", width: 120 },
      { id: "refbillid", name: "业务单据id", type: "ro", width: 120 },
      { id: "refbillno", name: "业务单据号", type: "ro", width: 120 },
      { id: "refbilltype", name: "业务单据类型", type: "ro", width: 120, render: function (v, rd) { return rd.refbilltypeName ? rd.refbilltypeName : v; } },
      { id: "rsumcases", name: "实际总件数", type: "ro", width: 120 },
      { id: "rsumpcs", name: "实际整件数", type: "ro", width: 120 },
      { id: "rsumboxnum", name: "实际拼箱数", type: "ro", width: 120 },
      { id: "rsumgoodsqty", name: "实际货物总数量", type: "ro", width: 120 },
      { id: "rsumgoodsweight", name: "实际货物总重量", type: "ro", width: 120 },
      { id: "rsumgoodsvol", name: "实际货物总体积", type: "ro", width: 120 },
      { id: "customerModelType", name: "客户最大车型", type: "ro", width: 120 },
      { id: "status", name: "状态", type: "ro", width: 120, render: function (v, rd) { return rd.statusName ? rd.statusName : v; } },
      { id: "memo", name: "备注", type: "ro", width: 120 }
    ],
    toolbar: [
      { type: "settings", labelWidth: 70, labelAlign: "right" },
      {
        type: "row",
        list: [
          { type: "calendar", className: "layui-col-md3", name: "startdate", label: "开始时间:", value: startTime },
          { type: "calendar", className: "layui-col-md3", name: "enddate", label: "结束时间:", value: endTime },
          {
            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: "input", className: "layui-col-md3", name: "shipbillno", label: "运单号:" },
          { type: "input", className: "layui-col-md3", name: "refbillno", label: "业务单号:" },
          { type: "combo", className: "layui-col-md3", readonly: true, name: "urgentIdentify", label: "是否急单:", options: dataSelect.TMS_URGENTIDENTIFY },
          { type: "input", className: "layui-col-md3", name: "lineno", label: "运输线路:" },
          { type: "input", className: "layui-col-md3", name: "carrierid", label: "承运商:" },
          {
            type: "block", className: "layui-col-md6 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-md6 right",
            list: [
              { type: "button", name: "car_export", className: "btn-normal", value: "导 出" }
            ]
          }
        ]
      }
    ],
    fixedLast: false, // 开启固定列，固定最后一行，倒数第二列不能隐藏
    multiselect: false, // 是否支持多选
    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/tmsShipbill/summaryQuery/v1.0", // 数据接口
    type: "post", // 数据提交方式，默认为get
    params: JSON.stringify({ startdate: startTime, enddate: endTime }),
    page: true, // 是否启用分页
    contentType: "application/json",
    totalCount: "totalCount" // 总记录数字段，默认totalCount
  };
  master_grid = new PlgGrid(master_grid_opts);
  master_grid.renderTo("waybillRecordQuery_master");
  master_grid.loadData();
  master_grid.attachToolBarEvent("onButtonClick", function (name) {
    switch (name) {
      case "master_search":
        var request = master_grid.getToolBarForm().getFormData();
        request.startdate = request.startdate ? Prolog.formatDate(request.startdate) + " 00:00:00" : "";
        request.enddate = request.enddate ? Prolog.formatDate(request.enddate) + " 23:59:59" : "";
        if (request.startdate > request.enddate) {
          layer.msg("开始时间不能大于结束时间");
          return false;
        }
        request.branchid = request.branchidList ? JSON.parse(request.branchidList)[0] : "";
        dealNullQueryCondition(request);
        delete request.branchidList;
        master_grid.setParams(JSON.stringify(request));
        master_grid.reload();
        break;
      case "master_reset":
        master_grid.getToolBarForm().clear();
        master_grid.getToolBarForm().setFormData({ startdate: startTime, enddate: endTime });
        master_grid.getToolBarForm().setText("branchidList", "");
        break;
    }
    if (name == "car_export") {
      unfilledWorktime_export();
    }
  });

  //主界面导出事件
  function unfilledWorktime_export() {
    let pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
    if (master_grid.getRowsNum() <= 0) {
      PlgDialog.close(pload);
      PlgDialog.msg("没有要导出的数据");
      return;
    }
    let url = UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/tmsShipbill/tmsShipbillSumExport";
    let name = "运单记录查询" + Prolog.formatDate(new Date(), "%YY%MM%DD") + ".xls";
    var request = master_grid.getToolBarForm().getFormData();
    request.startdate = request.startdate ? Prolog.formatDate(request.startdate) + " 00:00:00" : "";
    request.enddate = request.enddate ? Prolog.formatDate(request.enddate) + " 23:59:59" : "";
    if (request.startdate > request.enddate) {
      PlgDialog.close(pload);
      layer.msg("开始时间不能大于结束时间");
      return false;
    }
    request.branchid = request.branchidList ? JSON.parse(request.branchidList)[0] : "";
    delete request.branchidList;
    Prolog.exportFile(url, name, "application/json", "post", request);
    PlgDialog.close(pload);

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

  // 主页详情表grid
  var details_grid_opts = {
    skin: "card",
    columns: [
      { id: "lineid", name: "id", type: "ro", width: "130", hidden: true },
      { id: "shipbillno", name: "运单号", type: "ro" },
      { id: "itemno", name: "商品编码", type: "ro" },
      { id: "itemname", name: "商品名称", type: "ro" },
      { id: "itemtype", name: "商品类型", type: "ro", render: function (v, rd) { return rd.itemtypeName ? rd.itemtypeName : v; } },
      { id: "pcs", name: "件数", type: "ro" },
      { id: "weight", name: "重量", type: "ro" },
      { id: "vol", name: "体积", type: "ro" },
      { id: "remark", name: "备注", type: "ro" }
    ],
    fixedLast: false, // 开启固定列，固定最后一行，倒数第二列不能隐藏
    multiselect: false, // 是否支持多选
    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/tmsShipbill/detailsQuery/v1.0", // 数据接口
    type: "post", // 数据提交方式，默认为get
    params: "{}", // 查询提交参数，分页参数默认为pageNum,pageSize
    page: false, // 是否启用分页
    contentType: "application/json",
    totalCount: "totalCount" // 总记录数字段，默认totalCount
  };
  details_grid = new PlgGrid(details_grid_opts);
  details_grid.renderTo("waybillRecordQuery_details");
};