<template>
  <div class="app-container">
    <el-form :model="form" ref="form" :rules="rules" label-width="100px" style="width: 500px"
      :disabled="disabled">
      <el-form-item label="电台名称：" prop="name">
        <el-input v-model="form.name" placeholder="请输入电台名称" show-word-limit />
      </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="直播流：" style="width:1200px" prop="liveBrocastList">
        <el-button type="primary" icon="el-icon-plus" @click="getDialog">添加关联</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="开始时间" 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="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 key="detail" align="center" width="50px" v-if="dialogForm.platformId === 1">
          <template slot-scope="scope">
            <el-button type="text"
              :icon="scope.$index === index ? 'el-icon-arrow-down' : 'el-icon-arrow-right'"
              @click="handleExpand(scope.row, scope.$index)" />
          </template>
        </el-table-column>
        <el-table-column label="电台ID" prop="id" key="id" align="center" />
        <el-table-column label="电台名称" prop="name" key="name" align="center" />
        <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="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-column type="expand" key="expand" width="1">
          <template slot-scope="scope">
            <el-form v-if="scope.row.children.length > 0" class="table-form" inline label-width="140px">
              <div class="table-item" v-for="item in scope.row.children" :key="item.id">
                <el-form-item label="节目名称：" style="width: calc(100% - 565px)">
                  {{ item.title }}
                  <!-- <el-tooltip class="item" effect="dark" :content="item.title" placement="top">
                    <div></div>
                  </el-tooltip> -->
                </el-form-item>
                <el-form-item label="时间段：" style="width: 435px">
                  {{ item.start_time }} - {{ item.end_time }}
                </el-form-item>
                <el-form-item style="width: 100px">
                  <el-button type="text" @click="getChecked(scope.row, item)" :disabled="checked(item)">
                    选择
                  </el-button>
                </el-form-item>
              </div>
            </el-form>
            <el-empty v-else :image-size="100" description="暂无更多直播流"></el-empty>
          </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, liveList, submit } from '@/api/music/choiceness'
import { addressMixin, contentMixin, platformMixin } from '@/mixin/index'
export default {
  mixins: [addressMixin, contentMixin, platformMixin],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 表单
      form: {
        liveBrocastList: []
      },
      // 弹窗
      dialogVisible: false,
      // 弹窗表单
      dialogForm: {
        pageNum: 1,
        pageSize: 10,
        platformId: 1
      },
      total: 0,
      // 列表
      tableData: [],
      // 资源平台
      platformOptions: [{
        value: 1,
        label: '蜻蜓'
      }, {
        value: 4,
        label: '海外电台'
      }],
      // 只读
      disabled: false,
      // 阻止重复点击
      index: null,
      // 校验
      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'
        }]
      }
    }
  },
  watch: {
    'dialogForm.platformId'(val) {
      this.dialogForm.platformId = val
      this.getList()
    }
  },
  mounted() {
    if (this.$route.query.id) {
      this.form.id = this.$route.query.id
      this.disabled = Boolean(this.$route.query.disabled)
      this.getDetail()
    }
    this.getPlatform({})
  },
  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)
      })
    },

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

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

    // 列表
    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()
    },

    // 直播流
    handleExpand(row, index) {
      if (this.index !== index) {
        this.loading = true
        liveList(row.id).then(res => {
          if (res.code === 0) {
            this.tableData[index].children = res.data
            this.tableData.map(i => {
              if (i.id !== row.id) {
                this.$refs.table.toggleRowExpansion(i, false)
              }
            })
            this.$refs.table.toggleRowExpansion(row, true)
            this.index = index
            this.loading = false
          }
        })
      } else {
        this.$refs.table.toggleRowExpansion(row, false)
        this.index = null
      }
    },

    // 选择
    getChecked(row, item) {
      this.form.liveBrocastList.push({
        name: row.platformId === 1 ? item.title : '',
        startTime: row.platformId === 1 ? item.start_time + ':00' : '',
        endTime: row.platformId === 1 ? item.end_time + ':00' : '',
        subbroadcastId: row.id,
        platformId: row.platformId,
        subbroadcastName: row.name
      })
    },

    // 取消
    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.platformTypeOptions, row.platformId)
    },

    // 是否只读
    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>
::v-deep .table-form {
  .table-item:nth-child(even) {
    background-color: #f2f6fc;
  }

  .table-item {
    padding: 10px 10px 10px 30px;
  }

  .table-item:last-child {
    border: none;
  }

  .el-form-item {
    margin-bottom: 0;

    .el-form-item__content {
      width: calc(100% - 140px);

      div {
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
      }
    }
  }
}

.el-date-editor {
  width: 150px;
}
</style>