<!--
 * @Author: xiejiang
 * @Date: 2024-12-05 17:16:22
 * @LastEditors: xiejiang
 * @LastEditTime: 2024-12-11 23:14:14
 * @Description: 功能权限
 * Copyright（c）2024 by 好老师教育科技有限公司 All right Reserved.
-->
<template>
  <div class="p-[16px] pt-[0] capabilities" v-loading="loading">
    <t-collapse :borderless="true" default-expand-all :expand-on-row-click="false" v-if="!loading">
      <t-collapse-panel
        v-for="(item, index) in menuTree"
        :key="item.menuId"
        :class="index === 0 ? 'magrin-bottom' : ''"
      >
        <!-- <template #expandIcon><CaretDownSmallIcon class="text-[var(--td-brand-color)]" /></template> -->
        <template #header>
          <t-checkbox v-model="item.selected" @change="handleSelectAll(item)">{{ item.menuName }}</t-checkbox>
        </template>
        <div v-for="itx in item.children || []" :key="itx.menuId" class="flex">
          <div class="page-auth">
            <t-checkbox v-model="itx.selected" :label="itx.menuName" @change="handleSelectAll(itx)" />
          </div>
          <div class="page-btn">
            <t-checkbox
              :value="it.menuId"
              :label="it.menuName"
              v-for="it in itx.children || []"
              :key="it.menuId"
              v-model="it.selected"
              @change="handleSelectAll(it)"
              class="!mr-[20px] !my-[7px]"
            />
          </div>
        </div>
      </t-collapse-panel>
    </t-collapse>
  </div>
</template>

<script setup lang="ts" name="capabilities">
import { MessagePlugin } from 'tdesign-vue-next'
import { ResMenuTree } from '@/api/interface/menu/index'
import { getMenuTree, postSaveMenu } from '@/api/modules/menu/index'
const props = defineProps({
  roleId: {
    type: String,
    default: ''
  },
  condMenuName: {
    type: String,
    default: ''
  }
})

watch([toRef(props, 'roleId')], () => {
  getData()
})

const loading = ref(false)
const menuTree = ref<ResMenuTree[]>([])
// 勾选
function findNodeById(tree: ResMenuTree[], id: string) {
  tree.forEach(it => {
    if (it.menuId == id) {
      it.selected = true
    } else if (it.children) {
      it.children.forEach((v: ResMenuTree) => {
        if (v.menuId == id) {
          it.selected = true
          v.selected = true
        }
      })
    }
  })
}
const handleSelectAll = (itx: ResMenuTree) => {
  // 勾选则需要勾选父级
  if (itx.selected) {
    if (itx.parentId) {
      findNodeById(menuTree.value, itx.parentId as string)
    }
  } else {
    // 取消后面所有子集
    if (itx.children) {
      itx.children.forEach((v: ResMenuTree) => {
        v.selected = false
        if (v.children) {
          v.children.forEach((val: ResMenuTree) => {
            val.selected = false
          })
        }
      })
    }
  }
}

// 清空
const clear = () => {
  menuTree.value.forEach(it => {
    it.selected = false
    if (it.children) {
      it.children.forEach((v: ResMenuTree) => {
        v.selected = false
        if (v.children) {
          v.children.forEach((val: ResMenuTree) => {
            val.selected = false
          })
        }
      })
    }
  })
}

const getData = () => {
  loading.value = true
  getMenuTree({ roleId: props.roleId, condMenuName: props.condMenuName })
    .then(res => {
      if (Array.isArray(res.data)) {
        menuTree.value = res.data as ResMenuTree[]
      } else {
        menuTree.value = [] // 如果不是数组，设置为空数组
      }
    })
    .finally(() => {
      loading.value = false
    })
}

const submit = () => {
  emit('loading', true)
  postSaveMenu({ roleId: props.roleId, menus: menuTree.value })
    .then(() => {
      getData()
      MessagePlugin.success('保存成功')
    })
    .finally(() => {
      emit('loading', false)
    })
}

const emit = defineEmits<{
  loading: [value: boolean]
}>()

defineExpose({
  submit,
  clear,
  getData
})
</script>

<style lang="less" scoped>
.capabilities {
  padding: auto;

  .page-auth {
    border: 1px solid var(--td-border);
    padding: 10px 16px;
    margin-top: -1px;
    display: flex;
    width: 230px;
    box-sizing: border-box;
    align-items: center;
    min-height: 48px;

    :deep(.t-checkbox) {
      width: 100%;
    }

    :deep(.t-checkbox__label) {
      max-width: calc(100% - 30px);
    }
  }

  .page-btn {
    border: 1px solid var(--td-border);
    border-left: none;
    margin-top: -1px;
    padding: 7px 9px 7px 16px;
    flex: 1;
    display: flex;
    flex-wrap: wrap;
    align-items: center;

    :deep(.t-checkbox) {
      margin: 7px 10px 7px 0;
    }
  }

  :deep(.t-collapse-panel) {
    .t-collapse-panel__header {
      padding: 8px;
      margin-top: -1px;
      min-height: 48px;
      box-sizing: border-box;
      background: var(--td-brand-color-12) !important;
      border: 1px solid var(--td-border) !important;
    }

    .t-collapse-panel__content {
      padding: 0;
    }
  }

  .magrin-bottom {
    :deep(.t-collapse-panel__header) {
      margin: 0 !important;
    }
  }
}
</style>
