<template>
  <div class="guns-layout">
    <div class="guns-layout-content" style="padding: 12px 0">
      <div class="guns-layout-content-header">
        <span>管理权限</span>
        <a-space :size="16">
          <a-button type="primary" class="border-radius" @click="openAddEdit()"><plus-outlined />新建</a-button>
        </a-space>
      </div>
      <div class="guns-layout-content-application">
        <div class="content-mian">
          <div class="content-mian-body">
            <div class="table-content">
              <common-table
                :columns="columns"
                :where="where"
                :showToolTotal="false"
                :rowSelection="false"
                showTableTool
                rowId="id"
                :isInit="false"
                ref="tableRef"
                url="/portalDesignManager/page"
              >
                <template #toolLeft>
                  <a-input
                    v-model:value="where.searchText"
                    placeholder="请输入关键字"
                    @pressEnter="reload"
                    class="search-input"
                    :bordered="false"
                  >
                    <template #prefix>
                      <icon-font iconClass="icon-opt-search"></icon-font>
                    </template>
                  </a-input>
                </template>
                <template #bodyCell="{ column, record }">
                  <template v-if="column.dataIndex == 'authTypeList'">
                    {{ getAuthTypeListName(record) }}
                  </template>
                  <template v-if="column.key == 'action'">
                    <a-space :size="16">
                      <icon-font
                        iconClass="icon-opt-shanchu"
                        font-size="24px"
                        title="删除"
                        color="#60666b"
                        @click="remove(record)"
                      ></icon-font>
                    </a-space>
                  </template>
                </template>
              </common-table>
            </div>
          </div>
        </div>
      </div>
    </div>

    <!-- 新增编辑弹框 -->
    <PermissionAddEdit v-model:visible="showAddEdit" v-if="showAddEdit" :designId="props.designId" @done="reload" />
  </div>
</template>

<script setup>
import { PermissionApi } from './api/PermissionApi';
import { ref, createVNode, onMounted, defineAsyncComponent, computed } from 'vue';
import { message, Modal } from 'ant-design-vue/es';
import { ExclamationCircleOutlined } from '@ant-design/icons-vue';

const PermissionAddEdit = defineAsyncComponent(() => import('./components/permission-add-edit.vue'));

const props = defineProps({
  designId: String
});

// 表格配置
const columns = ref([
  {
    key: 'index',
    title: '序号',
    width: 48,
    align: 'center',
    isShow: true,
    hideInSetting: true
  },
  {
    dataIndex: 'managerUserIdWrapper',
    title: '人员范围',
    width: 100,
    isShow: true
  },
  {
    dataIndex: 'authTypeList',
    title: '权限内容',
    width: 100,
    isShow: true
  },
  {
    dataIndex: 'createTime',
    title: '创建时间',
    width: 150,
    isShow: true
  },
  {
    key: 'action',
    title: '操作',
    width: 40,
    isShow: true
  }
]);
// ref
const tableRef = ref(null);

// 搜索条件
const where = ref({
  designId: props.designId,
  searchText: ''
});

// 当前行数据
const current = ref(null);
// 是否显示新增编辑弹框
const showAddEdit = ref(false);

const typeList = ref([
  {
    key: 'design',
    name: '拖拽布局配置'
  },
  {
    key: 'content',
    name: '新闻、轮播图和应用链接维护'
  },
  {
    key: 'admin',
    name: '管理员权限'
  }
]);

const getAuthTypeListName = computed(() => {
  return record => {
    let name = '';
    if (record?.authTypeList?.length) {
      const filteredList = typeList.value.filter(item => record.authTypeList.includes(item.key));
      name = filteredList.map(item => item.name).join(',');
    }
    return name;
  };
});

onMounted(() => {
  if (props.designId) {
    reload();
  }
});

const reload = () => {
  tableRef.value.reload();
};

// 新增编辑点击
const openAddEdit = record => {
  current.value = record;
  showAddEdit.value = true;
};

// 删除单个
const remove = record => {
  Modal.confirm({
    title: '提示',
    content: '确定要删除选中的管理权限吗?',
    icon: createVNode(ExclamationCircleOutlined),
    maskClosable: true,
    onOk: async () => {
      const res = await PermissionApi.delete({ id: record.id });
      message.success(res.message);
      reload();
    }
  });
};
</script>

<style scoped lang="less"></style>
