<template>
  <div @click="openDrawer">
    <slot></slot>
  </div>
  <a-drawer
      :visible="visible"
      class="drawerStyle TimeManage"
      placement="right"
      width="1000"
  >
    <div class="drawerMain">
      <div class="header">
        <div class="headerTitle">选择试卷</div>
        <img
            style="width: 29px; height: 29px; cursor: pointer"
            src="../../assets/images/basicinfo/close.png"
            @click="closeDrawer"
        />
      </div>
      <div class="main">
        <div class="search">
          <div class="sealeft">
            <div class="namecon" style="margin-right: 30px">
              <div class="name">试卷名称：</div>
              <a-input
                  v-model:value="params.keyWord"
                  style="width: 270px; height: 40px; border-radius: 8px"
                  placeholder="请输入试卷名称"
              />
            </div>
          </div>
          <div class="btns">
            <div
                class="btn btn1"
                style="margin-right: 20px"
                @click="search"
            >
              <div class="img1"></div>
              <div class="wz">搜索</div>
            </div>
            <div class="btn btn2" @click="reset">
              <div class="img2"></div>
              <div class="wz">重置</div>
            </div>
          </div>
        </div>
        <div class="btnss" style="margin-top: 20px">
          <div class="btn btn1" @click="createPage" style="margin-right: 20px">
            <div class="wz">新建试卷</div>
          </div>
        </div>
        <div class="tab" style="margin-top: 20px; margin-bottom: 100px">
          <a-table
              :customRow="customRow"
              class="ant-table-striped"
              :row-class-name="(_, index) => (index % 2 === 1 ? 'table-striped' : null)"
              row-key="id"
              :columns="columns"
              :data-source="data"
              :loading="loading"
              :pagination="pagination"
              :row-selection="rowSelection"
          />
        </div>
      </div>
      <div class="btnn">
        <button class="btn1" @click="closeDrawer">取消</button>
        <button class="btn2" @click="confirm">确定</button>
      </div>
    </div>
  </a-drawer>

  <!-- 新建考试 -->
  <a-modal
      v-model:visible="createVisible"
      :footer="null"
      :closable="false"
      wrapClassName="modalStyle addExamModal"
      :zIndex="9999"
  >
    <iframe
        id="iframe"
        style="width: 100%; height: 100%"
        :src="iframeUrl + '/exam/papers?addnew=true'"
        name="myframe"
        sandbox="allow-forms allow-scripts allow-same-origin allow-popups"
    ></iframe>
  </a-modal>
</template>

<script setup>
import {computed, defineEmits, defineProps, ref} from "vue";
import {iframeUrl} from "@/api/method";
import {useBoeApiPage} from "@/api/request";
import {EXAM_PAPER_PAGE} from "@/api/ThirdApi";
import {message} from "ant-design-vue";

defineProps({
  id: String,
  name: String
})
const visible = ref(false)
const createVisible = ref(false)
const emit = defineEmits({})

const columns = ref([
  {
    title: "试卷名称",
    dataIndex: "testName",
    key: "testName",
    width: "40%",
    align: "center",
    className: "h head",
    ellipsis: true,
  },
  {
    title: "随机模式",
    dataIndex: "paperMode",
    key: "paperMode",
    width: "20%",
    align: "center",
    className: "h",
    ellipsis: true,
  },
  {
    title: "创建人",
    dataIndex: "sysCreateBy",
    key: "sysCreateBy",
    width: "20%",
    align: "center",
    className: "h",
    ellipsis: true,
  },
])
const initParams = {
  keyWord: '',
  pageIndex: 1,
  pageSize: 10,
  orderAsc: true
}
const params = ref(initParams)

const rowSelectKeys = ref([]);
const selectsData = ref([]);

const {data, loading, total, fetch} = useBoeApiPage(EXAM_PAPER_PAGE, params.value, {
  init: false,
  result: res => res.result.list,
  totalPage: res => res.result.totalPages,
  total: res => res.result.count
})

const customRow = (record) => ({
  onClick: () => {
    rowSelectKeys.value = [record.id]
    selectsData.value = [record]
  }
})

const pagination = computed(() => ({
  total: total.value,
  showSizeChanger: false,
  current: params.value.pageIndex,
  pageSize: params.value.pageSize,
  onChange: changePagination,
}));

const changePagination = (e) => {
  params.value.pageIndex = e;
  fetch()
};
const rowSelection = computed(() => ({
  type: 'radio',
  columnWidth: 20,
  selectedRowKeys: rowSelectKeys.value,
  onChange: onSelectChange,
  preserveSelectedRowKeys: true,
  getCheckboxProps: getCheckboxProps
}));
const getCheckboxProps = () => ({
  // 某几项默认禁止选中(R: 当state等于1时)
  disabled: false
})

