<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form inline size="mini">
      <el-form-item label="配置名称：">
        <el-input v-model="form.name" placeholder="请输入配置名称" />
      </el-form-item>
      <el-form-item label="当前状态：">
        <el-select v-model="form.status" placeholder="请选择当前状态">
          <el-option v-for="item in disabledOptions" :key="item.value" :value="item.value" :label="item.label" />
        </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" icon="el-icon-plus" plain @click="getDialog('新增')"
          v-hasPermi="['device:music:add']">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table :data="tableData" v-loading="loading">
      <el-table-column label="序号" type="index" align="center" />
      <el-table-column label="配置名称" prop="name" align="center" show-overflow-tooltip />
      <el-table-column label="关联设备" prop="deviceIds" align="center" show-overflow-tooltip>
        <template slot-scope="scope">
          {{ scope.row.deviceIds.split(',').map(i => selectDictLabel(allDev, i)).join(',') }}
        </template>
      </el-table-column>
      <el-table-column label="创建时间" prop="createTime" align="center" />
      <el-table-column label="当前状态" prop="status" align="center" :formatter="statusFormatter" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="getDialog('查看', scope.row.id)">查看</el-button>
          <el-button type="text" v-if="scope.row.status === 0" @click="getChange('下架', scope.row, 1)"
            v-hasPermi="['device:music:down']">下架</el-button>
          <span v-else>
            <el-button type="text" style="margin-left: 10px" @click="getDialog('编辑', scope.row.id)"
              v-hasPermi="['device:music:edit']">编辑</el-button>
            <el-button type="text" @click="getChange('上架', scope.row, 0)"
              v-hasPermi="['device:music:up']">上架</el-button>
            <el-button type="delete" @click="getChange('删除', scope.row, 2)"
              v-hasPermi="['device:music:delete']">删除</el-button>
          </span>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="form.pageNum" :limit.sync="form.pageSize"
      @pagination="getList" />
    <!-- 弹窗 -->
    <el-dialog :visible.sync="dialogVisible" :title="title" width="500px" :before-close="close">
      <el-form :model="dialogForm" :rules="rules" ref="dialogForm" label-width="100px"
        :disabled="title === '查看' ? true : false">
        <el-form-item label="配置名称：" prop="name">
          <el-input v-model="dialogForm.name" placeholder="请输入配置名称" />
        </el-form-item>
        <el-form-item label="关联设备：" prop="deviceIds">
          <el-select v-model="dialogForm.deviceIds" multiple filterable placeholder="请选择关联设备">
            <el-option v-for="item in devOptions" :key="item.value" :value="item.value" :label="item.label" />
          </el-select>
        </el-form-item>
        <el-form-item label="赠送信息：">
          <el-form-item label="资源平台：" prop="bullframeMusicGiveReqList.platformId">
            <el-select v-model="dialogForm.bullframeMusicGiveReqList.platformId" placeholder="请选择资源平台">
              <el-option v-for="item in platformOptions" :key="item.value" :value="item.value" :label="item.label" />
            </el-select>
          </el-form-item>
          <el-form-item label="栏目：" prop="bullframeMusicGiveReqList.groupIdList">
            <el-select v-model="dialogForm.bullframeMusicGiveReqList.groupIdList" multiple placeholder="请选择栏目">
              <el-option v-for="item in sceneOptions" :key="item.value" :value="item.id" :label="item.name" />
            </el-select>
          </el-form-item>
          <el-form-item label="有效时长：" prop="bullframeMusicGiveReqList.indate">
            <el-select v-model="dialogForm.bullframeMusicGiveReqList.indate" placeholder="请选择有效时长">
              <el-option v-for="item in timeOptions" :key="item.value" :value="item.value" :label="item.label" />
            </el-select>
          </el-form-item>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="close">取消</el-button>
        <el-button v-if="title !== '查看' ? true : false" type="primary" @click="getSubmit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list, detail, submit, change, devList } from '@/api/device/music'
