/**
 * guoj
 * 2020.05.25
 * 车辆规格管理
    */
    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 () => {
  let fromDataJson;
  var grid;
  var ddfrom = null;
  let dataSelete;
  //获取下拉框
  getSelectGJ([{ "objectcode": "VEHICLE_SMALL_TYPE" }], function (data) {
    data.data.VEHICLE_SMALL_TYPE.unshift({ "text": "", "value": "" });
    dataSelete = data.data;
  });
  var opts = {
    skin: "card",
    columns: [
      { id: "id", name: "id", type: "ro", width: "100", hidden: true },
      { id: "vehicleSpecsNo", name: "规格编号", type: "ro", width: 150 },
      { id: "vehicleSpecsName", name: "规格名称", type: "ro", width: 150 },
      {
        id: "length", name: "长（M）", type: "ro", width: 100
      },
      { id: "width", name: "宽（M）", type: "ro", width: 100 },
      { id: "height", name: "高（M）", type: "ro", width: 100 },
      {
        id: "vol", name: "体积（m³）", type: "ro", width: 100
      },
      {
        id: "maxVol", name: "最大装载体积（m³）", type: "ro", width: 140
      },
      {
        id: "maxWeight", name: "最大载重重量（kg）", type: "ro", width: 140
      },
      {
        id: "maxPkg", name: "最大装载件数", type: "ro", width: 140
      },
      {
        id: "maxLoadRate", name: "最大装载率", type: "ro", width: 140
      },
      {
        id: "minLoadRate", name: "最小装载率", type: "ro", width: 140
      },
      {
        id: "isSmall", name: "大小分类", type: "ro", width: 140, render: function (v) { return Prolog.getNameByID(dataSelete.VEHICLE_SMALL_TYPE, v); }
      },
      {
        id: "memo", name: "备注", type: "ro", width: 140
      },
      {
        id: "op", name: "操作", type: "button", align: "center", fixed: "right", width: 200, render: function () {
          return "删除^vehicleSpecification_deleteClick,修改^vehicleSpecification_updataClick,复制^vehicleSpecification_copy,详情^vehicleSpecification_details";
        }
      }
    ],
    multiselect: true,
    fixedLast: true,
    toolbar: [
      { type: "settings", labelWidth: 90, labelAlign: "right" },
      {
        type: "row",
        list: [
          { type: "input", name: "vehicleSpecsNo", className: "layui-col-md3", label: "规格编号:" },
          { type: "input", name: "vehicleSpecsName", className: "layui-col-md3", label: "规格名称:" },
          {
            type: "block", className: "layui-col-md3 form_btn_left",
            list: [
              { type: "button", className: "btn-normal", name: "querywork", value: "查 询" },
              { type: "newcolumn" },
              { type: "button", name: "work_Reset", className: "normal", value: "重 置" }
            ]
          },
          {
            type: "block", className: "layui-col-md3 right",
            list: [
              { type: "button", className: "btn-addBtn btn-normal", name: "addBtn", value: "<i class='layui-icon layui-icon-add-1'></i>新 增" }
            ]
          }
        ]
      }
    ],
    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/tBaseVehicleSpecs/queryTBaseVehicleSpecs",
    type: "post",
    pagebar: {
      buttons: [
        { text: "删除", className: "layui-btn-danger", name: "delete" }
      ]
    },
    contentType: "application/json",
    params: JSON.stringify({ pageNum: 1, pageSize: 10 }),
    page: true,
    totalCount: "totalCount"
  };
  //分页参数 pageSize pageNum
  grid = new PlgGrid(opts);
  grid.renderTo("vehicleSpecification_guojzhuans2");
  $("#vehicleSpecification_guojzhuans2").addClass("layui-form");
  grid.attachEvent("onDataReady", function () {
    setTimeout(function () {
      layui.form.render();
    }, 200);
  });
  grid.loadData();

  //按钮添加方法
  grid.attachToolBarEvent("onButtonClick", function (name) {
    if (name == "querywork") {
      var fdata = grid.getToolBarForm().getFormData();
      grid.setParams(JSON.stringify(fdata));
      grid.reload();
    }
    if (name == "addBtn") {
      setView("#vehicleSpecification_div1");
      $("#vehicleSpecification_div4").html("");
      ddfrom = new PlgForm(fromDataJson);
      ddfrom.enableItem("vehicleSpecsNo");
      $("#vehicleSpecification_title").html("新增");
    }
    if (name == "work_Reset") {
      grid.getToolBarForm().clear();
    }
  });
  //按钮添加方法
  grid.attachPageBarEvent(function (name) {
    //删除
    if (name == "delete") {
      let data = [];
      let datas = getDeleteAll(grid);
      datas.forEach(v => {
        data.push({ id: v });
      });
      utilDelete(UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/tBaseVehicleSpecs/deleteTBaseVehicleSpecs", "post", JSON.stringify(data), function (data) {
        grid.reload();
      }, "application/json");
    }
  });
  //删除
  window.vehicleSpecification_deleteClick = function (rid) {
    grid.selectRowById(rid);
    utilDelete(UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/tBaseVehicleSpecs/deleteTBaseVehicleSpecs", "post", JSON.stringify([{ id: rid }]), function (data) {

      grid.reload();
    }, "application/json");
  };
  //修改
  window.vehicleSpecification_updataClick = function (rid, data) {
    $("#vehicleSpecification_div4").html("");
    ddfrom = new PlgForm(fromDataJson);
    ddfrom.disableItem("vehicleSpecsNo");
    grid.selectRowById(rid);
    ddfrom.setFormData(data);
    setView("#vehicleSpecification_div1");
    $("#vehicleSpecification_title").html("编辑");
  };
  //复制
  window.vehicleSpecification_copy = function (rid, data) {
    $("#vehicleSpecification_div4").html("");
    ddfrom = new PlgForm(fromDataJson);
    ddfrom.enableItem("vehicleSpecsNo");
    grid.selectRowById(rid);
    data.id = "";
    ddfrom.setFormData(data);
    setView("#vehicleSpecification_div1");
    $("#vehicleSpecification_title").html("新增");
  };
  //详情
  window.vehicleSpecification_details = function (rid, rdata) {
    grid.selectRowById(rid);

    for (let key in rdata) {
      rdata[key] = rdata[key] == "0" ? "0" : rdata[key];
    }

    setView("#vehicleSpecification_div9");
    new PlgPanel({
      renderer: "vehicleSpecification_div422",
      skin: 2,
      header: {
        isShow: true,
        title: "基本信息"
      },
      defaultBody: {
        layoutCol: 3,
        valueBj: true,
        cols: [
          [{
            label: "规格编号",
            value: rdata.vehicleSpecsNo
          },
          {
            label: "规格名称",
            value: rdata.vehicleSpecsName
          },
          {
            label: "长（M）",
            value: rdata.length
          },
          {
            label: "宽（M）",
            value: rdata.width
          }],
          [
            {
              label: "高（M）",
              value: rdata.height
            },
            {
              label: "体积（m³）",
              value: rdata.vol
            },
            {
              label: "最大装载体积（m³）",
              value: rdata.maxVol
            },
            {
              label: "最大载重重量（kg）",
              value: rdata.maxWeight
            }
          ],
          [
            {
              label: "最大装载件数",
              value: rdata.maxPkg
            },
            {
              label: "最大装载率",
              value: rdata.maxLoadRate
            },
            {
              label: "最小装载率",
              value: rdata.minLoadRate
            },
            {
              label: "大小分类",
              value: Prolog.getNameByID(dataSelete.VEHICLE_SMALL_TYPE, rdata.isSmall)
            }
          ],
          [
            {
              label: "最多配送网点数",
              value: rdata.maxLocations
            },
            {
              label: "最大配送里程",
              value: rdata.maxDistance
            },
            {
              label: "备注",
              value: rdata.memo
            }
          ]
        ]
      }
    });
  };
  //跳转页面
  var carousel = layui.carousel;
  carousel.render({
    elem: "#vehicleSpecification_div2",
    autoplay: false,
    index: 0,
    arrow: "none",
    indicator: "none",
    anim: "fade",
    width: "100%", //设置容器宽度
    height: "100%"
  });
  let viewList = ["#vehicleSpecification_div1", "#vehicleSpecification_div3", "#vehicleSpecification_div9"];
  function setView(name) {
    for (let i = 0; i < viewList.length; i++) {
      if (name == viewList[i]) {
        $(name).css("display", "block");
      }
      else {
        $(viewList[i]).css("display", "none");
      }
    }
  }
  fromDataJson = {
    renderer: "vehicleSpecification_div4",
    items: [
      { type: "settings", labelWidth: 120, labelAlign: "right" },
      {
        type: "row",
        list: [
          { type: "hidden", name: "id", label: "id:", className: "layui-col-md4" },
          { type: "input", name: "vehicleSpecsNo", label: "规格编号:", className: "layui-col-md4", validate: "NotEmpty", required: true },
          { type: "input", name: "vehicleSpecsName", label: "规格名称:", className: "layui-col-md4", validate: "NotEmpty", required: true },
          { type: "input", name: "length", label: "长(M):", placeholder: "请输入数字", validate: "PlusNumeric", maxLength: 15, className: "layui-col-md4" }
        ]
      },
      {
        type: "row",
        list: [
          { type: "input", name: "width", label: "宽(M):", placeholder: "请输入数字", validate: "PlusNumeric", maxLength: 15, className: "layui-col-md4" },
          { type: "input", name: "height", label: "高(M):", placeholder: "请输入数字", validate: "PlusNumeric", maxLength: 15, className: "layui-col-md4" },
          { type: "input", name: "vol", label: "体积(m³):", placeholder: "请输入数字", validate: "PlusNumeric", maxLength: 15, className: "layui-col-md4" }
        ]
      },
      {
        type: "row",
        list: [
          { type: "input", name: "maxVol", label: "最大装载体积(m³):", placeholder: "请输入数字", validate: "PlusNumeric", maxLength: 15, className: "layui-col-md4" },
          { type: "input", name: "maxWeight", label: "最大装载重量(kg):", placeholder: "请输入数字", validate: "PlusNumeric", maxLength: 15, className: "layui-col-md4" },
          { type: "input", name: "maxPkg", label: "最大装载件数:", placeholder: "请输入数字", validate: "PlusNumeric", maxLength: 15, className: "layui-col-md4" }
        ]
      },
      {
        type: "row",
        list: [
          { type: "input", name: "maxLoadRate", label: "最大装载率:", placeholder: "请输入数字", validate: "PlusNumeric", maxLength: 15, className: "layui-col-md4" },
          { type: "input", name: "minLoadRate", label: "最小装载率:", placeholder: "请输入数字", validate: "PlusNumeric", maxLength: 15, className: "layui-col-md4" },
          { type: "combo", name: "isSmall", label: "大小分类:", className: "layui-col-md4", options: dataSelete.VEHICLE_SMALL_TYPE, readonly: true }
        ]
      },
      {
        type: "row",
        list: [
          { type: "input", name: "maxLocations", label: "最多配送网点数:", placeholder: "请输入数字", value: "0", validate: "NotNegativeNume", maxLength: 6, className: "layui-col-md4", required: true },
          { type: "input", name: "maxDistance", label: "最大配送里程(km):", placeholder: "请输入数字", value: "0", validate: "PlusNumeric", maxLength: 12, className: "layui-col-md4", required: true },
          { type: "input", name: "memo", label: "备注:", className: "layui-col-md4" }
        ]
      }
    ]
  };
  //返回
  $(document).on("click", "#vehicleSpecification_return", function () {
    setView("#vehicleSpecification_div3");
  });
  //保存
  document.querySelector("#vehicleSpecification_save").onclick = function () {
    //数据data
    var data = ddfrom.getFormData();
    data.branchid = userGJ.p_companyid;
    var feik = ddfrom.validate();

    console.log(feik);

    if (!feik) {
      return;
    }
    if (!isNumber(data.maxDistance)) {
      PlgDialog.msg("请输入正确的最大配送里程,支持3位小数");
      return;
    }
    for (var key in data) {
      if (key != "vehicleSpecsNo" && key != "vehicleSpecsName" && key != "isSmall" && key != "memo") {
        if (data[key] * 1 < 0) {
          return;
        }
      }
      if (key == "maxLoadRate" || key == "minLoadRate") {
        if (data[key] * 1 > 1) {
          PlgDialog.msg("装载率不能大于1");
          return;
        }
      }
    }
    if (Number(data.maxLoadRate) < Number(data.minLoadRate)) {
        PlgDialog.msg("最大承载率不能小于最小承载率");
        return;
    }
    //请求地址和类型
    var url = UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/tBaseVehicleSpecs/updateTBaseVehicleSpecs";
    var type = "post";
    var contentType = "application/json";
    if (data.id == null || data.id == "") {
      url = UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/tBaseVehicleSpecs/addTBaseVehicleSpecs";
      type = "post";
    }
    //调用ajax方法
    utilAjaxGJ(url, type, JSON.stringify(data), function (data) {

      setView("#vehicleSpecification_div3");
      grid.reload();
    }, contentType);
  };
  //验证数字
  function setMeathodInputIsNaN() {
    let inputs = $("#vehicleSpecification_div4")[0].getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) {
      inputs[i].nameValue = i;
      inputs[i].onblur = (d) => {
        let inputid = d.srcElement;
        let sum = inputid.value;
        let index = inputid.nameValue;
        if (index != 1 && index != 5 && index != 6 && index != 13) {
          if (isNaN(sum)) {
            msgShowLog(inputid);
            return;
          }
          if (sum * 1 < 0) {
            msgShowLog(inputid);
            return;
          }
        }
        if (index == 4 || index == 9) {
          if (sum > 1) {
            msgShowLog(inputid, "请输入0-1的数字");
            return;
          }
        }
      };
    }
  }
  function msgShowLog(input, text) {
    text = text == null ? "请输入数字" : text;
    PlgDialog.msg(text);
    setTimeout(() => {
      input.value = "";
    }, 500);
  }

  function isNumber(value) {
    let reg = /^\d*\.?\d{0,3}$/;
    return reg.test(value);
  }

};
