<template>
  <div class="page-header-index-wide">
  <a-card :bordered="false">
    <div class="ant-alert ant-alert-info ant-alert-light">
      <span class="ant-alert-title">客户端账号管理列表，授权客户端访问</span>
    </div>
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="12">
            <a-form-item label="客户端ID">
              <a-input placeholder="请输入客户端ID" v-model="queryParam.clientId"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
    </div>

    <!-- table区域-begin -->
    <div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">

        <span slot="action" slot-scope="text, record">
          <a @click="handleUnlock(record)">查看秘钥</a>
          <a-divider type="vertical" />
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <a @click="handleDelete(record.clientId)">删除</a>
        </span>

      </a-table>
    </div>

    <oauthClientDetails-modal ref="modalForm" @ok="modalFormOk"></oauthClientDetails-modal>
  </a-card>
  </div>
</template>

<script>

  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import OauthClientDetailsModal from './modules/OauthClientDetailsModal'
  import { httpAction,getAction,deleteAction } from '@/api/manage'

  export default {
    name: "OauthClientDetailsList",
    mixins:[JeecgListMixin],
    components: {
      OauthClientDetailsModal
    },
    data () {
      return {
        description: 'oauth_client_details管理页面',
        // 表头
        columns: [
          {
            title:'客户端ID',
            align:"center",
            dataIndex: 'clientId'
          },
          {
            title:'客户端秘钥',
            align:"center",
            dataIndex: 'clientSecret',
            customRender: (text) => {
              return '******'
            }
          },
          {
            title:'范围',
            align:"center",
            dataIndex: 'scope'
          },
          {
            title:'认证模式',
            align:"center",
            dataIndex: 'authorizedGrantTypes'
          },
          {
            title:'访问令牌有效期(秒)',
            align:"center",
            dataIndex: 'accessTokenValidity'
          },
          {
            title:'刷新令牌有效期(秒)',
            align:"center",
            dataIndex: 'refreshTokenValidity'
          },
          {
            title:'自动授权',
            align:"center",
            dataIndex: 'autoapprove'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            scopedSlots: { customRender: 'action' },
          }
        ],
        url: {
          list: "/oauth/oauthClientDetails/list",
          delete: "/oauth/oauthClientDetails/delete",
          deleteBatch: "/oauth/oauthClientDetails/deleteBatch",
          exportXlsUrl: "/oauth/oauthClientDetails/exportXls",
          importExcelUrl: "/oauth/oauthClientDetails/importExcel",
          getSecret: "/oauth/oauthClientDetails/getSecret",
        },
        dictOptions:{
        }
      }
    },
    computed: {
      importExcelUrl: function(){
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      }
    },
    methods: {
      initDictConfig(){
      },
      handleUnlock(record){
        var that=this
        getAction(that.url.getSecret,{id:record.clientId}).then((res)=>{
          if(res.success){
            that.$success({
              title: '原始秘钥',
              content: res.result,
              okText: '确定',
              onOk: () => {

              }
            })
          }
        });
      },
      handleDelete: function (id) {
        if(!this.url.delete){
          this.$message.error("请设置url.delete属性!")
          return
        }
        var that = this;
        const modal = that.$confirm({
          title: '删除客户端',
          closable: true,
          keyboard: false,
          maskClosable: true,
          content: '确定要删除该客户端吗？',
          okText: '确定删除',
          cancelText: '取消',
          onOk: function() {
            deleteAction(that.url.delete, {id: id}).then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.loadData();
              } else {
                that.$message.warning(res.message);
              }
            });
          },
          onCancel(e) {
            modal.destroy()
          }
        })
      },

    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less'
  .ant-alert-title{
    font-size: 14px;
    line-height: 18px;
  }
  .ant-alert-light{
    margin-bottom: 16px;
    background-color: #f4f4f5;
    color: #909399;
    padding: 0.8rem;
    border: none;
  }
</style>