/**
 * zzx
 * 2020.07.11
 * 离站异常查询
 */
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;

  // 获取默认时间
  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_SHIP_BILLTYPE" }
  ], function (data) {
    dataSelect = data.data;
    dataSelect.TMS_SHIP_BILLTYPE.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 carrierTable = {
    // terrace显示全部，card分页显示
    skin: "card",
    // renderer:"grid",
    columns: [
      { id: "carrierId", name: "id", type: "ro", width: "130", hidden: true },
      { id: "carrierCode", name: "预约单位编码", type: "ro" },
      { id: "carrierName", 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: "carrierCode", label: "预约单位编码:", width: 120 },
          { type: "newcolumn" },
          { type: "button", name: "querycarr", value: "查询" },
          { type: "newcolumn" },
          { type: "button", name: "car_Reset", value: "重置" }
        ]
      }],
    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/tmsBaseCarrier/queryTmsBaseCarrierForBooking",
    pagebar: { type: "simple" },
    type: "post", // 数据提交方式，默认为get
    params: "{}", // 查询提交参数，分页参数默认为pageNum,pageSize
    contentType: "application/json",
    page: true, // 是否启用分页
    totalCount: "totalCount" // 总记录数字段，默认totalCount
  };

  // 主页主表grid
  var master_grid_opts = {
    skin: "card",
    columns: [
      { id: "appointBillNo", name: "id", type: "ro", width: "130", hidden: true },
      { id: "branchid", name: "配送中心编号", type: "ro", width: 120 },
      { id: "branchname", name: "配送中心名称", type: "ro", width: 120 },
      { id: "status", name: "单据状态", type: "ro", width: 120, render: function (v, rd) { return rd.statusName ? rd.statusName : v; } },
      { id: "appointBillNo", name: "预约单号", type: "ro", width: 120 },
      { id: "billdate", name: "订单时间", type: "ro", width: 120 },
      { id: "appointDate", name: "预约日期", type: "ro", width: 120 },
      { id: "appointTime", name: "预约时间", type: "ro", width: 120 },
      { id: "appointVehicle", name: "车牌号", type: "ro", width: 120 },
      { id: "appointDriver", name: "司机", type: "ro", width: 120 },
      { id: "appointDriverTel", name: "司机手机号", type: "ro", width: 120 },
      { id: "appointDriverCard", name: "司机身份证号", type: "ro", width: 120 },
      { id: "customerCode", name: "供应商/承运商编号", type: "ro", width: 120 },
      { id: "customerName", name: "供应商/承运商名称", type: "ro", width: 120 },
      { id: "appointPackage", name: "预约件数", type: "ro", width: 120 },
      { id: "appointWhight", name: "预约重量", type: "ro", width: 120 },
      { id: "appointVolume", name: "预约体积", type: "ro", width: 120 },
      { id: "startWhight", name: "进场称重重量", type: "ro", width: 120 },
      { id: "endWhight", name: "离场称重重量", type: "ro", width: 120 },
      { id: "differentialWeight", name: "误差重量", type: "ro", width: 120 },
      { id: "releaseOrNot", name: "是否异常", type: "ro", width: 120 },
      { 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: "selectTable",
            className: "layui-col-md3",
            placeholder: "请选择预约方",
            readonly: false,
            table: carrierTable,
            tableWidth: 600,
            tableHeight: 500,
            fieldText: "carrierName",
            fieldValue: "carrierId",
            name: "customerIdList",
            label: "预约方:",
            listener: function (Grid) {
              setMethod(Grid, carrierTable, function () { });
            }
          },
          { type: "input", className: "layui-col-md3", name: "appointVehicle", label: "车牌号:" },
          { type: "input", className: "layui-col-md3", name: "appointDriver", label: "司机:" },
          { type: "combo", className: "layui-col-md3", readonly: true, name: "billtype", label: "单据类型:", options: dataSelect.TMS_SHIP_BILLTYPE },
          {
            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", name: "car_export", className: "btn-normal", value: "导 出" }
            ]
          }
        ]
      }
    ],
    fixedLast: false, // 开启固定列，固定最后一行，倒数第二列不能隐藏
    multiselect: false, // 是否支持多选
    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/departureException/query/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("departureExceptionQuery");
  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.customerId = request.customerIdList ? JSON.parse(request.customerIdList)[0] : "";
        dealNullQueryCondition(request);
        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("customerIdList", "");
        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+"/departureException/departureExceptionExport";
    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.customerId = request.customerIdList ? JSON.parse(request.customerIdList)[0] : "";
    Prolog.exportFile(url, name, "application/json", "post", request);
    PlgDialog.close(pload);
  }
};