/**
   * chengj
   * 2020.10.29
   * 离站异常查询
   */
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 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,
}
  //配送中心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
  };
  //承运商table
  var company_Table = {
    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: "btn_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"
  };
  // 获取默认时间
  var startTime = Prolog.formatDate(new Date(), "%YY-%MM") + "-01 00:00:00";
  var endTime = Prolog.formatDate(new Date()) + " 23:59:59";
  //分页参数
  var opts = {
    //terrace显示全部，card分页显示
    skin: "card",
    //renderer:"grid",
    columns: [
      { id: "loadbillno", name: "id", type: "ro", width: "80", hidden: true },
      { id: "branchname", name: "配送中心", type: "ro", width: "120" },
      { id: "loadbillno", name: "预约单号", type: "ro", width: "100" },
      { id: "carriername", name: "承运商", type: "ro", width: "120" },
      { id: "shipmentNumber", name: "件数", type: "ro", width: "80" },
      { id: "shipmentWeight", name: "重量", type: "ro", width: "80" },
      { id: "shipmentVol", name: "体积", type: "ro", width: "80" },
      { id: "driver1stname", name: "司机", type: "ro", width: "80" },
      { id: "voucherno", name: "车辆", type: "ro", width: "80" },
      { id: "startWhight", name: "进场重量", type: "ro", width: "100" },
      { id: "endWhight", name: "离场重量", type: "ro", width: "100" },
      { id: "differentialWeight", name: "误差重量", type: "ro", width: "100" },
      { id: "appointDate", name: "预约日期", type: "ro", width: "100" },
      { id: "arriveTime", name: "到场时间", type: "ro", width: "100" },
      { id: "intoTime", name: "进场时间", type: "ro", width: "100" },
      { id: "outTime", name: "离场时间", type: "ro", width: "100" },
      { id: "memo", name: "备注", type: "ro", width: "100" }
    ],
    multiselect: false,
    fixedLast: true, //开启固定列，固定最后一行，倒数第二列不能隐藏
    toolbar: [
      { type: "settings", labelWidth: 90, 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 },
          {
            className: "layui-col-md3",
            type: "selectTable",
            placeholder: "请选择配送中心",
            readonly: false,
            table: userTable,
            tableWidth: 500,
            tableHeight: 400,
            fieldText: "companyname",
            fieldValue: "companycode",
            name: "branchidList",
            label: "配送中心:",
            listener: function (consignorGrid) {
              setMethod(consignorGrid, userTable, function (data) {
                var datad = {};
                datad.branchid = data.companyid;
                grid.getToolBarForm().setFormData(datad);
              });
            }
          },
          { type: "hidden", name: "branchid", label: "配送中心:" },
          //{ type: "input", name: "voucherno",className: "layui-col-md3", 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", name: "driver1stname", className: "layui-col-md3", label: "司机:" },
          {
            className: "layui-col-md3",
            type: "selectTable",
            placeholder: "请选择承运商",
            readonly: false,
            table: company_Table,
            tableWidth: 500,
            tableHeight: 400,
            fieldText: "companyname",
            fieldValue: "companyid",
            name: "carrieridList",
            label: "承运商:",
            listener: {
              "init": function (sgrid) {
                sgrid.attachToolBarEvent("onButtonClick", function (name) {
                  if (name === "btn_search") {
                    var p = sgrid.getToolBarForm().getFormData();
                    p.companytype = "Y";
                    sgrid.setParams(JSON.stringify(p));
                    sgrid.reload();
                  }
                });
              }
            }
          },
          {
            type: "block", className: "layui-col-md3 form_btn_left",
            list: [
              { type: "button", className: "btn-normal", name: "querycarr", value: "查 询" },
              { type: "newcolumn" },
              { type: "button", name: "car_Reset", className: "normal", value: "重 置" }
            ]
          },
          {
            type: "block", className: "layui-col-md3 right",
            list: [
              { type: "button", name: "car_export", className: "btn-normal", value: "导 出" }
            ]
          }
        ]
      }],
    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/report/outStationAbnormalQuery",//数据接口
    type: "post",//数据提交方式，默认为get
    contentType: "application/json",
    params: JSON.stringify({ startdate: startTime, enddate: endTime }),//查询提交参数，分页参数默认为pageNum,pageSize
    page: true, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  //分页参数 pageSize pageNum
  grid = new PlgGrid(opts);
  grid.renderTo("outStationAbnormalQuery_zhu");
  grid.getToolBarForm().setFormData({ "systemId": "prolog-healthy-wms" });
  grid.loadData();
  //按钮添加方法
  grid.attachToolBarEvent("onButtonClick", function (name) {
    //查询
    if (name == "querycarr") {
      var jsonData = grid.getToolBarForm().getFormData();
      if (jsonData.startdate == null) {
        jsonData.startdate = FormatDate(new Date()) + " 00:00:00";
      }
      else {
        jsonData.startdate = Prolog.formatDate(jsonData.startdate) + " 00:00:00";
      }
      if (jsonData.enddate == null) {
        jsonData.enddate = FormatDate(new Date()) + " 23:59:59";
      }
      else {
        jsonData.enddate = Prolog.formatDate(jsonData.enddate) + " 23:59:59";
      }
      if (jsonData.startdate > jsonData.enddate) {
        layer.msg("开始时间不能大于结束时间");
        return false;
      }
      jsonData.carrierid = jsonData.carrieridList ? JSON.parse(jsonData.carrieridList)[0] : "";
      jsonData.carrierid = jsonData.branchidList ? JSON.parse(jsonData.branchidList)[0] : "";
      jsonData.carrierid = jsonData.vehicleList ? JSON.parse(jsonData.vehicleList)[0] : "";
      grid.setParams(JSON.stringify(jsonData));
      grid.reload();
    }
    //重置
    if (name == "car_Reset") {
      grid.getToolBarForm().getDForm().setText("branchidList", "");
      grid.getToolBarForm().getDForm().setText("carrieridList", "");
      grid.getToolBarForm().getDForm().setText("vehicleList", "");
      grid.getToolBarForm().clear();
      grid.getToolBarForm().setFormData({ startdate: startTime, enddate: endTime });
    }
    //导出
    if (name == "car_export") {
      unfilledWorktime_export();
    }
  });
  //主界面导出事件
  function unfilledWorktime_export() {
    let pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
    if (grid.getRowsNum() <= 0) {
      PlgDialog.close(pload);
      PlgDialog.msg("没有要导出的数据");
      return;
    }
    let url = UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/report/outStationAbnormalExport";
    let name = "离站异常" + Prolog.formatDate(new Date(), "%YY%MM%DD") + ".xls";
    var jsonData = grid.getToolBarForm().getFormData();
    if (jsonData.startdate == null) {
      jsonData.startdate = FormatDate(new Date()) + " 00:00:00";
    }
    else {
      jsonData.startdate = Prolog.formatDate(jsonData.startdate) + " 00:00:00";
    }
    if (jsonData.enddate == null) {
      jsonData.enddate = FormatDate(new Date()) + " 23:59:59";
    }
    else {
      jsonData.enddate = Prolog.formatDate(jsonData.enddate) + " 23:59:59";
    }
    if (jsonData.startdate > jsonData.enddate) {
      PlgDialog.close(pload);
      layer.msg("开始时间不能大于结束时间");
      return false;
    }
    jsonData.carrierid = jsonData.carrieridList ? JSON.parse(jsonData.carrieridList)[0] : "";
    jsonData.carrierid = jsonData.branchidList ? JSON.parse(jsonData.branchidList)[0] : "";
    jsonData.carrierid = jsonData.vehicleList ? JSON.parse(jsonData.vehicleList)[0] : "";
    Prolog.exportFile(url, name, "application/json", "post", jsonData);
    PlgDialog.close(pload);

  }
};