<script setup lang="ts">
import { ref } from "vue";
import { httpUpload } from "/@/api/InvoiceSales/capitalClaim";
import {
  initheaders,
  columns,
  requireHeaders,
  requsetHeaderIdxs
} from "./columns-config";
import { ElMessage, ElNotification } from "element-plus";
import { responseHandle } from "/@/utils/responseHandle";
import { useNav } from "/@/layout/hooks/nav";
import { execlUpload } from "/@/components/execlUpload";
const visible = ref(false);
const loading = ref(false);
const tableData = ref([]);
const columnsConfig = columns();
const { logout } = useNav();
const emit = defineEmits(["onSuccess"]);

const handleClose = () => {
  // execlFile.value = null;
  // uploadRef.value.clearFiles();
};
const Uploadsuccess = ({ results, header }) => {
  loading.value = true;

  if (results.length === 0) {
    ElMessage.error("表格无有效数据！");
    loading.value = false;
    return;
  }

  let headok = true;
  if (header.length !== initheaders.length) {
    headok = false;
  } else {
    initheaders.forEach((si, sii) => {
      if (si !== header[sii]) {
        headok = false;
      }
    });
  }
  if (!headok) {
    ElMessage.error("表头与导入模板不匹配！");
    loading.value = false;
    return;
  }
  tableData.value = [];

  try {
    results.forEach(v1 => {
      const b = Object.values(v1);
      let model = {};
      b.forEach((si, sii) => {
        model["value" + sii] = si + "";
      });
      tableData.value.push(model);
    });
    loading.value = false;
  } catch (e) {
    ElMessage.error("导入数据拼接有误！");
    loading.value = false;
  }
};
//提交
const handleSubmit = async () => {
  if (loading.value) return;
  loading.value = true;
  let isok = true,
    isnum = true,
    data = [];
  tableData.value.forEach(key => {
    let arr = [];
    for (let i in key) {
      if (requsetHeaderIdxs.includes(i) && !key[i]) {
        isok = false;
      }
      arr.push(key[i]);
    }

    data.push(arr);
    return;
  });
  if (!isok) {
    ElNotification({
      title: "必填字段缺失！",
      type: "error"
    });
    loading.value = false;
    return;
  }
  if (!isnum) {
    ElMessage.error("收入金额只能为正数！");
    loading.value = false;
    return;
  }

  const { code, message } = await httpUpload({ data });
  responseHandle({
    code,
    message,
    logout,
    handler: () => {
      loading.value = false;
      ElMessage.success("数据导入成功！");
      emit("onSuccess");
      visible.value = false;
    }
  });
};
const cancel = () => {
  tableData.value = [];
};

defineExpose({
  onDisplay: () => ((visible.value = true), (tableData.value = []))
});
</script>

<template>
  <el-dialog
    :close-on-click-modal="false"
    v-model="visible"
    title="导入客户付款资金数据"
    width="1040px"
    top="8vh"
    center
    @close="handleClose"
  >
    <execlUpload @on-success="Uploadsuccess" v-if="tableData.length === 0" />
    <el-table
      :data="tableData"
      stripe
      border
      max-height="500px"
      size="small"
      style="width: 100%"
    >
      <el-table-column
        v-for="(si, sii) in columnsConfig"
        :type="si.type"
        :minWidth="si.minWidth"
        :fixed="si.fixed"
        :key="sii"
        :prop="si.prop"
        show-overflow-tooltip
      >
        <template #header>
          <span
            v-if="!requireHeaders.includes(si.label) || si.label === '序号'"
            >{{ si.label }}</span
          >
          <p v-else>
            <span style="color: #f56c6c; font-size: 14px">* </span>
            {{ si.label }}
          </p>
        </template>
      </el-table-column>
    </el-table>
    <div
      flex
      justify-end
      gap-2
      v-if="tableData.length !== 0"
      style="padding: 10px 0 0 0"
    >
      <el-button size="small" @click="cancel">取消</el-button>
      <el-button
        size="small"
        type="primary"
        :loading="loading"
        @click="handleSubmit"
        >保存</el-button
      >
    </div>
  </el-dialog>
</template>

<style lang="scss" scoped>
:deep(.el-upload-list__item) {
  display: none !important;
}
</style>
