<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.doctorName" size="small" class="form-width" placeholder="请输入姓名"></el-input>
          </el-form-item>
          <el-form-item label="资源类型：" class="search-item">
            <el-select v-model="searchForm.status" placeholder="请选择" size="small" class="form-width">
              <el-option v-for="(item, index) in statusList" :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.doctorName" size="small" class="form-width" placeholder="请输入姓名"></el-input>
          </el-form-item>
          <!-- <el-form-item label="创建日期：" class="search-item" >
            <el-input v-model="searchForm.doctorName" size="small" class="form-width" placeholder="请输入姓名"></el-input>
          </el-form-item> -->
          <el-form-item label="创建日期：" class="search-item">
            <el-date-picker
              v-model="searchForm.beginTime"
              type="datetime"
              size="small"
              value-format="yyyy-MM-dd HH:mm:ss"
              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="seq" label="资源名称"></el-table-column>
        <el-table-column align="center" prop="doctorName" label="资源类型" ></el-table-column>
        <el-table-column align="center" prop="doctorName" label="创建人" ></el-table-column>
        <el-table-column align="center" prop="doctorName" label="创建日期" ></el-table-column>
        <el-table-column align="center" prop="authName" 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>
    <PreviewPDF :dialogVisible="isPreviewPDF" :pdfUrl="pdfUrl" @close="closePDF"></PreviewPDF>
    <PreviewProtocol :dialogVisible="isPreviewProtocol" :protocolId="protocolId" @close="closeProtocol"></PreviewProtocol>
    <PreviewEditCourse :dialogVisible="isPreviewEditCourse" @close="closeEditCourse"></PreviewEditCourse>
    <button @click="previewVideoAction">test video</button>
    <button @click="previewPDFAction">test pdf</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 PreviewPDF from "@/components/education/custom-resource/preview-pdf";
  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: {
          doctorName: '',
          status: '',
          beginTime: '',
          endTime: '',
          pageNo: 1,
          pageSize: 15,
        },
        addEditForm: {
          desc: '',
          doctorId: '',
          status: '',
        },
        statusList: [{
          name: '全部状态',
          value: ''
        },{
          name: '待审核',
          value: '1'
        },{
          name: '通过',
          value: '2'
        },{
          name: '拒绝',
          value: '3'
        }],  // 权限列表
        tableData: [],
        nameList: [],
        rules: {
          doctorId: [{ required: true, message: '请选择', trigger: 'blur' }],
          status: [{ required: true, message: '请选择', trigger: 'blur' }],
        },

        isPreviewVideo: false,
        videoUrl: '',

        isPreviewPDF: false,
        pdfUrl: '',

        isPreviewProtocol: false,
        protocolId: 0,

        isPreviewEditCourse: false,
      }
    },

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

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

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

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

      // 打开预览PDF弹框
      previewPDFAction(videoUrl) {
        this.pdfUrl = 'http://10.177.11.169:9000/test.pdf';
        this.isPreviewPDF = true;
      },  

      // 关闭预览PDF弹框
      closePDF() {
        this.isPreviewPDF = false;
      },

      // 打开预览PDF弹框
      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 = {
          doctorName: '',
          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") {
            // console.log('获取用户列表 ', res)
            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>