""" @author: yy @desc：反馈信息 @date: 2025-06-08 09:41:49 """
<template>
  <div>
    <!-- 下发的 -->
    <div v-if="data.type === '01'">
      <template v-if="pageData.detailFeedbackArr?.length">
        <section class="button" v-if="['01', '02'].includes(data.feedBackType)">
          <el-button type="primary" v-loading="exportLoading" @click="export2Excel">导出</el-button>
        </section>
        <MOSTY.Table
          :table-columns="pageData.tableConfig"
          :tableData="pageData.detailFeedbackArr"
          :tableloading="pageData.tableloading"
          :isSelect="false"
          :isSort="true"
          :span-method="arraySpanMethod"
          :showFirstSummary="true"
        >
          <template #actions="{ row }">
            <el-button v-if="row.dataDetails && row.dataDetails.length" type="primary" size="mini" @click="handleDownload(row)">下载</el-button>
          </template>
        </MOSTY.Table>
      </template>
      <el-empty v-else description="暂无数据" />
    </div>
    <!-- 接收的 -->
    <div v-else>
      <template v-if="pageData.detailFeedbackInfo?.feedBackType">
        <el-row style="width: 100%; margin-bottom: 10px">
          <el-col :span="6">反馈人：{{ pageData.detailFeedbackInfo?.userName }}</el-col>
          <el-col :span="6">反馈时间：{{ pageData.detailFeedbackInfo?.time }}</el-col>
          <el-col :span="6">反馈单位：{{ pageData.detailFeedbackInfo?.orgName }}</el-col>
          <el-col :span="6">反馈部门：{{ pageData.detailFeedbackInfo?.deptName }}</el-col>
        </el-row>
        <template v-if="['01', '02'].includes(pageData.detailFeedbackInfo?.feedBackType)">
          <el-row style="width: 100%; margin-left: 10px; margin-bottom: 10px" v-for="(item, i) in pageData.detailFeedbackInfo.dataDetails" :key="i">
            <el-col :span="22">
              <div style="margin-bottom: 10px">{{ item.fieldKey }}</div>
              <div>{{ item.fieldValue }}</div>
            </el-col>
          </el-row>
        </template>
        <template v-if="pageData.detailFeedbackInfo?.feedBackType === '03'">
          <MOSTY.Assort :title="'附件'" />
          <el-form-item label="附件" style="width: 600px">
            <div style="display: flex">
              <MOSTY.Upload
                isAutoWidth
                :disabled="true"
                :limit="500"
                :gsgfList="['pdf', 'ofd', 'doc', 'docx', 'xls', 'xlsx', 'wps']"
                :nopicer="true"
                :isImg="false"
                :ShowfileList="true"
                v-model="pageData.detailFeedbackInfo.dataDetails"
                :showinput="true"
              >
              </MOSTY.Upload>
            </div>
          </el-form-item>
        </template>
      </template>
      <el-empty v-else description="暂无数据" />
    </div>
  </div>
</template>
<script setup>
import { ref, reactive, onMounted } from "vue"
import * as MOSTY from "@/components/MyComponents/index"
import { exportXlsx } from "@/excel/Export2Excel.js"
import { getApi, postApi } from "@/api/tobAcco_api.js"
import { feedBackTableConfig } from "./../models/detail.js"
import { getUrl } from "./../hooks/download.js"

const props = defineProps({
  // 详情接口数据
  data: {
    default: () => ({}),
    type: Object
  }
})

const pageData = reactive({
  // 下发的
  detailFeedbackArr: [],
  // 接收的
  detailFeedbackInfo: {},
  tableConfig: JSON.parse(JSON.stringify(feedBackTableConfig)),
  tableloading: false,
  columnsArr: [],
  columnsArr2: []
})

