<style>
  .examForm .q-table__bottom {
    min-height: 10px;
  }

  .examForm .isOpen_Type {
    color: var(--q-color-primary);
  }

  .examForm .q-breadcrumbs__el {
    cursor: pointer;
  }
</style>
<template>
  <div class="page-content examForm">
    <q-table :pagination="pageMsg" :loading="loading" selection="multiple" :selected.sync="multipleSelection"
      no-data-label="暂无相关数据" flat class="sticky-column-table sticky-right-column-table" separator="none"
      :data="dataList" :columns="columns" row-key="PaperId">
      <template v-slot:top>
        <template v-if="(navList&&navList.length>0)||(multipleSelection&&multipleSelection.length>0)">
          <div style="height:38px;display:flex;align-items:center;">
            <div v-if="navList&&navList.length>0" style="margin-right:20px;">
              <q-breadcrumbs>
                <q-breadcrumbs-el label="全部" @click="getPaperChild(0)" />
                <q-breadcrumbs-el :label="item.PaperName" :class="{'lastExam':index==navList.length-1}"
                  @click="getPaperChild(item.PaperId)" v-for="(item,index) in navList" :key="index" />
              </q-breadcrumbs>
            </div>
            <div>
              <q-btn v-if="multipleSelection&&multipleSelection.length>0" color="accent" size="sm" class="q-mr-md"
                @click="movePaperFile()" label="移动到" />
              <q-btn v-if="multipleSelection&&multipleSelection.length>0" color="accent" size="sm" class="q-mr-md"
                label="删除" @click="batchDelete()" />
            </div>
          </div>
        </template>
        <template v-if="(navList&&navList.length)==0&&(multipleSelection&&multipleSelection==0)">
          <div class="col-2 q-table__title">试卷列表</div>
        </template>
      </template>
      <template v-slot:header-selection="scope">
        <q-checkbox v-model="scope.selected" />
      </template>
      <template v-slot:body-selection="scope">
        <q-checkbox v-model="scope.selected" v-if="scope.row.CreateBy==currentUserInfo.Id" />
      </template>
      <template v-slot:body-cell-PaperName="props">
        <q-td :props="props">
          <div class="Exam_PaperName">
            <i v-if="props.row.PaperType==1" title="文件夹" class="iconfont icon-weibiaoti5" style="color:#85CDFD;"></i>
            <i v-if="props.row.PaperType==2" title="试卷" class="iconfont icon-icon-test1" style="color:#85CDFD;"></i>
            <div class="Exam_PaperName">
              <template v-if="props.row.PaperType==1">
                <span style="cursor:pointer;" @click="getPaperChild(props.row.PaperId)">
                  {{ props.row.PaperName }}</span>
              </template>
              <template v-else>
                {{ props.row.PaperName }}
                <q-tooltip v-if="props.row.PaperName.length>10" :offset="[10, 10]">{{ props.row.PaperName }}
                </q-tooltip>
              </template>
            </div>
          </div>
        </q-td>
      </template>
      <template v-slot:body-cell-QuestionCount="props">
        <q-td :props="props">
          <template v-if="props.row.PaperType==1">
            <span style="color:gray">-</span>
          </template>
          <template v-if="props.row.PaperType==2">
            {{ props.row.QuestionCount }}
          </template>
        </q-td>
      </template>
      <template v-slot:body-cell-DifficultyTypeName="props">
        <q-td :props="props">
          <template v-if="props.row.PaperType==1">
            <span style="color:gray">-</span>
          </template>
          <template v-if="props.row.PaperType==2">
            {{ props.row.DifficultyTypeName }}
          </template>
        </q-td>
      </template>
      <template v-slot:body-cell-PublishCount="props">
        <q-td :props="props">
          <template v-if="props.row.PaperType==1">
            <span style="color:gray">-</span>
          </template>
          <template v-if="props.row.PaperType==2">
            {{ props.row.PublishCount }}
          </template>
        </q-td>
      </template>
      <template v-slot:body-cell-OpenStr="props">
        <q-td :props="props">
          <template v-if="props.row.PaperType==1">
            <span style="color:gray">-</span>
          </template>
          <template v-if="props.row.PaperType==2">
            <span :class="{'isOpen_Type':props.row.IsOpen==2}">{{ props.row.OpenStr }}</span>
          </template>
        </q-td>
      </template>
      <template v-slot:body-cell-ExamineStatusStr="props">
        <q-td :props="props">
          <template v-if="props.row.ExamineStatus==4">
            <el-popover placement="bottom" title="驳回原因" width="350" trigger="hover" :content='props.row.ExamineRemark'>
              <span slot="reference">{{props.row.ExamineStatusStr}}</span>
            </el-popover>
          </template>
          <template v-else>
            {{props.row.ExamineStatusStr}}
          </template>
        </q-td>
      </template>
      <template v-slot:body-cell-PaperType="props">
        <q-td :props="props">
          <template v-if="props.row.PaperType==1">
            <q-btn flat size="xs" color="primary" style="font-weight:400" @click="showExamFolder(props.row)" label="重命名"
              v-if="currentUserInfo.Id==props.row.CreateBy" />
          </template>
          <template v-if="props.row.PaperType==2">
            <q-btn flat size="xs" color="primary" icon="edit" style="font-weight:400" v-if="isEdit(props.row)"
              @click="goExameEdit(props.row)" label="编辑" />
            <q-btn flat size="xs" color="primary" icon="iconfont icon-View" style="font-weight:400"
              @click="showPaperInfo(props.row)" label="详情" />
          </template>
          <q-btn-dropdown flat size="xs" color="dark" label="更多" style="margin-left: 10px">
            <q-list>
              <q-item clickable v-close-popup @click="submitAudit(props.row)"
                v-if="props.row.PaperType==2&&currentUserInfo.Id==props.row.CreateBy &&(props.row.ExamineStatus==0||props.row.ExamineStatus==3||props.row.ExamineStatus==4) ">
                <q-item-section>
                  <q-item-label>{{props.row.ExamineStatus==0?'提交审核':"重新提交审核"}}</q-item-label>
                </q-item-section>
              </q-item>
              <q-item clickable v-close-popup @click="publishExam(props.row)"
                v-if="props.row.PaperType==2 &&currentUserInfo.Id==props.row.CreateBy&&props.row.ExamineStatus==2">
                <q-item-section>
                  <q-item-label>发布考试</q-item-label>
                </q-item-section>
              </q-item>
              <q-item clickable v-close-popup @click="movePaperInfo(props.row)"
                v-if="currentUserInfo.Id==props.row.CreateBy">
                <q-item-section>
                  <q-item-label>移动到</q-item-label>
                </q-item-section>
              </q-item>
              <q-item clickable v-close-popup @click="showOpenForm(props.row)"
                v-if="currentUserInfo.Id==props.row.CreateBy||IsPaperManager">
                <q-item-section>
                  <q-item-label>设置访问权限</q-item-label>
                </q-item-section>
              </q-item>
              <q-item clickable v-close-popup @click="copyPaperInfo(props.row)" v-if="props.row.PaperType==2">
                <q-item-section>
                  <q-item-label>复制</q-item-label>
                </q-item-section>
              </q-item>
              <q-item clickable v-close-popup @click="deletePaperInfo(props.row)"
                v-if="currentUserInfo.Id==props.row.CreateBy">
                <q-item-section>
                  <q-item-label>删除</q-item-label>
                </q-item-section>
              </q-item>
            </q-list>
          </q-btn-dropdown>
        </q-td>
      </template>
      <template v-slot:bottom>
      </template>
    </q-table>
    <paperfolderForm v-if="isShowExamFolder" :saveObj="examObj" :FaParentId="msg.ParentId"  @close="closeExamForm"
       @success="refreshPage">
    </paperfolderForm>
    <movefolderForm v-if="isShowMoveFolder" :saveObj="examObj" :Ids="MoveIds"  @close="closeExamForm"
       @success="refreshPage">
    </movefolderForm>
    <setopenForm v-if="isShowSetOpenFolder" :saveObj="examObj"  @close="closeExamForm"  @success="refreshPage">
    </setopenForm>
  </div>
