<template>
  <a-card>
    <sd-data-table
      ref="table"
      data-url="api/framework/v1/trust/businessList"
      :columns="columns"
      :actions="actions"
      show-selection
      :search-fields="['trustorName']"
      @rowClick="rowClick"
    >
    </sd-data-table>
    <a-modal
      :visible="visible"
      :width="1000"
      title="全员委托设置"
      :confirm-loading="btnLoading"
      destroy-on-close
      @ok="saveInfo"
      @cancel="visible = false"
    >
      <SdSettingPage ref="setting" />
    </a-modal>
  </a-card>
</template>

<script>
import { Message } from 'ant-design-vue'
import TableColumnTypes from '@/common/services/table-column-types'
import TableActionTypes from '@/common/services/table-action-types'
import SdSettingPage from './components/sd-setting-page.vue'
import EntrustService from './entrust-service'
import components from './_import-components/sd-admin-entrust-import'

export default {
  name: 'SdAdminEntrust',
  metaInfo: {
    title: '全员委托设置',
  },
  components: { SdSettingPage, ...components },
  data() {
    return {
      columns: [
        {
          dataIndex: 'id',
          defaultSortOrder: 'desc',
          sdHidden: true,
        },
        {
          title: '状态',
          dataIndex: 'dataStatus',
        },
        {
          title: '委托人',
          dataIndex: 'trustorName',
          sdClickable: true,
        },
        {
          title: '开始时间',
          dataIndex: 'startTime',
          sdRender: TableColumnTypes.dateTime,
          sorter: true,
        },
        {
          title: '结束时间',
          dataIndex: 'finishTime',
          sdRender: TableColumnTypes.dateTime,
          sorter: true,
        },
        {
          title: '备注',
          dataIndex: 'remark',
        },
      ],
      actions: [
        {
          label: '新建',
          id: 'new',
          type: TableActionTypes.primary,
          permission: 'oaTrustInfo-create',
          callback: () => {
            this.visible = true
          },
        },
      ],
      visible: false,
      btnLoading: false,
    }
  },
  methods: {
    rowClick(record, { rowIndex }) {
      this.visible = true
      EntrustService.getEntrustById(record.id).then((res) => {
        if (res) {
          this.$refs.setting.setFieldsValue(res)
        }
      })
    },
    saveInfo() {
      this.$refs.setting.validateFields().then(() => {
        this.btnLoading = true
        const formInfo = this.$refs.setting.getBackendValues()
        EntrustService.saveEntrust(formInfo)
          .then((res) => {
            this.visible = false
          })
          .catch((err) => {
            Message.error(err.response?.data?.errors || '保存失败')
          })
          .finally(() => {
            this.$refs.table.refresh()
            this.btnLoading = false
          })
      })
    },
  },
}
</script>

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