import { list as sceneList } from '@/api/operation/scene'
import { disabledMixin } from '@/mixin/index'
export default {
  mixins: [disabledMixin],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 表单
      form: {
        pageNum: 1,
        pageSize: 10
      },
      // 总数居
      total: 0,
      // 列表
      tableData: [],
      // 弹窗
      dialogVisible: false,
      title: '新增',
      // 弹窗表单
      dialogForm: {
        bullframeMusicGiveReqList: {},
      },
      // 设备列表
      devOptions: [],
      // 全部列表
      allDev: [],
      // 资源平台
      platformOptions: [{
        value: 10,
        label: '爱听音乐'
      }],
      // 栏目
      sceneOptions: [],
      // 有效时长
      timeOptions: [
        { value: -1, label: '1天' },
        { value: 3, label: '1个月' },
        { value: 10, label: '2个月' },
        { value: 4, label: '3个月' },
        { value: 11, label: '4个月' },
        { value: 12, label: '5个月' },
        { value: 5, label: '6个月' },
        { value: 13, label: '7个月' },
        { value: 14, label: '8个月' },
        { value: 15, label: '9个月' },
        { value: 16, label: '10个月' },
        { value: 17, label: '11个月' },
        { value: 6, label: '12个月' },
      ],
      // 校验
      rules: {
        name: [{
          required: true, message: '请输入配置名称', trigger: 'blur'
        }],
        deviceIds: [{
          required: true, message: '请选择关联设备', trigger: 'blur'
        }],
        platformId: [{
          required: true, message: '请选择资源平台', trigger: 'blur'
        }],
        groupIdList: [{
          required: true, message: '请选择栏目', trigger: 'blur'
        }],
        indate: [{
          required: true, message: '请选择有效时长', trigger: 'blur'
        }]
      }
    }
  },
  mounted() {
    this.getDev(1)
    this.getList()
  },
  methods: {
    // 列表
    getList() {
      this.loading = false
      list(this.form).then(res => {
        if (res.code === 0) {
          this.tableData = res.data.records
          this.total = res.data.total
          this.loading = false
        }
      })
    },

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

    // 重置
    getRefresh() {
      this.form = {
        bullframeMusicGiveReqList: {}
      }
      this.getList()
    },

    // 弹窗
    getDialog(title, id) {
      this.dialogVisible = true
      this.title = title
      this.getDev(2)
      this.getScene()
      if (id) {
        detail(id).then(res => {
          this.dialogForm = res.data
          this.dialogForm.deviceIds = res.data.deviceIds.split(',')
          res.data.existDeviceList.map(i => {
            this.devOptions.unshift({
              value: i.clientTypeId,
              label: i.name
            })
          })
        })
      }
    },

    // 关闭弹窗
    close() {
      this.dialogVisible = false
      this.dialogForm = {
        bullframeMusicGiveReqList: {}
      }
    },

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

    // 设备列表
    getDev(type) {
      this.devOptions = []
      devList(type).then(res => {
        if (res.code === 0) {
          res.data.map(i => {
            let obj = {
              value: i.clientTypeId,
              label: i.name
            }
            type === 1 ? this.allDev.push(obj) : this.devOptions.push(obj)
          })
        }
      })
    },

    // 栏目接口
    getScene() {
      sceneList().then(res => {
        if (res.code === 0) {
          this.sceneOptions = res.data
        }
      })
    },

    // 提交
    getSubmit() {
      this.$refs.dialogForm.validate((valid) => {
        if (valid) {
          this.dialogForm.deviceIds = this.dialogForm.deviceIds.join(',')
          submit(this.dialogForm).then(res => {
            if (res.code === 0) {
              this.$message.success(`${this.title}成功！`)
              this.close()
              this.getList()
            }
          })
        } else {
          return false
        }
      })
    },

    // 字典翻译
    statusFormatter(row) {
      return this.selectDictLabel(this.disabledOptions, row.status)
    }
  }
}
</script>

<style lang="scss" scoped>
.el-form-item .el-form-item {
  margin-bottom: 22px;
}

.el-form-item .el-form-item:last-child {
  margin: 0;
}
</style>