<script lang="ts" setup>
// import { useSettingStore } from '@/stores/settingStore'
import { syncSendCmd } from 'apis/system'
import { ElMessageBox } from 'element-plus'
import { 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 !== 1) {
    FtMessage.warning('请选择一条数据进行删除')
    return
  }
  ElMessageBox.confirm(
    '请确认是否删除?',
    '删除',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    },
  ).then(() => {
    const delParams = {
      className: 'DisinfectionLogsService',
      fnName: 'deleteReport',
      params: {
        logName: selectedRecords.value.map(item => item.name)[0],
      },
    }
    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: 'exportRecord',
    params: {
      logNames: selectedRecords.value.map(item => item.name),
    },
  }
  syncSendCmd(exportParams)
}

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

const onClose = () => {
  visible.value = false
}
</script>

<template>
  <div>
    <div class="history-export">
      <bt-button
        type="primary"
        button-text="导出"
        @click="onExportHistory"
      />
      <bt-button
        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 prop="name" label="消毒日期" />
        <el-table-column prop="detail" label="操作" width="100">
          <template #default="scoped">
            <el-link type="primary" @click="showDetail(scoped.row)">
              查看
            </el-link>
          </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;
  }
}
</style>
