/**
 * zzx
 * 2020.09.10
 * 叫号登记
 */
 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';

let pickUpPlatformC_zhu_timerCount = $("#pickUpPlatformC_zhu");
export default () => {
  var grid, grid2, gridB;
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,
}

  // 清空定时器
  clearInterval(pickUpPlatformC_zhu_timerCount.__proto__.timer);
  pickUpPlatformC_zhu_timerCount.__proto__.timer = setInterval(() => {
    grid.reload();
    // gridB.clearAll();
  }, 60000);

  // 站台table
  var platform_table = {
    columns: [
      { id: "platformId", name: "id", type: "ro", width: "130", hidden: true },
      { id: "platform", name: "站台编号", type: "ro" },
      { id: "platformName", name: "站台名称", type: "ro" }
    ],
    multiselect: false,
    pagebar: { type: "simple" },
    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/platform/platformQuery",//数据接口
    contentType: "application/json",
    type: "post",//数据提交方式，默认为get
    params: JSON.stringify({
      "platformTypes": ["01", "02"],
      "status": "01"
    }), //查询提交参数，分页参数默认为pageNum,pageSize
    page: true, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };

  //分页参数
  var opts = {
    //terrace显示全部，card分页显示
    skin: "card",
    //renderer:"grid",
    columns: [
      { id: "loadbillid", name: "id", type: "ro", width: "130", hidden: true },
      { id: "status", name: "状态", type: "ro", width: "120", hidden: true },
      { id: "statusName", name: "状态", type: "ro", width: "120" },
      { id: "branchid", name: "机构号", type: "ro", width: "120", hidden: true },
      { id: "branchname", name: "机构名称", type: "ro", width: "120" },
      { id: "billdate", name: "单据日期", type: "ro", width: "120" },
      { id: "carrierid", name: "承运商", type: "ro", width: "120", hidden: true },
      { id: "carrierno", name: "承运商编号", type: "ro", width: "120", hidden: true },
      { 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", hidden: true },
      { id: "driver1stname", name: "司机名称", type: "ro", width: "120" },
      { id: "drivertel", name: "司机电话", type: "ro", width: "120" },
      { id: "drivercard", name: "司机身份证", type: "ro", width: "120", hidden: true },
      { 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: "op", name: "操作", type: "button", align: "center", fixed: "right", width: "100", render: function (v, rd) {
          let list = [];
          if (rd.releaseMark == "1") {
            list.push("空车放行^pickUpPlatformC_emptyCarRelease");
          }
          if (rd.status == 24) {
            list.push("插队^pickUpPlatformC_jumpInLine");
          }
          let text = list.length ? list.join(",") : " ";
          return text;
        }
      }
    ],
    multiselect: false,
    fixedLast: true,
    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/callRegistration/query",//数据接口
    type: "post",//数据提交方式，默认为get
    contentType: "application/json",
    params: JSON.stringify({ "branchid": userinfo.p_companyid, pageNum: 1, pageSize: 9999 }),//查询提交参数，分页参数默认为pageNum,pageSize
    page: false, //是否启用分页
    pagebar: null,
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  grid = new PlgGrid(opts);
  grid.renderTo("pickUpPlatformC_zhuTableLeftB1");
  grid.loadData();

  window.pickUpPlatformC_emptyCarRelease = function (rid) {
    layer.open({
      title: "空车放行",
      content: "是否空车放行？",
      btn: ["确认", "取消"],
      yes: function () {
        let request = {
          loadbillid: rid,
          branchid: userinfo.p_companyid
        };
        let pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
        Prolog.ajax({
          url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/callRegistration/emptyRelease", type: "post", contentType: "application/json", data: JSON.stringify(request),
          success: function (res) {
            PlgDialog.close(pload);
            if (res.success) {
              layer.closeAll();
              PlgDialog.msg(res.message);
              grid.reload();
              // gridB.clearAll();
              return true;
            }
                        return false;

          }, error: function () { PlgDialog.close(pload); return false; }
        });
      }
    });
  };
  // 插队
  window.pickUpPlatformC_jumpInLine = function (rid) {
    layer.open({
      title: "插队",
      content: "是否插队？",
      btn: ["确认", "取消"],
      yes: function () {
        let request = {
          loadbillid: rid
        };
        let pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
        Prolog.ajax({
          url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/callRegistration/jumpInLine", type: "post", contentType: "application/json", data: JSON.stringify(request),
          success: function (res) {
            PlgDialog.close(pload);
            if (res.success) {
              layer.closeAll();
              PlgDialog.msg(res.message);
              grid.reload();
              // gridB.clearAll();
              return true;
            }
                        return false;

          }, error: function () { PlgDialog.close(pload); return false; }
        });
      }
    });
  };

  // //单击事件
  // grid.on("onRowSelect", function () {
  //   var data = grid.getSelectedRowData();
  //   if (data.orders == null) {
  //     return;
  //   }
  //   gridB.setParams(JSON.stringify({ "loadbillid": data.loadbillid, "branchid": data.branchid }));
  //   gridB.reload();
  // });
  // //到场车辆明细信息
  // var optsB = {
  //   //terrace显示全部，card分页显示
  //   skin: "card",
  //   //renderer:"grid",
  //   columns: [
  //     { id: "seqno", name: "id", type: "ro", width: "130", hidden: true },
  //     { id: "branchname", name: "装卸点", type: "ro", width: 120 },
  //     { id: "arriverName", name: "售达方", type: "ro", width: 120 },
  //     { id: "orders", name: "运单号", type: "ro", width: 120 },
  //     { id: "refbillno", name: "业务单号", type: "ro", width: 120 },
  //     { id: "itemno", name: "商品编号", type: "ro", width: 120 },
  //     { id: "itemname", name: "商品名称", type: "ro", width: 120 },
  //     { id: "codenumber", name: "打码号", type: "ro", width: 120 },
  //     { id: "pcs", name: "件数", type: "ro", width: 120 },
  //     { id: "stocknumber", name: "实时库存", type: "ro", width: 120 },
  //     { id: "realstocknumber", name: "扣除预占库存", type: "ro", width: 120 }
  //   ],
  //   multiselect: false,
  //   imagePath: "/tms/prologui/dist/images/PlgGrid",
  //   url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/platformRelease/applyApointbillDetailQuery",//数据接口
  //   type: "post",//数据提交方式，默认为get
  //   contentType: "application/json",
  //   params: "{}", //查询提交参数，分页参数默认为pageNum,pageSize
  //   page: false, //是否启用分页
  //   totalCount: "totalCount" //总记录数字段，默认totalCount
  // };
  // gridB = new PlgGrid(optsB);
  // gridB.renderTo("pickUpPlatformC_zhuTableLeftB2");
  //分页参数
  var opts2 = {
    //terrace显示全部，card分页显示
    skin: "card",
    //renderer:"grid",
    columns: [
      { id: "platformId", name: "id", type: "ro", width: "130", hidden: true },
      { id: "platform", name: "站台编码", type: "ro", width: "100" },
      { id: "modelType", name: "最大车型", type: "ro", width: "100", hidden: true },
      { id: "modelTypeName", name: "最大车型", type: "ro", width: "100" },
      { id: "vehicleNo", name: "装货车牌", type: "ro", width: "100" },
      {
        id: "op", name: "操作", type: "button", align: "center", fixed: "right", render: function (v, rd) {
          let text = rd.status == "02" ? "释放^pickUpPlatformC_release;站台置换^pickUpPlatformC_permutation" : "<span style='color:#C0C0C0'>已释放</span>^carrier_updataClick3";
          return text;
        }
      }
    ],
    multiselect: false,
    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/platform/platformQuery",//数据接口
    type: "post",//数据提交方式，默认为get
    contentType: "application/json",
    params: JSON.stringify({ platformTypes: ["01", "02"], "branchid": userinfo.p_companyid, pageNum: 1, pageSize: 9999 }),//查询提交参数，分页参数默认为pageNum,pageSize
    page: false, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  grid2 = new PlgGrid(opts2);
  grid2.renderTo("pickUpPlatformC_zhuFromRightB");
  grid2.loadData();
  //释放
  window.pickUpPlatformC_release = function (v, rdata) {
    let request = {
      branchid: rdata.branchid,
      userplatform: rdata.platform,
      vehicleNo: rdata.vehicleNo
    };
    utilAjaxGJ(UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/callRegistration/platformRelease", "post", JSON.stringify(request), () => {
      layer.msg("成功");
      grid.reload();
      // gridB.clearAll();
      grid2.reload();
    }, "application/json");
  };
  // 站台置换
  window.pickUpPlatformC_permutation = function (v, rdata) {
    platformReplacementDialog(rdata);
  };
  window.carrier_updataClick3 = function () { };

  // 按钮事件
  $(".pickUpPlatformC_zhu_btn").click(function () {
    let name = $(this).attr("name");
    // 叫号
    if (name == "pickUpPlatformC_zhu_btn_save") {
      let idh = grid.getSelectedRowId();
      if (idh == "" || idh == null) {
        layer.msg("请选择一行车辆");
        return;
      }
      let datah = grid.getRowData(idh);
      if (datah.isCall == "0") {
        layer.msg("当前车辆已叫号");
        return;
      }
      let idd = grid2.getSelectedRowId();
      if (idd == "" || idd == null) {
        layer.msg("请选择一行站台");
        return;
      }
      let datad = grid2.getRowData(idd);
      if (datad.status == "02") {
        layer.msg("该站台被占用");
        return;
      }
      let request = {
        loadbillid: grid.getSelectedRowId(),
        branchid: userinfo.p_companyid,
        forceMark: 0,
        platform: datad.platform
      };
      platformCalling(request);
    }
    //刷新
    else if (name == "pickUpPlatformC_zhu_btn_refresh") {
      grid.reload();
      // gridB.clearAll();
      grid2.reload();
    }
  });

  // 叫号请求
  function platformCalling(request) {
    let pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
    Prolog.ajax({
      url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/callRegistration/platformCallnum", type: "post", contentType: "application/json", data: JSON.stringify(request),
      success: function (res) {
        PlgDialog.close(pload);
        if (res.success) {
          layer.closeAll();
          PlgDialog.msg(res.message);
          grid.reload();
          // gridB.clearAll();
          grid2.reload();
        }
        else if (res.data && request.forceMark == 0) {
          dialogOpen(res.message);
        }
        else {
                  }
      }, error: function () { PlgDialog.close(pload); return false; }
    });
  }

  // 叫号弹窗
  function dialogOpen(message) {
    let dialog_form;
    let html = "";
    html += "<div id ='pickUpPlatformC_dialog'>";
    html += "   <div style='color: red;font-size: 16px;'>" + message + "</div>";
    html += "   <div id ='pickUpPlatformC_dialogForm'></div>";
    html += "</div>";
    PlgDialog.open({
      title: "强制叫号",
      shadeClose: false,
      type: 1, // 设置为1，手动控制dialog的开关
      area: ["450px", "250px"],
      content: html, //iframe的url
      btn: ["是", "否"],
      btnAlign: "c",
      yes: function () {
        let vdata = dialog_form.validate();
        if (!vdata) return false;
        let { forceMarkPerson, forceMarkPassword, forceMarkMemo } = dialog_form.getFormData();
        let request = {
          loadbillid: grid.getSelectedRowId(),
          branchid: userinfo.p_companyid,
          platform: grid2.getSelectedRowId(),
          forceMark: 1,
          forceMarkPerson,
          forceMarkPassword,
          forceMarkMemo
        };
        platformCalling(request);
      }
    });

    var dialog_form_opts = {
      renderer: "pickUpPlatformC_dialogForm",
      items: [
        { type: "settings", position: "label-left", labelWidth: 60 },
        {
          type: "block", width: "*",
          list: [
            { type: "input", name: "forceMarkPerson", label: "授权账号:", width: 250, required: true },
            { type: "input", name: "forceMarkPassword", label: "授权密码:", width: 250, required: true },
            { type: "input", name: "forceMarkMemo", label: "备注:", width: 250 }
          ]
        }
      ]
    };
    dialog_form = new PlgForm(dialog_form_opts);
  }

  // 站台置换弹窗
  function platformReplacementDialog(params) {
    let dialog_form;
    let html = "";
    html += "<div id ='pickUpPlatformC_platformReplacementDialog'>";
    html += "   <div id ='pickUpPlatformC_platformReplacementDialogForm'></div>";
    html += "</div>";
    PlgDialog.open({
      title: "站台置换",
      shadeClose: false,
      type: 1, // 设置为1，手动控制dialog的开关
      area: ["450px", "250px"],
      content: html, //iframe的url
      btn: ["置换", "取消"],
      btnAlign: "c",
      yes: function () {
        let formData = dialog_form.getFormData();
        if (!JSON.parse(formData.emptyplatformList) || !JSON.parse(formData.emptyplatformList).length) {
          layer.msg("请选择置换站台");
          return;
        }
        let request = {
          vehicleNo: formData.vehicleNo,
          userplatform: formData.platform,
          branchid: userinfo.p_companyid,
          emptyplatform: JSON.parse(formData.emptyplatformList)[0]
        };
        platformsReplace(request);
      }
    });

    var dialog_form_opts = {
      renderer: "pickUpPlatformC_platformReplacementDialogForm",
      items: [
        { type: "settings", position: "label-left", labelWidth: 60 },
        {
          type: "block", width: "*",
          list: [
            { type: "input", name: "branchname", label: "配送中心:", width: 250, className: "no-border", readonly: true },
            { type: "input", name: "platform", label: "当前站台:", width: 250, className: "no-border", readonly: true },
            {
              width: 250,
              type: "selectTable",
              placeholder: "请选择置换站台",
              readonly: false,
              table: platform_table,
              tableWidth: 500,
              tableHeight: 400,
              fieldText: "platformName",
              fieldValue: "platform",
              name: "emptyplatformList",
              label: "置换站台:",
              listener: function (Grid) {
                setMethod(Grid, platform_table, function () { });
              }
            }
          ]
        }
      ]
    };
    dialog_form = new PlgForm(dialog_form_opts);
    dialog_form.setFormData(params);
  }
  // 置换站台
  function platformsReplace(request) {
    let pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
    Prolog.ajax({
      url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/callRegistration/platformReplace", type: "post", contentType: "application/json", data: JSON.stringify(request),
      success: function (res) {
        PlgDialog.close(pload);
        if (res.success) {
          layer.closeAll();
          PlgDialog.msg(res.message);
          grid.reload();
          // gridB.clearAll();
          grid2.reload();
        }
        else {
                  }
      }, error: function () { PlgDialog.close(pload); return false; }
    });
  }
};