/**
   * guoj
   * 2020.06.22
   * 到场确认
      */
    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 dataSelete = {};
  //获取下拉框
  getSelectGJ([{ "objectcode": "TMS_APPOINT_TYPE" },{ "objectcode": "TIME_SECTION" }], function(data) {
      data.data.TMS_APPOINT_TYPE.unshift({ "text": "", "value": "" });
      dataSelete = data.data;
  });
  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/queryPubCompany", //数据接口
  //     type: "post", //数据提交方式，默认为get
  //     pagebar: { type: "simple" },
  //     params: JSON.stringify({"companytype": "S"}), //查询提交参数，分页参数默认为pageNum,pageSize
  //     page: true, //是否启用分页
  //     totalCount: "totalCount" //总记录数字段，默认totalCount
  // };
  //单位table
  var company_Table = {
      columns: [
        { id: "carrierId", name: "id", type: "ro", width: "130", hidden: true },
        { id: "carrierCode", name: "单位编号", type: "ro" },
        { id: "carrierName", name: "单位名称", type: "ro" }
      ],
      multiselect: false,

      url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/tmsBaseCarrier/queryTmsBaseCarrierForBooking", //数据接口
      type: "post", //数据提交方式，默认为get
      contentType: "application/json",
      pagebar: { type: "simple" },
      params: "{}", //查询提交参数，分页参数默认为pageNum,pageSize
      page: true, //是否启用分页
      totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  //分页参数
  var opts = {
    //terrace显示全部，card分页显示
    skin: "card",
    //renderer:"grid",
    columns: [
      { id: "appointBillNo", name: "id", type: "ro", width: "80", hidden: true },
      { id: "branchname", name: "配送中心", type: "ro", width: "100" },
      { id: "appointBillNo", name: "预约单号", type: "ro", width: "100" },
      { id: "appointDate", name: "预约日期", type: "ro", width: "80" },
      { id: "appointTime", name: "预约时间", type: "ro", width: "80" ,render: (v)=>{
        return getSelectValue(dataSelete.TIME_SECTION ,v);
      }},
      { id: "appointType", name: "预约类型", type: "ro", width: "70" ,render: (v)=>{
        return getSelectValue(dataSelete.TMS_APPOINT_TYPE,v);
      }},
      { id: "customerName", name: "预约单位", type: "ro", width: "80" },
      { id: "appointVehicle", name: "预约车辆", type: "ro", width: "80" },
      { id: "appointDriver", name: "预约司机", type: "ro", width: "100" },
      { id: "appointDriverTel", name: "手机号", type: "ro", width: "80" },
      { id: "appointPackage", name: "预约件数", type: "ro", width: "80" },
      { id: "appointWhight", name: "预约重量", type: "ro",width: "80" },
      { id: "memo", name: "备注", type: "ro", width: "80" }
    ],
    multiselect: true,
    toolbar: [
      { type: "settings", labelWidth: 90, labelAlign: "right" },
      {
        type: "row",
        list: [
          { type: "calendar", name: "sdate",className: "layui-col-md3", label: "开始时间:" },
          { type: "calendar", name: "edate",className: "layui-col-md3", label: "结束时间:" },
          // {
          //   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: "combo", name: "appointType", className: "layui-col-md3", label: "预约类型:",options: dataSelete.TMS_APPOINT_TYPE },
          {
            className: "layui-col-md3",
            type: "selectTable",
            placeholder: "请选择单位",
            readonly: false,
            table: company_Table,
            tableWidth: 500,
            tableHeight: 400,
            fieldText: "carrierCode",
            fieldValue: "carrierId",
            name: "customercode1",
            label: "单位编号:",
            listener: function(consignorGrid) {
                setMethod(consignorGrid, company_Table, function(data) {
                    var fromdata = grid.getToolBarForm().getFormData();
                    data = fromdata.customercode1.length == 2 ? "" : data;
                    var datad = {};
                    datad.customerId = data.carrierId;
                    grid.getToolBarForm().setFormData(datad);
                });
            }
          },
          { type: "hidden", name: "customerId", label: "预约单位:" },
          { type: "input", name: "appointDriver",className: "layui-col-md3", label: "司机:" },
          { type: "input", name: "appointVehicle",className: "layui-col-md3", label: "车牌号:" },
          {
            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", className: "btn-addBtn btn-normal", name: "arrivalConfirmation_print", value: "到场确认" },
              { type: "newcolumn" },
              { type: "button", name: "car_export", className: "normal", value: "导 出" }
            ]
          }
        ]
      }],

    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/bookingManage/arrivalConfirmQuery",//数据接口
    type: "post",//数据提交方式，默认为get
    contentType: "application/json",
    params: JSON.stringify({ sdate: ship_FormatDate(new Date()) + " 00:00:00", edate: FormatDate(new Date()) + " 23:59:59" }),//查询提交参数，分页参数默认为pageNum,pageSize
    page: true, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  //分页参数 pageSize pageNum
  grid = new PlgGrid(opts);
  grid.renderTo("arrivalConfirmation_zhu");
  grid.getToolBarForm().setFormData({ "systemId": "prolog-healthy-wms" });
  grid.getToolBarForm().setFormData({ sdate: ship_FormatDate(new Date()),edate: FormatDate(new Date())});
  grid.loadData();
  //按钮添加方法
  grid.attachToolBarEvent("onButtonClick", function (name) {
    //查询
    if (name == "querycarr") {
      var jsonData = grid.getToolBarForm().getFormData();
      if (jsonData.sdate == null) {
        jsonData.sdate = ship_FormatDate(new Date()) + " 00:00:00";
      }
      else {
        jsonData.sdate = Prolog.formatDate(jsonData.sdate) + " 00:00:00";
      }
      if (jsonData.edate == null) {
        jsonData.edate = FormatDate(new Date()) + " 23:59:59";
      }
      else {
        jsonData.edate = Prolog.formatDate(jsonData.edate) + " 23:59:59";
      }
      grid.setParams(JSON.stringify(jsonData));
      grid.reload();
    }
    //重置
    if (name == "car_Reset") {
      // grid.getToolBarForm().getDForm().setText("buyername1", "");
      grid.getToolBarForm().getDForm().setText("customercode1", "");
      grid.getToolBarForm().clear();
      grid.getToolBarForm().setFormData({ sdate: ship_FormatDate(new Date()),edate: FormatDate(new Date())});
    }
    //到场确认
    if (name == "arrivalConfirmation_print") {
      let data = getDeleteAll(grid);
      if(data == "" || data == null){
        layer.msg("请选择一行数据");
        return;
      }
      for(var i = 0;i < data.length;i++){
        data[i] = grid.getRowData(data[i]);
      }
      utilAjaxGJ(UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/bookingManage/arrivalConfirm", "post", JSON.stringify(data), (data)=>{
        layer.msg(data.message);
        grid.reload();
      }, "application/json");
    }
    //导出
    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+"/bookingManage/arrivalConfirmExport";
      let name = "到场确认" + Prolog.formatDate(new Date(), "%YY%MM%DD") + ".xls";
      var jsonData = grid.getToolBarForm().getFormData();
      if (jsonData.sdate == null) {
        jsonData.sdate = ship_FormatDate(new Date()) + " 00:00:00";
      }
      else {
        jsonData.sdate = Prolog.formatDate(jsonData.sdate) + " 00:00:00";
      }
      if (jsonData.edate == null) {
        jsonData.edate = FormatDate(new Date()) + " 23:59:59";
      }
      else {
        jsonData.edate = Prolog.formatDate(jsonData.edate) + " 23:59:59";
      }
      Prolog.exportFile(url, name, "application/json", "post", jsonData);
      PlgDialog.close(pload);

  }
};