<template>
  <div class="mainContentBox">
    <avue-crud
      ref="crudRef"
      v-model:search="search"
      v-model="form"
      :data="data"
      :option="option"
      v-model:page="page"
      @row-save="createRow"
      @row-update="updateRow"
      @row-del="deleteRow"
      @search-change="searchChange"
      @search-reset="resetChange"
      @size-change="dataList"
      @current-change="dataList"
      @selection-change="selectionChange"
    >
      <template #menu="{ row, index, type }">
        <el-button type="primary" size="small" @click="showDetail(row)"
          >查看</el-button
        >
        <el-button type="primary" size="small" @click="exportDataZip(row)"
        >导出</el-button
        >
      </template>
      <template #menu-right="{}">
        <el-button
          class="ml-3"
          @click="exportData('/api/v1/quality/report/export')"
        >
          <template #icon> <i-ep-download /> </template>导出
        </el-button>
      </template>
    </avue-crud>
<!--    <reportTemplate v-model="showStatus" :detail="detail" />-->
    <ToPrintDialog ref="toPrintRef"></ToPrintDialog>
  </div>
</template>

<script setup>
import { ref, getCurrentInstance } from "vue";
import { useCrud } from "@/hooks/userCrud";
import reportTemplate from "@/components/ReportView/reportTemplate.vue";
import ToPrintDialog from "./toPrintTables.vue";
import {exportDataInfo} from "@/api/report";
// 传入一个url，后面不带/
const { form, data, option, search, page, toDeleteIds, Methords, Utils } =
  useCrud({
    src: "/api/v1/quality/report",
  });
const { dataList, createRow, updateRow, deleteRow, searchChange, resetChange } =
  Methords; //增删改查
const { selectionChange, multipleDelete } = Methords; //选中和批量删除事件
const { checkBtnPerm, downloadTemplate, exportData } = Utils; //按钮权限等工具
const tablesData = ref([]);
const crudRef = ref(null); //crudRef.value 获取avue-crud对象

onMounted(() => {
  // console.log("crudRef", crudRef)
  dataList();
});
const showStatus = ref(false);
const importExcelData = () => {
  if (uploadRef.value) {
    uploadRef.value.show("/api/v1/plan/order/import");
  }
};



const toPrintRef = ref(null);

const detail = ref({});
const showDetail = (row) => {
  // showStatus.value = true;
  // detail.value = row;
  toPrintRef.value && toPrintRef.value.openPrintDialog(row);
};

const exportDataZip = async (row) => {

    search.value=row;
    exportData("/api/v1/quality/report/zip");

};

const downFile = (response) => {
  const fileData = response.data;
  const fileName = decodeURI(
    response.headers["content-disposition"].split(";")[1].split("=")[1]
  );
  const fileType =
    "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8";
  const blob = new Blob([fileData], { type: fileType });
  const downloadUrl = window.URL.createObjectURL(blob);
  const downloadLink = document.createElement("a");
  downloadLink.href = downloadUrl;
  downloadLink.download = fileName;
  document.body.appendChild(downloadLink);
  downloadLink.click();
  document.body.removeChild(downloadLink);
  window.URL.revokeObjectURL(downloadUrl);
};

// 设置表格列或者其他自定义的option
option.value = Object.assign(option.value, {
  searchEnter: true,
  viewBtn: false,
  column: [
    {
      label: "报告编号",
      prop: "reportCode",
      search: true,
    },
    {
      label: "报告名称",
      prop: "reportName",
      search: true,
    },
    {
      label: "报告类型",
      prop: "reportType",
      search: true,
      type: "select", //类型为单选框
      dicData: [
        {
          label: "日报",
          value: "1",
        },
        {
          label: "月报",
          value: "2",
        },
      ],
      value: "1",
      control: (val, form) => {
        if (val === "1") {
          return {
            generationDate: {
              type: "date",
              format: "YYYY年MM月DD日",
              valueFormat: "YYYY-MM-DD",
            },
          };
        } else {
          return {
            generationDate: {
              type: "month",
              format: "YYYY年MM月",
              valueFormat: "YYYY-MM",
            },
          };
        }
      },
    },
    {
      label: "日期",
      prop: "generationDate",
      type: "datetime",
    },
  ],
});
</script>
