<template>
  <avue-form :option="option" v-model="form" @submit="onSubmit" />
</template>

<script lang="ts">
import Vue from 'vue'
import formOption from "@/avue/form/liveroominfo"
import { isInteger } from 'lodash'
import httpx from '@/utils/httpx'
import dayjs from 'dayjs'

export default Vue.extend({
  data() {
    return {
      form: {
        title: "",
        imageShare: [""],
        imageCover: [""],
        startTime: "",
        videoKind: "",
        livePushUrl: "",
        livePullRtmpUrl: "",
        livePullHlsUrl: "",
        jmessageRoomId: "",
        replayVideoUrl: "",
        desc: "",
        summary: "",
        showInList: false,
        hours: 0,
      },
      id: 0,
      new: false,
    }
  },

  mounted() {
    const id_str = this.$route.params.id
    const id = parseInt(id_str)
    if (isNaN(id) || !isInteger(id)) {
      if (id_str === "new") {
        this.new = true
      } else {
        this.notFound()
      }
    } else {
      this.new = false
      this.id = id
      this.getRoomInfo(id)
    }
  },

  computed: {
    option() {
      //@ts-ignore
      return formOption(this.new)
    }
  },

  methods: {
    getRoomInfo(id?: number) {
      id = id || this.id
      return httpx.post("/rooms/getRoom", { id }).then(({ data }) => {
        const roominfo = data.roomInfo
        this.form = {
          title: roominfo.title,
          imageShare: [roominfo.imageShare],
          imageCover: [roominfo.imageCover],
          startTime: roominfo.startTime,
          videoKind: roominfo.videoKind,
          livePushUrl: roominfo.livePushUrl,
          livePullHlsUrl: roominfo.livePullHlsUrl,
          livePullRtmpUrl: roominfo.livePullRtmpUrl,
          jmessageRoomId: roominfo.jmessageRoomId,
          replayVideoUrl: roominfo.replayVideoUrl,
          desc: roominfo.desc,
          summary: roominfo.summary,
          showInList: roominfo.showInList,
          hours: roominfo.hours,
        }
      }).catch(this.notFound)
    },

    onSubmit(form: any, done: Function) {
      const path = this.new ? "/rooms/addLiveRoom" : "/rooms/updateRoom"
      const postdata = {
        id: this.new ? undefined : this.id,
        adminId: this.new ? 0 : undefined,
        viewCount: this.new ? 0 : undefined,
        hours: 0,
        title: form.title,
        imageShare: form.imageShare[0] || "",
        imageCover: form.imageCover[0] || "",
        startTime: dayjs(form.startTime).format("YYYY-MM-DD HH:mm:ss"),
        livePushUrl: form.livePushUrl,
        livePullHlsUrl: form.livePullHlsUrl,
        livePullRtmpUrl: form.livePullRtmpUrl,
        jmessageRoomId: form.jmessageRoomId,
        replayVideoUrl: form.replayVideoUrl,
        videoKind: form.videoKind,
        desc: form.desc,
        summary: form.summary,
        showInList: form.showInList,
      }

      httpx.post(path, postdata).then((data) => {
        console.log(data)
        this.$message({
          message: '操作成功',
          type: 'success',
          duration: 1500
        })
        this.$router.back()
      })
      done()
    },
    notFound() {
      this.$router.replace("/404")
    }
  }

})

</script>