<template>
  <!-- 资源库管理 -->
  <div class="zykglManage">
    <div class="searchDiv">
      <el-tabs v-model="activeTab" class="demo-tabs" @tab-change="initData">
        <el-tab-pane v-for="(item, index) in tabData" :key="index" :label="item.label" :name="item.name"></el-tab-pane>
      </el-tabs>
      <el-form ref="ruleFormRef" :inline="true" :model="ruleForm" class="demo-form-inline">
        <el-form-item label="课程名称：" prop="zf_kcmc">
          <el-input size="default" v-model="ruleForm.zf_kcmc" placeholder="" />
        </el-form-item>
        <el-form-item label="创建人：" prop="zf_cjr">
          <el-input size="default" v-model="ruleForm.zf_cjr" placeholder="" />
        </el-form-item>
        <el-form-item label="年级：" prop="grade_id">
          <el-select filterable v-model="ruleForm.grade_id" placeholder="" size="default">
            <el-option v-for="(item, index) in gradeData" :key="index" :label="item.grade_name" :value="item.grade_id" />
          </el-select>
        </el-form-item>
        <el-form-item label="学科：" prop="subject_id">
          <el-select filterable v-model="ruleForm.subject_id" placeholder="" size="default">
            <el-option v-for="(item, index) in subjectData" :key="index" :label="item.subject_name" :value="item.subject_id" />
          </el-select>
        </el-form-item>
        <el-form-item label="创建时间：" prop="create_dateline">
          <el-date-picker size="default" v-model="ruleForm.create_dateline" type="date" value-format="YYYY-MM-DD" />
        </el-form-item>
        <el-form-item label="ID：" prop="zf_id">
          <el-input size="default" v-model="ruleForm.zf_id" placeholder="" />
        </el-form-item>
        <el-form-item label="资源名称：" prop="keyword">
          <el-input size="default" v-model="ruleForm.keyword" placeholder="" />
        </el-form-item>
        <el-form-item>
          <el-button size="default" type="primary" @click="initData">搜索</el-button>
          <el-button size="default" type="warning" @click="resetForm(ruleFormRef)">清空</el-button>
        </el-form-item>
      </el-form>
    </div>

    <div class="searchContent">
      <div class="searchBtn flex">
        <div class="flex">
          <el-button size="default" type="success" @click="handleDownload">导出</el-button>
        </div>
        <div>
          <img src="/images/print.png" alt="" />
        </div>
      </div>
      <div class="tableList">
        <div class="tableDiv">
          <el-table ref="multipleTableRef" :data="tableData" height="100%" style="width: 100%" @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" />
            <el-table-column prop="zf_id" width="" label="ID" show-overflow-tooltip></el-table-column>
            <el-table-column prop="zf_name" width="" label="资源名称" show-overflow-tooltip></el-table-column>
            <el-table-column prop="grade_name" width="" label="年级" show-overflow-tooltip></el-table-column>
            <el-table-column prop="subject_name" width="" label="学科" show-overflow-tooltip></el-table-column>
            <el-table-column prop="zf_cjr" width="" label="创建人" show-overflow-tooltip></el-table-column>
            <el-table-column prop="zf_class_name" width="" label="班级" show-overflow-tooltip></el-table-column>
            <el-table-column prop="zf_kcmc" width="" label="课程名称" show-overflow-tooltip></el-table-column>
            <el-table-column prop="zf_cjsj" width="180" label="创建时间" show-overflow-tooltip></el-table-column>
            <el-table-column prop="zf_size" width="" label="大小" show-overflow-tooltip></el-table-column>
            <el-table-column label="操作" width="80">
              <template #default="{row}">
                <el-button v-if="activeTab==1" text size="default" type="primary" @click="lookDetail(row)">查看</el-button>
                <el-button v-if="activeTab==2" text size="default" type="primary" @click="handleEdit(row.zf_name, row.zf_id)">修改</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="paginationDiv">
          <el-pagination background layout="total, prev, pager, next" :total="total" @current-change="handleCurrentChange" :page-size="limit" :currentPage="currentPage"></el-pagination>
        </div>
      </div>
    </div>

    <el-dialog width="80%" v-model="dialogVisible" title="查看" custom-class="commonDialog" @close="handleClose" :close-on-click-modal="false">
      <preview-attach :commonUrl="commonUrl" :suffix="suffix" style="width: 100%; height: 600px;"></preview-attach>
    </el-dialog>
  </div>
</template>

<script setup >
import { reactive, ref, onMounted } from "vue";
import { download } from "~/utils/request";
import { dealWithDateTime } from "~/utils/common-util.js"
import { filesList, filesDetail, filesEdit, gradeList, subjectList } from "./api.js";

const activeTab = ref("1");
const tabData = [
  {
    name: "1",
    label: "资源库",
  },
  {
    name: "2",
    label: "异常资源",
  },
];

const ruleFormRef = ref(null);
const ruleForm = reactive({
  zf_kcmc: "",
  zf_cjr: "",
  grade_id: "",
  subject_id: "",
  create_dateline: "",
  zf_id: "",
  keyword: "",
});

const resetForm = (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
  initData();
};

initSelectData();
const gradeData = ref([]);
const subjectData = ref([]);
async function initSelectData() {
  let data1 = await gradeList();
  gradeData.value = [{ grade_name: "全部", grade_id: "" }].concat(data1.page_data);

  let data2 = await subjectList();
  subjectData.value = [{ subject_name: "全部", subject_id: "" }].concat(data2.page_data);
}

