<template>
  <div class='app-container'>
    <el-button type="primary" icon="el-icon-plus" size="mini" @click="getDialog()"
      v-hasPermi="['operation:channels:add']">新增多频多台</el-button>
    <el-table :data="tableData" v-loading="loading">
      <el-table-column label="频道名称" align="center" prop="name" />
      <el-table-column label="设备" align="center">
        <template slot-scope="scope">
          <span v-for="(item, index) in scope.row.deviceList" :key="item.clientTypeId">
            <span>{{ item.name }}</span>
            <span v-show="index + 1 !== scope.row.deviceList.length">,</span>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="getDialog(scope.row, true)">查看</el-button>
          <el-button type="text" @click="getDialog(scope.row)" v-hasPermi="['operation:channels:edit']">编辑</el-button>
          <el-button type="delete" @click="getRemove(scope.row)"
            v-hasPermi="['operation:channels:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 弹窗 -->
    <el-dialog :visible.sync="dialogVisible" :title="title" width="1000px" :before-close="getClose">
      <el-form :model="dialogForm" ref="dialogForm" :rules="rules" label-width="auto">
        <el-form-item label="频道名称：" prop="name" style="width: 300px">
          <el-input v-model="dialogForm.name" placeholder="请输入规则名称" :disabled="readOnly" />
        </el-form-item>

        <el-form-item label="关联设备：" prop="deviceIds">
          <el-select v-model="dialogForm.deviceIds" filterable multiple placeholder="请选择关联设备" :disabled="readOnly">
            <el-option v-for="item in devOptions" :key="item.clientTypeId" :value="item.clientTypeId"
              :label="item.name" />
          </el-select>
        </el-form-item>

        <el-form-item v-if="dialogForm.id" label="个人频道分类：">
          <div class="classList" v-if="readOnly">
            <el-button v-for="item in dialogForm.personalChannelList" :key="item.channelTypeId"
              :type="active === item.sort ? 'primary' : ''" size="small" @click="getActive(item)">
              {{ item.channelTypeName }}
            </el-button>
          </div>
          <div class="classList" v-else>
            <!-- isFix：0 不可以编辑删除 -->
            <el-button v-for="item in dialogForm.personalChannelList.filter(i => i.isFix === 0)"
              :key="item.channelTypeId" size="small" @click="getActive(item)">
              {{ item.channelTypeName }}
            </el-button>
            <!-- isFix:1 可以编辑删除 -->
            <el-dropdown v-for="item in dialogForm.personalChannelList.filter(i => i.isFix === 1)"
              :key="item.channelTypeId">
              <el-button :type="active === item.sort ? 'primary' : ''" size="small" @click="getActive(item)">
                {{ item.channelTypeName }}<i class="el-icon-arrow-down" style="margin-left: 10px;" />
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item>
                  <div @click="getInputDialog(item)">编辑</div>
                </el-dropdown-item>
                <el-dropdown-item>
                  <div @click="getDelete(item)">删除</div>
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
            <el-button v-if="dialogForm.personalChannelList.length < 9" icon="el-icon-plus" size="small"
              @click="inputVisible = true, type = 0">新增</el-button>
          </div>
        </el-form-item>

        <el-form-item v-if="dialogForm.id" label="其他频道分类：">
          <div class="classList" v-if="readOnly">
            <el-button v-for="item in dialogForm.otherChannelList" :key="item.channelTypeId"
              :type="active === item.sort ? 'primary' : ''" size="small" @click="getActive(item)">
              {{ item.channelTypeName }}
            </el-button>
          </div>
          <div class="classList" v-else>
            <el-dropdown v-for="item in dialogForm.otherChannelList" :key="item.channelTypeId">
              <el-button :type="active === item.sort ? 'primary' : ''" size="small" @click="getActive(item)">
                {{ item.channelTypeName }}<i class="el-icon-arrow-down" style="margin-left: 10px;" />
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item>
                  <div @click="getInputDialog(item)">编辑</div>
                </el-dropdown-item>
                <el-dropdown-item>
                  <div @click="getDelete(item)">删除</div>
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
            <el-button icon="el-icon-plus" size="small" @click="inputVisible = true, type = 1">新增</el-button>
          </div>
        </el-form-item>
      </el-form>

      <el-table v-show="dialogForm.id" :data="dialogForm.defaultList">
        <el-table-column label="频道号" align="center" prop="channelId" width="75px" />
        <el-table-column label="频道名称" align="center" prop="aliasName" show-overflow-tooltip />
        <el-table-column label="频道封面" align="center" width="100px">
          <template slot-scope="scope">
            <el-image :src="scope.row.pic" />
          </template>
        </el-table-column>
        <el-table-column label="频道简介" align="center" prop="description" show-overflow-tooltip />
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="getRouter(scope, true)">查看</el-button>
            <el-button type="text" @click="getRouter(scope)" :disabled="readOnly">编辑</el-button>
            <el-button v-if="scope.row.sort > 3" type="text" icon="el-icon-caret-top"
              @click="getChange(scope.row, scope.row.sort - 1)" :disabled="readOnly" />
            <el-button v-if="scope.row.sort > 2 && scope.row.sort < 12" type="text" icon="el-icon-caret-bottom"
              @click="getChange(scope.row, scope.row.sort + 1)" :disabled="readOnly" />
          </template>
        </el-table-column>
      </el-table>
      <div v-if="!readOnly" slot="footer">
        <el-button @click="getClose">取消</el-button>
        <el-button type="primary" @click="getSubmit">确定</el-button>
      </div>
    </el-dialog>

    <el-dialog :visible.sync="inputVisible" title="分类名称" width="500px" :before-close="getInputClose">
      <el-input v-model="classForm.channelTypeName" placeholder="请输入分类名称" />
      <div slot="footer">
        <el-button @click="getInputClose">取消</el-button>
        <el-button type="primary" @click="getInputSubmit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list, dev, submit, edit, detail, change, remove, channels, classSubmit, classEdit, classRemove } from "@/api/operation/channels"