function onSelectChange(e, l) {
  rowSelectKeys.value = e;
  selectsData.value = l;
}

function search() {
  params.value.pageIndex = 1
  fetch()
}

function reset() {
  rowSelectKeys.value = [];
  selectsData.value = [];
  params.value.pageIndex = 1
  params.value.keyWord = ''
  fetch()
}

const closeDrawer = () => {
  visible.value = false
  reset()
};

function confirm() {
  if (!selectsData.value.length || !rowSelectKeys.value.length) {
    message.warning("请选择试卷！");
    return
  }
  emit('update:id', selectsData.value[0].id)
  emit('update:name', selectsData.value[0].testName)
  closeDrawer()
}

function openDrawer() {
  visible.value = true
}

function createPage() {
  createVisible.value = true
}

defineExpose({openDrawer})
</script>

<style lang="scss">
.TimeManage {
  .drawerMain {
    min-width: 550px;
    margin: 0px 32px 0px 32px;
    overflow-x: auto;
    display: flex;
    flex-direction: column;

    .header {
      height: 73px;
      border-bottom: 1px solid #e8e8e8;
      display: flex;
      flex-shrink: 0;
      justify-content: space-between;
      align-items: center;
      margin-bottom: 20px;

      .headerTitle {
        font-size: 18px;
        font-weight: 600;
        color: #333333;
        line-height: 25px;
      }
    }

    .main {
      width: 100%;
      height: 100%;
      overflow-y: auto;
      padding-right: 10px;

      .search {
        width: 100%;
        display: flex;
        flex-wrap: wrap;
        margin-top: 20px;


        .sealeft {
          display: flex;
          flex-wrap: wrap;

          .namecon {
            display: flex;
            flex-wrap: nowrap;
            margin-bottom: 10px;

            .name {
              margin-top: 8px;
              white-space: nowrap;
            }
          }
        }

        .btns {
          display: flex;
          flex-wrap: nowrap;

          .btn {
            cursor: pointer;
            width: 100px;
            height: 40px;
            border-radius: 8px;
            display: flex;
            justify-content: center;
            align-items: center;

            .img1 {
              width: 15px;
              height: 17px;
              background-image: url(../../assets/images/courseManage/search0.png);
              background-size: 100% 100%;
              margin-right: 7px;
            }

            .img2 {
              width: 16px;
              height: 18px;
              background-image: url(../../assets/images/courseManage/reset1.png);
              background-size: 100% 100%;
              margin-right: 7px;
            }
          }

          .btn1 {
            background: #4ea6ff;

            color: #ffffff;
          }

          .btn2 {
            background: #ffffff;

            color: #4ea6ff;
            border: 1px solid #4ea6ff;
          }
        }
      }

      .btnss {
        display: flex;
        flex-wrap: nowrap;

        .btn {
          cursor: pointer;
          width: 130px;
          height: 40px;
          border-radius: 8px;
          display: flex;
          justify-content: center;
          align-items: center;
        }

        .btn1 {
          background: #4ea6ff;
          color: #ffffff;
        }
      }

      .tab {
        th,
        th.h {
          background-color: #eff4fc !important;
        }

        .ant-table-tbody
        > tr:hover:not(.ant-table-expanded-row):not(.ant-table-row-selected)
        > td {
          background: #f6f9fd;
        }

        .tableBox {
          .pa {
            margin-top: 15px;
            width: 100%;
            display: flex;
            justify-content: center;

            .ant-pagination-prev,
            .ant-pagination-next,
            .ant-pagination-item,
            .ant-pagination-options {
              margin-bottom: 10px;
            }
          }
        }
      }
    }

    .btnn {
      height: 72px;
      width: 100%;
      position: absolute;
      bottom: 0;
      left: 0;
      background-color: #fff;
      display: flex;
      align-items: center;
      justify-content: center;
      box-shadow: 0px 1px 35px 0px rgba(118, 136, 166, 0.16);

      .btn1 {
        width: 100px;
        height: 40px;
        border: 1px solid #4ea6ff;
        border-radius: 8px;
        color: #4ea6ff;
        background-color: #fff;
        cursor: pointer;
      }

      .btn2 {
        cursor: pointer;
        width: 100px;
        height: 40px;
        background: #4ea6ff;
        border-radius: 8px;
        border: 0;
        margin-left: 15px;
        color: #fff;
      }
    }
  }
}

.addExamModal {
  .ant-modal {
    width: 80% !important;
  }
}
</style>
  