<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input v-model="KEYWORDS" placeholder="请输入关键字" class="filter-item" style="width: 150px;"/>
      <el-button v-waves class="filter-item" type="primary" icon="el-icon-search" @click="getQuery">
        搜索
      </el-button>
      <el-button v-waves class="filter-item" type="success" icon="el-icon-refresh" @click="goKeyReset">
        重置
      </el-button>
    </div>
    <el-table
      v-loading="listLoading"
      ref="multipleTable"
      :data="varList"
      :row-key="getRowKey"
      :header-cell-style="{
        'font-weight': 'bold',
        'color': '#000'
      }"
      tooltip-effect="dark"
      border
      fit
      highlight-current-row
    >
      <el-table-column
        :reserve-selection="true"
        type="selection"
        width="55"
        align="center"/>
      <el-table-column type="index" label="序号" width="50" align="center" />
      <el-table-column prop="VIDEONAME" label="视频名称" />
      <el-table-column prop="ISSHOW" label="是否首页展示" >
        <template slot-scope="{row}">
          <template v-if="row.ISSHOW==1">是</template>
          <template v-else-if="row.ISSHOW==0">否</template>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="left" width="350">
        <template slot-scope="{row}">
          <el-button type="success" icon="el-icon-caret-right" size="mini" @click="showVideo(row.VIDEOURL)">播放</el-button>
          <el-button v-show="row.ISSHOW==0" icon="el-icon-top" type="primary" size="mini" style="margin-right:10px" @click="zhiding(row.VIDEOMANAGER_ID)">发布</el-button>
          <el-button v-show="edit" type="info" icon="el-icon-edit" size="mini" @click="handleEdit(row.VIDEOMANAGER_ID)">编辑</el-button>
          <el-button v-show="del" type="danger" icon="el-icon-delete" plain @click="handleDelete(row.VIDEOMANAGER_ID)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="page-btn-group">
      <div>
        <el-button type="primary" icon="el-icon-plus" @click="handleAdd">新增</el-button>
        <el-button plain type="info" @click="goBack">返 回</el-button>
        <!--        <el-button type="success" icon="el-icon-caret-right" @click="showAll">播放全部</el-button>-->
        <!--        <el-button v-show="del" type="danger" icon="el-icon-delete" plain @click="batchDel">删除</el-button>-->
      </div>
      <pagination :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    </div>
    <el-dialog :visible.sync="dialogForm" title="视频" width="600px">
      <el-form ref="form" :model="form" :rules="rules" label-width="110px" style="width: 500px;">
        <el-form-item label="视频名称" prop="VIDEONAME">
          <el-input v-model="form.VIDEONAME" placeholder="这里输入视频名称..." />
        </el-form-item>
        <el-form-item label="播放地址" prop="VIDEOURL">
          <el-input v-model="form.VIDEOURL" placeholder="这里输入播放地址..." />
        </el-form-item>
        <!--        <el-form-item label="首页展示" prop="ISSHOW">-->
        <!--          <el-radio-group v-model="form.ISSHOW">-->
        <!--            <el-radio label="1">是</el-radio>-->
        <!--            <el-radio label="0">否</el-radio>-->
        <!--          </el-radio-group>-->
        <!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="back">取 消</el-button>
        <el-button type="primary" @click="upload">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogVideo" title="视频" width="600px">
      <video v-if="VIDEOURL.endsWith('.m3u8')" ref="video" class="full-height full-width" controls/>
      <iframe v-else :src="VIDEOURL" width="100%" height="380" allowfullscreen allow="autoplay; fullscreen;microphone" style="position: relative;border:none"/>
      <div slot="footer" class="dialog-footer">
        <el-button @click="back">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="dialogVideo" title="视频" width="600px">
      <iframe :src="VIDEOURL" width="100%" height="380" allowfullscreen allow="autoplay; fullscreen" style="position: relative;border:none"/>
      <div slot="footer" class="dialog-footer">
        <el-button @click="back">取 消</el-button>
      </div>
    </el-dialog>

    <!--    <div class="ui-height" />-->
    <!--    <div class="ui-foot">-->
    <!--      <el-button plain type="info" @click="goBack">返 回</el-button>-->
    <!--    </div>-->
  </div>
