<!-- 多频多台 详情 -->
<template>
  <div class="app-container">
    <!-- 表单 -->
    <el-form class="form" label-width="100px" :disabled="isRead">
      <el-row>
        <el-col :span="12">
          <el-form-item label="频道名称：">
            <el-input v-model="form.aliasName" placeholder="请输入频道名称" />
          </el-form-item>
          <el-form-item label="频道属性：">
            <el-select
              v-model="form.channelType"
              placeholder="请选择频道属性"
              @change="handleChange"
            >
              <el-option
                v-for="item in channelTypeOptions"
                :key="item.value"
                :value="Number(item.value)"
                :label="item.label"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="频道封面：">
            <Upload
              listType="picture-card"
              :url="form.pic"
              @upload="getUpload"
              :disabled="isRead"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="频道简介：">
            <el-input
              v-model="form.description"
              type="textarea"
              rows="12"
              placeholder="请输入频道简介"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="频道内容：">
        <el-button type="primary" icon="el-icon-plus" @click="getDialog">
          新增内容
        </el-button>
        <el-table :data="form.list" v-loading="loading">
          <el-table-column
            label="音频ID"
            prop="audioId"
            align="center"
            show-overflow-tooltip
          />
          <el-table-column
            label="音频名称"
            prop="audioName"
            align="center"
            show-overflow-tooltip
          />
          <el-table-column label="音频封面" align="center" width="100px">
            <template slot-scope="scope">
              <el-image :src="scope.row.audioPic" />
            </template>
          </el-table-column>
          <el-table-column
            label="音频作者"
            align="center"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <span>
                {{ scope.row.singerName ? scope.row.singerName : "-" }}
              </span>
            </template>
          </el-table-column>
          <el-table-column
            label="专辑名称"
            prop="songName"
            align="center"
            show-overflow-tooltip
          >
            <template slot-scope="scope">
              <span>
                {{ scope.row.songName ? scope.row.songName : "-" }}
              </span>
            </template>
          </el-table-column>
          <el-table-column
            label="付费类型"
            prop="isFree"
            align="center"
            :formatter="freeFormatter"
          />
          <el-table-column
            label="资源平台"
            align="center"
            :formatter="platfromFormatter"
          />
          <el-table-column
            label="当前状态"
            align="center"
            :formatter="statusFormatter"
          />

          <!-- ///排序 -->

          <el-table-column label="序号" align="center">
            <template slot-scope="scope">
              <div v-if="editData.currentEditIndex !== scope.$index">
                <span style="margin-right: 8px">
                  {{ scope.row.indexNum }}
                  <!-- {{ scope.$index + 1 }} -->
                  <!-- {{ scope.row.sort || "" }} -->
                </span>
                <svg-icon
                  v-if="!isRead"
                  icon-class="edit"
                  @click.native="handleEditClick(scope.row, scope.$index)"
                />
              </div>
              <!-- 
              <el-input
                v-else
                v-model="scope.row.indexNum"
                type="number"
                rows="12"
                placeholder="序号"
                @blur="onNumberBlur(scope.row, scope.$index)"
                @keyup.enter.native="onNumberBlurEnter(scope.row, scope.$index)"
              /> -->

              <el-input
                v-else
                v-model="scope.row.indexNum"
                size="mini"
                style="width: 60px"
                type="number"
                @blur="onNumberBlur(scope.row, scope.$index)"
                @keyup.enter.native="onNumberBlurEnter(scope.row, scope.$index)"
                placeholder="序号"
                ref="numberInput"
              />
            </template>
          </el-table-column>

          <!-- ///排序 -->

          <el-table-column label="操作" align="center">
            <template slot-scope="scope">
              <el-button type="delete" @click="getDelete(scope.row)"
                >删除</el-button
              >

              <el-button
                type="text"
                icon="el-icon-caret-top"
                @click="getChangeSort(scope.$index, scope.$index - 1)"
                :disabled="scope.$index <= 0"
              />

              <el-button
                type="text"
                icon="el-icon-caret-bottom"
                @click="getChangeSort(scope.$index, scope.$index + 1)"
                :disabled="scope.$index > form.list.length - 2"
              />
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
    </el-form>
    <!-- 按钮 -->
    <div class="form-btn">
      <el-button @click="cancel">取消</el-button>
      <el-button v-if="!isRead" type="primary" @click="submit">提交</el-button>
    </div>
    <!-- 弹窗 -->
    <Dialog
      :visible="dialogVisible"
      :data="form.list"
      :channelType="form.channelType"
      @close="close"
    />
  </div>
</template>

<script>
import { channelDetail, channeledit } from "@/api/operation/channels";

