<template>
  <a-card>
    <a-row justify="end" >
      <a-col  >
        <a-button type="primary" @click="openModal">创建推流</a-button>
      </a-col>
    </a-row>
    <a-table
      style="margin-top: 20px;"
      :columns="columns"
      :data-source="state.dataSource"
      :loading="state.loading"
    >
      <template #bodyCell="{column, record}" >
        <template v-if="column.key === 'StartTime'" >
          {{dayjs(record.StartTime).format('YYYY/MM/DD HH:mm:ss')}}
        </template>
        <template v-if="column.key === 'action'"  >
          <a-space>
            <a @click="stopPush(record.RemoteURL)" >停止</a>
          </a-space>
        </template>
      </template>
    </a-table>
  </a-card>

  <modal-pro
    label="创建推流"
    :open="state.visible"
    @cancel="state.visible = false "
    @ok="ok"
  >
    <a-form :label-col="{span: 5}" >
      <a-form-item label="推流类型" >
        <a-select allowClear v-model:value="bodyParamsState.name" >
          <a-select-option
            v-for="item in pullList"
            :key="item.key"
            :value="item.value"
          >
            {{item.value}}
          </a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item label="推流地址" v-bind='validateInfos.target' >
        <a-input allowClear v-model:value="bodyParamsState.target" > </a-input>
      </a-form-item>
      <a-form-item label="视频流标识" v-bind='validateInfos.streamPath' >
        <a-input allowClear v-model:value="bodyParamsState.streamPath" > </a-input>
      </a-form-item>
    </a-form>
  </modal-pro>
</template>
<script lang="ts" setup >
import { RtsController } from '@/controller/rts'
import { onMounted, reactive } from 'vue'
import { Form } from 'ant-design-vue'
import dayjs from 'dayjs'

const columns = [
  {
    title: 'ID',
    dataIndex: 'ID'
  },
  {
    title: '推流类型',
    dataIndex: 'Type'
  },
  {
    title: '流地址',
    dataIndex: 'StreamPath'
  },
  {
    title: '流id',
    dataIndex: 'StreamID'
  },
  {
    title: '重连参数',
    dataIndex: 'ReConnectCount'
  },
  {
    title: '推流参数',
    dataIndex: 'Args'
  },
  {
    title: '开始时间',
    dataIndex: 'StartTime',
    key: 'StartTime'
  },
  {
    title: '操作',
    dataIndex: 'action',
    key: 'action'
  }
]

const useForm = Form.useForm

const pullList = [
  {
    key: 'RTSP',
    value: 'RTSP'
  },
  {
    key: 'RTMP',
    value: 'RTMP'
  }
]

const bodyParamsState = reactive({
  name: 'RTSP',
  target: '',
  streamPath: '',
  save: 2
})

const state = reactive<{
  dataSource: RTS.PullStream.Detail[]
  loading: boolean
  visible: boolean
  detail: Partial<RTS.PullStream.Detail>
}>({
  loading: false,
  visible: false,
  detail: {},
  dataSource: []
})

const { resetFields, validate, validateInfos } = useForm(bodyParamsState, reactive({
  streamPath: [{ required: 'true', message: '请填写流地址' }],
  target: [{ required: 'true', message: '请填写流地址' }]
}))

const openModal = () => {
  state.visible = true
}

const ok = () => {
  validate().then(async () => {
    await RtsController.createPush(bodyParamsState)
    state.visible = false
    getPushList()
  })
}

const stopPush = async (remoteurl: string) => {
  await RtsController.stopPush(remoteurl)
  getPushList()
}

const getPushList = async () => {
  const { data } = await RtsController.listPush()
  state.dataSource = data
}

onMounted(() => {
  getPushList()
})

</script>

<style lang="less" scoped >

</style>
