/**
   * 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';
let accessRegistrationInterval = $("#accessRegistration_zhu");
export default () => {
  let indexTime = 0;
  var dataSelete = {};
  //获取下拉框
  getSelectGJ([{ "objectcode": "TMS_APPOINT_TYPE" }], function (data) {
    data.data.TMS_APPOINT_TYPE.unshift({ "text": "", "value": "" });
    dataSelete = data.data;
  });
  var gridLic;
  var gridzhengJinc;
  //配送中心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 carousel = layui.carousel;
  carousel.render({
    elem: "#accessRegistration_div2",
    autoplay: false,
    index: 0,
    arrow: "none",
    indicator: "none",
    anim: "fade",
    width: "100%", //设置容器宽度
    height: "100%"
  });
  let dataView = ["#accessRegistration_zhuA", "#accessRegistration_zhuB", "#accessRegistration_zhuC"];
  function setViewName(name) {
    for (var i = 0; i < dataView.length; i++) {
      if (name == dataView[i]) {
        $(name).css("display", "block");
      }
      else {
        $(dataView[i]).css("display", "none");
      }
    }
  }
  //返回
  $(document).on("click", "#accessRegistration_return", function () {
    setViewName("#accessRegistration_zhuA");
  });
  clearInterval(accessRegistrationInterval.__proto__.timer);
  let dataItem;
  $("#accessRegistration_zhuTableLeftARightBrch").text(userGJ.p_companyname);
  // 更新二维码
  function setViewData() {
    let qrcodeText = userGJ.p_companyid;
    utilSyncAjaxGJ(UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/baseParameter/getBranchScanCode", "post", userGJ.p_companyid, (data) => {
      if (data.success) {
        qrcodeText += data.data.branchScanCode;
      }
    }, "application/json");
    $("#accessRegistration_zhuTableLeftALeft").html("");
    new QRCode("accessRegistration_zhuTableLeftALeft", {
      text: qrcodeText,
      width: 320,
      height: 320,
      colorDark: "#000000",
      colorLight: "#ffffff",
      correctLevel: QRCode.CorrectLevel.H
    });
  }

  setViewData();
  var grid2;
  let PoSingle_div1 = {
    renderer: "accessRegistration_zhuTableLeftB",
    items: [
      { type: "settings", PoSingle: "label-left", labelWidth: 115, labelAlign: "right", inputWidth: 280 },
      { type: "input", name: "driver1stname", className: "no-border", label: "当前司机：", readonly: true },
      { type: "newcolumn" },
      { type: "input", name: "voucherno", className: "no-border", label: "当前车牌：", readonly: true },
      { type: "newcolumn" },
      { type: "input", name: "drivertel", className: "no-border", label: "手&nbsp;&nbsp;机&nbsp;&nbsp;号：", readonly: true },
      { type: "newcolumn" },
      { type: "input", name: "orders", label: "运输单据：", width: 205 },
      { type: "newcolumn" },
      { type: "button", name: "diz_fanh4", className: "btn-normal", value: "提 取" },
      { type: "newcolumn" },
      { type: "input", name: "drivercard", label: "身份证号：" },
      { type: "newcolumn" },
      { type: "input", name: "remark", label: "备&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;注：", width: 205 },
      { type: "newcolumn" },
      { type: "button", name: "diz_fanh3", className: "btn-normal", value: "登 记" }
    ]
  };
  let PoSingle_from1 = new PlgForm(PoSingle_div1);
  PoSingle_from1.attachEvent("onButtonClick", function (name) {
    //登记
    if (name == "diz_fanh3") {
      let data = PoSingle_from1.getFormData();
      utilAjaxGJ(UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/appoint/appointRegister", "post", JSON.stringify({ loadbillid: dataItem.loadbillid, drivercard: data.drivercard, remark: data.remark }), (data) => {
        if (data.success) {
          layer.msg("成功");
          PoSingle_from1.clear();
          PoSingle_from1.enableItem("shipbillno");
          PoSingle_from1.enableItem("diz_fanh4");
          accessRegistrationInterval.__proto__.timer = setInterval(() => {
            getuserData();
          }, 5000);
        }
      }, "application/json");
    }
    //提取
    if (name == "diz_fanh4") {
      let data = PoSingle_from1.getFormData();
      if (data.orders == "" || data.orders == null) {
        layer.msg("请输入运输单据");
        return;
      }
      utilAjaxGJ(UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/appoint/getShipbill", "post", JSON.stringify({ shipbillno: data.orders }), (data) => {
        PlgDialog.msg(data.message);
        if (data.success) {
          PoSingle_from1.setFormData({ shipbillno: data.data.memo });
          PoSingle_from1.disableItem("shipbillno");
          PoSingle_from1.disableItem("diz_fanh4");
        }
        else {
          PoSingle_from1.setFormData({ shipbillno: "" });
        }
      }, "application/json");
    }
  });
  //分页参数
  var opts2 = {
    //terrace显示全部，card分页显示
    skin: "card",
    //renderer:"grid",
    columns: [
      { id: "loadbillid", name: "id", type: "ro", width: "*", hidden: true },
      { id: "voucherno", name: "车辆", type: "ro", width: "80" },
      { id: "driver1stname", name: "司机", type: "ro", width: "80" },
      { id: "drivertel", name: "手机号", type: "ro", width: "100" },
      //{ id: "appointBillNo", name: "预约单号", type: "ro", width: "100" },
      { id: "statusName", name: "状态", type: "ro", width: "80" },
      { id: "status", name: "状态", type: "ro", width: "80", hidden: true },
      { id: "calculateWeight", name: "理论重量", type: "ro", width: "80" },
      { id: "startWhight", name: "入场重量", type: "ro", width: "100" },
      { id: "endWhight", name: "出场重量", type: "ro", width: "100" },
      { id: "differentialWeight", name: "差异", type: "ro", width: "80" },
      { id: "remark", name: "备注", type: "ro", width: "200" },
      {
        id: "buttonType", name: "是否放行", type: "button", width: "160", render: (v, rdata) => {
          if (rdata.buttonType == "1" && rdata.isRelease == "YES") {
            return "进场登记^accessRegistration_addClick;<span style='color:#C0C0C0'>离场登记</span>^accessRegistration_deleteClick";
          }
          if (rdata.buttonType == "2" && rdata.isRelease == "YES") {
            return "<span style='color:#C0C0C0'>进场登记</span>^accessRegistration_addClick;离场登记^accessRegistration_deleteClick";
          }
          return "<span style='color:#C0C0C0'>进场登记</span>^accessRegistration_addClick;<span style='color:#C0C0C0'>离场登记</span>^accessRegistration_deleteClick";

        }
      }
    ],
    multiselect: false,
    fixedLast: true, //开启固定列，固定最后一行，倒数第二列不能隐藏

    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/appoint/appointRegisterQuery",//数据接口
    type: "post",//数据提交方式，默认为get
    contentType: "application/json",
    params: JSON.stringify({ pageNum: 1, pageSize: 500 }),//查询提交参数，分页参数默认为pageNum,pageSize
    page: false, //是否启用分页
    pagebar: null,
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  grid2 = new PlgGrid(opts2);
  grid2.renderTo("accessRegistration_zhuFromRightB");
  grid2.loadData();
  function getuserData() {
    setViewData();
    if (indexTime == 5) {
      indexTime = 0;
      grid2.reload();
    }
    indexTime++;
    utilAjaxGJ(UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/appoint/appointRegisterCatchQuery", "post", "{}", (data) => {
      if (data.data != null) {
        clearInterval(accessRegistrationInterval.__proto__.timer);
        dataItem = data.data;
        dataItem.shipbillno = dataItem.memo;
        dataItem.memo = "";
        PoSingle_from1.setFormData(dataItem);
        if (dataItem.shipbillno != null && dataItem.shipbillno != "") {
          PoSingle_from1.disableItem("shipbillno");
          PoSingle_from1.disableItem("diz_fanh4");
        }
      }
    }, "application/json");
  }
  accessRegistrationInterval.__proto__.timer = setInterval(() => {
    getuserData();
  }, 5000);
  //离场登记
  window.accessRegistration_deleteClick = function (rid, rdata) {
    if (rdata.buttonType == "2" && rdata.isRelease == "YES") {
      utilAjaxGJ(UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/appoint/leaveRegister", "post", JSON.stringify(rdata), (data) => {
        if (data.success) {
          layer.msg("成功");
          grid2.reload();
        }
      }, "application/json");
    }
  };
  //进场登记
  window.accessRegistration_addClick = function (rid, rdata) {

    if (rdata.buttonType == "1" && rdata.isRelease == "YES") {
      utilAjaxGJ(UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/appoint/intoRegister", "post", JSON.stringify(rdata), (data) => {
        if (data.success) {
          layer.msg("成功");
          grid2.reload();
        }
      }, "application/json");
    }
  };
  //返回
  $("#accessRegistration_zhuFromRightC").on("click", () => {
    indexTime = 5;
    getuserData();
  });
  //离场登记table-------------------------------------------------------------

  //分页参数
  var optsLic = {
    //terrace显示全部，card分页显示
    skin: "card",
    //renderer:"grid",
    columns: [
      { id: "appointBillNo", name: "id", type: "ro", hidden: true },
      { id: "branchid", name: "配送中心", type: "ro" },
      { id: "callTime", name: "排号时间", type: "ro" },
      { id: "callNumber", name: "排号", type: "ro" },
      { id: "voucherno", name: "车辆", type: "ro" },
      { id: "driver1stname", name: "司机", type: "ro" },
      {
        id: "appointType", name: "预约类型", type: "ro", render: (v, rd) => {
          return rd.appointTypeName ? rd.appointTypeName : getSelectValue(dataSelete.TMS_APPOINT_TYPE, v);
        }
      },
      { id: "intoTime", name: "进场时间", type: "ro" },
      { id: "callPerson", name: "登记人", type: "ro" },
      { id: "customerName", name: "承运商", type: "ro" },
      { id: "customerName", name: "供应商", type: "ro" },
      { id: "appointPackage", name: "总件数", type: "ro" },
      { id: "appointWhight", name: "总重量", type: "ro" },
      { id: "appointVolume", name: "总体积", type: "ro" },
      { id: "remark", name: "备注", type: "ro" }
    ],
    multiselect: false,
    toolbar: [
      { type: "settings", labelWidth: 90, labelAlign: "right" },
      {
        type: "row",
        list: [
          { type: "calendar", name: "startdate", className: "layui-col-md3", label: "开始时间:" },
          { type: "calendar", name: "enddate", 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;
                gridLic.getToolBarForm().setFormData(datad);
              });
            }
          },
          { type: "hidden", name: "branchid", label: "配送中心:", className: "layui-col-md3" },
          { type: "input", name: "voucherno", label: "车牌号:", className: "layui-col-md3" },
          { type: "input", name: "driver1stname", label: "司机:", className: "layui-col-md3" },
          { type: "combo", name: "appointType", className: "layui-col-md3", label: "预约类型:", options: dataSelete.TMS_APPOINT_TYPE },
          {
            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: "departureRegistration_print", value: "离场登记" }
            ]
          }
        ]
      }],

    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/leaveRegistration/query/v1.0",//数据接口
    type: "post",//数据提交方式，默认为get
    contentType: "application/json",
    params: JSON.stringify({ startdate: ship_FormatDate(new Date()) + " 00:00:00", enddate: FormatDate(new Date()) + " 23:59:59" }),//查询提交参数，分页参数默认为pageNum,pageSize
    page: true, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };
  //分页参数 pageSize pageNum
  gridLic = new PlgGrid(optsLic);
  gridLic.renderTo("accessRegistration_zhu_lichang");
  gridLic.getToolBarForm().setFormData({ "systemId": "prolog-healthy-wms" });
  gridLic.getToolBarForm().setFormData({ startdate: ship_FormatDate(new Date()), enddate: FormatDate(new Date()) });
  // gridLic.loadData();
  //按钮添加方法
  gridLic.attachToolBarEvent("onButtonClick", function (name) {
    //查询
    if (name == "querycarr") {
      var jsonData = gridLic.getToolBarForm().getFormData();
      if (jsonData.startdate == null) {
        jsonData.startdate = ship_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";
      }
      gridLic.setParams(JSON.stringify(jsonData));
      gridLic.reload();
    }
    //重置
    if (name == "car_Reset") {
      gridLic.getToolBarForm().getDForm().setText("buyername1", "");
      gridLic.getToolBarForm().clear();
      gridLic.getToolBarForm().setFormData({ startdate: ship_FormatDate(new Date()), enddate: FormatDate(new Date()) });
    }
    //离场登记
    if (name == "departureRegistration_print") {
      let idh = gridLic.getSelectedRowId();
      if (idh == "" || idh == null) {
        layer.msg("请选择一行");
        return;
      }
      let datad = gridLic.getRowData(idh);
      utilAjaxGJ(UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/leaveRegistration/confirm/v1.0", "post", JSON.stringify(datad), () => {
        layer.msg("成功");
        gridLic.reload();
      }, "application/json");
    }
  });

  //进场登记table-------------------------------------------------------------

  //分页参数
  var optszhengjinc = {
    //terrace显示全部，card分页显示
    skin: "card",
    //renderer:"grid",
    columns: [
      { id: "appointBillNo", name: "id", type: "ro", hidden: true },
      { id: "branchname", name: "配送中心", type: "ro" },
      { id: "getNumberTime", name: "取号时间", type: "ro" },
      { id: "callNumber", name: "取号", type: "ro" },
      { id: "voucherno", name: "车辆", type: "ro" },
      { id: "driver1stname", name: "司机", type: "ro" },
      {
        id: "appointType", name: "配送类型", type: "ro", render: (v) => {
          return getSelectValue(dataSelete.TMS_APPOINT_TYPE, v);
        }
      },
      { id: "customerName", name: "承运商", type: "ro" },
      { id: "customerName", name: "供应商", type: "ro" },
      { id: "appointPackage", name: "总件数", type: "ro" },
      { id: "appointWhight", name: "总重量", type: "ro" },
      { id: "appointVolume", name: "总体积", type: "ro" },
      { id: "remark", name: "备注", type: "ro" }
    ],
    multiselect: false,
    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;
                gridzhengJinc.getToolBarForm().setFormData(datad);
              });
            }
          },
          { type: "hidden", name: "branchid", label: "配送中心:" },
          { type: "input", name: "voucherno", className: "layui-col-md3", label: "车牌号:" },
          { type: "input", name: "driver1stname", label: "司机:", className: "layui-col-md3" },
          { type: "combo", name: "appointType", className: "layui-col-md3", label: "配送类型:", options: dataSelete.TMS_APPOINT_TYPE },
          {
            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: "entryRegistration_print", value: "进场登记" }
            ]
          }
        ]
      }],

    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/appoint/intoRegisterQuery",//数据接口
    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
  gridzhengJinc = new PlgGrid(optszhengjinc);
  gridzhengJinc.renderTo("accessRegistration_zhu_Jinc");
  gridzhengJinc.getToolBarForm().setFormData({ "systemId": "prolog-healthy-wms" });
  gridzhengJinc.getToolBarForm().setFormData({ sdate: ship_FormatDate(new Date()), edate: FormatDate(new Date()) });
  // gridzhengJinc.loadData();
  //按钮添加方法
  gridzhengJinc.attachToolBarEvent("onButtonClick", function (name) {
    //查询
    if (name == "querycarr") {
      var jsonData = gridzhengJinc.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";
      }
      gridzhengJinc.setParams(JSON.stringify(jsonData));
      gridzhengJinc.reload();
    }
    //重置
    if (name == "car_Reset") {
      gridzhengJinc.getToolBarForm().getDForm().setText("buyername1", "");
      gridzhengJinc.getToolBarForm().clear();
      gridzhengJinc.getToolBarForm().setFormData({ sdate: ship_FormatDate(new Date()), edate: FormatDate(new Date()) });
    }
    //进场登记
    if (name == "entryRegistration_print") {
      let idh = gridzhengJinc.getSelectedRowId();
      if (idh == "" || idh == null) {
        layer.msg("请选择一行");
        return;
      }
      let datad = gridzhengJinc.getRowData(idh);
      utilAjaxGJ(UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/appoint/intoRegister", "post", JSON.stringify(datad), () => {
        layer.msg("成功");
        gridzhengJinc.reload();
      }, "application/json");
    }
  });
};