<template>
  <a-card>
    <sd-data-table
      ref="table"
      data-url="api/framework/v1/ssoinfo/ssoList"
      :columns="columns"
      :actions="actions"
      show-selection
      row-key="id"
      @rowClick="rowClick"
    ></sd-data-table>
    <sd-detail-modal
      ref="detail"
      :record-id="beanId"
      page-id="base/thirdpartysystem/settingSso"
      :modal-props="{ width: 1100 }"
      @actionBtnClick="actionBtnClick"
    >
      <template v-slot="{ model }">
        <sd-form-item name="thirdpartyName" />
        <sd-form-item name="thirdpartyCode" />
        <sd-form-item name="accessWay" />
        <sd-form-item name="authMode" />
        <sd-form-item name="analysisMode" />
        <sd-form-item name="handshakeUrl">
          <a-textarea v-model="model.handshakeUrl"></a-textarea>
        </sd-form-item>
        <sd-form-item name="settingSsoParamEntityList" :label="null" />
        <sd-form-item
          name="pcUrl"
          :label="model.authMode === 'THIRD' ? '单点登录地址' : '预执行地址'"
        />
        <sd-form-item name="redirectUrl" />
        <sd-form-item name="charEncoding" />
        <sd-form-item name="httpMethod" />
        <sd-form-item name="cipherMode" />
      </template>
    </sd-detail-modal>
  </a-card>
</template>

<script>
import TableActionTypes from '@/common/services/table-action-types'
import { Modal } from 'ant-design-vue'
import axios from '@/common/services/axios-instance'
import components from './_import-components/sd-sso-setting-import'

export default {
  name: 'SdSsoSetting',
  metaInfo: {
    title: '单点登录配置',
  },
  components,
  data() {
    return {
      beanId: '',
      columns: [
        {
          dataIndex: 'thirdpartyName',
          title: '单点配置名称',
          sdClickable: true,
        },
        {
          dataIndex: 'thirdpartyCode',
          title: '单点配置编码',
        },
      ],
      actions: [
        {
          label: '新建',
          id: 'new',
          type: TableActionTypes.primary,
          permission: 'settingSso-create',
          callback: () => {
            this.beanId = ''
            this.$refs.detail.show()
          },
        },
        {
          label: '删除',
          type: TableActionTypes.batch,
          id: 'delete',
          permission: 'settingSso-delete',
          callback: (keys, records) => {
            Modal.confirm({
              title: '确认删除所选中的数据吗？',
              okType: 'danger',
              onOk: () => {
                axios.delete(`api/framework/v1/ssoinfo/deleteBatch/${keys}`).then((res) => {
                  if (res.data) {
                    this.$refs.table.clearSelection()
                    this.$refs.table.refresh()
                  }
                })
              },
            })
          },
        },
      ],
    }
  },
  methods: {
    rowClick(record) {
      this.beanId = record.id
      this.$refs.detail.show()
    },
    actionBtnClick(evt, btn) {
      if (btn.buttonId === 'save') {
        evt.preventDefault()
        const thirdpartyCode = this.$refs.detail.getFieldValue('thirdpartyCode')
        const authMode = this.$refs.detail.getFieldValue('authMode')
        axios
          .get(
            `api/framework/v1/page/checkFieldUnique?formId=entity%3AsettingSso&beanId=${this.beanId}&field=thirdpartyCode%2CauthMode&fieldType=xcoaSpecStringType&value=${thirdpartyCode}%2C${authMode}`,
            {}
          )
          .then((res) => {
            if (res.data) {
              this.$refs.detail.save().then(() => {
                this.$refs.detail.hide()
                this.$refs.table.refresh()
              })
            }
          })
      }
    },
  },
}
</script>

<style module lang="scss">
@use '@/common/design' as *;
</style>
