<template>
  <div>
    <div class="app-container">
      <el-row :gutter="20">
        <!--部门数据-->
        <el-col :span="4" :xs="24">
          <div class="head-container">
            <el-input
              v-model="name"
              placeholder="请输入分类名称"
              clearable
              prefix-icon="Search"
              style="margin-bottom: 20px"
            />
          </div>
          <div class="head-container">
            <el-tree
              :data="typeList"
              :props="{ label: 'name', children: 'children' }"
              :expand-on-click-node="false"
              :filter-node-method="filterNode"
              ref="typeTreeRef"
              node-key="archiveTypeId"
              highlight-current
              default-expand-all
              @node-click="handleNodeClick"
            />
          </div>
        </el-col>

        <!--用户数据-->
        <el-col :span="20" :xs="24">
          <el-form
            :model="queryParams"
            ref="queryRef"
            :inline="true"
            v-show="showSearch"
            label-width="68px"
          >
            <el-form-item label="档案名称" prop="name">
              <el-input
                style="width: 12vw"
                v-model="queryParams.name"
                placeholder="请输入档案名称"
                clearable
                @keyup.enter="handleQuery"
              />
            </el-form-item>
            <!-- <el-form-item label="档案编号" prop="archiveNo">
            <el-input
              style="width: 12vw"
              v-model="queryParams.archiveNo"
              placeholder="请输入档案编号"
              clearable
              @keyup.enter="handleQuery"
            />
          </el-form-item> -->
            <!--        <el-form-item label="归档时间" prop="placeonTime">
          <el-date-picker clearable
                          v-model="queryParams.placeonTime"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择归档时间">
          </el-date-picker>
        </el-form-item>-->

            <!-- <el-form-item label="密级" prop="levels">
            <el-select
            style="width:12vw"
              v-model="queryParams.levels"
              placeholder="请选择密级"
              clearable
            >
              <el-option
                v-for="dict in archive_levels"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item> -->
            <el-form-item>
              <el-button type="primary" icon="Search" @click="handleQuery"
                >搜索</el-button
              >
              <el-button icon="RefreshRight" @click="resetQuery"
                >重置</el-button
              >
            </el-form-item>
          </el-form>

          <el-row :gutter="10" class="mb8">
            <!--     <el-col :span="1.5">
              <el-button
                  type="primary"
                  plain
                  icon="Plus"
                  @click="handleAdd"
                  v-hasPermi="['offcial:archive:add']"
              >新增</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button
                  type="success"
                  plain
                  icon="Edit"
                  :disabled="single"
                  @click="handleUpdate"
                  v-hasPermi="['offcial:archive:edit']"
              >修改</el-button>
            </el-col>
           <el-col :span="1.5">
              <el-button
                  type="danger"
                  plain
                  icon="Delete"
                  :disabled="multiple"
                  @click="handleDelete"
                  v-hasPermi="['offcial:archive:remove']"
              >删除</el-button>
            </el-col>-->
            <!-- <right-toolbar
            v-model:showSearch="showSearch"
            @queryTable="getList"
          ></right-toolbar> -->
          </el-row>
          <el-table
            v-loading="loading"
            :data="archiveList"
            @selection-change="handleSelectionChange"
          >
            <!-- <el-table-column type="selection" width="55" align="center" /> -->
            <el-table-column
              width="100"
              label="序号"
              align="center"
              type="index"
            >
            </el-table-column>
            <el-table-column label="档案名称" align="center" prop="name">
              <template #default="scope">
                <el-link type="primary" @click="openPdfViewer(scope.row)">{{
                  scope.row.name
                }}</el-link>
              </template>
            </el-table-column>
            <!-- <el-table-column label="档案编号" align="center" prop="archiveNo" /> -->

            <!--      <el-table-column label="归档分类" align="center" prop="typeId" />-->
            <!--      <el-table-column label="归档部门" align="center" prop="deptId" />-->
            <!--      <el-table-column label="归档文件id" align="center" prop="fileId" />-->
            <!-- <el-table-column label="密级" align="center" prop="levels">
            <template #default="scope">
              <dict-tag :options="archive_levels" :value="scope.row.levels" />
            </template>
          </el-table-column> -->
            <!--      <el-table-column label="归档分数" align="center" prop="archiveNum" />-->
            <!--      <el-table-column label="打印分数" align="center" prop="printNum" />-->
            <!--      <el-table-column label="" align="center" prop="remark" />-->
            <!--      <el-table-column label="档案归档表单编号 " align="center" prop="archiveFormNo" />-->
            <!--      <el-table-column label="流程id" align="center" prop="workId" />-->
            <el-table-column
              label="归档时间"
              align="center"
              prop="placeonTime"
              width="250"
            >
              <template #default="scope">
                <span>{{
                  parseTime(scope.row.placeonTime, "{y}-{m}-{d} {h}:{i}:{s}")
                }}</span>
              </template>
            </el-table-column>
            <el-table-column
              width="150"
              label="档案推送状态"
              align="center"
              prop="pushSucc"
            >
              <template #default="scope">
                <span
                  :style="{
                    color:
                      scope.row.pushSucc == null
                        ? 'black'
                        : scope.row.pushSucc == 0
                        ? 'red'
                        : 'green',
                  }"
                >
                  {{
                    scope.row.pushSucc == null
                      ? "无需归档"
                      : scope.row.pushSucc == 0
                      ? "推送失败"
                      : "推送成功"
                  }}
                </span>
              </template>
            </el-table-column>

            <el-table-column
              width="150"
              label="操作"
              align="center"
              class-name="small-padding fixed-width"
            >
              <template #default="scope">
                <el-button
                  v-if="scope.row.pushSucc == 0"
                  link
                  type="primary"
                  @click="reFile(scope.row)"
                >
                  <el-icon><FolderRemove /></el-icon>重新推送</el-button
                >
              </template>
            </el-table-column>
          </el-table>

          <pagination
            v-show="total > 0"
            :total="total"
            v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize"
            @pagination="getList"
          />
        </el-col>
      </el-row>
      <!-- 添加或修改归档对话框 -->
      <el-dialog :title="title" v-model="open" width="500px" append-to-body>
        <el-form
          ref="archiveRef"
          :model="form"
          :rules="rules"
          label-width="80px"
        >
          <el-form-item label="档案名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入档案名称" />
          </el-form-item>
          <!-- <el-form-item label="档案编号" prop="archiveNo">
          <el-input v-model="form.archiveNo" placeholder="请输入档案编号" />
        </el-form-item> -->
          <el-form-item label="归档时间" prop="placeonTime">
            <el-date-picker
              clearable
              v-model="form.placeonTime"
              type="date"
              value-format="YYYY-MM-DD"
              placeholder="请选择归档时间"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="归档分类" prop="typeId">
            <el-input v-model="form.typeId" placeholder="请输入归档分类" />
          </el-form-item>
          <el-form-item label="归档部门" prop="deptId">
            <el-input v-model="form.deptId" placeholder="请输入归档部门" />
          </el-form-item>
          <el-form-item label="归档文件id" prop="fileId">
            <el-input v-model="form.fileId" placeholder="请输入归档文件id" />
          </el-form-item>
          <!-- <el-form-item label="密级" prop="levels">
          <el-select v-model="form.levels" placeholder="请选择密级">
            <el-option
              v-for="dict in archive_levels"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item> -->
          <el-form-item label="归档分数" prop="archiveNum">
            <el-input v-model="form.archiveNum" placeholder="请输入归档分数" />
          </el-form-item>
          <el-form-item label="打印分数" prop="printNum">
            <el-input v-model="form.printNum" placeholder="请输入打印分数" />
          </el-form-item>
          <el-form-item label="" prop="remark">
            <el-input v-model="form.remark" placeholder="请输入" />
          </el-form-item>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </template>
      </el-dialog>
      <el-dialog width="85%" :title="titleFw" v-model="ReceivedQiLinDialog">
        <ReceivedQiLin
          ref="QiLinRef"
          :hanldType="hanldType"
          @closeViewLeave="closeViewLeave"
          :ReceivedQiLinData="ReceivedQiLinData"
          :nowShow="nowShow"
        ></ReceivedQiLin>
      </el-dialog>
      <el-dialog
        width="85%"
        title="论文审批"
        v-model="paperApprovalQiLinDialog"
      >
        <paperApprovalQiLin
          ref="paperQiLinRef"
          :hanldType="hanldType"
          @closeViewLeave="closeViewLeave"
          :paperQiLinData="paperQiLinData"
        ></paperApprovalQiLin>
      </el-dialog>
      <el-dialog width="70%" title="信息审核" v-model="InfoDialog">
        <InfoApply
          ref="child"
          :showcolumn="showcolumn"
          :showTable="showTable"
          :trueLeave="trueLeave"
          :WorkId1="WorkId1"
          @change="change"
          :dialogVisible="dialogVisible"
          :before-close="handleClose"
        ></InfoApply>
      </el-dialog>
      <el-dialog width="80%" title="请假审批" v-model="leaveDialog">
        <leaveApplication
          ref="child"
          :showTable="showTable"
          :trueLeave="trueLeave"
          :WorkId="WorkId"
          @change="change"
          :FK_Flow="FK_Flow"
          :FKNode1="FKNode1"
          :dialogVisible="dialogVisible"
          :before-close="handleClose"
        ></leaveApplication>
      </el-dialog>
      <!-- <view-pdf
      ref="importRef"
      @guanbi="handleQuery"
      :key="$route.fullPath"
      :receiverData="receiverData"
    /> -->
      <tripRecord ref="tripRef" @guanbi="handleQuery"> </tripRecord>
      <outTeacher ref="teacherRef" @guanbi="handleQuery"> </outTeacher>
      <carApproval ref="carRef" @guanbi="handleQuery"></carApproval>
      <workApply ref="workRef" @guanbi="handleQuery"></workApply>

      <sw-view-pdf ref="importRef1" @guanbi="handleQuery"> </sw-view-pdf>
      <qtsw-view-pdf ref="importRef" @guanbi="handleQuery" />
      <Supervise
        :showSub="showSub"
        ref="superRef"
        @guanbi="handleQuery"
      ></Supervise>
      <sealApplicational
        ref="sealRef"
        @guanbi="handleQuery"
      ></sealApplicational>
      <contSeal ref="contSealRef" @guanbi="handleQuery"></contSeal>
    </div>
  </div>