const getData = async () => {
  try {
    pageData.tableloading = true
    const _url = props.data.type === "01" ? "selectCountFeedBack" : "selectFeedBack"
    const res = await getApi({ taskId: props.data.id }, "/mosty-jcgl/taskController/" + _url)

    if (props.data.type === "01") {
      pageData.detailFeedbackArr = res
      // 反馈方式为附件
      if (props.data.feedBackType === "03") {
        pageData.tableConfig.push({ title: "操作", prop: "actions", slot: true })
      } else if (["01", "02"].includes(props.data.feedBackType)) {
        // 反馈方式为 数字和字符串的
        const source = (props.data.dataValue && JSON.parse(props.data.dataValue)) || []
        const dataConfig = source.map(t => {
          const obj = {
            title: t.fieldKey,
            prop: t.fieldKey
          }
          pageData.columnsArr.push(t.fieldKey)
          return obj
        })
        pageData.tableConfig.push(...dataConfig)
        // if (props.data.feedBackType === "02") { // 反馈方式为字符串的
        //   pageData.tableConfig.unshift({ title: "合计", prop: "actions", slot: true }
        //   )
        // }
        pageData.detailFeedbackArr = pageData.detailFeedbackArr.map(t => {
          const data = (t.dataDetails && t.dataDetails.length && t.dataDetails) || source

          if (data) {
            data.forEach(item => {
              // pageData.columnsArr.push(item.fieldKey)
              t[item.fieldKey] = item.fieldValue || "未反馈"
            })
          }
          return t
        })
      }

      if (props.data.feedBackType === "01") {
        // 反馈方式为数字
        const temp = { orgName: "合计" }
        pageData.detailFeedbackArr.forEach(t => {
          pageData.columnsArr.forEach(r => {
            if (t[r] && !isNaN(t[r])) {
              temp[r] = +t[r] + (temp[r] || 0)
            }
          })
        })
        pageData.detailFeedbackArr.unshift(temp)
      }
    } else {
      pageData.detailFeedbackInfo = res
      if (pageData.detailFeedbackInfo?.feedBackType === "03") {
        pageData.detailFeedbackInfo.dataDetails = (pageData.detailFeedbackInfo.dataDetails || []).map(t => t.fieldValue).join(",")
      }
    }
  } catch (e) {
    console.log(e)
  } finally {
    pageData.tableloading = false
  }
}

//导出表格
const export2Excel = async () => {
  let title = {
    // xh: "序号",
    // ssdwmc: "所属单位",
    // deptName: "所属部门",
    // xtCjr: "反馈人",
    // orgName: "发布单位",
    // userName: "发布人",
    // issuedTime: "任务发布日期",
    // feedBackDate: "反馈期限",
    // stateDesc: "状态",
  }

  pageData.tableConfig.forEach(t => {
    if (t.prop === "actions") return
    title[t.prop] = t.title
  })

  try {
    let tableList = JSON.parse(JSON.stringify(pageData.detailFeedbackArr))
    if (!tableList.length) {
      ElMessage.warning("暂无数据可导出！")
      return
    }
    // tableList.forEach((el) => {
    //   el.typeDesc = el.type == "01" ? "下发" : "接收"
    //   el.stateDesc = getStateInfo.value(el.state)?.label
    //   // el.content = parser.write(el.content)
    //   // parser.end();
    // })

    let tablename = "反馈汇总.xlsx"

    exportXlsx(title, tableList, "反馈汇总", tablename, "反馈汇总")
  } catch (error) {
    ElMessage.warning("导出失败，请稍后再试！")
    exportLoading.value = false
  }
}

const handleDownload = async row => {
  if (row.dataDetails && row.dataDetails.length) {
    getUrl(row.dataDetails.map(t => t.fieldKey))
  }
}

// 数字类型，合并第一行
const arraySpanMethod = ({ row, column, rowIndex, columnIndex }) => {
  if (props.data.feedBackType === "01" && rowIndex === 0) {
    if (columnIndex === 1) {
      return [1, 3]
    } else if (columnIndex === 2 || columnIndex === 3) {
      return [0, 1]
    }
  }
}

onMounted(() => {
  getData()
})
</script>
<style lang="scss" scoped></style>
