<template>
  <div class="padding-20">
    <div class="search-box">
      <el-form
        ref="form"
        :inline="true"
        :model="searchForm"
        :rules="formRules"
        clearable
        class="mt-10">
        <el-form-item label="用户ID:"
                      prop="id"
                      :rules="formRules.required">
          <el-input v-model="searchForm.id" placeholder="请输入用户ID" clearable></el-input>
        </el-form-item>
        <el-form-item class="ml-10">
          <el-button icon="el-icon-search" type="primary" @click="handleSearch">查询</el-button>
        </el-form-item>
        <el-form-item class="ml-10">
          <el-button icon="el-icon-plus" type="primary" v-permission="'business_sms_playList_add'" @click="add">新增
          </el-button>
        </el-form-item>
      </el-form>
    </div>
    <h3 class="marginT-10">{{ user_song_name }}的曲库</h3>
    <el-table :data="tableData"
              stripe
              v-loading="tableLoading"
              fit
              class="order-table"
              border
              :max-height="vheight">
      <el-table-column label="ID" prop="id"></el-table-column>
      <el-table-column label="歌曲名字" prop="song_name"></el-table-column>
      <el-table-column label="原唱" prop="song_auth_name"></el-table-column>
      <el-table-column label="歌曲状态">
        <template slot-scope="scope">
          <p>{{ ['无效', '有效'][scope.row.song_status] }}</p>
        </template>
      </el-table-column>
      <el-table-column label="点歌次数" prop="song_num"></el-table-column>
      <el-table-column label="单价（元）">
        <template slot-scope="scope">
          <p>{{ scope.row.song_price | fen2Yuan }}</p>
        </template>
      </el-table-column>
      <el-table-column label="添加时间" prop="created_at" width="160"></el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="text" v-permission="'business_sms_playList_edit'" @click="edit(scope.row)">编辑</el-button>
          <el-button type="text" v-permission="'business_sms_playList_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" :exData="detailsDialog.exData"
            @success="init"></detail>
  </div>
</template>

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

export default {
  mixins: [page],
  components: {
    detail,
  },
  data () {
    return {
      detailsDialog: {
        show: false,
        exData: {}
      },
      searchForm: {},
      tableData: [],
      tableUrl: '/v1/user/song/list',
      user_song_name: ''
    }
  },
  methods: {
    add () {
      this.detailsDialog.exData = {}
      this.detailsDialog.show = true
    },
    edit (row) {
      this.detailsDialog.exData = row
      this.detailsDialog.show = true
    },
    del (row) {
      this.$confirm('确定要删除吗', '确认', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        const data = await this.$fetch('/v1/user/song/delete', { id: row.id }, 'get')
        if (data.code === 200) {
          this.$message.success('删除成功')
          this.init()
        }
      }).catch(() => {})
    },
    handleSearch () {
      this.$refs.form.validate(async valid => {
        if (valid) {
          this.searchSubmit()
        }
      })
    }
  },
  mounted () {
    // 用户ID
    this.$set(this.searchForm, 'id', this.$route.query.id)
    this.init()
  }
}
</script>

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