</template>

<script setup name="Archive">
import {
  listArchive,
  getArchive,
  delArchive,
  addArchive,
  updateArchive,
  rePush,
} from "@/api/archive/archive";
import {
  listType,
  getType,
  delType,
  addType,
  updateType,
} from "@/api/archive/type";
import { GetUrlbase, kg_uuid } from "../official/hookFunction";
import { getHandleList, currGenerWorkFlowInfo } from "@/api/liucheng/index";
import { getReceiveByWorkId } from "@/api/office/received";
import { getToken, getJToken } from "@/utils/auth";
import Cookies from "js-cookie";
import Supervise from "@/views/liucheng/todowork/formView/newSupervise.vue";
import sealApplicational from "@/views/liucheng/todowork/formView/sealApplicational.vue";
import contSeal from "@/views/liucheng/todowork/formView/contSeal.vue";
import carApproval from "@/views/liucheng/todowork/formView/carApproval.vue";
import workApply from "@/views/liucheng/todowork/formView/workApply.vue";
import InfoApply from "@/views/liucheng/todowork/InfoApply.vue";
import leaveApplication from "@/views/liucheng/leaveApplication1.vue";

import ReceivedQiLin from "@/views/liucheng/todowork/formView/ReceivedQiLin.vue";
import paperApprovalQiLin from "@/views/liucheng/todowork/formView/paperApprovalQiLin.vue";
import QtswViewPdf from "@/views/liucheng/todowork/formView/qtswViewPdf";
import SwViewPdf from "@/views/liucheng/todowork/formView/swViewPdf";
import tripRecord from "@/views/liucheng/todowork/formView/businessTripRecord.vue";
import outTeacher from "@/views/liucheng/todowork/formView/recordOfOutTeaching.vue";
import { POPPER_INJECTION_KEY } from "element-plus";
const { proxy } = getCurrentInstance();
const { archive_levels } = proxy.useDict("archive_levels");

