<template>
  <div>
    <el-dialog
      :visible.sync="isShow"
      :close-on-click-modal="false"
      :modal-append-to-body="false"
      title="关联报告"
      @close="cancelHandle"
      custom-class="connetive-report-dialog"
      center
      width="896px"
    >
      <div class="dialog-container">
        <div class="search-box">
          <!-- <el-select placeholder="请选择报告类型" v-model="searchType" clearable  @change="handleChange">
            <el-option v-for="item in reportTypeList" :key="item.Id"
              :value="item.Id" :label="item.ClassifyName"
            ></el-option>
          </el-select> -->
          <el-cascader placeholder="请选择报告类型"
            v-model="searchType"
            :options="reportTypeList"
            :props="{ expandTrigger: 'hover',
                      children:'Child',
                      label:'ClassifyName',
                      value:'Id'}"
            @change="handleChange"></el-cascader>
          <el-input placeholder="标题关键字搜索" prefix-icon="el-icon-search" v-model="searchTitle" @input="handleInput"/>
        </div>
        <div class="table-box">
          <el-table :data="reportList" border style="margin-top:20px">
            <el-table-column align="center" label="报告标题">
              <template slot-scope="{row}">
                <div style="text-align:left">
                    <el-radio v-model="ReportId" :label="row.ReportId">
                        <span style="white-space: normal;">{{ row.Title }}</span>
                    </el-radio>
                </div>
              </template>
            </el-table-column>
            <el-table-column align="center" label="发布时间">
              <template slot-scope="scope">
                  {{scope.row.PublishTime}}
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <div slot="footer" class="foot-container">
        <el-button type="primary" @click="saveHandle">保 存</el-button>
        <el-button @click="cancelHandle">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {videoInterface} from '@/api/api.js'
export default {
  props:{
    isShow:{
      type:Boolean,
      default:false
    },
    reportId:{
      type:Number,
      default:0
    }
  },
  data() {
    return {
      ReportId:0,
      searchTitle:'',
      searchType:'',
      reportTypeList:[],
      reportList:[]
    };
  },
  watch:{
    isShow(val){
      if(val){
        this.ReportId=this.reportId
        this.searchTitle=''
        this.searchType=''
        this.getTableData()
        this.getClassify()
      }
    }
  },
  methods: {
    cancelHandle(){
      this.$emit('cancel')
    },
    getTableData(){
      videoInterface.getReportList({
        ClassifyIdSecond:this.searchType[this.searchType.length-1],
        Keyword:this.searchTitle
      }).then(res=>{
        if(res.Ret!==200) return 
        this.reportList = res.Data
      })
    },
    getClassify(){
      videoInterface.getReportType().then(res=>{
        if(res.Ret!==200) return 
          this.reportTypeList = res.Data||[]
      })
    },
    handleChange(){
      this.getTableData()
    },
    handleInput(){this.getTableData()},
    saveHandle(){
      //检查该报告是否已关联视频
      const item = this.reportList.find(i=>i.ReportId==this.ReportId)
      if(!item) return 
      if(item.BindVideo){
        this.$message.error(`该报告已关联${item.BindVideoTitle}，请重新选择`)
        return 
      }
      this.$emit('reportChange',item)
    }
  },
};
</script>

<style lang="scss">
.connetive-report-dialog{
  .dialog-container{
    .search-box{
      .el-cascader .el-input{
        width:100%;
      }
    }
  }
}
</style>
<style lang="scss" scoped>
.connetive-report-dialog{
  .dialog-container{
    .search-box{
      display: flex;
      justify-content: space-between;
      .el-input{
        width:400px;
      }
    }
  }
}
</style>
