/**
 * zzx
 * 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 () => {
  // 跳转页面
  var carousel = layui.carousel;
  carousel.render({
    elem: "#smartDeviceManage",
    autoplay: false,
    index: 0,
    arrow: "none",
    indicator: "none",
    anim: "fade",
    width: "100%", // 设置容器宽度
    height: "100%"
  });
  // 切换页面
  function changePanel(name) {
    var slideList = ["#smartDeviceManage_main", "#smartDeviceManage_edit"];
    for (var i = 0; i < slideList.length; i++) {
      if (slideList[i] == name) {
        $(name).css("display", "block");
      }
      else {
        $(slideList[i]).css("display", "none");
      }
    }
  }
  // 返回
  $(document).on("click", "#smartDeviceManage_edit_return", function () {
    changePanel("#smartDeviceManage_main");
  });

  // 获取下拉框
  var dataSelect;
  getSelectGJ([
    { "objectcode": "DEVICE_TYPE" },
    { "objectcode": "DEVICE_STATUS" }], function (data) {
      dataSelect = data.data;
    });

  var master_grid, edit_form_opts, edit_form;

  // 配送中心/站点table
  var branchTable = {
    // terrace显示全部，card分页显示
    skin: "card",
    // renderer:"grid",
    columns: [
      { id: "companyid", name: "id", type: "ro", width: "130", hidden: true },
      { id: "companycode", name: "配送中心/站点编号", type: "ro" },
      { id: "companyname", name: "配送中心/站点名称", type: "ro" }
    ],
    multiselect: false,
    toolbar: [
      { type: "settings", position: "label-left", width: 200, labelWidth: 100 },
      {
        type: "block", id: "bk", blockOffset: 0, list: [
          { type: "input", name: "companycode", label: "配送中心/站点编号:", width: 120 },
          { type: "newcolumn" },
          { type: "button", name: "querycarr", value: "查询" },
          { type: "newcolumn" },
          { type: "button", name: "car_Reset", value: "重置" }
        ]
      }
    ],

    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/pubCompany/queryCompanyBranch",
    pagebar: { type: "simple" },
    type: "post", // 数据提交方式，默认为get
    params: JSON.stringify({ companytype: "S" }), // 查询提交参数，分页参数默认为pageNum,pageSize
    contentType: "application/json",
    page: true, // 是否启用分页
    totalCount: "totalCount" // 总记录数字段，默认totalCount
  };

  // 主页grid
  var master_grid_opts = {
    skin: "card",
    columns: [
      { id: "smartDeviceId", name: "id", type: "ro", width: "130", hidden: true },
      { id: "branchName", name: "配送中心/站点名称", type: "ro" },
      { id: "smartDeviceCode", name: "智能设备编号", type: "ro" },
      { id: "smartDeviceName", name: "智能设备名称", type: "ro" },
      {
        id: "smartDeviceType", name: "智能设备类型", type: "ro", render: function (v) {
          return getSelectValue(dataSelect.DEVICE_TYPE, v);
        }
      },
      {
        id: "status", name: "状态", type: "ro", render: function (v) {
          return getSelectValue(dataSelect.DEVICE_STATUS, v);
        }
      },
      { id: "memo", name: "备注", type: "ro" },
      {
        id: "op", name: "操作", type: "button", align: "left", width: 150, fixed: "right", render: function () {
          return "修改^smartDeviceManage_edit,删除^smartDeviceManage_del";
        }
      }
    ],
    toolbar: [
      { type: "settings", labelWidth: 90, labelAlign: "right" },
      {
        type: "row",
        list: [
          {
            type: "selectTable",
            className: "layui-col-md3",
            placeholder: "请选择配送中心/站点",
            readonly: false,
            table: branchTable,
            tableWidth: 600,
            tableHeight: 500,
            fieldText: "companyname",
            fieldValue: "companyid",
            name: "branchidList",
            label: "配送中心/站点:",
            listener: function (Grid) {
              setMethod(Grid, branchTable, function () { });
            }
          },
          { type: "input", className: "layui-col-md3", name: "smartDeviceCode", label: "智能设备编号:" },
          { type: "combo", className: "layui-col-md3", readonly: true, name: "smartDeviceType", label: "智能设备类型:", options: [{ "value": "", "text": "" }].concat(dataSelect.DEVICE_TYPE) },
          {
            type: "block", className: "layui-col-md2 form_btn_left",
            list: [
              { type: "button", className: "btn-normal", name: "master_search", value: "查询" },
              { type: "newcolumn" },
              { type: "button", name: "master_reset", value: "重置" }
            ]
          },
          {
            type: "block", className: "layui-col-md1 right",
            list: [
              { type: "button", className: "btn-normal", name: "master_add", value: "<i class='layui-icon layui-icon-add-1'></i>新 增" }
            ]
          }
        ]
      }
    ],
    pagebar: {
      buttons: [
        { text: "<i class='layui-icon layui-icon-delete'></i>删除", className: "plg-btn-normal", name: "page_del" }
      ]
    },
    fixedLast: true, // 开启固定列，固定最后一行，倒数第二列不能隐藏
    multiselect: true, // 是否支持多选

    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/tBaseSmartDevice/queryTBaseSmartDevice", // 数据接口
    type: "post", // 数据提交方式，默认为get
    params: "{}", // 查询提交参数，分页参数默认为pageNum,pageSize
    page: true, // 是否启用分页
    contentType: "application/json",
    totalCount: "totalCount" // 总记录数字段，默认totalCount
  };
  master_grid = new PlgGrid(master_grid_opts);
  master_grid.renderTo("smartDeviceManage_master");
  master_grid.loadData();
  master_grid.attachToolBarEvent("onButtonClick", function (name) {
    switch (name) {
      case "master_search":
        var request = master_grid.getToolBarForm().getFormData();
        request.branchId = request.branchidList ? JSON.parse(request.branchidList)[0] : "";
        delete request.branchidList;
        master_grid.setParams(JSON.stringify(request));
        master_grid.reload();
        break;
      case "master_reset":
        master_grid.getToolBarForm().clear();
        master_grid.getToolBarForm().setText("branchidList", "");
        break;
      case "master_add":
        $("#smartDeviceManage_edit_form").html("");
        edit_form = new PlgForm(edit_form_opts);
        edit_form.enableItem("smartDeviceCode");
        $("#smartDeviceManage_edit_title").html("新增");
        changePanel("#smartDeviceManage_edit");
        break;
    }
  });
  // 单个删除
  window.smartDeviceManage_del = function (rid) {
    deleteTBaseSmartDevice([{ smartDeviceId: rid }]);
  };
  // 批量删除
  master_grid.attachPageBarEvent(function (name, ids) {
    let list = ids.split(",").map(item => {
      return { smartDeviceId: item };
    });
    deleteTBaseSmartDevice(list);
  });
  // 删除方法
  function deleteTBaseSmartDevice(idList) {
    layer.open({
      title: "删除",
      content: "是否删除？",
      btn: ["确认", "取消"],
      yes: function () {
        let pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
        Prolog.ajax({
          url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/tBaseSmartDevice/deleteTBaseSmartDevice", type: "post", contentType: "application/json", data: JSON.stringify(idList),
          success: function (data) {
            PlgDialog.close(pload);
            if (data.success) {
              layer.closeAll();
              PlgDialog.msg(data.message);
              master_grid.reload();
              return true;
            }
            return false;

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

  // 修改
  window.smartDeviceManage_edit = function (rid, rdata) {
    $("#smartDeviceManage_edit_form").html("");
    edit_form = new PlgForm(edit_form_opts);
    edit_form.disableItem("smartDeviceCode");
    rdata.branchidList = rdata.branchId ? JSON.stringify([rdata.branchId]) : "[]";
    edit_form.setFormData(rdata);
    edit_form.setText("branchidList", rdata.branchName || "");
    $("#smartDeviceManage_edit_title").html("修改");
    changePanel("#smartDeviceManage_edit");
  };


  // 详情form
  edit_form_opts = {
    renderer: "smartDeviceManage_edit_form",
    items: [
      { type: "settings", position: "label-left", labelWidth: 90 },
      {
        type: "row",
        list: [
          {
            type: "selectTable",
            className: "layui-col-md3",
            placeholder: "请选择配送中心/站点",
            readonly: false,
            table: branchTable,
            tableWidth: 600,
            tableHeight: 500,
            fieldText: "companyname",
            fieldValue: "companyid",
            name: "branchidList",
            label: "配送中心/站点:",
            listener: {
              "init": function (Grid) {
                // 注册按钮事件
                Grid.attachToolBarEvent("onButtonClick", function (name) {
                  switch (name) {
                    case "querycarr":
                      var { companycode } = Grid.getToolBarForm().getFormData();
                      Grid.setParams(JSON.stringify({ companycode, companytype: "S" }));
                      Grid.reload();
                      break;
                    case "car_Reset":
                      Grid.getToolBarForm().clear();
                      break;
                  }
                });
              },
              "afterSelect": function (isSelected, selectId, sgrid) {
                let sdata = sgrid.getRowData(selectId);
                edit_form.setFormData({
                  branchId: isSelected ? sdata.companyid : "",
                  branchName: isSelected ? sdata.companyname : ""
                });
              }
            }
          },
          { type: "hidden", name: "branchId", label: "配送中心/站点id:" },
          { type: "hidden", name: "branchName", label: "配送中心/站点名称:" },
          { type: "input", className: "layui-col-md3", name: "smartDeviceCode", label: "智能设备编号:", required: true },
          { type: "input", className: "layui-col-md3", name: "smartDeviceName", label: "智能设备名称:", required: true }
        ]
      },
      {
        type: "row",
        list: [
          { type: "combo", className: "layui-col-md3", name: "smartDeviceType", label: "智能设备类型:", options: dataSelect.DEVICE_TYPE, readonly: true, required: true },
          { type: "combo", className: "layui-col-md3", name: "status", label: "状态:", options: dataSelect.DEVICE_STATUS, readonly: true, required: true }
        ]
      },
      {
        type: "row",
        list: [
          { type: "input", className: "layui-col-md3", name: "memo", label: "备注:", rows: 3 },
          { type: "hidden", name: "smartDeviceId", label: "智能设备id:" }
        ]
      }
    ]
  };

  // 保存
  $("#smartDeviceManage_edit_save").on("click", function () {
    let feik = edit_form.validate();
    if (!feik) return;
    let formData = edit_form.getFormData();
    if (!formData.branchId) {
      layer.msg("请选择配送中心/站点");
      return false;
    }
    delete formData.branchidList;
    let url = $("#smartDeviceManage_edit_title").html() === "新增" ? UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/tBaseSmartDevice/addTBaseSmartDevice" : UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/tBaseSmartDevice/updateTBaseSmartDevice";
    let pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
    Prolog.ajax({
      url, type: "post", contentType: "application/json", data: JSON.stringify(formData),
      success: function (res) {
        PlgDialog.close(pload);
        if (res.success) {
          PlgDialog.msg(res.message);
          changePanel("#smartDeviceManage_main");
          master_grid.reload();
          return true;
        }
                return false;
      }, error: function () { PlgDialog.close(pload); return false; }
    });
  });
};