const archiveList = ref([]);
const InfoDialog = ref(false);
const leaveDialog = ref(false);
const trueLeave = ref(false);
const showcolumn = ref(false);
const open = ref(false);
const loading = ref(true);
const showTable = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const WorkId1 = ref(null);
const nowShow = ref(null);
const FKNode1 = ref(null);
const WorkId = ref(null);
const typeList = ref([]);
const titleFw = ref("");
const ReceivedQiLinData = ref([]);
const ReceivedQiLinDialog = ref(false);
const paperQiLinData = ref([]);
const paperApprovalQiLinDialog = ref(false);
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    orderByColumn: "placeonTime",
    isAsc: "desc",
    name: null,
    archiveNo: null,
    placeonTime: null,
    typeId: null,
    deptId: null,
    fileId: null,
    levels: null,
    archiveNum: null,
    printNum: null,
    archiveFormNo: null,
    workId: null,
  },
  typeQueryParams: {
    name: null,
    parentId: null,
    sortNum: null,
  },
  rules: {},
  showSub: false,
});

const { queryParams, typeQueryParams, form, rules } = toRefs(data);
function reFile(row) {
  console.log("row", row);
  proxy.$modal.loading("正在重新推送...");
  rePush(row.workId).then((res) => {
    if (res.code == 200) {
      proxy.$modal.msgSuccess("推送成功");
      proxy.$modal.closeLoading();
    }
  });
}
function closeViewLeave() {
  leaveDialog.value = false;
  InfoDialog.value = false;
  ReceivedQiLinDialog.value = false;
  paperApprovalQiLinDialog.value = false;
  handleQuery();
  WorkId.value = null;
  WorkId1.value = null;
  ReceivedQiLinData.value = null;
  paperQiLinData.value = null;
}
function openPdfViewer(rowData) {
  console.log("rowData", rowData);
  if (rowData.archiveFormNo == "016") {
    proxy.$refs.superRef.showDetail1(rowData);
    showSub.value = true;
  } else {
    currGenerWorkFlowInfo({ workID: rowData.workId, token: getJToken() }).then(
      (resdata) => {
        console.log("resdata", resdata);
        if (resdata.FK_Flow == "012") {
          getReceiveByWorkId(resdata.WorkID).then((response) => {
            nextTick(() => {
              proxy.$refs["importRef"].show(response.data, resdata, false);
            });
          });
        } else if (resdata.FK_Flow == "003") {
          getReceiveByWorkId(resdata.WorkID).then((response) => {
            nextTick(() => {
              proxy.$refs["importRef1"].showdetal2(
                response.data,
                resdata,
                false,
                "no"
              );
            });
          });
        } else if (resdata.FK_Flow == "002") {
          var agent = navigator.userAgent.toLowerCase();
          if (agent.indexOf("windows") > -1) {
            handleOpenAppReceived(
              resdata.WorkID,
              resdata.NodeName,
              resdata.FK_Flow,
              "no"
            );
          } else {
            handleOpenAppReceivedQiLin(resdata, "no");
          }
        } else if (resdata.FK_Flow == "005") {
          var agent = navigator.userAgent.toLowerCase();
          if (agent.indexOf("windows") > -1) {
            handleOpenAppReceived(
              resdata.WorkID,
              resdata.NodeName,
              resdata.FK_Flow
            );
          } else {
            handleOpenAppReceivedQiLin(resdata);
          }
        } else if (resdata.FK_Flow == "008") {
          var agent = navigator.userAgent.toLowerCase();
          if (agent.indexOf("windows") > -1) {
            this.handleOpenAppReceived1(
              resdata.WorkID,
              resdata.NodeName,
              resdata.FK_Flow
            );
          } else {
            handleOpenAppPaperQiLin1(resdata);
          }
        } else if (resdata.FK_Flow == "006") {
          proxy.$refs.tripRef.showDetail1(resdata);
          proxy.$refs.tripRef.closeSendNext();
        } else if (resdata.FK_Flow == "007") {
          proxy.$refs.teacherRef.showDetail1(resdata);
          proxy.$refs.teacherRef.closeSendNext();
        } else if (data.FK_Flow == "009") {
          leaveDialog.value = true;
          showTable.value = true;
          WorkId.value = data.WorkID;
          trueLeave.value = true;
          FKNode1.value = data.FK_Node;
          FK_Flow.value = data.FK_Flow;
          nodeName.value = data.NodeName;
        } else if (data.FK_Flow == "010") {
          InfoDialog.value = true;
          WorkId1.value = data.WorkID;
          showcolumn.value = true;
          FK_Flow.value = data.FK_Flow;
          nodeName.value = data.NodeName;
        } else if (data.FK_Flow == "015") {
          proxy.$refs.carRef.showDetail1(data);
          proxy.$refs.carRef.closeSendNext();
        } else if (data.FK_Flow == "020") {
          proxy.$refs.workRef.showDetail1(data);
          proxy.$refs.workRef.closeSendNext();
        } else if (data.FK_Flow == "016") {
          // proxy.$refs.superRef.showDetail1(data);
          // proxy.$refs.superRef.closeSendNext();
        } else if (data.FK_Flow == "017") {
          proxy.$refs.sealRef.showDetail1(data);
          proxy.$refs.sealRef.closeSendNext();
        } else if (data.FK_Flow == "018") {
          proxy.$refs.contSealRef.showDetail1(data);
          proxy.$refs.contSealRef.closeSendNext();
        }
      }
    );
  }
}
//麒麟（论文）
function handleOpenAppPaperQiLin1(data, nowShow) {
  paperApprovalQiLinDialog.value = true;
  paperQiLinData.value = data;
  nowShow.value = nowShow;
  proxy.$nextTick(() => {
    proxy.$refs.paperQiLinRef.OnReady();
  });
}
//麒麟(发文)
function handleOpenAppReceivedQiLin(data) {
  ReceivedQiLinDialog.value = true;
  if (data.FK_Flow == "002") {
    titleFw.value = "发文";
  } else if (data.FK_Flow == "005") {
    titleFw.value = "其他处室发文";
  }
  ReceivedQiLinData.value = data;
  proxy.$nextTick(() => {
    proxy.$refs.QiLinRef.OnReady();
  });
}
//打开收文弹窗浏览器
function handleOpenAppReceived1(workId, NodeName, FK_Flow, FK_Node) {
  proxy.$modal.loading("正在打开视图，请稍候...");
  let webUrl =
    GetUrlbase() +
    "/todopaperApproval?FK_Flow=" +
    FK_Flow +
    "&FK_Node=" +
    FK_Node +
    "&hanldType=false&workId=" +
    workId +
    "&NodeName=" +
    encodeURIComponent(NodeName) +
    "&fkNode=";
  var uuid = kg_uuid(16, 32);
  var targetuuid = kg_uuid(16, 32);
  let urlString =
    "http://127.0.0.1:9588/StartApp?" +
    "&id=" +
    uuid +
    "&title=" +
    "发表学术论文审批登记" +
    "&targetid=" +
    targetuuid +
    "&showMode=1" +
    "&cookie=Admin-Token=" +
    getToken() +
    ";J-Token=" +
    getJToken() +
    ";jFontIp=" +
    encodeURIComponent(Cookies.get("jFontIp")) +
    "&weburl=" +
    encodeURIComponent(webUrl);
  fetch(urlString, {
    method: "get",
  }).then((res) => {
    proxy.$modal.closeLoading();
  });
}

