<template>
  <div class="app-container">
    <div v-loading.fullscreen.lock="loadingFull" />
    <div class="filter-container">
      <el-input
        v-model="searchParams.search_name"
        placeholder="标题"
        class="filter-item filter-input"
      />
      <el-button
        type="primary"
        icon="el-icon-search"
        class="filter-item"
        @click="searchParams.page_index = 1;getList()"
      >
        搜索
      </el-button>
      <el-button
        type="primary"
        icon="el-icon-edit"
        class="filter-item"
        @click="handleAddLive"
      >
        添加
      </el-button>
    </div>
    <el-table
      :data="table"
      :header-cell-style="{ background: '#f8fbfc' }"
      class="tableBorder"
      border
      stripe
    >
      <el-table-column
        type="index"
        :index="indexMethod"
        width="70"
        label="编号"
        align="center"
      />
      <el-table-column
        prop="imgurl"
        label="封面"
        align="center"
        width="70"
      >
        <template
          slot-scope="{ row }"
        >
          <el-image
            style="width: 40px; height: 40px"
            :src="row.imgurl"
            fit="contain"
            :preview-src-list="[row.imgurl]"
            class="hoverImg"
          />
        </template>
      </el-table-column>
      <el-table-column
        prop="title"
        label="标题"
        align="center"
      />
      <el-table-column
        prop="desc"
        label="简介"
        align="center"
      >
        <template
          slot-scope="{ row }"
        >
          {{ row.summary ? row.summary : '暂无简介' }}
        </template>
      </el-table-column>
      <el-table-column
        prop="startime"
        label="开始时间"
        align="center"
        width="250"
      />
      <el-table-column
        prop="contents"
        label="链接"
        align="center"
      >
        <template
          slot-scope="{ row }"
        >
          <el-button
            type="text"
          >
            {{ row.contents }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="150"
        align="center"
      >
        <template slot-scope="{ row }">
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <pagination
      :total="tableTotal"
      :limit="searchParams.page_size"
      :page="searchParams.page_index"
      @pagination="changePagination"
      @update:limit="changePagination"
    />
    <!-- 添加直播 -->
    <el-dialog
      title="添加直播"
      :visible.sync="visible.live"
    >
      <el-form
        ref="liveForm"
        :model="liveForm"
        :rules="liveRules"
        label-width="80px"
      >
        <el-form-item label="标题:" prop="title">
          <el-input
            v-model="liveForm.title"
            placeholder="标题"
          />
        </el-form-item>
        <el-form-item label="简介:">
          <el-input
            v-model="liveForm.desc"
            type="textarea"
            placeholder="简介"
          />
        </el-form-item>
        <el-form-item label="封面:" required>
          <el-upload
            :action="upload_url"
            :show-file-list="false"
            :http-request="thumbPictureUpload"
          >
            <el-button
              size="mini"
            >
              点击上传
            </el-button>
            <div
              slot="tip"
              class="el-upload__tip"
            >
              (建议尺寸 600*600,大小在500k以内,只能上传1张，如需修改请重新上传主图)
            </div>
          </el-upload>
          <div v-if="liveForm.thumb">
            <el-image
              style="width: 145px; height: 145px"
              :src="liveForm.thumb"
              fit="rtl"
              :preview-src-list="[liveForm.thumb]"
            />
          </div>
        </el-form-item>
        <el-form-item label="直播链接:" prop="link">
          <el-input
            v-model="liveForm.link"
            placeholder="链接"
          />
        </el-form-item>
        <el-form-item label="开始时间:" prop="start_time">
          <el-date-picker
            v-model="liveForm.start_time"
            type="datetime"
            placeholder="选择开始直播时间"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            size="mini"
            @click="handleCancel"
          >
            取消
          </el-button>
          <el-button
            type="primary"
            size="mini"
            @click="handleSubmit"
          >
            添加
          </el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- loading -->
    <div v-if="thumbLoad" v-loading="thumbLoad" class="loadingShade" />
  </div>
</template>
<script>
// api
import {
  getList,
  uploadImage,
  addLive,
  deleteLive
} from '@/api/community/live'
export default {
  data() {
    return {
      searchParams: {
        search_name: '',
        page_index: 1,
        page_size: 20
      },
      visible: {
        live: false
      },
      upload_url: 'http://192.168.0.15:8011/api/article/uploadarticleimage',
      liveForm: {
        title: '',
        desc: '',
        start_time: '',
        thumb: '',
        link: ''
      },
      liveRules: {
        title: [
          { required: true, message: '请输入直播标题', trigger: 'blur' }
        ],
        link: [
          { required: true, message: '请输入直播链接', trigger: 'blur' }
        ],
        start_time: [
          { required: true, message: '请选择开始时间', trigger: 'blur' }
        ]
      },
      thumbLoad: false,
      table: [],
      tableTotal: 0,
      loadingFull: false
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 获取直播列表
    async getList() {
      this.loadingFull = true
      try {
        const res = await getList(this.searchParams)
        const { message, code } = res
        if (code === 200) {
          const { data } = res
          const { list, total } = data
          this.tableTotal = total
          this.table = list
        } else {
          this.$message.error(message)
        }
      } catch (e) {
        console.log(`获取社区文章失败:${e}`)
      }
      this.loadingFull = false
    },
    // 上传封面图(缩略图)
    thumbPictureUpload(params) {
      this.thumbLoad = true
      const file = params.file
      const formData = new FormData()
      formData.append('file', file)
      uploadImage(formData).then(res => {
        this.thumbLoad = false
        try {
          const { message, code, data } = res
          if (code === 200) {
            this.liveForm.thumb = data
          } else {
            this.liveForm.thumb = ''
            this.$message.error(message)
          }
        } catch (e) {
          console.log(`上传直播封面失败:${e}`)
        }
      }).catch(() => {
        this.thumbLoad = false
      })
    },
    // 添加直播
    handleAddLive() {
      this.visible.live = true
    },
    // 取消
    handleCancel() {
      this.liveForm.desc = ''
      this.liveForm.thumb = ''
      this.liveForm.title = ''
      this.liveForm.link = ''
      this.liveForm.start_time = ''
      this.$refs.liveForm.resetFields()
      this.visible.live = false
    },
    // 添加直播
    handleSubmit() {
      this.$refs.liveForm.validate((valid) => {
        if (valid) {
          if (!this.liveForm.thumb) {
            this.$message.warning('请添加封面')
            return false
          }
          this.addLive(this.liveForm)
        } else {
          return false
        }
      })
    },
    // 添加直播
    async addLive(data) {
      const res = await addLive(data)
      try {
        const { message, code } = res
        if (code === 200) {
          this.$message.success(message)
          this.handleCancel()
          this.getList()
        } else {
          this.$message.error(message)
        }
      } catch (e) {
        console.log(`添加直播失败:${e}`)
      }
    },
    // 分页页码变化
    changePagination(data) {
      const { page, limit } = data
      this.searchParams.page_size = limit
      this.searchParams.page_index = page
      this.getList()
    },
    // 删除
    handleDelete(row) {
      const { id, title } = row
      this.$confirm(`确认删除直播: ${title}?`, '删除', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteLive(id)
      })
    },
    // 删除
    async deleteLive(id) {
      try {
        const res = await deleteLive({ id })
        const { code, message } = res
        if (code === 200) {
          this.$message.success(message)
          this.getList()
        } else {
          this.$message.error(message)
        }
      } catch (e) {
        console.log(`删除直播失败: ${e}`)
      }
    },
    // 编号
    indexMethod(index) {
      const { page_index, page_size } = this.searchParams
      index += (page_index - 1) * page_size + 1
      return index
    }
  }
}
</script>
