<template>
  <div class="review-access-wrap">
    <bread-crumb :curmbFirst="curmbFirst" :curmbSecond="curmbSecond"></bread-crumb>

    <div class="review-access screenSet" id="screenSet">
      <div class="header-title">外部资源管理</div>
      <el-form ref="form" :model="searchForm" label-width="100px" class="search-form-wrap">
        <el-row>
          <el-form-item label="课程名称：" class="search-item" >
            <el-input v-model="searchForm.courseName" size="small" class="form-width" placeholder="请输入课程名称"></el-input>
          </el-form-item>
          <el-form-item label="状态：" class="search-item">
            <el-select v-model="searchForm.checkStatus" placeholder="请选择" size="small" class="form-width">
              <el-option v-for="(item, index) in checkStatusList" :key="index" :label="item.name" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="创建人：" class="search-item" >
            <el-input v-model="searchForm.createdDoctorName" size="small" class="form-width" placeholder="请输入创建人"></el-input>
          </el-form-item>
          <el-form-item label="创建日期：" class="search-item">
            <el-date-picker
              v-model="searchForm.createdTime"
              type="datetime"
              size="small"
              class="form-width"
              value-format="yyyy-MM-dd"
              placeholder="请选择创建日期">
            </el-date-picker>
            <!-- <span style="color: #DCDFE6;">～</span>
            <el-date-picker
              v-model="searchForm.endTime"
              type="datetime"
              size="small"
              value-format="yyyy-MM-dd HH:mm:ss"
              placeholder="请选择结束时间">
            </el-date-picker> -->
          </el-form-item>          
          <el-form-item class="btn-list">
            <el-button @click="goToSearch(1)" type="primary" size="small">查询</el-button>
            <el-button @click="goToReset" size="small">重置</el-button>
          </el-form-item>
        </el-row>
      </el-form>
      <!-- <div>
        <el-button @click="goToAdd" type="primary" size="small" class="new-add">新增</el-button>
      </div> -->
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column align="center" prop="courseName" label="课程名称"></el-table-column>
        <el-table-column align="center" prop="subjectName" label="课程分类" ></el-table-column>
        <el-table-column align="center" prop="createdDoctorName" label="创建人" ></el-table-column>
        <el-table-column align="center" prop="createdHospitalName" label="所属机构" ></el-table-column>
        <el-table-column align="center" prop="createdTime" label="创建日期" ></el-table-column>
        <el-table-column align="center" prop="checkStatus" label="审核状态" ></el-table-column>
        <el-table-column align="center" label="操作">
          <template slot-scope="scope">
            <el-button @click="goToEdit(scope.row)" type="text" size="small"><span style="color: #449284;">查看</span></el-button>
            <!-- <el-button @click="goToDelete(scope.row)" type="text" size="small">删除</el-button> -->
          </template>
        </el-table-column>
        <div slot="empty">
          <div class="table-empty">
            <img src="../../assets/image/no-content1.png">
            <p>没有查询到外部资源</p>
          </div>
        </div>
      </el-table>

      <div class="pagination">
        <el-pagination
          class="pagination-style"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :total="totalRows"
          :current-page="searchForm.pageNo"
          :page-sizes="[15, 30, 50, 100]"
          :page-size="searchForm.pageSize"
          layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
      </div>
    </div>

    <!-- 拒绝原因 -->
    <el-dialog
      title="拒绝原因"
      @close="hideAddEditForm"
      :visible.sync="addEditVisible"
      :close-on-click-modal="false"
      width="600px"
      center>
      <el-form ref="addEditForm" :rules="rules" :model="addEditForm" label-width="120px">
        <el-form-item label="拒绝原因：">
          <el-radio-group v-model="addEditForm.doctorId">
            <div style="padding-top: 12px;">
              <el-radio label="含有政治类、医疗健康、社会事件类不实信息" value="1"></el-radio>
            </div>
            <div style="padding-top: 16px;">
              <el-radio label="含有欺诈，色情，诱导、违法犯罪非法字符" value="2"></el-radio>
            </div>
            <div style="padding-top: 16px;">
              <el-radio label="其他" value="3"></el-radio>
            </div>
          </el-radio-group>
          <el-input type="textarea" v-model="addEditForm.desc" maxlength="25" style="width: 310px;margin-top: 10px;"></el-input>
          <span class="word-num">{{(addEditForm.desc).replace(/\s+/g,"").length}}/25</span>
        </el-form-item>        
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="hideAddEditForm">取 消</el-button>
        <el-button type="primary" @click="submitForm('addEditForm')">确 定</el-button>
      </span>
    </el-dialog>

    
    <!-- 删除 弹窗 -->
    <el-dialog
      title="删除"
      @close="hideDeleteFrom"
      :visible.sync="deleteVisible"
      :close-on-click-modal="false"
      width="600px"
      center>
      <p style="text-align: center;">确定删除该权限？</p>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmDelete">确 定</el-button>
        <el-button @click="hideDeleteFrom">取 消</el-button>
      </span>
    </el-dialog>

    <PreviewVideo :dialogVisible="isPreviewVideo" :videoUrl="videoUrl" @close="closeVideo"></PreviewVideo>
    <PreviewProtocol :dialogVisible="isPreviewProtocol" :protocolId="protocolId" @close="closeProtocol"></PreviewProtocol>
    <PreviewEditCourse :dialogVisible="isPreviewEditCourse" @close="closeEditCourse"></PreviewEditCourse>
    <button @click="previewVideoAction">test video</button>
    <button @click="previewProtocolAction">test protocol</button>
    <button @click="previewEditCourseAction">test course</button>
  </div>
