<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form inline size="mini">
      <el-form-item :label="form.typeList[0] === '13' ? '套餐名称：' : '流量名称：'">
        <el-input v-model="form.name" placeholder="请输入套餐名称" clearable />
      </el-form-item>
      <el-form-item v-if="form.typeList[0] !== '12'" label="资源平台：">
        <el-select v-model="form.platformId" placeholder="请选择资源平台" clearable>
          <el-option v-for="item in platformOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item v-else label="流量套餐：">
        <el-select v-model="form.goodsId" placeholder="请选择流量套餐" clearable>
          <el-option v-for="(item, index) in goodsOptions" :key="index" :label="item" :value="index" />
        </el-select>
      </el-form-item>
      <el-form-item label="关联设备：">
        <el-select v-model="form.deviceIds" filterable placeholder="请选择关联设备" clearable>
          <el-option v-for="item in devOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="当前状态：">
        <el-select v-model="form.status" placeholder="请选择当前状态" clearable>
          <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="getSearch">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="getRefresh">重置</el-button>
        <el-button type="primary" plain icon="el-icon-plus" @click="getDetail()"
          v-hasPermi="['service:package:add']">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table :data="tableData" v-loading="loading" :default-sort="{ prop: 'sort' }">
      <el-table-column label="排序" align="center" prop="sort" key="sort" :formatter="sortFormatter" />
      <el-table-column :label="form.typeList[0] === '13' ? '套餐名称' : '流量名称'" align="center" prop="name" key="name"
        show-overflow-tooltip />
      <el-table-column label="关联设备" align="center" prop="deviceIds" key="deviceIds" :formatter="devFormatter"
        show-overflow-tooltip />
      <el-table-column label="原价/元" align="center" prop="price" key="price" />
      <el-table-column label="活动价/元" align="center" prop="discount" key="discount" />
      <el-table-column v-if="form.typeList[0] === '12'" label="流量套餐" align="center" prop="intro" key="intro"
        show-overflow-tooltip />
      <el-table-column v-if="form.typeList[0] !== '12'" label="有效期" align="center" prop="indate" key="indate"
        :formatter="timeFormatter" />
      <el-table-column v-if="form.typeList[0] !== '12'" label="资源平台" align="center" prop="platformId" key="platformId"
        :formatter="platformFormatter" />
      <el-table-column v-if="form.typeList[0] !== '12'" label="栏目" align="center" prop="groupId" key="groupId"
        :formatter="sceneFormatter" />
      <el-table-column label="当前状态" align="center" prop="status" key="status" :formatter="statusFormatter" />
      <el-table-column label="是否推荐" align="center" prop="isRecommend" key="isRecommend" :formatter="recommendFormatter" />
      <el-table-column label="创建时间" align="center" prop="createTime" key="createTime" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="getDetail(scope.row.id)" v-hasPermi="['service:package:edit']">
            编辑
          </el-button>
          <el-button v-if="scope.row.status === 1" type="text" @click="getChange(scope.row)"
            v-hasPermi="['service:package:down']">下架</el-button>
          <el-button v-else type="delete" @click="getDelete(scope.row)"
            v-hasPermi="['service:package:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="form.pageNum" :limit.sync="form.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
import { list, remove, shelve, goodsList } from '@/api/service/package'
import { list as sceneList } from '@/api/operation/scene'
import { devMixin, serviceTimeMixin } from '@/mixin/index'
export default {
  mixins: [devMixin, serviceTimeMixin],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 表单
      form: {
        pageNum: 1,
        pageSize: 10,
        typeList: this.$route.query.typeList.split(',')
      },
      // 列表
      tableData: [],
      // 总数据
      total: 0,
      // 是否推荐
      recommendOptions: [{
        value: 0,
        label: '是'
      }, {
        value: 1,
        label: '否'
      }],
      // 当前状态
      statusOptions: [{
        value: 1,
        label: '上架'
      }, {
        value: 4,
        label: '下架'
      }],
      // 资源平台
      platformOptions: [{
        value: 6,
        label: 'QQ音乐'
      }, {
        value: 9,
        label: '酷狗音乐'
      }, {
        value: 10,
        label: '爱听音乐'
      }],
      // 流量套餐
      goodsOptions: [],
      // 爱听专区
      sceneOptions: []
    }
  },
  mounted() {
    this.getList()
    if (this.form.typeList[0] === '12') {
      this.getGoodSList()
    } else {
      this.getSceneList()
    }
  },
  methods: {
    // 列表
    getList() {
      this.loading = true
      list(this.form).then(res => {
        if (res.code === 0) {
          this.tableData = res.data.records
          this.total = res.data.total
          this.loading = false
        }
      })
    },

    // 流量套餐
    getGoodSList() {
      goodsList().then(res => {
        if (res.code === 0) {
          this.goodsOptions = res.data
        }
      })
    },

    // 爱听专区
    getSceneList() {
      sceneList().then(res => {
        if (res.code === 0) {
          res.data.map(i => {
            this.sceneOptions.push({
              value: i.id,
              label: i.name
            })
          })
        }
      })
    },

    // 搜索
    getSearch() {
      this.form.pageNum = 1
      this.getList()
    },

    // 重置
    getRefresh() {
      this.form = {
        pageNum: 1,
        pageSize: 10,
        typeList: this.$route.query.typeList.split(',')
      }
      this.getList()
    },

    // 新增 / 编辑
    getDetail(id) {
      this.$router.push({
        path: `/service/package/detail`,
        query: {
          id: id,
          typeList: this.form.typeList.join(',')
        }
      })
      this.$route.meta.activeMenu = this.$route.query.typeList.split(',')[0] === '13' ? '/service/musicPackage' : '/service/flowPackage'
    },

    // 删除
    getDelete(row) {
      this.$confirm(`是否删除${row.name}?`, '提示', {
        type: 'warning'
      }).then(() => {
        remove(row.id).then(res => {
          if (res.code === 0) {
            this.$message.success('删除成功！')
            this.getList()
          }
        })
      }).catch(() => { })
    },

    // 下架
    getChange(row) {
      this.$confirm(`是否下架${row.name}?`, '提示', {
        type: 'warning'
      }).then(() => {
        shelve(row.id).then(res => {
          if (res.code === 0) {
            this.$message.success('下架成功！')
            this.getList()
          }
        })
      }).catch(() => { })
    },

    // 字典翻译
    sortFormatter(row) {
      return row.businessType === 5 ? row.sort : '/'
    },
    // 资源平台
    platformFormatter(row) {
      return this.selectDictLabel(this.platformOptions, row.platformId)
    },
    // 服务时间
    timeFormatter(row) {
      return this.selectDictLabel(this.serviceTimeOptions, row.indate)
    },
    // 当前状态
    statusFormatter(row) {
      return this.selectDictLabel(this.statusOptions, row.status)
    },
    // 是否推荐
    recommendFormatter(row) {
      return this.selectDictLabel(this.recommendOptions, row.isRecommend)
    },
    // 关联设备
    devFormatter(row) {
      return row.deviceIds ? row.deviceIds.split(',').map(i => this.selectDictLabel(this.devOptions, i)).join(',') : ''
    },
    // 栏目
    sceneFormatter(row) {
      return this.selectDictLabel(this.sceneOptions, row.groupId)
    }
  }
}
</script>