<template>
  <div class="app-container">
    <el-button type="primary" icon="el-icon-plus" size="mini" @click="getDialog()"
      v-hasPermi="['music:platform:add']">新增</el-button>
    <!-- 列表 -->
    <el-table :data="tableData" v-loading="loading">
      <el-table-column type="index" label="序号" align="center" />
      <el-table-column label="资源平台" prop="name" align="center" />
      <el-table-column label="音频类型" prop="audioType" align="center" :formatter="audioFormatter" />
      <el-table-column label="对接方式" prop="joinType" align="center" :formatter="joinTypeFormatter" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="getDialog(scope.row.id)" v-hasPermi="['music:platform:edit']">编辑
          </el-button>
          <el-button type="delete" @click="getDelete(scope.row)" v-hasPermi="['music:platform: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" />

    <!-- 弹窗 -->
    <el-dialog :visible.sync="dialogVisible" :title="title" width="500px" :before-close="cancel">
      <el-form :model="dialogForm" :rules="rules" ref="dialogForm" label-width="100px">
        <el-form-item label="资源平台：" prop="name">
          <el-input v-model="dialogForm.name" placeholder="请输入资源平台名称" />
        </el-form-item>
        <el-form-item label="音频类型：" prop="audioType">
          <el-select v-model="dialogForm.audioType" multiple placeholder="请选择音频类型">
            <el-option v-for="item in audioOptions" :key="item.value" :value="item.value.toString()"
              :label="item.label" />
          </el-select>
        </el-form-item>
        <el-form-item label="对接方式：" prop="joinType">
          <el-select v-model="dialogForm.joinType" multiple placeholder="请选择资源对接方式">
            <el-option v-for="item in joinTypeOptions" :key="item.value" :value="item.value.toString()"
              :label="item.label" :disabled="item.disabled" />
          </el-select>
        </el-form-item>
        <el-form-item style="text-align: right;">
          <el-button @click="cancel">取消</el-button>
          <el-button type="primary" @click="getSubmit">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { list, submit, detail, remove } from '@/api/music/platform'
import { audioMixin } from '@/mixin/index'
export default {
  mixins: [audioMixin],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 列表表单
      form: {
        pageNum: 1,
        pageSize: 10
      },
      // 总数
      total: 0,
      // 列表
      tableData: [],
      // 弹窗
      dialogVisible: false,
      title: '',
      // 提交表单
      dialogForm: {
        name: '',
        audioType: [],
        joinType: []
      },
      // 校验
      rules: {
        name: [{
          required: true, message: '请输入资源平台名称', trigger: 'blur'
        }, {
          max: 20, message: '名称不得超过20个字符', trigger: 'blur'
        }],
        audioType: [{
          required: true, message: '请选择音频类型', trigger: 'change'
        }],
        joinType: [{
          required: true, message: '请选择资源对接方式', trigger: 'change'
        }]
      },
      // 对接方式
      joinTypeOptions: [{
        value: 1,
        label: 'API接口'
      }, {
        value: 2,
        label: '本地管理'
      }, {
        value: 3,
        label: '小程序'
      }]
    }
  },
  watch: {
    'dialogForm.joinType'(val) {
      if (val) {
        this.joinTypeOptions[1].disabled = val.findIndex(i => i === '1') !== -1 ? true : false
        this.joinTypeOptions[0].disabled = val.findIndex(i => i === '2') !== -1 ? true : false
      }
    }
  },
  mounted() {
    this.getList()
  },
  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
        }
      })
    },
    // 弹窗
    getDialog(id) {
      this.dialogVisible = true
      this.title = id ? '编辑' : '新增'
      if (id) {
        detail(id).then(res => {
          if (res.code === 0) {
            this.dialogForm = res.data
            this.dialogForm.audioType = res.data.audioType.split(',')
            this.dialogForm.joinType = res.data.joinType.split(',')
          }
        })
      }
    },

    // 取消
    cancel() {
      this.dialogVisible = false
      this.dialogForm = {}
      this.$refs.dialogForm.resetFields();
    },

    // 确定
    getSubmit() {
      this.$refs.dialogForm.validate((valid) => {
        if (valid) {
          this.dialogForm.audioType = this.dialogForm.audioType.join(',')
          this.dialogForm.joinType = this.dialogForm.joinType.join(',')
          submit(this.dialogForm).then(res => {
            if (res.code === 0) {
              this.$message.success(`${this.title}成功！`)
              this.dialogVisible = false
              this.getList()
            }
          }).catch(() => {
            this.dialogForm.id ? this.dialogVisible = false : this.$refs.dialogForm.resetFields()
          })
        } else {
          return false
        }
      })
    },

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

    // 字典翻译
    audioFormatter(row) {
      return row.audioType ? row.audioType.split(',').map(i => this.selectDictLabel(this.audioOptions, i)).join(',') : '/'
    },
    joinTypeFormatter(row) {
      return row.joinType ? row.joinType.split(',').map(i => this.selectDictLabel(this.joinTypeOptions, i)).join(',') : '/'
    }
  }
};
</script>