/**
 * zzx
 * 2020.12.09
 * 费用项取数逻辑管理
    */
    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 master_grid, edit_form;

  // 跳转页面
  var carousel = layui.carousel;
  carousel.render({
    elem: "#costItemNumberingLogic",
    autoplay: false,
    index: 0,
    arrow: "none",
    indicator: "none",
    anim: "fade",
    width: "100%", // 设置容器宽度
    height: "100%"
  });
  // 切换页面
  function changePanel(name) {
    var slideList = ["#costItemNumberingLogic_main", "#costItemNumberingLogic_deit"];
    for (var i = 0; i < slideList.length; i++) {
      if (slideList[i] == name) {
        $(name).css("display", "block");
      }
      else {
        $(slideList[i]).css("display", "none");
      }
    }
  }
  // 返回
  $("#costItemNumberingLogic_deit_return").on("click", function () {
    changePanel("#costItemNumberingLogic_main");
  });

  // 获取下拉框数据
  var dataSelect;
  getSelectGJ([
    { objectcode: "BMS_FEE_CALCTYPE" },
    { objectcode: "BMS_FEE_TMCONDIT" }], function (data) {
      dataSelect = data.data;
      dataSelect.BMS_FEE_CALCTYPE.unshift({ "value": "", "text": "" });
      dataSelect.BMS_FEE_TMCONDIT.unshift({ "value": "", "text": "" });
    }
  );

  //费用项目table
  var objectTable = {
    columns: [
      { id: "feetypeid", name: "id", type: "ro", width: "130", hidden: true },
      { id: "feetypecode", name: "费用项目编号", type: "ro" },
      { id: "feetypename", name: "费用项目名称", type: "ro" }
    ],
    multiselect: false,

    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/bmsFeeType/queryBmsFeeType",//数据接口
    type: "post", //数据提交方式，默认为get
    pagebar: { type: "simple" },
    params: "{}", //查询提交参数，分页参数默认为pageNum,pageSize
    contentType: "application/json",
    page: true, //是否启用分页
    totalCount: "totalCount" //总记录数字段，默认totalCount
  };

  // 主页grid
  var master_grid_opts = {
    skin: "card",
    columns: [
      { id: "feetypeid", name: "id", type: "ro", width: "130", hidden: true },
      { id: "feetypecode", name: "费用项目编号", type: "ro", width: 120 },
      { id: "feetypename", name: "费用项目名称", type: "ro", width: 120 },
      {
        id: "calctype", name: "计算方式", type: "ro", width: 120, render: function (v) {
          return getSelectValue(dataSelect.BMS_FEE_CALCTYPE, v);
        }
      },
      {
        id: "tmcondit", name: "匹配方式", type: "ro", width: 120, render: function (v) {
          return getSelectValue(dataSelect.BMS_FEE_TMCONDIT, v);
        }
      },
      { id: "groupnum", name: "分组数量", type: "ro", width: 120 },
      { id: "group1name", name: "分组1名称", type: "ro", width: 120 },
      { id: "group2name", name: "分组2名称", type: "ro", width: 120 },
      { id: "group3name", name: "分组3名称", type: "ro", width: 120 },
      { id: "group4name", name: "分组4名称", type: "ro", width: 120 },
      { id: "group5name", name: "分组5名称", type: "ro", width: 120 },
      { id: "memo", name: "备注", type: "ro", width: 120 },
      {
        id: "op", name: "操作", type: "button", align: "left", fixed: "right", width: "100", render: function () {
          return "修改^costItemNumberingLogic_mod,删除^costItemNumberingLogic_del";
        }
      }
    ],
    toolbar: [
      { type: "settings", labelWidth: 90, labelAlign: "right" },
      {
        type: "row",
        list: [
          {
            type: "selectTable",
            className: "layui-col-md3",
            placeholder: "请选择费用项目",
            readonly: false,
            table: objectTable,
            tableWidth: 600,
            tableHeight: 500,
            fieldText: "feetypename",
            fieldValue: "feetypeid",
            name: "feetypeidList",
            label: "费用项目:",
            listener: function (Grid) {
              setMethod(Grid, objectTable, function () { });
            }
          },
          { type: "combo", className: "layui-col-md3", readonly: true, name: "calctype", label: "计算方式:", options: dataSelect.BMS_FEE_CALCTYPE },
          { type: "combo", className: "layui-col-md3", readonly: true, name: "tmcondit", label: "匹配方式:", options: dataSelect.BMS_FEE_TMCONDIT },
          {
            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: "pagebar_delete" }
      ]
    },
    fixedLast: true, // 开启固定列，固定最后一行，倒数第二列不能隐藏
    multiselect: true, // 是否支持多选

    url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/bmsFeeGetData/queryBmsFeeGetData", // 数据接口
    type: "post", // 数据提交方式，默认为get
    params: "{}", // 查询提交参数，分页参数默认为pageNum,pageSize
    page: true, // 是否启用分页
    contentType: "application/json",
    totalCount: "totalCount" // 总记录数字段，默认totalCount
  };
  master_grid = new PlgGrid(master_grid_opts);
  master_grid.renderTo("costItemNumberingLogic_master");
  master_grid.loadData();
  master_grid.attachToolBarEvent("onButtonClick", function (name) {
    switch (name) {
      case "master_search":
        var request = master_grid.getToolBarForm().getFormData();
        request.feetypeid = request.feetypeidList ? JSON.parse(request.feetypeidList)[0] : "";
        dealNullQueryCondition(request);
        delete request.feetypeidList;
        master_grid.setParams(JSON.stringify(request));
        master_grid.reload();
        break;
      case "master_reset":
        master_grid.getToolBarForm().clear();
        master_grid.getToolBarForm().setText("feetypeidList", "");
        break;
      case "master_add":
        editFormCreated();
        break;
    }
  });

  // 单个删除
  window.costItemNumberingLogic_del = function (rid) {
    deleteBmsFeeGetData([{ feetypeid: rid }]);
  };
  // 批量删除
  master_grid.attachPageBarEvent(function (name, ids) {
    let list = ids.split(",").map(item => {
      return { feetypeid: item };
    });
    deleteBmsFeeGetData(list);
  });
  function deleteBmsFeeGetData(idList) {
    layer.open({
      title: "删除",
      content: "是否删除？",
      btn: ["确认", "取消"],
      yes: function () {
        let pload = PlgDialog.load(2, { shade: [0.6, "#fff"] });
        Prolog.ajax({
          url: UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/bmsFeeGetData/deleteBmsFeeGetData", 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.costItemNumberingLogic_mod = function (rid, rdata) {
    editFormCreated(rdata);
  };


  // 注册编辑页form
  function editFormCreated(formData) {
    changePanel("#costItemNumberingLogic_deit");
    $("#costItemNumberingLogic_deit_form").html("");
    var edit_form_opts = {
      renderer: "costItemNumberingLogic_deit_form",
      items: [
        { type: "settings", labelAlign: "left", labelWidth: 95 },
        {
          type: "row",
          list: [
            {
              type: "selectTable",
              className: "layui-col-md3",
              placeholder: "请选择费用项目",
              readonly: false,
              table: objectTable,
              tableWidth: 600,
              tableHeight: 500,
              fieldText: "feetypename",
              fieldValue: ["feetypeid", "feetypecode", "feetypename"],
              name: "feetypeidList",
              label: "费用项目:<span class='dhxform_item_required'>*<span>",
              listener: function (Grid) {
                setMethod(Grid, objectTable, function () { });
              }
            },
            { type: "combo", className: "layui-col-md3", readonly: true, name: "calctype", label: "计算方式:", options: dataSelect.BMS_FEE_CALCTYPE, required: true }
          ]
        },
        {
          type: "row",
          list: [
            { type: "combo", className: "layui-col-md3", readonly: true, name: "tmcondit", label: "匹配方式:", options: dataSelect.BMS_FEE_TMCONDIT, required: true },
            { type: "input", className: "layui-col-md3", name: "groupnum", label: "分组数量:", required: true }
          ]
        },
        {
          type: "row",
          list: [
            { type: "input", className: "layui-col-md3", name: "group1name", label: "分组1名称:" },
            { type: "input", className: "layui-col-md6", name: "group1listsql", label: "分组1数据源:" }
          ]
        },
        {
          type: "row",
          list: [
            { type: "input", className: "layui-col-md3", name: "group2name", label: "分组2名称:" },
            { type: "input", className: "layui-col-md6", name: "group2listsql", label: "分组2数据源:" }
          ]
        },
        {
          type: "row",
          list: [
            { type: "input", className: "layui-col-md3", name: "group3name", label: "分组3名称:" },
            { type: "input", className: "layui-col-md6", name: "group3listsql", label: "分组3数据源:" }
          ]
        },
        {
          type: "row",
          list: [
            { type: "input", className: "layui-col-md3", name: "group4name", label: "分组4名称:" },
            { type: "input", className: "layui-col-md6", name: "group4listsql", label: "分组4数据源:" }
          ]
        },
        {
          type: "row",
          list: [
            { type: "input", className: "layui-col-md3", name: "group5name", label: "分组5名称:" },
            { type: "input", className: "layui-col-md6", name: "group5listsql", label: "分组5数据源:" }
          ]
        },
        {
          type: "row",
          list: [
            { type: "input", className: "layui-col-md9", name: "datasource", label: "数据源:", rows: 3, required: true }
          ]
        },
        {
          type: "row",
          list: [
            { type: "input", className: "layui-col-md9", name: "memo", label: "备注:", rows: 3 }
          ]
        }
      ]
    };
    edit_form = new PlgForm(edit_form_opts);

    if (formData) {
      $("#costItemNumberingLogic_deit_title").html("修改");
      formData.feetypeidList = JSON.stringify([{ feetypeid: formData.feetypeid, feetypecode: formData.feetypecode, feetypename: formData.feetypename }]);
      edit_form.setFormData(formData);
      edit_form.disableItem("feetypeidList");
      edit_form.setText("feetypeidList", formData.feetypename);
    }
    else {
      $("#costItemNumberingLogic_deit_title").html("新增");
      edit_form.enableItem("feetypeidList");
    }
  }

  // 保存
  $("#costItemNumberingLogic_deit_save").on("click", function () {
    let feik = edit_form.validate();
    if (!feik) return false;
    let formData = edit_form.getFormData();
    let url = $("#costItemNumberingLogic_deit_title").html() === "新增" ? UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/bmsFeeGetData/addBmsFeeGetData" : UPCLOUD_RDC_TMS_BUSINESS_SERVICE+"/bmsFeeGetData/updateBmsFeeGetData";
    formData.feetypeidList = formData.feetypeidList ? JSON.parse(formData.feetypeidList) : "";
    if (!formData.feetypeidList.length) {
      layer.msg("请选择费用项目");
      return false;
    }
    if (!/^[1-9]\d*$/.test(formData.groupnum)) {
      layer.msg("分组数量为正整数");
      return false;
    }
    formData.feetypeid = formData.feetypeidList[0].feetypeid;
    formData.feetypecode = formData.feetypeidList[0].feetypecode;
    formData.feetypename = formData.feetypeidList[0].feetypename;

    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("#costItemNumberingLogic_main");
          master_grid.reload();
          return true;
        }
                return false;
      }, error: function () { PlgDialog.close(pload); return false; }
    });
  });
};