<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="permissionId"
                :isInit="false"
                ref="tableRef"
                url="/portalDesignPermission/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 == 'linkScopeType'">
                    <span v-if="record.linkScopeType == 10">机构</span>
                    <span v-if="record.linkScopeType == 20">人员</span>
                  </template>
                  <template v-if="column.dataIndex == 'scopeType'">
                    <span v-if="record.scopeType == 1">指定范围</span>
                    <span v-if="record.scopeType == 2">指定范围及以下</span>
                  </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>

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

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

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

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

// 表格配置
const columns = ref([
  {
    key: 'index',
    title: '序号',
    width: 48,
    align: 'center',
    isShow: true,
    hideInSetting: true
  },
  {
    dataIndex: 'linkScopeType',
    title: '类型',
    ellipsis: true,
    width: 150,
    isShow: true
  },
  {
    dataIndex: 'scopeType',
    title: '范围类型',
    align: 'center',
    width: 100,
    isShow: true
  },
  {
    dataIndex: 'linkBusinessName',
    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);

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 EffectiveApi.delete({ permissionId: record.permissionId });
      message.success(res.message);
      reload();
    }
  });
};
</script>

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