/***
 * 系统参数维护
 * jiw
 * 2020-07-30
 ***/

 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 userinfo = window.localStorage.getItem("userinfo");
  if (userinfo != null) userinfo = JSON.parse(userinfo);
  var master_grid, details_form_opts, details_form;

  // 跳转页面
  var carousel = layui.carousel;
  carousel.render({
    elem: "#sysConfig",
    autoplay: false,
    index: 0,
    arrow: "none",
    indicator: "none",
    anim: "fade",
    width: "100%", // 设置容器宽度
    height: "100%"
  });
  // 切换页面
  function changePanel(name) {
    var slideList = ["#sysConfig_master", "#sysConfig_details"];
    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", "#sysConfig_details_return", function () {
    changePanel("#sysConfig_master");
  });


  // 主页grid
  var master_grid_opts = {
    skin: "card",
    columns: [
      { id: "seqno", name: "序号", type: "ro", width: 100, hidden: true },
      { id: "branchCode", name: "当前仓id", type: "ro" },
      { id: "branchName", name: "当前仓名称", type: "ro" },
      { id: "parameterCode", name: "参数编号", type: "ro" },
      { id: "parameterName", name: "参数名称", type: "ro" },
      { id: "fatherParameterCode", name: "父级分组编号", type: "ro" },
      { id: "fatherParameterName", name: "父级分组名称", type: "ro" },
      { id: "parameterValue", name: "参数值", type: "ro" },
      { id: "memo", name: "备注", type: "ro" },
      {
        id: "op", name: "操作", type: "button", align: "left", width: 150, fixed: "right", render: function () {
          return "修改^sysConfig_master_edit,删除^sysConfig_master_del";
        }
      }
    ],
    toolbar: [
      { type: "settings", labelWidth: 90, labelAlign: "right" },
      {
        type: "row",
        list: [
          { type: "input", className: "layui-col-md3", name: "parameterCode", label: "参数编号:", labelWidth: 65 },
          { type: "input", className: "layui-col-md3", name: "parameterName", label: "参数名称:" },
          { type: "input", className: "layui-col-md3", name: "fatherParameterCode", label: "父级分组编号:" },
          { type: "input", className: "layui-col-md3", name: "fatherParameterName", label: "父级分组名称:" },
          {
            type: "block", className: "layui-col-md6 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-md6 right",
            list: [
              { type: "button", className: "btn-normal", name: "master_add", value: "<i class='layui-icon layui-icon-add-1'></i>新 增" }
            ]
          }
        ]
      }
    ],
    fixedLast: true,
    multiselect: true,
    url: UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/sysParameter/query",
    type: "post",
    params: JSON.stringify({
      branchCode: userinfo.p_companyid,
      pageNum: 1,
      pageSize: 10
    }),
    page: true,
    pagebar: {
      buttons: [
        { text: "<i class='layui-icon layui-icon-delete'></i>删除", className: "plg-btn-normal", name: "delete" }
      ]
    },
    contentType: "application/json",
    totalCount: "totalCount"
  };
  master_grid = new PlgGrid(master_grid_opts);
  master_grid.renderTo("sysConfig_master");
  master_grid.loadData();
  master_grid.attachToolBarEvent("onButtonClick", function (name) {
    switch (name) {
      case "master_search":
        var request = master_grid.getToolBarForm().getFormData();
        request.branchCode = userinfo.p_companyid;
        master_grid.setParams(JSON.stringify(request));
        master_grid.reload();
        break;
      case "master_reset":
        master_grid.getToolBarForm().clear();
        break;
      case "master_add":
        details_form = new PlgForm(details_form_opts);
        details_form.enableItem("parameterCode");
        details_form.setItemValue("branchName", userinfo.p_companyname);
        details_form.setItemValue("branchCode", userinfo.p_companyid);
        $("#sysConfig_details_title").html("新增");
        changePanel("#sysConfig_details");
        break;
    }
  });
  // 批量删除
  master_grid.attachPageBarEvent(function (name, ids) {
    //删除
    if (name == "delete") {
      let rowIds = ids.split(",");
      let par = [];
      rowIds.forEach(t => {
        par.push({ seqno: t });
      });
      Prolog.delGridRowData(master_grid, UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/sysParameter/delete", "post", "application/json", JSON.stringify(par), false);
    }
  });
  // 修改
  window.sysConfig_master_edit = function (rid, rdata) {
    details_form = new PlgForm(details_form_opts);
    details_form.setFormData(rdata);
    details_form.disableItem("parameterCode");
    $("#sysConfig_details_title").html("修改");
    changePanel("#sysConfig_details");
  };
  // 删除
  window.sysConfig_master_del = function (rid) {
    master_grid.selectRowById(rid);
    Prolog.delGridRowData(master_grid, UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/sysParameter/delete", "post", "application/json", JSON.stringify([{ seqno: rid }]), false);
  };

  // 详情form
  details_form_opts = {
    renderer: "sysConfig_details_form",
    items: [
      { type: "settings", position: "label-left", labelWidth: 100, labelAlign: "right" },
      {
        type: "row",
        list: [
          { type: "hidden", name: "branchCode" },
          { type: "input", className: "layui-col-md4", name: "branchName", label: "当前仓名称:", readonly: true, disabled: true },
          { type: "input", className: "layui-col-md4", name: "parameterCode", label: "参数编号:", required: true }
        ]
      },
      {
        type: "row",
        list: [
          { type: "input", className: "layui-col-md4", name: "parameterName", label: "参数名称:", required: true },
          { type: "input", className: "layui-col-md4", name: "parameterValue", label: "参数值:", required: true }
        ]
      },
      {
        type: "row",
        list: [
          { type: "input", className: "layui-col-md4", name: "fatherParameterName", label: "父级分组名称:", required: true },
          { type: "input", className: "layui-col-md4", name: "fatherParameterCode", label: "父级分组编号:", required: true }
        ]
      },
      {
        type: "row",
        list: [
          { type: "input", className: "layui-col-md8", name: "memo", label: "备注:" }
        ]
      }

    ]
  };

  // 保存
  $(document).on("click", "#sysConfig_details_save", function () {
    let b = details_form.validate();
    if (!b) return;
    let formData = details_form.getFormData();
    let url = formData.seqno ? UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/sysParameter/update" : UPCLOUD_RDC_TMS_MAINDATA_SERVICE+"/sysParameter/save";
    let pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
    Prolog.ajax({
      url, type: "post", contentType: "application/json", data: JSON.stringify(formData),
      success: function (data) {
        PlgDialog.close(pload);
        if (data.success) {
          setTimeout(() => { PlgDialog.msg(data.message || "接口调用失败"); }, 300);
          changePanel("#sysConfig_master");
          master_grid.reload();
          return;
        }
        setTimeout(() => { PlgDialog.alert(data.message || "接口调用失败"); }, 300);
      }, error: function () { PlgDialog.close(pload); }
    });

  });
};