/**
 * zzx
 * 2020.09.25
 * 到场登记
 */
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: "AUDIT_STATUS" }], function (data) {
      dataSelect = data.data;
    }
  );

  // 预约单位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+"/company/queryAppointCompany",
    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: "loadbillid", name: "装车单id", type: "ro", width: "130", hidden: true },
      { id: "loadbillno", name: "装车单号", type: "ro", width: 120 },
      { id: "branchid", name: "机构号", type: "ro", width: 120 },
      { id: "branchname", name: "机构名称", type: "ro", width: 120 },
      { id: "appointDate", name: "预约日期", type: "ro", width: 120 },
      { id: "appointTime", name: "预约时间", type: "ro", width: 120, hidden: true },
      { id: "billdate", name: "单据日期", type: "ro", width: 120 },
      { id: "carrierid", name: "承运商", type: "ro", width: 120, hidden: true },
      { id: "carrierno", name: "承运商编号", type: "ro", width: 120 },
      { id: "carriername", name: "承运商名称", type: "ro", width: 120 },
      { id: "voucherno", name: "车牌号", type: "ro", width: 120 },
      { id: "vouchernane", name: "运输工具名称", type: "ro", width: 120 },
      { id: "driver1st", name: "司机id", type: "ro", width: 120 },
      { id: "driver1stname", name: "司机名称", type: "ro", width: 120 },
      { id: "drivertel", name: "司机电话", type: "ro", width: 120 },
      { id: "drivercard", name: "司机身份证", type: "ro", width: 120 },
      { id: "orderNumber", name: "运单数", type: "ro", width: 120 },
      { id: "orders", name: "运单号", type: "ro", width: 120 },
      { id: "coldBillNumber", name: "冷链运单数", type: "ro", width: 120 },
      { id: "customerNumber", name: "客户数", type: "ro", width: 120 },
      { id: "customerCode", name: "客户码", type: "ro", width: 120 },
      { id: "shipmentNumber", name: "配送装载件数", type: "ro", width: 120 },
      { id: "shipmentVol", name: "配送装载体积", type: "ro", width: 120 },
      { id: "shipmentWeight", name: "配送装载重量", type: "ro", width: 120 },
      { id: "loadingRateVol", name: "配送体积装载率", type: "ro", width: 120 },
      { id: "loadingRateWeight", name: "配送重量装载率", type: "ro", width: 120 },
      { id: "pickUpNumber", name: "提货件数", type: "ro", width: 120 },
      { id: "stoppointnum", name: "停靠站点数", type: "ro", width: 120 },
      { id: "relateColdEquipment", name: "关联冷链设备", type: "ro", width: 120 },
      { id: "relateGpsEquipment", name: "关联gps设备", type: "ro", width: 120 },
      { id: "precoolOpentime", name: "预冷开启时间", type: "ro", width: 120 },
      { id: "precoolArrivaltime", name: "预冷温度到达时间", type: "ro", width: 120 },
      { id: "appointTimeName", name: "预约时间", type: "ro", width: 120 },
      {
        id: "status", name: "状态", type: "ro", width: 120, render: function (v) {
          return getSelectValue(dataSelect.AUDIT_STATUS, v);
        }
      },
      { id: "remark", name: "备注", type: "ro", width: 120 }
    ],
    toolbar: [
      { type: "settings", labelWidth: 90, labelAlign: "right" },
      {
        type: "row",
        list: [
          { type: "calendar", readonly: true, className: "layui-col-md3", name: "startTime", label: "开始时间:", value: startTime },
          { type: "calendar", readonly: true, className: "layui-col-md3", name: "endTime", label: "结束时间:", value: endTime },
          {
            type: "selectTable",
            className: "layui-col-md3",
            placeholder: "请选择预约单位",
            readonly: false,
            table: carrierTable,
            tableWidth: 600,
            tableHeight: 500,
            fieldText: "carrierName",
            fieldValue: "carrierId",
            name: "carrieridList",
            label: "预约单位:",
            listener: function (Grid) {
              setMethod(Grid, carrierTable, function () { });
            }
          },
          { type: "input", className: "layui-col-md3", name: "driver1stname", label: "司机:" },
          { type: "input", className: "layui-col-md3", name: "voucherno", 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-md3 right",
            list: [
              { type: "button", name: "master_register", className: "btn-normal", value: "到场登记" }
            ]
          }
        ]
      }
    ],
    fixedLast: false, // 开启固定列，固定最后一行，倒数第二列不能隐藏
    multiselect: true, // 是否支持多选
    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/appoint/arrivalConfirmQuery", // 数据接口
    type: "post", // 数据提交方式，默认为get
    params: JSON.stringify({ startTime: startTime, endTime: endTime }), // 查询提交参数，分页参数默认为pageNum,pageSize
    page: true, // 是否启用分页
    contentType: "application/json",
    totalCount: "totalCount" // 总记录数字段，默认totalCount
  };
  master_grid = new PlgGrid(master_grid_opts);
  master_grid.renderTo("onSiteRegistration");
  master_grid.loadData();
  master_grid.attachToolBarEvent("onButtonClick", function (name) {
    switch (name) {
      case "master_search":
        var request = master_grid.getToolBarForm().getFormData();
        request.startTime = request.startTime ? Prolog.formatDate(request.startTime) + " 00:00:00" : "";
        request.endTime = request.endTime ? Prolog.formatDate(request.endTime) + " 23:59:59" : "";
        if (request.startTime > request.endTime) {
          layer.msg("开始时间不能大于结束时间");
          return false;
        }
        request.carrierid = request.carrieridList ? JSON.parse(request.carrieridList)[0] : "";
        dealNullQueryCondition(request);
        delete request.carrieridList;
        delete request.gsCodeList;
        master_grid.setParams(JSON.stringify(request));
        master_grid.reload();
        break;
      case "master_reset":
        master_grid.getToolBarForm().clear();
        master_grid.getToolBarForm().setText("carrieridList", "");
        master_grid.getToolBarForm().setFormData({ startTime: startTime, endTime: endTime });
        break;
      case "master_register":
        var ids = master_grid.getCheckedIds();
        if (!ids) {
          layer.msg("请选择数据");
          return false;
        }
        var list = ids.split(",").map(item => {
          return {
            loadbillid: item
          };
        });
        arrivalConfirm(list);
        break;
    }
  });
  // 到场登记
  function arrivalConfirm(list) {
    let pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
    Prolog.ajax({
      url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/appoint/arrivalConfirm", type: "post", contentType: "application/json", data: JSON.stringify(list),
      success: function (res) {
        PlgDialog.close(pload);
        if (res.success) {
          PlgDialog.msg(res.message);
          master_grid.reload();
          return true;
        }
                return false;
      },
      error: function () { PlgDialog.close(pload); return false; }
    });
  }
};