<script lang="ts" setup>
// import { useSettingStore } from '@/stores/settingStore'
import { syncSendCmd } from 'apis/system'
import { ElMessageBox } from 'element-plus'
import { computed, onMounted, ref } from 'vue'

import { FtMessage } from '@/libs/message'

import HistoryDetail from './HistoryDetail.vue'

// const settingStore = useSettingStore()
const tableData = ref<string[]>([])
const selectedRecords = ref<Setting.History[]>([])
const visible = ref(false)

onMounted(() => {
  getRecord()
})

const getRecord = () => {
  const params = {
    className: 'DisinfectionLogsService',
    fnName: 'getRecordList',
    params: {},
  }
  syncSendCmd(params).then((res) => {
    if (res.ackcode === 0 && res.rely && res.rely.length) {
      const list: string[] = res.rely.map((item: Setting.History) => {
        return {
          name: item,
        }
      })
      tableData.value = list
    }
  })
}
const showDetail = (historyItem: Setting.History) => {
  const detailParams = {
    className: 'DisinfectionLogsService',
    fnName: 'getRecord',
    params: {
      logName: historyItem.name,
    },
  }
  syncSendCmd(detailParams).then(() => {
    visible.value = true
  })
}
const onDelHistory = () => {
  if (!selectedRecords.value.length) {
    FtMessage.warning('请选择要删除的数据')
    return
  }
  ElMessageBox.confirm('请确认是否删除?', '删除', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(() => {
    const delParams = {
      className: 'DisinfectionLogsService',
      fnName: 'deleteReports',
      params: {
        logNames: selectedRecords.value.map(item => item.name),
      },
    }
    syncSendCmd(delParams).then((res) => {
      if (res.ackcode === 0) {
        FtMessage.success('删除成功')
        getRecord()
      }
    })
  })
}
const onExportHistory = () => {
  // if (!selectedRecords.value.length) {
  //   FtMessage.warning('请选择要导出的数据')
  //   return
  // }
  const exportParams = {
    className: 'DisinfectionLogsService',
    fnName: 'exportAllRecord',
    params: {},
  }
  syncSendCmd(exportParams)
    .then((res) => {
      if (res.ackcode === 0) {
        FtMessage.success('导出成功')
      }
    })
    .catch((error) => {
      console.error('导出记录失败:', error)
    })
}

const handleSelectionChange = (rows: Setting.History[]) => {
  selectedRecords.value = rows
}

const onClose = () => {
  visible.value = false
}
const deleteBtnVisible = computed(() => {
  return JSON.parse(localStorage.user).roleType.includes('admin')
})
</script>

<template>
  <div>
    <div class="history-export">
      <bt-button type="primary" button-text="全部导出" @click="onExportHistory" />
      <bt-button v-if="deleteBtnVisible" type="primary" button-text="删除" @click="onDelHistory" />
    </div>
    <div class="history-table">
      <el-table :data="tableData" style="width: 100%" height="100%" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" />
        <el-table-column type="index" label="序号" width="80" />
        <el-table-column prop="name" label="消毒日期" />
        <el-table-column prop="detail" label="操作" width="100">
          <template #default="scoped">
            <div class="user-opera">
              <el-button class="view-button" @click.stop="showDetail(scoped.row)">
                查看
              </el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <ft-dialog v-model="visible" title="消毒详情" width="80vw" @cancel="onClose">
      <div>
        <HistoryDetail />
      </div>
    </ft-dialog>
  </div>
</template>

<style lang="scss" scoped>
.main-content {
  height: $main-container-height;
  padding: 10px;
  background: $gradient-color;
  .history-export {
    margin: 10px;
  }
  .history-table {
    height: 73vh;
    overflow: auto;
  }
  .view-button,
  .delete-button {
    border: none;
    padding: 4px 8px;
    border-radius: 3px;
    cursor: pointer;
    margin-right: 5px;
    transition: all 0.3s;
    font-family: serif;
  }

  .view-button {
    background-color: #e6f0ff;
    color: #1890ff;
  }

  .view-button:hover {
    background-color: #1890ff;
    color: white;
  }
  .delete-button {
    background-color: #ffe6e6;
    color: #ff4d4f;
  }

  .delete-button:hover {
    background-color: #ff4d4f;
    color: white;
  }
}
</style>
