/***
 * jiw
 * 2020-08-05
 * 承运商车辆管理
 ***/
 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 userinfo = window.localStorage.getItem("userinfo");
  if (userinfo != null) {
    userinfo = JSON.parse(userinfo);
  }

  //车辆规格table
  var userTable = {
    columns: [
      { id: "id", name: "id", type: "ro", width: "130", hidden: true },
      { id: "vehicleSpecsNo", name: "规格编号", type: "ro" },
      { id: "vehicleSpecsName", name: "规格名称", type: "ro" }
    ],
    multiselect: false,
    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/tBaseVehicleSpecs/queryTBaseVehicleSpecs",
    type: "post",
    pagebar: { type: "simple" },
    contentType: "application/json",
    params: "{}",
    page: true,
    totalCount: "totalCount"
  };

  var ddfromH = null;
  var fromData = null;
  var grid = null;
  var dataSelete = {};
  let dataType = 0;
  //获取下拉框
  getSelectGJ([{ "objectcode": "TMS_VEHICLE_BELONG" }, { "objectcode": "TMS_VEHICLE_TYPE" },
  { "objectcode": "TMS_VEHICLE_STATUS" }], function (data) {
    data.data.TMS_VEHICLE_STATUS.unshift({ "text": "", "value": "" });
    data.data.TMS_VEHICLE_TYPE.unshift({ "text": "", "value": "" });
    dataSelete = data.data;
  });
  var opts = {
    //terrace显示全部，card分页显示
    skin: "card",
    //renderer:"grid",
    columns: [
      { id: "vehicleId", name: "id", type: "ro", width: "100", hidden: true },
      { id: "vehicleNo", name: "车牌号", type: "ro", width: 150 },
      { id: "vehicleName", name: "车辆名称", type: "ro", width: 150 },
      {
        id: "vehicleType", name: "车辆类型", type: "ro", width: 100, render: (v) => {
          return getSelectValue(dataSelete.TMS_VEHICLE_TYPE, v);
        }
      },
      { id: "vehicleSpecsName", name: "车辆规格", type: "ro", width: 100 },
      { id: "model", name: "品牌型号", type: "ro", width: 100 },
      {
        id: "belong", name: "归属类型", type: "ro", width: 100, render: (v) => {
          return getSelectValue(dataSelete.TMS_VEHICLE_BELONG, v);
        }
      },
      {
        id: "status", name: "车辆状态", type: "ro", width: 130, render: (v) => {
          return getSelectValue(dataSelete.TMS_VEHICLE_STATUS, v);
        }
      },
      { id: "mileage", name: "当前里程", type: "ro", width: 130 },
      { id: "maxVol", name: "最大装载体积", type: "ro", width: 130 },
      { id: "maxWeight", name: "最大装载重量", type: "ro", width: 130 },
      { id: "maxPkg", name: "最大装载件数", type: "ro", width: 130 },
      { id: "memo", name: "备注", type: "ro", width: 130 },
      {
        id: "op",
        name: "操作",
        type: "button",
        align: "center",
        fixed: "right",
        width: 200,
        render: function () {
          return "删除^vehicleManagementByY_deleteClick,修改^vehicleManagementByY_updataClick,复制^vehicleManagementByY_copy,详情^vehicleManagementByY_details";
        }
      }
    ],
    multiselect: true,
    fixedLast: true,
    toolbar: [
      { type: "settings", labelWidth: 90, labelAlign: "right" },
      {
        type: "row",
        list: [
          { type: "input", name: "vehicleNo", label: "车牌号:", className: "layui-col-md3" },
          { type: "combo", readonly: true, name: "vehicleType", label: "车辆类型:", className: "layui-col-md3", options: dataSelete.TMS_VEHICLE_TYPE },
          { type: "hidden", name: "vehicleSpecs", label: "车辆规格:" },
          {
            className: "layui-col-md3",
            type: "selectTable",
            placeholder: "请选择车辆规格",
            readonly: false,
            table: userTable,
            tableWidth: 500,
            tableHeight: 400,
            fieldText: "vehicleSpecsName",
            fieldValue: "vehicleSpecsNo",
            name: "buyername5",
            label: "车辆规格:",
            listener: function (consignorGrid) {
              setMethod(consignorGrid, userTable, function (data) {
                var datad = {};
                datad.vehicleSpecs = data.id;
                grid.getToolBarForm().setFormData(datad);
              });
            }
          },
          {
            type: "block", className: "layui-col-md2 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-md1 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+"/tBaseVehicle/queryCarrierVehicle",
    type: "post",
    pagebar: {
      buttons: [
        { text: "删除", className: "layui-btn-danger", name: "delete" }
      ]
    },
    contentType: "application/json",
    params: JSON.stringify({ carrierId: userinfo.p_companyid }),
    page: true,
    totalCount: "totalCount"
  };
  //分页参数 pageSize pageNum
  grid = new PlgGrid(opts);
  grid.renderTo("vehicleManagementByY_guojzhuans2");
  grid.loadData();

  //按钮添加方法
  grid.attachToolBarEvent("onButtonClick", function (name) {
    //查询
    if (name == "querywork") {
      var fdata = grid.getToolBarForm().getFormData();
      fdata.type = 1;
      fdata.carrierId = userinfo.p_companyid;
      grid.setParams(JSON.stringify(fdata));
      grid.reload();
    }
    //新增
    if (name == "addBtn") {
      $("#vehicleManagementByY_div4").html("");
      $("#vehicleManagementByY_div4B").html("");
      ddfromH = new PlgForm(fromData);
      cuttingScreen("#vehicleManagementByY_div1");
      $("#vehicleManagementByY_title").html("新增");
    }
    //重置
    if (name == "work_Reset") {
      grid.getToolBarForm().clear();
      grid.getToolBarForm().getDForm().setText("buyername5", "");
      grid.getToolBarForm().getDForm().setText("vendorcode2", "");
      grid.getToolBarForm().setFormData({ consignor: "" });
    }
  });
  //按钮添加方法
  grid.attachPageBarEvent(function (name) {
    //删除
    if (name == "delete") {
      let data = [];
      let datas = getDeleteAll(grid);
      datas.forEach(v => {
        data.push({ vehicleId: v });
      });
      data = JSON.stringify(data);
      utilDelete(UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/tBaseVehicle/deleteTBaseVehicle", "post", data, function (data) {
        grid.reload();
      }, "application/json");
    }
  });
  //删除
  window.vehicleManagementByY_deleteClick = function (rid) {
    grid.selectRowById(rid);
    let data = [];
    let datas = getDeleteAll(grid);
    datas.forEach(v => {
      data.push({ vehicleId: v });
    });
    data = JSON.stringify(data);
    utilDelete(UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/tBaseVehicle/deleteTBaseVehicle", "post", data, function (data) {

      grid.reload();
    }, "application/json");
  };
  //修改
  window.vehicleManagementByY_updataClick = function (rid, data) {
    $("#vehicleManagementByY_div4").html("");
    ddfromH = new PlgForm(fromData);
    grid.selectRowById(rid);
    ddfromH.setFormData(data);
    ddfromH.getDForm().setText("buyername1", data.vehicleSpecsName == null ? "" : data.vehicleSpecsName);
    ddfromH.setItemValue("buyername1",JSON.stringify([data.vehicleSpecs]));
    cuttingScreen("#vehicleManagementByY_div1");
    $("#vehicleManagementByY_title").html("编辑");
  };
  //复制
  window.vehicleManagementByY_copy = function (rid, data) {
    $("#vehicleManagementByY_div4").html("");
    ddfromH = new PlgForm(fromData);
    $("#vehicleManagementByY_div4B").html("");
    grid.selectRowById(rid);
    data.vehicleId = "";
    ddfromH.setFormData(data);
    ddfromH.getDForm().setText("buyername1", data.vehicleSpecs == null ? "" : data.vehicleSpecs);
    cuttingScreen("#vehicleManagementByY_div1");
    $("#vehicleManagementByY_title").html("新增");
  };
  //详情
  window.vehicleManagementByY_details = function (rid, rdata) {
    dataType = 0;
    grid.selectRowById(rid);
    cuttingScreen("#vehicleManagementByY_div9");
    new PlgPanel({
      renderer: "vehicleManagementByY_div422",
      skin: 2,
      header: {
        isShow: true,
        title: "基本信息"
      },
      defaultBody: {
        layoutCol: 3,
        valueBj: true,
        cols: [
          [
            {
              label: "车牌号",
              value: rdata.vehicleNo
            },
            {
              label: "车辆名称",
              value: rdata.vehicleName
            },
            {
              label: "车辆类型",
              value: rdata.vehicleType
            }
          ],
          [
            {
              label: "车辆规格",
              value: rdata.vehicleSpecs
            },
            {
              label: "品牌型号",
              value: rdata.model
            }, {
              label: "归属类型",
              value: rdata.belong
            }
          ],
          [
            {
              label: "车辆状态",
              value: rdata.status
            },
            {
              label: "当前里程",
              value: rdata.mileage
            }
          ],
          [
            {
              label: "最大装载体积",
              value: rdata.maxVol
            }, {
              label: "最大装载重量",
              value: rdata.maxWeight
            },
            {
              label: "最大装载件数",
              value: rdata.maxPkg
            },
            {
              label: "备注",
              value: rdata.memo
            }
          ]
        ]
      }
    });
  };
  //跳转页面
  var carousel = layui.carousel;
  carousel.render({
    elem: "#vehicleManagementByY_div2",
    autoplay: false,
    index: 0,
    arrow: "none",
    indicator: "none",
    anim: "fade",
    width: "100%", //设置容器宽度
    height: "100%"
  });
  fromData = {
    renderer: "vehicleManagementByY_div4",
    items: [
      { type: "settings", position: "label-left", inputWidth: 200, labelWidth: 110, labelAlign: "right" },
      {
        type: "block", width: "*", blockOffset: 0,
        list: [
          { type: "hidden", name: "carrierId", value: userinfo.p_companyid },
          { type: "hidden", name: "vehicleId", label: "id:" },
          { type: "input", name: "vehicleNo", label: "车牌号:", validate: "NotEmpty", required: true },
          { type: "newcolumn" },
          { type: "input", name: "vehicleName", label: "车辆名称:" },
          { type: "newcolumn" },
          {
            width: 200,
            type: "selectTable",
            placeholder: "请选择车辆规格",
            readonly: false,
            table: userTable,
            tableWidth: 500,
            tableHeight: 400,
            fieldText: "vehicleSpecsName",
            fieldValue: "id",
            name: "buyername1",
            label: "车辆规格:<span class=\"dhxform_item_required\">*</span>",
            listener: function (consignorGrid) {
              setMethod(consignorGrid, userTable, function (data) {
                var datad = {};
                datad.vehicleSpecs = data.id;
                datad.vehicleSpecsName = data.vehicleSpecsName;
                ddfromH.setFormData(datad);
              });
            }
          },
          { type: "hidden", name: "vehicleSpecs", label: "车辆规格:" },
          { type: "hidden", name: "vehicleSpecsName", label: "车辆规格:" }

        ]
      },
      {
        type: "block", width: "*", blockOffset: 0,
        list: [
          { type: "combo", readonly: true, name: "vehicleType", label: "车辆类型:", width: 200, options: dataSelete.TMS_VEHICLE_TYPE },
          { type: "newcolumn" },
          { type: "combo", readonly: true, name: "belong", label: "归属类型:", width: 200, options: dataSelete.TMS_VEHICLE_BELONG },
          { type: "newcolumn" },
          { type: "input", name: "model", label: "品牌型号:" }

        ]
      },
      {
        type: "block", width: "*", blockOffset: 0,
        list: [

          { type: "combo", readonly: true, name: "status", label: "车辆状态:", options: dataSelete.TMS_VEHICLE_STATUS, width: 200 },
          { type: "newcolumn" },
          { type: "input", name: "displacement", label: "百公里油耗:" },
          { type: "newcolumn" },
          { type: "input", name: "mileage", label: "当前里程:" }
        ]
      },
      {
        type: "block", width: "*", blockOffset: 0,
        list: [
          { type: "newcolumn" },
          { type: "input", name: "maxVol", label: "最大装载体积(m³):", labelWidth: 110 },
          { type: "newcolumn" },
          { type: "input", name: "maxWeight", label: "最大装载重量(kg):", labelWidth: 110 },
          { type: "newcolumn" },
          { type: "input", name: "maxPkg", label: "最大装载件数:", labelWidth: 110 }
        ]
      },
      {
        type: "block", width: "*", blockOffset: 0,
        list: [
          { type: "input", name: "memo", label: "备注:" }
        ]
      }
    ]
  };

  //返回
  $(document).on("click", "#vehicleManagementByY_return", function () {
    if (dataType == 0) {
      cuttingScreen("#vehicleManagementByY_div3");
    }
    else {
      dataType = 0;
      cuttingScreen("#vehicleManagementByY_div1");
    }
  });

  function cuttingScreen(name) {
    var dataList = ["#vehicleManagementByY_div1", "#vehicleManagementByY_div3",
      "#vehicleManagementByY_div9", "#vehicleManagementByY_Template"];
    for (var i = 0; i < dataList.length; i++) {
      if (dataList[i] == name) {
        $(dataList[i]).css("display", "block");
      }
      else {
        $(dataList[i]).css("display", "none");
      }
    }
  }
  //保存
  document.querySelector("#vehicleManagementByY_save").onclick = function () {
    //数据data
    var data = ddfromH.getFormData();
    data.branchid = userGJ.p_companyid;
    var feik = ddfromH.validate();
    if (!feik) {
      return;
    }
    if (!Prolog.isCarID(data.vehicleNo)) {
      PlgDialog.msg("请输入正确的车牌号");
      return;
    }
    if (data.vehicleSpecs == "" || data.vehicleSpecs == null) {
      layer.msg("车辆规格不能为空");
      return;
    }
    //请求地址和类型
    var url = UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/tBaseVehicle/updateTBaseVehicle";
    var type = "post";
    var contentType = "application/json";
    //message新增为0，修改为1
    if (data.vehicleId == null || data.vehicleId == "") {
      url = UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/tBaseVehicle/addCarrierVehicle";
    }
    //调用ajax方法
    utilAjaxGJ(url, type, JSON.stringify(data), function (data) {

      cuttingScreen("#vehicleManagementByY_div3");
      grid.reload();
    }, contentType);
  };

};