/**
   * chengj
   * 2020.10.26
   * 预约执行情况查询
   */
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 dataSelect;
  getSelectGJ([
    { "objectcode": "URG_FLG" }, { "objectcode": "TMS_LOADING_BILLSTATUS" }, { "objectcode": "TMS_REF_BILLTYPE" }, { "objectcode": "TMS_SHIP_BILLTYPE" },
    { "objectcode": "TEMP_REQ" }, { "objectcode": "TMS_SHIP_BILLSTATUS" }, { "objectcode": "TMS_LOADING_PLAN_DETAIL_STATUS" }, { "objectcode": "APPOINT_TIME" }
  ], function (data) {
    dataSelect = data.data;
    dataSelect.URG_FLG.unshift({ "value": "", "text": "" });
  });
  //分页参数
  var opts = {
    //terrace显示全部，card分页显示
    skin: "card",
    //renderer:"grid",
    columns: [
      { id: "seqno", name: "id", type: "ro", width: "80", hidden: true },
      { id: "branchname", name: "配送中心", type: "ro", width: "120" },
      { id: "loadbillno", name: "预约单号", type: "ro", width: "100" },
      { id: "shipbillno", name: "运单号", type: "ro", width: "100" },
      { id: "consignorname", name: "货主", type: "ro", width: "80" },
      {
        id: "billtype", name: "运单类型", type: "ro", width: "80", render: (v) => {
          return getSelectValue(dataSelect.TMS_SHIP_BILLTYPE, v);
        }
      },
      { id: "billdate", name: "单据日期", type: "ro", width: "80" },
      {
        id: "urgentIdentify", name: "是否急单", type: "ro", width: "80", render: (v) => {
          return getSelectValue(dataSelect.URG_FLG, v);
        }
      },
      { id: "refbillno", name: "业务单号", type: "ro", width: "100" },
      {
        id: "refbilltype", name: "业务类型", type: "ro", width: "100", render: (v) => {
          return getSelectValue(dataSelect.TMS_REF_BILLTYPE, v);
        }
      },
      { id: "carriername", name: "承运商", type: "ro", width: "120" },
      { id: "shiptoCustomerName", name: "客户名称", type: "ro", width: "120" },
      { id: "shiptoAddress", name: "客户地址", type: "ro", width: "100" },
      { id: "rsumcases", name: "件数", type: "ro", width: "80" },
      { id: "rsumgoodsweight", name: "重量", type: "ro", width: "80" },
      { id: "rsumgoodsvol", name: "体积", type: "ro", width: "80" },
      { id: "driver1stname", name: "司机", type: "ro", width: "80" },
      { id: "voucherno", name: "车辆", type: "ro", width: "80" },
      { id: "appointVehicleModelName", name: "车型", type: "ro", width: "100" },
      {
        id: "temperatureRequire", name: "运输温度要求", type: "ro", width: "100", render: (v) => {
          return getSelectValue(dataSelect.TEMP_REQ, v);
        }
      },
      { id: "appointDate", name: "预约日期", type: "ro", width: "100" },
      {
        id: "appointTime", name: "预约时间", type: "ro", width: "100", render: (v) => {
          return getSelectValue(dataSelect.APPOINT_TIME, v);
        }
      },
      { id: "arriveTime", name: "到场时间", type: "ro", width: "100" },
      { id: "arrivePerson", name: "到场登记人", type: "ro", width: "100" },
      { id: "getNumberTime", name: "取号时间", type: "ro", width: "100" },
      { id: "getNumberPerson", name: "取号登记人", type: "ro", width: "100" },
      { id: "callTime", name: "叫号时间", type: "ro", width: "100" },
      { id: "callPerson", name: "叫号登记人", type: "ro", width: "100" },
      { id: "callNumber", name: "叫号顺序", type: "ro", width: "100" },
      { id: "intoTime", name: "进场时间", type: "ro", width: "100" },
      { id: "intoPerson", name: "进场登记人", type: "ro", width: "100" },
      { id: "startLoadingTime", name: "开始装车时间", type: "ro", width: "100" },
      { id: "startLoadingPerson", name: "开始装车登记人", type: "ro", width: "100" },
      { id: "endLoadingTime", name: "完成装车时间", type: "ro", width: "100" },
      { id: "endLoadingPerson", name: "完成装车登记人", type: "ro", width: "100" },
      { id: "outPlatformTime", name: "站台释放时间", type: "ro", width: "100" },
      { id: "outPlatformPerson", name: "站台释放登记人", type: "ro", width: "100" },
      { id: "outTime", name: "离场时间", type: "ro", width: "100" },
      { id: "outPerson", name: "离场登记人", type: "ro", width: "100" },
      {
        id: "status", name: "状态", type: "ro", width: "100", render: (v) => {
          return getSelectValue(dataSelect.TMS_LOADING_PLAN_DETAIL_STATUS, v);
        }
      },
      { id: "remark", name: "备注", type: "ro", width: "100" }
    ],
    multiselect: false,
    fixedLast: false, //开启固定列，固定最后一行，倒数第二列不能隐藏
    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: "loadbillno", className: "layui-col-md3", 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-md6 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-md6 right",
            list: [
              { type: "button", name: "car_export", className: "btn-normal", value: "导 出" }
            ]
          }
        ]
      }],
    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/report/appointExecuteQuery",//数据接口
    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("appointExecuteQuery_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.voucherno = jsonData.vehicleList ? JSON.parse(jsonData.vehicleList)[0] : "";
      jsonData.branchid = jsonData.branchidList ? JSON.parse(jsonData.branchidList)[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() {
    if (grid.getRowsNum() <= 0) {
      PlgDialog.msg("没有要导出的数据");
      return;
    }
    let url = UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/report/appointExecuteExport";
    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) {
      layer.msg("开始时间不能大于结束时间");
      return false;
    }
    jsonData.carrierid = jsonData.carrieridList ? JSON.parse(jsonData.carrieridList)[0] : "";
    jsonData.voucherno = jsonData.vehicleList ? JSON.parse(jsonData.vehicleList)[0] : "";
    jsonData.branchid = jsonData.branchidList ? JSON.parse(jsonData.branchidList)[0] : "";
    let pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
    Prolog.exportFile(url, name, "application/json", "post", jsonData);
    PlgDialog.close(pload);

  }
};