<template>
  <div class="padding-20">
    <div class="search-box">
      <el-form
        ref="form"
        :inline="true"
        :model="searchForm"
        clearable
        class="mt-10">
        <el-form-item label="演出日期:">
          <el-date-picker
            v-model="searchForm.plan_time"
            align="right"
            type="date"
            placeholder="选择日期"
            :picker-options="pickerOptions"
            value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item class="ml-10">
          <el-button icon="el-icon-search" type="primary" @click="searchSubmit">查询</el-button>
        </el-form-item>
        <el-form-item class="ml-10">
          <el-button icon="el-icon-plus" type="primary" v-permission="'business_sms_plan_add'" @click="add">新增
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="tableData"
              stripe
              v-loading="tableLoading"
              fit
              class="marginT-10 order-table"
              border
              :max-height="vheight">
      <el-table-column label="ID" prop="id" width="80"></el-table-column>
      <el-table-column label="艺人" prop="plan_author_name"></el-table-column>
      <el-table-column label="演唱时间">
        <template slot-scope="scope">
          <span>{{ scope.row.plan_start_time }}</span>
          <span>至</span>
          <span>{{ scope.row.plan_end_time }}</span>
        </template>
      </el-table-column>
      <el-table-column label="本节点歌歌单" prop="song_num" width="120"></el-table-column>
      <el-table-column label="操作" width="160">
        <template slot-scope="scope">
          <el-button type="text" v-permission="'business_sms_plan_set'" @click="editPlayList(scope.row)">设置歌单
          </el-button>
          <el-button type="text" v-permission="'business_sms_plan_del'" @click="del(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      class="marginT-20"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :hide-on-single-page="true"
      :current-page="page"
      :page-size="page_size"
      :page-sizes="[10, 20, 100, 200, 300, 400]"
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="totalCount"/>
    <detail v-if="detailsDialog.show"
            v-model="detailsDialog.show"
            :singerData="singerData"
            :exData="detailsDialog.exData"
            @success="init"></detail>
    <playList v-if="playListDialog.show"
              v-model="playListDialog.show"
              :exData="playListDialog.exData"
              @success="init"></playList>
  </div>
</template>

<script>
import page from '@/mixin/page'
import detail from './details'
import playList from './playList'

export default {
  mixins: [page],
  components: {
    detail,
    playList
  },
  data () {
    return {
      pickerOptions: {
        shortcuts: [{
          text: '今天',
          onClick (picker) {
            picker.$emit('pick', new Date())
          }
        }, {
          text: '昨天',
          onClick (picker) {
            const date = new Date()
            date.setTime(date.getTime() - 3600 * 1000 * 24)
            picker.$emit('pick', date)
          }
        }, {
          text: '一周前',
          onClick (picker) {
            const date = new Date()
            date.setTime(date.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', date)
          }
        }]
      },
      detailsDialog: {
        show: false,
        exData: {}
      },
      playListDialog: {
        show: false,
        exData: {}
      },
      searchForm: {},
      tableData: [],
      tableUrl: '/v1/bar/show/plan/list',
      singerData: [] // 歌手列表
    }
  },
  methods: {
    add () {
      this.detailsDialog.exData = {}
      this.detailsDialog.show = true
    },
    del (row) {
      this.$confirm('确定要删除吗', '确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const data = await this.$fetch('/v1/bar/show/plan/delete', { id: row.id }, 'get')
        if (data.code === 200) {
          this.$message.success('删除成功')
          this.init()
        }
      }).catch(() => {})
    },
    // 设置歌单弹窗
    editPlayList (row) {
      this.playListDialog.exData = row
      this.playListDialog.show = true
    },
    // 获取所有歌手列表
    async fetchSingerList () {
      const api = '/v1/user/singer/list'
      const { code, data } = await this.$fetch(api, {
        page: 1,
        page_size: 1000
      }, 'get')
      if (this.tableData && (data.data || data.list) && code === 200) {
        this.singerData = data.data || data.list
      }
    }
  },
  mounted () {
    this.init()
    this.fetchSingerList()
  }
}
</script>

<style lang="scss" scoped>
</style>