export default {
  data() {
    return {
      // 遮罩层
      loading: false,
      // 列表
      tableData: [],
      // 全部设备列表
      allDevOptions: [],
      // 剔除已选的设备列表
      devOptions: [],
      // 弹窗
      dialogVisible: false,
      // 查看
      readOnly: false,
      // 弹窗名称
      title: '新增',
      // 弹窗表单
      dialogForm: {
        deviceIds: []
      },
      row: {},
      // 分类焦点
      active: 1,
      // 分类弹窗
      inputVisible: false,
      // 分类表单
      classForm: {},
      // 分类类型
      type: null,
      // 校验
      rules: {
        name: [{
          required: true, message: '请输入规则名称', trigger: 'blur'
        }],
        deviceIds: [{
          required: true, message: '请选择关联设备', trigger: 'change'
        }]
      }
    }
  },
  async mounted() {
    await this.getDev(1)
    this.getList()
  },
  methods: {
    // 列表
    getList() {
      this.loading = true
      list().then(res => {
        if (res.code === 0) {
          this.tableData = res.data
          this.loading = false
        }
      })
    },

    // 设备列表
    async getDev(type) {
      await dev(type).then(res => {
        if (type === 0) {
          this.devOptions = res.data
        } else {
          this.allDevOptions = res.data
        }
      })
    },

    // 删除频道规则
    getRemove(row) {
      this.$confirm(`是否删除${row.name}?`, '提示').then(() => {
        remove(row.id).then(res => {
          if (res.code === 0) {
            this.$message.success('删除成功！')
            this.getList()
          }
        })
      })
    },

    // 弹窗
    async getDialog(row, boolean) {
      this.dialogVisible = true
      this.readOnly = boolean
      await this.getDev(0)
      if (row !== undefined) {
        this.row = row
        this.devOptions.unshift.apply(this.devOptions, this.row.deviceList)
        this.getDetail()
      }
    },

    // 详情
    getDetail() {
      detail(this.row.id).then(res => {
        if (res.code === 0) {
          this.active = 1
          this.dialogForm = res.data
          this.dialogForm.deviceIds = res.data.deviceIds.split(',')
        }
      })
    },

    // 获取焦点
    getActive(item) {
      this.active = item.sort
      this.getChannels(item.channelTypeId)
    },

    // 12频道
    getChannels(id) {
      channels(id).then(res => {
        if (res.code === 0) {
          this.dialogForm.defaultList = res.data
        }
      })
    },

    // 打开分类弹窗
    getInputDialog(item) {
      this.inputVisible = true
      this.classForm = JSON.parse(JSON.stringify(item))
    },
    // 关闭分类表单
    getInputClose() {
      this.inputVisible = false
      this.classForm = {}
    },
    // 提交分类表单
    getInputSubmit() {
      if (this.classForm.name === "") {
        this.$message.error('请输入频道分类名称')
      } else {
        if (this.type === null) {
          classEdit(this.classForm.channelTypeName, this.classForm.channelTypeId).then(res => {
            if (res.code === 0) {
              this.inputVisible = false
              this.$message.success('编辑成功！')
              this.getDetail()
            }
          })
        } else {
          classSubmit(this.classForm.channelTypeName, this.type, this.dialogForm.id).then(res => {
            if (res.code === 0) {
              this.inputVisible = false
              this.$message.success('新增成功！')
              this.getDetail()
            }
          })
        }
      }
    },

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

        } else {
          return false
        }
      })
    },

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

    // 删除
    getDelete(item) {
      this.$confirm(`是否删除${item.channelTypeName}?`, '提示').then(() => {
        classRemove(item.channelTypeId).then(res => {
          if (res.code === 0) {
            this.$message.success('删除成功！')
            this.getDetail()
          }
        })
      }).catch(() => { })
    },

    // 12频道详情
    getRouter(e, boolean) {
      this.$router.push({
        path: `/operation/channels/detail`,
        query: {
          channelId: e.row.id,
          audioType: e.row.channelType,
          index: e.$index,
          boolean: boolean
        }
      })
    },

    // 排序
    getChange(row, sort) {
      change({
        id: row.id,
        sort: sort
      }).then(res => {
        if (res.code === 0) {
          this.$message.success('修改成功！')
          this.getChannels(row.templateCategoryId)
        }
      })
    },

  }
}
</script>

<style lang="scss" scoped>
.classList {
  ::v-deep .el-button {
    margin: 0 10px 10px 0;
  }
}
</style>