<template>
  <section class="vpList">
    <ai-list>
      <ai-title slot="title" title="视频宣传" isShowBottomBorder/>
      <template #content>
        <ai-search-bar>
          <template #left>
            <el-button type="primary" icon="iconfont iconAdd" @click="handleEdit()">添加</el-button>
          </template>
          <template #right>
            <el-input size="small" placeholder="搜索标题" v-model="search.productName" clearable
                      @change="page.current=1,getTableData()"/>
          </template>
        </ai-search-bar>
        <ai-table :tableData="tableData" :total="page.total" :current.sync="page.current" :size.sync="page.size"
                  @getList="getTableData" :col-configs="colConfigs" :dict="dict">
          <el-table-column slot="options" label="操作" fixed="right" align="center" width="200px">
            <template slot-scope="{row}">
              <el-button v-if="row.status==0" type="text" @click="handlePublic(row)">发布</el-button>
              <el-button v-else-if="row.status==1" type="text" @click="handlePublic(row)">取消发布</el-button>
              <el-button type="text" @click="handleEdit(row.id)">编辑</el-button>
              <el-button type="text" @click="handleDelete(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </ai-table>
      </template>
    </ai-list>
  </section>
</template>

<script>
import {mapState} from "vuex";

export default {
  name: "vpList",
  props: {
    instance: Function,
    dict: Object,
    permissions: Function
  },
  computed: {
    ...mapState(['user']),
    isFinanceUser() {
      return !!this.user.financeUser?.id
    }
  },
  data() {
    return {
      search: {productName: ""},
      page: {current: 1, size: 10, total: 0},
      tableData: [],
      colConfigs: [
        {label: "标题", prop: "title"},
        {label: "浏览数量", prop: "viewCount", align: "center", width: 100},
        {label: "发布人", prop: "publishUserName", width: 120},
        {label: "发布时间", prop: "publishTime"},
        {label: "状态", prop: "status", dict: "videoNewsStatus", align: "center"},
        {slot: "options"}
      ]
    }
  },
  methods: {
    getTableData() {
      this.instance.post("/appvideonews/list", null, {
        params: {...this.page, ...this.search}
      }).then(res => {
        if (res?.data) {
          this.tableData = res.data?.records
          this.page.total = res.data.total
        }
      })
    },
    showDetail(id) {
      this.$router.push({query: {id}})
    },
    handleEdit(id) {
      this.$router.push({query: {id}, hash: "#add"})
    },
    handleDelete(ids) {
      this.$confirm("是否要删除该视频?").then(() => {
        this.instance.post("/appvideonews/delete", null, {
          params: {ids}
        }).then(res => {
          if (res?.code == 0) {
            this.$message.success("删除成功!")
            this.getTableData()
          }
        })
      }).catch(() => 0)
    },
    handleIsHot(row) {
      let {id, isHot} = row
      this.instance.post("appfinancialproduct/setIsHot", null, {
        params: {id, isHot}
      }).then(res => {
        if (res?.code == 0) {
          this.$message.success("修改成功!")
          this.getTableData()
        } else {

        }
      })
    },
    handlePublic(row) {
      let openLabel = row.status == 1 ? "取消发布" : "发布", status = (Number(row.status) + 1) % 2
      this.$confirm(`是否要${openLabel}视频?`).then(() => {
        this.instance.post("/appvideonews/setStatus", null, {
          params: {id: row.id, status}
        }).then(res => {
          if (res?.code == 0) {
            this.$message.success(openLabel + "成功!")
            this.getTableData()
          }
        })
      }).catch(() => 0)
    },
  },
  created() {
    this.getTableData()
  }
}
</script>

<style lang="scss" scoped>
.vpList {
  height: 100%;
}
</style>
