<template>
  <page-header-wrapper>
    <a-card>
      <JeepaySearchForm :searchFunc="searchFunc" :resetFunc="() => { data.searchData= {} }" v-if = "$access('ENT_UR_ROLE_SEARCH')">
        <jeepay-text-up v-model:value="data.searchData['roleId']" :placeholder="'角色ID'" />
        <jeepay-text-up v-model:value="data.searchData['roleName']" :placeholder="'角色名称'" />
      </JeepaySearchForm>
      <!-- 列表渲染 -->
      <JeepayTable
        ref="infoTable"
        :init-data="true"
        :req-table-data-func="reqTableDataFunc"
        :table-columns="tableColumns"
        :search-data="data.searchData"
        row-key="roleName"
        @btnLoadClose="data.btnLoading=false"
      >
        <template #topBtnSlot>
          <div>
            <a-button
              v-if="$access('ENT_UR_ROLE_ADD')"
              type="primary" 
              class="mg-b-30"
              @click="addFunc"
            >
              <plus-outlined />
              新建
            </a-button>
          </div>
        </template>
        <template #bodyCell="{column,record}">
          <!-- {{ record }} -->
          <template v-if="column.key === 'roleId'">
            <b>{{ record.roleId }}</b>
          </template>

          <template v-if="column.key === 'op'">
            <a v-if="$access('ENT_UR_ROLE_EDIT')" style="padding-left: 15px; padding-right: 15px;" @click="editFunc(record.roleId)">修改</a>
            <a v-if="$access('ENT_UR_ROLE_DEL')" style="color: red" @click="delFunc(record.roleId)">删除</a>
          </template>
        </template>
      </JeepayTable>
    </a-card>

    <!-- 新增 / 修改 页面组件  -->
    <InfoAddOrEdit ref="infoAddOrEdit" :callback-func="searchFunc" />
  </page-header-wrapper>
</template>
<script setup lang="ts">
import { API_URL_ROLE_LIST, req } from '@/api/manage'
import InfoAddOrEdit from './AddOrEdit.vue'
import {ref,reactive,getCurrentInstance} from 'vue'
// eslint-disable-next-line no-unused-vars
import {message} from 'ant-design-vue'
const { $infoBox,$access } = getCurrentInstance()!.appContext.config.globalProperties

const tableColumns =reactive([
  {
    key: 'roleId', // key为必填项，用于标志该列的唯一
    title: '角色ID',
    sorter: true,
    fixed: 'left',
  },
  {
    key: 'roleName',
    title: '角色名称',
    dataIndex: 'roleName',
    sorter: true
  },
  {
    key: 'op',
    title: '操作',
    width: '200px',
    fixed: 'right',
    align: 'center',
  }
]) 
    const infoTable = ref()
    const infoAddOrEdit = ref()
    const data = reactive ({
      tableColumns: tableColumns,
      searchData: {},
      btnLoading: false
    })


    // 请求table接口数据
 const reqTableDataFunc =(params) => {
      return req.list(API_URL_ROLE_LIST, params)
    }

   function searchFunc () { // 点击【查询】按钮点击事件
      data.btnLoading = true // 打开查询按钮上的loading
     infoTable.value.refTable(true)
    }

  function  addFunc () { // 业务通用【新增】 函数
     infoAddOrEdit.value.show()
    }

   function editFunc (recordId) { // 业务通用【修改】 函数
     infoAddOrEdit.value.show(recordId)
    }

  function  delFunc (recordId) { // 业务通用【删除】 函数
   
    $infoBox.confirmDanger('确认删除？', '', () => {
        // 需要【按钮】loading 请返回 promise对象， 不需要请直接返回null
        return req.delById(API_URL_ROLE_LIST, recordId).then(res => {
          message.success('删除成功！')
          infoTable.value.refTable(false)
        })
      })
    }

</script>
