<template>
  <div class="app-container">
    <div class="head-container flex">
      <el-button type="primary" icon="plus" @click="$refs.addSong.show()">添加歌曲</el-button>
    </div>
    <div class="flex u-m-b-10">
      <div class="flex">
        <span>歌手页地址:</span>
        <a class="cursor-pointer" target="_blank" :href="singgerUrl">{{ singgerUrl }}</a>
      </div>
    </div>
    <div class="head-container" id="table_drag">
      <el-table :data="tableData.list" v-loading="tableData.loading" row-key="id">
        <el-table-column label="排序" sortable prop="sort"></el-table-column>
        <el-table-column label="id" prop="id"></el-table-column>
        <el-table-column label="歌曲图片" prop="img">
          <template v-slot="scope">
            <el-image
              :src="scope.row.img"
              style="width: 40px; height: 40px; border-radius: 4px; background-color: #efefef"
            >
              <template #error>
                <div class="img_error">
                  <i class="icon el-icon-document-delete"></i>
                </div>
              </template>
            </el-image>
          </template>
        </el-table-column>
        <el-table-column label="歌曲名称" prop="name"></el-table-column>
        <el-table-column label="演出歌手" prop="singer"></el-table-column>
        <el-table-column label="单价" prop="price"></el-table-column>
        <el-table-column label="点唱次数" prop="salesNumber"></el-table-column>
        <el-table-column label="歌曲状态" prop="status">
          <template v-slot="scope">
            <el-switch
              v-model="scope.row.status"
              :active-value="1"
              :inactive-value="0"
              @change="showChange($event, scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="240">
          <template v-slot="scope">
            <!-- <el-button link icon="el-icon-rank" v-if="isPcBowser">排序</el-button>
            <el-button link size="small" round icon="el-icon-edit" @click="$refs.addSong.show(scope.row)"
              style="margin-left: 20px !important;">编辑</el-button> -->
            <el-button
              link
              size="small"
              round
              icon="el-icon-edit"
              @click="$refs.addSong.show(scope.row)"
            >
              编辑
            </el-button>
            <el-popconfirm title="确定删除吗？" @confirm="delHandle([scope.row.id])">
              <template #reference>
                <el-button link size="small" round icon="el-icon-delete">删除</el-button>
              </template>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <addSong ref="addSong" @success="addSongSuccess"></addSong>
    <el-pagination
      @size-change="paginationSizeChange"
      :total="tableData.total"
      :current-page="tableData.page"
      :page-size="tableData.size"
      @current-change="paginationChange"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>
  </div>
</template>

<script>
import shopSongApi from "@/api/account/shopSong";
import addSong from "./components/add-song.vue";
export default {
  components: {
    addSong,
  },
  data() {
    return {
      singgerUrl: "",
      tableData: {
        page: 1,
        size: 10,
        total: 0,
        loading: false,
        list: [],
      },
    };
  },
  mounted() {
    this.getTableData();
    this.getUrl();
  },
  methods: {
    async getUrl() {
      const res = await shopSongApi.url();
      console.log(res);
      const baseUrl =
        location.hostname === "localhost" ? "https://tapi.cashier.sxczgkj.cn" : location.origin;
      this.singgerUrl = baseUrl + "/song-H5/index.html?t=" + res;
    },
    jumpUrl(url) {
      window.open(url);
    },
    addSongSuccess() {
      this.getTableData();
    },
    paginationSizeChange(e) {
      this.tableData.size = e;
      this.getTableData();
    },
    async showChange(e, row) {
      try {
        this.tableData.loading = true;
        const data = { ...row };
        data.status = e;
        await shopSongApi.update(data);
        this.getTableData();
      } catch (error) {
        console.log(error);
        this.tableData.loading = false;
      }
    },
    // 重置查询
    resetHandle() {
      this.tableData.page = 0;
      this.getTableData();
    },
    // 分页回调
    paginationChange(e) {
      this.tableData.page = e;
      this.getTableData();
    },
    // 删除
    async delHandle(ids) {
      try {
        await shopSongApi.delete(ids);
        ElMessage({
          title: "成功",
          message: `删除成功`,
          type: "success",
        });
        this.getTableData();
      } catch (error) {
        console.log(error);
      }
    },
    // 获取音乐列表
    async getTableData() {
      try {
        this.tableData.loading = true;
        const res = await shopSongApi.getList({
          page: this.tableData.page,
          size: this.tableData.size,
        });
        this.tableData.loading = false;
        this.tableData.list = res.records;
        this.tableData.total = res.totalRow;
      } catch (error) {}
    },
  },
};
</script>
<style>
.flex {
  display: flex;
  align-content: center;
}
.cursor-pointer {
  cursor: pointer;
  color: #1890ff !important;
  transition: all 0.3s;
  font-size: 16px;
}
.cursor-pointer:hover {
  opacity: 0.7;
}
.app-container {
  margin: 20px;
  background-color: #fff;
}
</style>