import Dialog from "@/components/Dialog/index.vue";
import { disabledMixin, isFreeMixin, platformMixin } from "@/mixin/index";
export default {
  name: "operationChannelsDetail",
  mixins: [platformMixin, isFreeMixin, disabledMixin],
  components: {
    Dialog,
  },
  data() {
    return {
      editData: {
        currentEditIndex: -1,
        currentEditSort: 1,
      },
      // 遮罩层
      loading: false,
      // 弹窗
      dialogVisible: false,
      // 表单
      form: {},
      channelTypeOptions: [
        { value: 2, label: "广播" },
        { value: 6, label: "节目" },
        { value: 8, label: "专辑" },
      ],
      // 只读
      isRead: Boolean(this.$route.query.boolean),
    };
  },
  mounted() {
    this.getDetail();
    this.getPlatform({});
  },

  methods: {
    // 点击编辑图标显示输入框
    handleEditClick(row, index) {
      if (this.isRead) {
        return;
      }

      this.editData.currentEditIndex = index;
      this.editData.currentEditSort = row.indexNum;
      this.$nextTick(() => {
        this.$refs.numberInput.focus();
      });
    },

    onNumberBlurEnter(row, index) {
      this.editData.currentEditIndex = -1;
    },

    // 输入框失去焦点时隐藏
    onNumberBlur(row, index) {
      var newSort = row.indexNum;

      if (newSort === "") {
        row.indexNum = this.editData.currentEditSort;
        this.$message.warning("序号不能为空");
        return;
      }

      if (newSort <= 0) {
        row.indexNum = this.editData.currentEditSort;
        this.$message.warning("序号必须大于0");
        return;
      }

      if (newSort === this.editData.currentEditSort) {
        this.editData.currentEditIndex = -1;
        return;
      }

      ///删除第index个元素
      this.form.list.splice(index, 1);
      this.form.list.splice(newSort - 1, 0, row);

      for (var i = 0; i < this.form.list.length; i++) {
        this.form.list[i].indexNum = i + 1;
      }
      this.form.list = this.form.list.filter((i) => i);
      this.editData.currentEditIndex = -1;
    },

    // 排序
    getChangeSort(row, sort) {
      const rowBean = this.form.list[row];
      const sortBean = this.form.list[sort];
      this.form.list[row] = sortBean;
      this.form.list[sort] = rowBean;

      for (var i = 0; i < this.form.list.length; i++) {
        this.form.list[i].indexNum = i + 1;
      }

      this.form.list = this.form.list.filter((i) => i);

      // this.form.audioList = [];
      // this.form.list.map((i) => {
      //   this.form.audioList.push({
      //     audioId: i.audioId,
      //     audioType: i.audioType,
      //   });
      // });

      // channeledit(this.form).then((res) => {
      //   if (res.code === 0) {
      //     this.$message.success("修改成功！");
      //     this.getDetail();
      //   }
      // });
    },

    // 频道详情
    getDetail() {
      const channelId = this.$route.query.channelId;
      channelDetail({
        channelId: channelId,
      }).then((res) => {
        if (res.code === 0) {
          this.form = res.data;
          if (this.form.list) {
            for (var i = 0; i < this.form.list.length; i++) {
              this.form.list[i].indexNum = i + 1;
            }
            this.form.list = this.form.list.filter((i) => i);
          }
        }
      });
    },

    // 修改频道属性
    handleChange() {
      this.form.list = this.form.audioList = [];
    },

    // 上传频道封面
    getUpload(e) {
      this.form.pic = e.file;
    },

    // 打开弹窗
    getDialog() {
      this.dialogVisible = true;
    },

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

    // 删除
    getDelete(row) {
      this.form.list = this.form.list.filter((i) => i.audioId !== row.audioId);
    },

    // 提交
    submit() {
      this.form.audioList = [];
      this.form.list.map((i) => {
        this.form.audioList.push({
          audioId: i.audioId,
          audioType: i.audioType,
        });
      });
      channeledit(this.form).then((res) => {
        if (res.code === 0) {
          this.$message.success("修改成功！");
          this.getDetail();
        }
      });
    },

    // 取消
    cancel() {
      this.$tab.closeOpenPage("/operation/operationChannels");
    },

    // 字典翻译
    platfromFormatter(row) {
      return this.selectDictLabel(this.platformTypeOptions, row.platformId);
    },
    freeFormatter(row) {
      return this.selectDictLabel(this.freeOptions, row.isFree);
    },
    statusFormatter(row) {
      return this.selectDictLabel(this.disabledOptions, row.status);
    },
  },
};
</script>

<style lang="scss" scoped>
.el-image {
  width: 80px;
  height: 80px;
}

::v-deep .el-dialog__body {
  height: 610px;
  overflow-y: scroll;
}
</style>