/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import { defineComponent, h, PropType, ref, toRefs, watch } from 'vue'
import { useI18n } from 'vue-i18n'
import { NIcon, NLog } from 'naive-ui'
import { SyncOutlined } from '@vicons/antd'
import { useModal } from './use-modal'
import Modal from '@/components/modal'

const props = {
  showModalRef: {
    type: Boolean as PropType<boolean>,
    default: false
  },
  row: {
    type: Object as PropType<any>,
    default: {}
  }
}

const LogModal = defineComponent({
  name: 'LogModal',
  props,
  emits: ['confirmModal'],
  setup(props, ctx) {
    const { t } = useI18n()
    const { variables, getLogs } = useModal()
    const renderIcon = (icon: any) => {
      return () => h(NIcon, null, { default: () => h(icon) })
    }

    const confirmModal = () => {
      ctx.emit('confirmModal', props.showModalRef)
    }

    const refreshLogs = () => {
      variables.logRef = ''
      variables.loadingRef = true
      variables.skipLineNum = 0
      variables.limit = 1000

      getLogs()
    }

    watch(
      () => props.showModalRef,
      () => {
        if (props.showModalRef) {
          variables.id = props.row.id
          props.showModalRef && variables.id && getLogs()
        } else {
          variables.id = ''
          variables.logRef = ''
          variables.loadingRef = true
          variables.skipLineNum = 0
          variables.limit = 1000
        }
      }
    )

    return { t, ...toRefs(variables), confirmModal, renderIcon, refreshLogs }
  },
  render() {
    const { t, renderIcon, refreshLogs } = this

    return (
      <Modal
        title={t('project.task.view_log')}
        show={this.showModalRef}
        cancelShow={false}
        onConfirm={this.confirmModal}
        style={{ width: '60%' }}
        headerLinks={ref([
          {
            text: t('project.task.refresh'),
            show: true,
            action: refreshLogs,
            icon: renderIcon(SyncOutlined)
          }
        ])}
      >
        <NLog rows={30} log={this.logRef} loading={this.loadingRef} />
      </Modal>
    )
  }
})

export default LogModal
