<template>
  <div class="app-container">
    <el-form class="form" :model="form" ref="form" :rules="rules" label-width="auto" :disabled="disabled">
      <el-form-item label="电台名称：" prop="name">
        <el-input v-model="form.name" placeholder="请输入电台名称" />
      </el-form-item>
      <el-form-item label="电台简介：" prop="description">
        <el-input v-model="form.description" type="textarea" :autosize="{ minRows: 5, maxRows: 10 }" placeholder="请输入电台简介"
          maxlength="300" show-word-limit />
      </el-form-item>
      <el-form-item label="电台封面：" prop="thumb">
        <Upload listType="picture-card" :url="form.thumb" @upload="upload" :disabled="disabled" />
      </el-form-item>
      <el-form-item label="直播流：" prop="liveBrocastList" style="width: 100%;">
        <el-button type="primary" icon="el-icon-plus" @click="getPush">添加</el-button>
        <el-table :data="form.liveBrocastList">
          <el-table-column type="index" label="序号" align="center" />
          <el-table-column label="电台名称" prop="subbroadcastName" align="center" show-overflow-tooltip />
          <el-table-column label="节目名称" align="center">
            <template slot-scope="scope">
              <el-input v-model="scope.row.name" />
            </template>
          </el-table-column>
          <el-table-column label="资源平台" prop="platformId" align="center" :formatter="platformFormatter" />
          <el-table-column label="当前状态" prop="status" align="center" :formatter="statusFormatter" />
          <el-table-column label="开始时间" align="center">
            <template slot-scope="scope">
              <el-time-picker v-model="scope.row.startTime" start-placeholder="开始时间" end-placeholder="结束时间"
                :readonly="readonly(scope.row)" value-format="HH:mm:ss" format="HH:mm:ss" />
            </template>
          </el-table-column>
          <el-table-column label="结束时间" align="center">
            <template slot-scope="scope">
              <el-time-picker v-model="scope.row.endTime" start-placeholder="开始时间" end-placeholder="结束时间"
                :readonly="readonly(scope.row)" value-format="HH:mm:ss" format="HH:mm:ss" />
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="100px">
            <template slot-scope="scope">
              <el-button type="text" @click="getDialog(scope.$index)">关联</el-button>
              <el-button type="delete" @click="getDelete(scope.row, scope.$index)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
    </el-form>
    <div class="form-btn">
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="getSubmit" v-if="!disabled">确定</el-button>
    </div>

    <!-- 弹窗 -->
    <el-dialog :visible.sync="dialogVisible" width="1000px" :before-close="beforeClose">
      <!-- 搜索 -->
      <el-form inline size="mini">
        <el-form-item label="资源平台：">
          <el-select v-model="dialogForm.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="地域分类：">
          <el-select v-model="dialogForm.addressClassifyId" placeholder="请选择地域分类">
            <el-option v-for="item in addressOptions" :key="item.value" :value="item.value" :label="item.label" />
          </el-select>
        </el-form-item>
        <el-form-item label="内容分类：">
          <el-select v-model="dialogForm.contentClassifyId" placeholder="请选择内容分类">
            <el-option v-for="item in contentOptions" :key="item.value" :value="item.value" :label="item.label" />
          </el-select>
        </el-form-item>
        <el-form-item label="电台名称：">
          <el-input v-model="dialogForm.name" placeholder="请输入电台名称" />
        </el-form-item>
        <el-form-item label="电台ID：">
          <el-input v-model="dialogForm.id" placeholder="请输入电台ID" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="getSearch">搜索</el-button>
          <el-button icon="el-icon-refrash" @click="getRefrash">重置</el-button>
        </el-form-item>
      </el-form>
      <!-- 列表 -->
      <el-table :data="tableData" ref="table" v-loading="loading">
        <el-table-column label="电台ID" prop="id" key="id" align="center" show-overflow-tooltip />
        <el-table-column label="电台名称" prop="name" key="name" align="center" show-overflow-tooltip />
        <el-table-column label="电台封面" key="thumb" align="center" width="100px">
          <template slot-scope="scope">
            <el-image v-if="scope.row.thumb" :src="scope.row.thumb" />
          </template>
        </el-table-column>
        <el-table-column label="地域分类" key="address" align="center" :formatter="addressFormatter" />
        <el-table-column label="内容分类" key="content" align="center" :formatter="contentFormatter" />
        <el-table-column label="当前状态" key="status" align="center" :formatter="statusFormatter" />
        <el-table-column label="操作" key="checked" align="center" v-if="dialogForm.platformId === 4">
          <template slot-scope="scope">
            <el-button type="text" @click="getChecked(scope.row)" :disabled="checked(scope.row)">
              选择
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer">
        <pagination v-show="total > 0" :total="total" :page.sync="dialogForm.pageNum" :limit.sync="dialogForm.pageSize"
          @pagination="getList" />
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list, detail, submit } from '@/api/music/choiceness'
import { addressMixin, contentMixin, onOrOffMixin } from '@/mixin/index'
export default {
  mixins: [addressMixin, contentMixin, onOrOffMixin],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 表单
      form: {
        liveBrocastList: []
      },
      // 弹窗
      dialogVisible: false,
      // 弹窗表单
      dialogForm: {
        pageNum: 1,
        pageSize: 10,
        platformId: 4
      },
      total: 0,
      // 列表
      tableData: [],
      // 只读
      disabled: false,
      // 关联索引
      index: null,
      // 资源平台
      platformOptions: [{
        value: 4,
        label: '海外电台'
      }],
      // 校验
      rules: {
        name: [{
          required: true, message: '请输入电台名称', trigger: 'blur'
        }],
        description: [{
          required: true, message: '请输入电台简介', trigger: 'blur'
        }],
        thumb: [{
          required: true, message: '请上传电台封面', trigger: 'change'
        }],
        liveBrocastList: [{
          required: true, message: '请配置电台直播流', trigger: 'change'
        }]
      }
    }
  },
  mounted() {
    if (this.$route.query.id) {
      this.form.id = this.$route.query.id
      this.disabled = Boolean(this.$route.query.disabled)
      this.getDetail()
    }
  },
  methods: {
    // 详情
    getDetail() {
      detail(this.form.id).then(res => {
        if (res.code === 0) {
          this.form = res.data
        }
      })
    },

    // 上传
    upload(e) {
      this.form.thumb = e.file
    },

    // 删除
    getDelete(row, index) {
      this.$confirm(`是否删除${row.name}?`, '提示', {
        type: 'warning'
      }).then(() => {
        this.form.liveBrocastList.splice(index, 1)
      })
    },

    // 新增直播流
    getPush() {
      this.form.liveBrocastList.push({})
    },

    // 弹窗 
    getDialog(index) {
      this.dialogVisible = true
      this.index = index
      this.getList()
    },

    // 关闭弹窗
    beforeClose() {
      this.dialogVisible = false
    },

    // 列表
    getList() {
      this.loading = true
      list(this.dialogForm).then(res => {
        if (res.code === 0) {
          this.tableData = res.data.records
          this.total = res.data.total
          this.loading = false
        }
      })
    },

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

    // 重置
    getRefrash() {
      this.dialogForm = {
        pageNum: 1,
        pageSize: 10,
        platformId: this.dialogForm.platformId
      }
      this.getList()
    },

    // 选择
    getChecked(row) {
      let e = this.form.liveBrocastList[this.index]
      this.$set(e, 'subbroadcastId', row.id)
      this.$set(e, 'subbroadcastName', row.name)
      this.$set(e, 'platformId', row.platformId)
      this.$set(e, 'status', row.status)
    },

    // 取消
    cancel() {
      this.$tab.closeOpenPage('/music/choiceness')
    },

    // 提交
    getSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          submit(this.form).then(res => {
            if (res.code === 0) {
              this.$message.success('提交成功！')
              this.form.id ? this.getDetail() : this.cancel()
            }
          })
        } else {
          return false
        }
      })
    },

    // 字典翻译
    addressFormatter(row) {
      return row.platformId === 1 ? this.selectDictLabel(this.addressOptions, row.addressClassifyId) : '/'
    },
    contentFormatter(row) {
      return row.platformId === 1 ? this.selectDictLabel(this.contentOptions, row.contentClassifyId) : '/'
    },
    platformFormatter(row) {
      return this.selectDictLabel(this.platformOptions, row.platformId)
    },
    // 当前状态
    statusFormatter(row) {
      return this.selectDictLabel(this.onOrOffOptions, row.status)
    },

    // 是否只读
    readonly(row) {
      return row.platformId === 1 ? true : false
    },

    // 是否已选
    checked(row) {
      return this.dialogForm.platformId === 1 ? this.form.liveBrocastList.findIndex(i => i.name === row.title) === -1 ? false : true : this.form.liveBrocastList.findIndex(i => i.subbroadcastId === row.id) === -1 ? false : true
    }
  }
}
</script>

<style lang="scss" scoped>
.form .el-form-item {
  width: 500px;
}
</style>