</template>
<script>
import Pagination from '@/components/Pagination' // 通过 el-pagination二次打包
import { requestFN } from '@/utils/request'
import waves from '@/directive/waves' // waves directive
export default {
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      msg: 'add',
      config: config,
      listLoading: true,
      dialogForm: false,
      dialogImageUrl: '',
      dialogVisible: false,
      add: true,
      del: true,
      edit: true,
      listQuery: {
        page: 1,
        limit: 10
      },
      total: 0,
      KEYWORDS: '',
      dates: [],
      varList: [],
      hideUpload: false,
      form: {
        VIDEOMANAGER_ID: '',
        VIDEONAME: '', //
        VIDEOURL: '',
        ISSHOW: ''
      },

      rules: {
        VIDEONAME: [{ required: true, message: '视频名称不能为空', trigger: 'blur' }],
        VIDEOURL: [{ required: true, message: '播放地址不能为空', trigger: 'blur' }],
        ISSHOW: [{ required: true, message: '是否首页显示不能为空', trigger: 'blur' }]
      },
      dialogVideo: false,
      dialogVideoAll: false,
      VIDEOURL: ''
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getRowKey(row) {
      return row.VIDEOMANAGER_ID
    },
    // 搜索
    getQuery() {
      this.$refs.multipleTable.clearSelection()
      this.getList()
    },
    // 保存
    upload() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.listLoading = true
          requestFN(
            '/videomanager/' + this.msg,
            {
              VIDEOMANAGER_ID: this.form.VIDEOMANAGER_ID,
              VIDEONAME: this.form.VIDEONAME,
              VIDEOURL: this.form.VIDEOURL,
              ISSHOW: this.form.ISSHOW,
              CORPINFO_ID: this.form.CORPINFO_ID
            }
          ).then((data) => {
            this.listLoading = false
            this.dialogForm = false
            this.getList()
          }).catch((e) => {
            this.listLoading = false
          })
        } else {
          return false
        }
      })
    },
    zhiding(id) {
      this.$confirm('确定要发布吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        requestFN(
          '/videomanager/editZhiding',
          {
            VIDEOMANAGER_ID: id
          }
        ).then(() => {
          this.$message({
            message: '发布成功',
            type: 'success'
          })
          this.listLoading = false
          this.varList = []
          this.listQuery.page = 1
          this.getList()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },
    // 获取列表
    getList() {
      this.listLoading = true
      requestFN(
        '/videomanager/list?showCount=' + this.listQuery.limit + '&currentPage=' + this.listQuery.page,
        {
          KEYWORDS: this.KEYWORDS,
          CORPINFO_ID: this.$parent.CORPINFO_ID
        }
      ).then((data) => {
        this.listLoading = false
        this.varList = data.varList
        this.total = data.page.totalResult
        this.hasButton()
      }).catch((e) => {
        this.listLoading = false
      })
    },
    // 添加
    handleAdd() {
      this.msg = 'add'
      this.resetForm()
      this.dialogForm = true
    },
    // 修改
    handleEdit(id) {
      this.msg = 'edit'
      this.form.VIDEOMANAGER_ID = id
      this.getData()
      this.CORPINFO_ID = this.$parent.CORPINFO_ID
      console.log(this.CORPINFO_ID)
      this.dialogForm = true
    },
    // 修改
    showVideo(path) {
      if (path.indexOf('ezopen:') > -1) {
        path = 'https://open.ys7.com/jssdk/theme.html?url=' + path + '&id=ysopen&accessToken=' + this.token
      }
      this.VIDEOURL = path
      this.dialogVideo = true
      // m3u8播放
    },
    handleDelete(id) {
      this.$confirm('确定要删除吗?', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.listLoading = true
        console.log(this.VIDEOMANAGER_ID)
        requestFN(
          '/videomanager/delete',
          {
            VIDEOMANAGER_ID: id,
            CORPINFO_ID: this.$parent.CORPINFO_ID
          }
        ).then(() => {
          this.$message({
            message: '删除成功',
            type: 'success'
          })
          this.listLoading = false
          this.varList = []
          this.listQuery.page = 1
          this.getList()
        }).catch((e) => {
          this.listLoading = false
        })
      }).catch(() => {
      })
    },
    showAll() {
      this.dialogVideoAll = true
    },
    // 获取详情
    getData() {
      this.listLoading = true
      requestFN(
        '/videomanager/goEdit',
        {
          VIDEOMANAGER_ID: this.form.VIDEOMANAGER_ID,
          CORPINFO_ID: this.$parent.CORPINFO_ID
        }
      ).then((data) => {
        this.form = data.pd
        this.VIDEOURL = data.pd.VIDEOURL + '&aspect=600x380'
        console.log(this.$parent.CORPINFO_ID)
        console.log(data.pd)
        this.listLoading = false
      }).catch((e) => {
        this.listLoading = false
      })
    },

    goKeyReset() {
      this.KEYWORDS = ''
      this.getList()
    },
    back() {
      this.dialogVideo = false
      this.dialogVideoAll = false
      this.dialogForm = false
      this.listLoading = false
    },
    resetForm() {
      this.form = {
        NAME: '', //
        FFILE: [],
        ISSHOW: '',
        CORPINFO_ID: this.CORPINFO_ID
      }
    },
    goBack() {
      this.$parent.activeName = 'CorpList'
    }
  }

}

</script>
<style>
.hide .el-upload--picture-card {
  display: none;
}
</style>