//打开收文弹窗浏览器
function handleOpenAppReceived(workId, NodeName, FK_Flow, nowShow) {
  proxy.$modal.loading("正在打开视图，请稍候...");
  let webUrl =
    GetUrlbase() +
    "/todoReceived?FK_Flow=" +
    FK_Flow +
    "&nowShow=" +
    nowShow +
    "&hanldType=false&dangan=true&workId=" +
    workId +
    "&NodeName=" +
    encodeURIComponent(NodeName) +
    "&fkNode=";
  var uuid = kg_uuid(16, 32);
  var targetuuid = kg_uuid(16, 32);
  let urlString =
    "http://127.0.0.1:9588/StartApp?" +
    "&id=" +
    uuid +
    "&title=" +
    "发文" +
    "&targetid=" +
    targetuuid +
    "&showMode=1" +
    "&cookie=Admin-Token=" +
    getToken() +
    ";J-Token=" +
    getJToken() +
    ";jFontIp=" +
    encodeURIComponent(Cookies.get("jFontIp")) +
    "&weburl=" +
    encodeURIComponent(webUrl);
  fetch(urlString, {
    method: "get",
  }).then((res) => {
    proxy.$modal.closeLoading();
  });
}

/** 查询归档列表 */
function getList() {
  loading.value = true;
  listArchive(queryParams.value).then((response) => {
    archiveList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

/** 节点单击事件 */
function handleNodeClick(data) {
  queryParams.value.typeId = data.archiveTypeId;
  handleQuery();
}
/** 查询归档类型列表 */
function getTypeList() {
  loading.value = true;
  listType(typeQueryParams.value).then((response) => {
    typeList.value = proxy.handleTree(
      response.data,
      "archiveTypeId",
      "parentId"
    );
    loading.value = false;
  });
}
// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    archiveId: null,
    name: null,
    archiveNo: null,
    placeonTime: null,
    typeId: null,
    deptId: null,
    fileId: null,
    levels: null,
    archiveNum: null,
    printNum: null,
    createTime: null,
    createBy: null,
    remark: null,
    archiveFormNo: null,
    workId: null,
  };
  proxy.resetForm("archiveRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.archiveId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加归档";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _archiveId = row.archiveId || ids.value;
  getArchive(_archiveId).then((response) => {
    form.value = response.data;
    open.value = true;
    title.value = "修改归档";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["archiveRef"].validate((valid) => {
    if (valid) {
      if (form.value.archiveId != null) {
        updateArchive(form.value).then((response) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addArchive(form.value).then((response) => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _archiveIds = row.archiveId || ids.value;
  proxy.$modal
    .confirm('是否确认删除归档编号为"' + _archiveIds + '"的数据项？')
    .then(function () {
      return delArchive(_archiveIds);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download(
    "offcial/archive/export",
    {
      ...queryParams.value,
    },
    `archive_${new Date().getTime()}.xlsx`
  );
}
getTypeList();
getList();
</script>