</template>
<script>
  import paperfolderForm from '../../components/exam/paperfolder-form'
  import movefolderForm from '../../components/exam/movefolder-form'
  import setopenForm from '../../components/exam/setopen-form'
  import {
    deletePaperInfo,
    copyPaperInfo,
    submitApproval
  } from '../../api/teacher/index';

  export default {
    components: {
      paperfolderForm,
      movefolderForm,
      setopenForm
    },
    meta: {
      title: "试卷库管理"
    },
    props: {
      dataList: {
        type: Array,
        default: null
      },
      loading: {
        type: Boolean,
        default: null
      },
      msg: {
        type: Object,
        default: null
      },
      navList: {
        type: Array,
        default: null
      }
    },
    data() {
      return {
        isShowExamFolder: false, //是否显示新增文件夹
        isShowMoveFolder: false, //是否显示
        isShowSetOpenFolder: false, //是否显示设置权限
        examObj: {},
        multipleSelection: [],
        MoveIds: '',
        currentUserInfo: {}, //当前登录用户
        IsPaperManager: false,
        pageMsg: {
          rowsPerPage: 10,
        },
        columns: [{
          name: "PaperName",
          label: "文件夹/试卷",
          field: "PaperName",
          align: "left"
        }, {
          name: "QuestionCount",
          label: "题量",
          field: "QuestionCount",
          align: "left"
        }, {
          name: "DifficultyTypeName",
          label: "难度",
          field: "DifficultyTypeName",
          align: "left"
        }, {
          name: "PublishCount",
          label: "发布次数",
          field: "PublishCount",
          align: "left"
        }, {
          name: "OpenStr",
          label: "开放状态",
          field: "OpenStr",
          align: "left"
        }, {
          name: "CreateByName",
          label: "创建人",
          field: "CreateByName",
          align: "left"
        }, {
          name: "CreateTimeStr",
          label: "创建时间",
          field: "CreateTimeStr",
          align: "left"
        }, {
          name: "ReviewerName",
          label: "审核人",
          field: "ReviewerName",
          align: "left"
        }, {
          name: "ExamineStatusStr",
          label: "状态",
          field: "ExamineStatusStr",
          align: "left"
        }, {
          name: "PaperType",
          label: "操作",
          field: "PaperType",
          align: "left"
        }]
      }
    },
    created() {
      let userinfo = this.getLocalStorage();
      this.currentUserInfo = userinfo;
      userinfo.ActionMenuList.map(x => { //判断权限
        if (x.FunctionCode == "Paper_Manager") { //判断是否是试卷管理员
          this.IsPaperManager = true;
        }
      })
    },
    mounted() {},
    watch: {
      'multipleSelection': {
        handler(val) {
          this.multipleSelection = val;
          var myArray = [];
          if (this.multipleSelection.length > 0) {
            this.multipleSelection.forEach(x => {
              if (x.CreateBy == this.currentUserInfo.Id) {
                myArray.push(x.PaperId);
              }
            })
            this.MoveIds = myArray.toString();
          }
        }
      }
    },
    methods: {
      //是否可修改
      isEdit(item) {
        if (this.currentUserInfo.Id == 1) {
          return true;
        }
        return this.currentUserInfo.Id == item.CreateBy &&
          (item.ExamineStatus == 0 || item.ExamineStatus == 3 || item.ExamineStatus == 4)
      },
      //提交审核，重新提交审核
      submitAudit(item) {
        submitApproval({
          PaperId: item.PaperId
        }).then(res => {
          if (res.Code == 1) {
            this.$q.notify({
              icon: 'iconfont icon-chenggong',
              color: 'accent',
              timeout: 2000,
              message: '操作成功!',
              position: 'top'
            })
            this.refreshPage();
          }
        })
      },
      //移动文件
      movePaperFile() {
        if (this.multipleSelection.length == 0) {
          this.$q.notify({
            type: 'negative',
            position: "top",
            message: `请选择文件夹`
          })
          return
        } else {
          this.isShowMoveFolder = true;
        }
      },
      rowsClick(row, column, event) {
        this.$refs.examPaperTab.toggleRowExpansion(row);
      },
      getPaperChild(Id) {
        this.msg.ParentId = Id;
        this.refreshPage();
      },
      refreshPage() {
        this.$emit('success');
        this.multipleSelection = [];
      },
      //点击编辑 跳转
      goExameEdit(item) {
        var obj = {
          Id: 0,
          ToParentId: this.msg.ParentId
        }
        if (item) {
          obj.Id = item.PaperId;
          obj.ToParentId = item.ParentId;
        }
        this.OpenNewUrl('/exam/paperEdit', obj)
      },
      //新增修改文件夹
      showExamFolder(obj) {
        this.examObj = obj;
        this.isShowExamFolder = true;
      },
      //实在开放权限
      showOpenForm(obj) {
        this.examObj = obj;
        this.isShowSetOpenFolder = true;
      },
      //关闭弹窗
      closeExamForm() {
        this.isShowExamFolder = false;
        this.isShowMoveFolder = false;
        this.isShowSetOpenFolder = false;
      },
      //发布考试
      publishExam(item) {
        let PaperId = 0;
        if (item) {
          PaperId = item.PaperId
        }
        this.OpenNewUrl('/exam/paperPublish', {
          PaperId: PaperId
        })
      },
      //试卷详情
      showPaperInfo(item) {
        let PaperId = 0;
        if (item) {
          PaperId = item.PaperId
        }
        this.OpenNewUrl('/exam/paperInfo', {
          Id: PaperId
        })
      },
      //移动到文件夹
      movePaperInfo(item) {
        this.examObj = item;
        this.isShowMoveFolder = true;
      },
      //复制试卷
      copyPaperInfo(item) {
        var msg = {
          PaperId: item.PaperId
        }
        var message = "是否要复制【" + item.PaperName + "】试卷?";
        this.$q.dialog({
          title: '提示信息',
          message: message,
          cancel: true,
          persistent: true,
          ok: "确定",
          cancel: "取消",
        }).onOk(() => {
          copyPaperInfo(msg).then(res => {
            if (res.Code == 1) {
              this.$q.notify({
                icon: 'iconfont icon-chenggong',
                color: 'accent',
                timeout: 2000,
                message: '操作成功!',
                position: 'top'
              })
              this.refreshPage();
            }
          });
        })
      },
      //批量删除
      batchDelete() {
        var msg = {
          PaperIds: this.MoveIds
        }
        var message = "是否要删除选中的文件信息";
        this.deletePaperCommon(msg, message);
      },
      //删除
      deletePaperInfo(item) {
        var msg = {
          PaperId: item.PaperId
        }
        var message = "";
        if (item.PaperType == 1) {
          message = "是否要删除【" + item.PaperName + "】及下面的内容?";
        } else {
          message = "是否要删除【" + item.PaperName + "】试卷?"
        }
        this.deletePaperCommon(msg, message);
      },
      deletePaperCommon(msg, tipMsg) {
        this.$q.dialog({
          title: '提示信息',
          message: tipMsg,
          cancel: true,
          persistent: true,
          ok: "确定",
          cancel: "取消",
        }).onOk(() => {
          deletePaperInfo(msg).then(res => {
            if (res.Code == 1) {
              this.$q.notify({
                icon: 'iconfont icon-chenggong',
                color: 'accent',
                timeout: 2000,
                message: '操作成功!',
                position: 'top'
              })
              this.refreshPage();
            }
          });
        })
      }
    }
  }
</script>
<style lang="sass">
  @import url('~assets/css/table.sass')
</style>