</template>

<script>
  import { openLoading, closeLoading } from "../../utils/utils";
  import BreadCrumb from "../../components/breadcrumb.vue";
  import PreviewVideo from "@/components/education/custom-resource/preview-video";
  import PreviewProtocol from "@/components/education/custom-resource/preview-protocol";
  import PreviewEditCourse from "@/components/education/custom-resource/edit-course-dialog";
  import * as commonUtil from "../../utils/utils";
  import { 
    getAuthTableReq,
    // getUserListReq,
    handleSaveEditReq,
    handleDeleteReq,
  } from '@/utils/cme/cmeApi'

  export default {
    data() {
      return {
        curmbFirst: "CME",
        curmbSecond: "外部资源管理",
        totalRows: 0,
        addEditVisible: false,
        isAdd: true,            // 新增或编辑状态：新增 true; 编辑 false 
        deleteVisible: false,
        currentRow: null,       // 当前行数据
        searchForm: {
          courseName: '',
          checkStatus: '',
          createdDoctorName: '',
          createdTime: '',
          // createdHospitalName: '',
          // subjectName: '',
          pageNo: 1,
          pageSize: 15,
        },
        addEditForm: {
          desc: '',
          doctorId: '',
          status: '',
        },
        checkStatusList: [{
          name: '全部状态',
          value: '-1'
        },{
          name: '待审核',
          value: '0'
        },{
          name: '通过',
          value: '1'
        },{
          name: '拒绝',
          value: '2'
        }],  
        tableData: [],
        nameList: [],
        rules: {
          doctorId: [{ required: true, message: '请选择', trigger: 'blur' }],
          status: [{ required: true, message: '请选择', trigger: 'blur' }],
        },

        isPreviewVideo: false,
        videoUrl: '',

        isPreviewProtocol: false,
        protocolId: 0,

        isPreviewEditCourse: false,
      }
    },

    components: {
      BreadCrumb,
      PreviewVideo,
      PreviewProtocol,
      PreviewEditCourse
    },

    created() {
      this.goToSearch(1);
      // this.getUserList();
    },
    mounted() {
      commonUtil.resizeHeight();
    },
    methods: {

      // 查询列表
      goToSearch() {
        this.GET("contents/course/custom/check/v1/list", this.searchForm).then(res => {
          if (res.code == "000000") {
            // this.protocolInfo = res.data;
          }
        });
      },

      // 打开预览课程弹框
      previewVideoAction(videoUrl) {
        this.videoUrl = 'http://10.177.11.169:9000/1584002682204479.mp4';
        this.isPreviewVideo = true;
      },

      // 关闭预览课程弹框
      closeVideo() {
        this.isPreviewVideo = false;
      },

      // 打开预览协议弹框
      previewProtocolAction() {
        this.protocolId = 5;
        this.isPreviewProtocol = true;
      },

      // 关闭预览协议弹框
      closeProtocol() {
        this.isPreviewProtocol = false;
      },

      // 打开编辑课程弹框
      previewEditCourseAction() {
        this.isPreviewEditCourse = true;
      },

      // 关闭编辑课程弹框
      closeEditCourse() {
        this.isPreviewEditCourse = false;
      },

      // 搜索
      // goToSearch(pageNo) {
      //   if(pageNo){  // 自定义第几页，不传则表示当前页
      //     this.searchForm.pageNo = pageNo;
      //   }
      //   this.getAuthTable();
      // },

      // getAuthTable() {
      //   let req = this.searchForm;
      //   getAuthTableReq(req).then(res => {
      //     if (res.code == "000000") {
      //       // console.log('表格列表 ', res)
      //       this.tableData = res.data.contentList;
      //       this.totalRows = res.data.total;
      //     } else {
      //       this.$message(res.message);
      //     }
      //   }).catch(err => {
      //     this.$message.error('请求失败');
      //   });
      // },
      
      // 重置
      goToReset() {
        this.searchForm = {
          courseName: '',
          status: '',
          pageNo: 1,
          pageSize: 15,
        };
      },
      // 新增
      goToAdd() {
        this.isAdd = true;
        this.addEditVisible = true;
      },
      
      // 编辑
      goToEdit(row) {
        this.isAdd = false;
        this.currentRow = row;
        // console.log('编辑 ', row)
        this.addEditVisible = true;
        // 编辑回显状态
        this.addEditForm = {
          desc: '',
          status: row.status,
          doctorId: row.doctorId,
        };
      },

      // // 获取新增、编辑的用户列表
      // getUserList() {
      //   let req = {};
      //   getUserListReq(req).then(res => {
      //     if (res.code == "000000") {
      //       this.nameList = res.data;
      //     } else {
      //       this.$message(res.message);
      //     }
      //   }).catch(err => {
      //     this.$message.error('请求失败');
      //   });
      // },
      
      goToDelete(row) {
        this.deleteVisible = true;
        this.currentRow = row;
      },

      
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            // alert('submit!');
            this.handleSaveEdit();
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      },
      handleSaveEdit() {
        // 新增、编辑传参不同
        // debugger;
        let req = {};
        if (this.isAdd) {
          req = {
            status: this.addEditForm.status,
            doctorId: this.addEditForm.doctorId,
          }
        } else {
          req = {
            status: this.addEditForm.status,
            doctorId: this.addEditForm.doctorId,
            id: this.currentRow.id,
          };
        }
        handleSaveEditReq(req).then(res => {
          if (res.code == "000000") {
            this.addEditVisible = false;
            // this.goToSearch();
            // console.log('新增/编辑权限 ', res)
          } else {
            this.$message(res.message);
          }
        }).catch(err => {
          this.$message.error('请求失败');
        });
      },
      hideAddEditForm() {
        this.addEditVisible = false;
        this.resetForm('addEditForm');
        this.addEditForm = {
          desc: '',
          doctorId: '',
          status: '',
        };
      },
      confirmDelete() {
        // 调接口
        let req = {
          id: this.currentRow.id,
        };
        handleDeleteReq(req).then(res => {
          if (res.code == "000000") {
            // console.log('删除用户列表 ', res)
            // this.goToSearch();
            this.deleteVisible = false;
          } else {
            this.$message(res.message);
          }
        }).catch(err => {
          this.$message.error('请求失败');
        });
      },
      hideDeleteFrom() {
        this.deleteVisible = false;
      },
      handleSizeChange(val){
        // console.log('handleSizeChange>>>')
        this.searchForm.pageSize = val;
        // this.goToSearch(1);
      },
      
      handleCurrentChange(val){
        // 切换当前页
        this.searchForm.pageNo = val;
        this.getAuthTable();
      },
    },

  }
</script>  

<style lang="scss">
  .review-access-wrap{
    .review-access{
      padding: 10px;
      background: #fff;
      .el-button--small{
        font-size: 14px;
      }
      .header-title {
        padding: 10px 12px 20px;
        font-size: 14px;
        color: #606266;
        border-bottom: 1px solid #efefef;
      }
      .search-form-wrap{
        padding: 25px 0 20px;
        overflow: hidden;
        // border-bottom: 1px dashed #efefef;
      }
      .search-item{
        float: left;
      }
      .btn-list{
        float: right;
        .el-form-item__content{
          margin-left: 0 !important;
        }
      }
      .new-add{
        float: right;
        margin: 10px 0 20px;
      }
      .table-empty {
        img {
          width: 100px;
        }
        p {
          margin-top: -50px;
        }
      }
    }
  }
</style>