/**
   * chengj
   * 2020.10.23
   * 冷链运输查询
   */
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;
  //配送中心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
  };
  // 获取默认时间
  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" }
  ], 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: "linenane", name: "线路", type: "ro", width: "100" },
      { 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: "temperatureRequire", name: "运输温度要求", type: "ro", width: "100", render: (v) => {
          return getSelectValue(dataSelect.TEMP_REQ, v);
        }
      },
      { id: "shipfromtemperature", name: "出库温度", type: "ro", width: "100" },
      { id: "shiptotemperature", name: "到货温度", type: "ro", width: "100" },
      { id: "shiptohjwd", name: "到货环境温度", type: "ro", width: "100" },
      { id: "appointDate", name: "预约时间", type: "ro", width: "100" },
      { id: "loadingdfrom", name: "装车时间", type: "ro", width: "100" },
      { id: "departuretime", name: "发车时间", type: "ro", width: "100" },
      { id: "returndate", name: "回车时间", type: "ro", width: "100" },
      { id: "signdate", name: "签收时间", type: "ro", width: "100" },
      { id: "signqty", name: "签收件数", type: "ro", width: "100" },
      { id: "memo", 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: "buyername1",
            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: "shipbillno", className: "layui-col-md3", label: "运单号:" },
          { type: "input", name: "refbillno", className: "layui-col-md3", label: "业务单号:" },
          { type: "input", name: "voucherno", className: "layui-col-md3", label: "车辆:" },
          { type: "input", name: "driver1stname", className: "layui-col-md3", label: "司机:" },
          {
            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-md3 right",
            list: [
              { type: "button", name: "car_export", className: "btn-normal", value: "导 出" }
            ]
          }
        ]
      }],
    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/report/coldTransportQuery",//数据接口
    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("coldTransportQuery_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;
      }
      grid.setParams(JSON.stringify(jsonData));
      grid.reload();
    }
    //重置
    if (name == "car_Reset") {
      grid.getToolBarForm().getDForm().setText("buyername1", "");
      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/coldTransportExport";
    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;
    }
    let pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
    Prolog.exportFile(url, name, "application/json", "post", jsonData);
    PlgDialog.close(pload);

  }
};