/*
* chengj
* 2020.10.22
* 装车单查询
* */
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 userinfo = {
  p_id: '',
  p_username: '',
  p_workernumber: '',
  p_nickname: '',
  p_phone: null,
  p_email: null,
  p_companyid: '',
  p_enterpriseid: '',
  p_organizationid: '',
  p_companyname: '',
  p_orgname: '',
  p_enterprisename: '',
  p_usertype: '',
  p_lastlogindate: '',
  p_lastmodipassdate: null,
  p_validdate: '',
  p_import_batchno: null,
  p_data_source: null,
  forceModifyPasswdFlag: false,
  vehicle: null,
}
  // 获取默认时间
  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": "URG_FLG" }, { "objectcode": "TMS_LOADING_BILLHSTATUS" }, { "objectcode": "TMS_REF_BILLTYPE" }, { "objectcode": "TMS_SHIP_BILLTYPE" },
    { "objectcode": "TEMP_REQ" }, { "objectcode": "SIGNTYPE" },{ "objectcode": "SIGNSTATUS" }
  ], function (data) {
    dataSelect = data.data;
    dataSelect.URG_FLG.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
  };
  //承运商table
  var companyTable = {
    columns: [
      { id: "companyid", name: "id", type: "ro", width: 100, hidden: true },
      { id: "companycode", name: "编号", type: "ro" },
      { id: "companyname", name: "名称", type: "ro" }
    ],
    multiselect: false,
    toolbar: [
      { type: "settings", position: "label-left", width: 300, labelWidth: 70 },
      { type: "input", name: "companycode", label: "查询条件:", placeholder: "请输入编号/名称", width: 190 },
      { type: "newcolumn" },
      { type: "button", name: "master_search", value: "查询" }, { type: "newcolumn" },
      { type: "button", name: "btn_reset", className: " normal", value: "重置" }

    ],
    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/pubCompany/queryCompanyCarrier",
    type: "post",
    contentType: "application/json",
    pagebar: { type: "simple" },
    params: JSON.stringify({ "companytype": "Y" }),
    page: true,
    totalCount: "totalCount"
  };
  // （查询）车牌号table
  var vehicleTable = {
    skin: "card",
    columns: [
      { id: "vehicleId", name: "id", type: "ro", width: "130", hidden: true },
      { id: "vehicleNo", name: "车牌号", type: "ro" },
      { id: "vehicleName", name: "车辆名称", type: "ro" }
    ],
    multiselect: false,
    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/tBaseVehicle/queryVehicleInfo",
    type: "post",
    pagebar: { type: "simple" },
    contentType: "application/json",
    params: JSON.stringify({ companyId: userinfo.p_companyid }),
    page: true,
    totalCount: "totalCount"
  };

  // 主页主表grid
  var master_grid_opts = {
    skin: "card",
    columns: [
      { id: "INDEX", name: "id", type: "ro", width: "130", hidden: true },
      { id: "loadbillno", name: "装车单号", type: "ro", width: 120 },
      {
        id: "status", name: "状态", type: "ro", width: 120, render: (v) => {
          return getSelectValue(dataSelect.TMS_LOADING_BILLHSTATUS, v);
        }
      },
      { id: "branchid", name: "配送中心编号", type: "ro", width: 120 },
      { id: "branchname", name: "配送中心名称", type: "ro", width: 120 },
      { id: "billdate", name: "单据时间", type: "ro", width: 120 },
      { id: "driver1stname", name: "司机", type: "ro", width: 120 },
      { id: "dirver2ndname", name: "副驾驶", type: "ro", width: 120 },
      { id: "voucherno", name: "车辆", type: "ro", width: 120 },
      { id: "distperson1name", name: "配送员", type: "ro", width: 120 },
      { id: "distperson2name", name: "副配送员", type: "ro", width: 120 },
      { id: "orderNumber", name: "运单数", type: "ro", width: 120 },
      { id: "customerNumber", name: "客户数", type: "ro", width: 120 },
      { id: "carriername", name: "承运商", type: "ro", width: 120 },
      { id: "shipmentNumber", name: "件数", type: "ro", width: 120 },
      { id: "shipmentWeight", name: "重量", type: "ro", width: 120 },
      { id: "shipmentVol", name: "体积", type: "ro", width: 120 },
      { id: "loadingRateVol", name: "体积装载率", type: "ro", width: 120 },
      { id: "loadingRateWeight", name: "重量装载率", type: "ro", width: 120 },
      { id: "remark", name: "备注", type: "ro", width: 120 }
    ],
    beforeLoadData: function (res) {
      res.data.list.forEach((item, index) => {
        item.INDEX = index + "";
      });
      return res;
    },
    toolbar: [
      { type: "settings", labelWidth: 70, labelAlign: "right" },
      {
        type: "row",
        list: [
          { type: "calendar", className: "layui-col-md3", name: "startdate", label: "开始时间:", value: startTime, readonly: true },
          { type: "calendar", className: "layui-col-md3", name: "enddate", label: "结束时间:", value: endTime, readonly: true },
          {
            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: "loadbillno", label: "装车单号:" },
          { type: "input", className: "layui-col-md3", name: "driver1stname", label: "司机:" },
          {
            type: "selectTable",
            className: "layui-col-md3",
            placeholder: "请选择车辆",
            readonly: false,
            table: vehicleTable,
            tableWidth: 600,
            tableHeight: 500,
            fieldText: "vehicleNo",
            fieldValue: "vehicleNo",
            name: "vehicleList",
            label: "车辆:",
            listener: function (Grid) {
              setMethod(Grid, vehicleTable, function () { });
            }
          },
          //{ type: "input", className: "layui-col-md3", name: "voucherno", label: "车辆:" },
          {
            className: "layui-col-md3",
            type: "selectTable",
            placeholder: "请选择承运商",
            readonly: false,
            table: companyTable,
            tableWidth: 500,
            tableHeight: 400,
            fieldText: "companyname",
            fieldValue: "companyid",
            name: "carrieridList",
            label: "承运商:",
            listener: {
              "init": function (sgrid) {
                sgrid.attachToolBarEvent("onButtonClick", function (name) {
                  if (name === "master_search") {
                    var p = sgrid.getToolBarForm().getFormData();
                    p.companytype = "Y";
                    sgrid.setParams(JSON.stringify(p));
                    sgrid.reload();
                  }
                });
              }
            }
          },
          {
            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+"/report/loadBillQuery", // 数据接口
    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("loadBillQuery_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] : "";
        request.carrierid = request.carrieridList ? JSON.parse(request.carrieridList)[0] : "";
        request.voucherno = request.vehicleList ? JSON.parse(request.vehicleList)[0] : "";
        dealNullQueryCondition(request);
        delete request.branchidList;
        delete request.carrieridList;
        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", "");
        master_grid.getToolBarForm().setText("carrieridList", "");
        master_grid.getToolBarForm().setText("vehicleList", "");
        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+"/report/loadBillExport";
    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] : "";
    request.carrierid = request.carrieridList ? JSON.parse(request.carrieridList)[0] : "";
    request.voucherno = request.vehicleList ? JSON.parse(request.vehicleList)[0] : "";
    delete request.branchidList;
    delete request.carrieridList;
    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({ loadbillno: rdata.loadbillno, branchid: rdata.branchid }));
    details_grid.reload();
  });

  // 主页详情表grid
  var details_grid_opts = {
    skin: "card",
    columns: [
      { id: "seqno", name: "id", type: "ro", width: "130", hidden: true },
      { id: "loadbillno", name: "装车单号", type: "ro", width: 120 },
      { id: "shipbillno", name: "运单号", type: "ro", width: 120 },
      {
        id: "billtype", name: "运单类型", type: "ro", width: 120, render: (v) => {
          return getSelectValue(dataSelect.TMS_SHIP_BILLTYPE, v);
        }
      },
      { id: "consignor", name: "货主编号", type: "ro", width: 120 },
      { id: "consignorname", name: "货主名称", type: "ro", width: 120 },
      { id: "refbillno", name: "业务单号", type: "ro", width: 120 },
      {
        id: "refbilltype", name: "业务类型", type: "ro", width: 120, render: (v) => {
          return getSelectValue(dataSelect.TMS_REF_BILLTYPE, v);
        }
      },
      {
        id: "urgentIdentify", name: "是否急单", type: "ro", width: 120, render: (v) => {
          return getSelectValue(dataSelect.URG_FLG, v);
        }
      },
      { id: "linenane", name: "线路名称", type: "ro", width: 120 },
      {
        id: "temperatureRequire", name: "运输温度要求", type: "ro", width: 120, render: (v) => {
          return getSelectValue(dataSelect.TEMP_REQ, v);
        }
      },
      { id: "shipfromAddress", name: "发货地址", type: "ro", width: 120 },
      { id: "shiptoAddress", name: "客户地址", type: "ro", width: 120 },
      { id: "consignee", name: "联系人", type: "ro", width: 120 },
      { id: "reqshipmentdate", name: "预计发货日期", type: "ro", width: 120 },
      { id: "reqarrivvaldate", 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: "signer", name: "签收人", type: "ro", width: 120 },
      {
        id: "signtype", name: "签收类型", type: "ro", width: 120, render: (v) => {
          return getSelectValue(dataSelect.SIGNTYPE, v);
        }
      },
      {
        id: "signstatus", name: "签收状态", type: "ro", width: 120, render: (v) => {
          return getSelectValue(dataSelect.SIGNSTATUS, v);
        }
      },
      { id: "signer2nd", name: "第二签收人", type: "ro", width: 120 },
      { id: "signdate", name: "签收时间", type: "ro", width: 120 },
      { id: "signqty", name: "签收件数", type: "ro", width: 120 },
      { id: "rejectreason", name: "拒收原因", type: "ro", width: 120 },
      { id: "shipfromtemperature", name: "出库温度", type: "ro", width: 120 },
      { id: "shiptotemperature", name: "到货温度", type: "ro", width: 120 },
      { id: "shiptohjwd", name: "到货环境温度", type: "ro", width: 120 },
      { id: "memo", name: "备注", type: "ro", width: 120 }
    ],
    fixedLast: false, // 开启固定列，固定最后一行，倒数第二列不能隐藏
    multiselect: false, // 是否支持多选
    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/report/loadBillDetailQuery", // 数据接口
    type: "post", // 数据提交方式，默认为get
    params: "{}", // 查询提交参数，分页参数默认为pageNum,pageSize
    page: false, // 是否启用分页
    contentType: "application/json",
    totalCount: "totalCount" // 总记录数字段，默认totalCount
  };
  details_grid = new PlgGrid(details_grid_opts);
  details_grid.renderTo("loadBillQuery_details");
};