/**
 * Created by 1 at 2022-08-01 17:11:04
 * 注册事件类【菜单方案】，参看event.defaultEvent.js
 */
window.$swEvent.setup("bpm.sys.user.menuPlan.menuPlanList", {
  setup(page){
    const { $refs, $model, $utils, $api } = page || {};
    const { $$message, $$http, $$sys } = $utils || {};
    // 保存方案
    const saveMenuPlan =async () => {
      await $api.saveOne("planCard");
      await $api.loadOne("planList");
    }
    // 删除方案
    const afterDelMenuPlan = async () => {
      await $api.loadOne("planList");
    }
    // 调用新增之前 校验是否选择了方案
    const beforeAddPlanItem = () => {
      const row = $model.planList?.data?.selectedRow;
      if(!row || !row["smp_id"]){
        $$sys.throwBizException("请选择菜单方案！",true);
      }
    };
    // 保存菜单项
    const savePlanItem = async () => {
     const pass = await $api.checkSaveOne("menuCard");
      if(pass){
        const data = $model.menuCard.data.form;
        const rt = await $$http.post("bpm/menuPlan/savePlanItem",data);
        $model.menuCard.setFormVal("id",rt.data.id);
        $api.loadOne("menuList");
        $refs.planTree?.refresh();
        $$message.notify.success("保存成功！");
      }
    }
    // 删除菜单项
    const delPlanItem = async (params) => {
      const data = {
        id: params?.row?.id,
        plan_id: params?.row?.plan_id
      }
      $$message.confirm("确定要删除吗?",async () => {
        const rt = await $$http.post("bpm/menuPlan/delPlanItem",data);
        $refs.planTree?.refresh();
        await $api.loadOne("menuList");
        $$message.notify.success("删除成功！");
      });
    }
    // 下拉框onChange事件
    const onMenuChange = (value,row) => {
      $model.menuCard.setFormVal("label",row?.label);
      $model.menuCard.setFormVal("icon",row?.icon);
      $model.menuCard.setFormVal("seq",row?.seq);
    }
    // 菜单类型onChange事件
    const onTypeChange = (value) => {
      if(value === 1){
        $refs.selectMenu.refVars.disabled = false;
      }else {
        $refs.selectMenu.refVars.disabled = true;
      }
    }
    return {
      saveMenuPlan,
      afterDelMenuPlan,
      beforeAddPlanItem,
      savePlanItem,
      delPlanItem,
      onMenuChange,
      onTypeChange,
    }
  }
});