const tableData = ref([]);
const total = ref(0);
const limit = ref(10);
const currentPage = ref(1);
function handleCurrentChange(val) {
  currentPage.value = val;
  initData();
}

initData();
async function initData() {
  let obj = {
    limit: limit.value,
    page: currentPage.value,
    zf_status: activeTab.value,
    zf_kcmc: ruleForm.zf_kcmc,
    zf_cjr: ruleForm.zf_cjr,
    grade_id: ruleForm.grade_id,
    subject_id: ruleForm.subject_id,
    create_dateline: ruleForm.create_dateline,
    zf_id: ruleForm.zf_id,
    keyword: ruleForm.keyword,
    site: "tyyx"
  };
  let data = await filesList(obj);
  total.value = parseInt(data.total_rows);
  tableData.value = data.page_data;
}

const dialogVisible = ref(false);
const currentId = ref("");
const commonUrl = ref("");
const suffix = ref("");
const lookDetail = (row) => {
  dialogVisible.value = true;
  currentId.value = row.zf_id;
  if (row.zf_path == "" || row.zf_path == null) {
    ElMessage.error("暂无可查看的文件！");
    return
  }
  initDetailInfo(row);
};

const initDetailInfo = (row) => {
  commonUrl.value = row.zf_path;
  suffix.value = commonUrl.value.substr(commonUrl.value.lastIndexOf(".") + 1);
}

const handleEdit = (name, id) => {
  ElMessageBox.prompt('请输入资源名称', '修改', {
    confirmButtonText: '提交',
    cancelButtonText: '取消',
    buttonSize: "default",
    inputValue: name,
    beforeClose: function (action, instance, done) {
      if (action == "confirm" && instance.inputValue == "") {
        ElMessage.error("请输入资源名称！");
      } else {
        done();
      }
    }
  })
    .then(async ({ value }) => {
      let obj = {
        zf_id: id,
        zyk_file: {
          zf_name: value
        }
      }
      let data = await filesEdit(obj);
      if (data) {
        ElMessage.success("提交成功！");
        initData();
      }
    })
    .catch(() => {
      // ElMessage({
      //   type: 'info',
      //   message: 'Input canceled',
      // })
    })
}

const handleClose = () => {
  dialogVisible.value = false;
  currentId.value = "";
  commonUrl.value = "";
  suffix.value = "";
}

const multipleSelection = ref([]);
const handleSelectionChange = (val) => {
  multipleSelection.value = val;
};

const handleDownload = () => {
  let arr = [];
  multipleSelection.value.map(value => {
    arr.push(value.zf_id);
  })
  let obj = {
    limit: limit.value,
    page: currentPage.value,
    zf_status: activeTab.value,
    zf_kcmc: ruleForm.zf_kcmc,
    zf_cjr: ruleForm.zf_cjr,
    grade_id: ruleForm.grade_id,
    subject_id: ruleForm.subject_id,
    create_dateline: ruleForm.create_dateline,
    zf_id: ruleForm.zf_id,
    keyword: ruleForm.keyword,
  };

  download("/zyk/file/index", obj);
};

onMounted(() => {
  setTimeout(() => {
    let searchContent = document.querySelector(".searchContent");
    let offset = document.querySelector(".searchDiv");
    let parentObj = document.querySelector(".zykglManage");
    searchContent.style.height = parentObj.clientHeight - offset.clientHeight + "px";
    window.onresize = function () {
      let searchContent = document.querySelector(".searchContent");
      let parentObj = document.querySelector(".zykglManage");
      searchContent.style.height = parentObj.clientHeight - offset.clientHeight + "px";
    }
  }, 0)
})


</script>

<style lang="scss" scoped>
@import "~/styles/dialog.scss";
.zykglManage {
  height: calc(100vh - 50px - 2rem);
  .flex {
    display: flex;
    align-items: center;
  }
  .searchDiv {
    background: #fff;
    border-radius: 6px;
    // box-shadow: 0 0 5px rgba($color: #000000, $alpha: 0.1);
    // padding: 0 20px;
    // .el-tabs::v-deep {
    //   .el-tabs__header {
    //     margin: 0;
    //   }
    // }
    .el-form-item::v-deep {
      margin-bottom: 0;
      .el-form-item__label {
        padding: 0;
      }
    }
  }
  .searchContent {
    margin-top: 12px;
    height: calc(100% - 62px - 12px);
    background: #fff;
    border-radius: 6px;
    // box-shadow: 0 0 5px rgba($color: #000000, $alpha: 0.1);
    // padding: 0 20px;
    .searchBtn {
      justify-content: space-between;
      padding: 10px 0;
      border-bottom: 1px solid #e3e3e3;
      ::v-deep .el-button {
        height: 32px;
      }
    }
    .tableList {
      height: calc(100% - 52px - 10px);
      position: relative;
      .tableDiv {
        // background: #ddd;
        height: calc(100% - 42px);
        .el-button {
          padding: 0;
        }
        .split {
          color: #00a3ff;
          margin: 0 10px;
        }
      }
      .paginationDiv {
        position: absolute;
        bottom: -5px;
        padding: 3px;
        width: 100%;
        display: -webkit-box;
        -webkit-box-pack: end;
      }
    }
  }
}
</style>