<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="120px"
    >
      <el-form-item label="ID号" prop="warnCode">
        <el-input
          v-model="queryParams.warnCode"
          placeholder="请输入告警/异常ID"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="AGV编号" prop="robotNo">
        <el-input
          v-model="queryParams.robotNo"
          placeholder="请输入AGV编号"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="时间段" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table
      v-loading="loading"
      :data="list"
      :show-overflow-tooltip="true"
      row-class-name="table-row-class"
      :header-cell-style="{ backgroundColor: '#EBF1FF', padding: '13px 0' }"
    >
      <el-table-column label="序号" align="center" prop="id" />
      <el-table-column label="告警等级（1-4）" align="center" prop="warnLevel" />
      <el-table-column label="告警/异常ID" align="center" prop="warnCode" />
      <el-table-column label="车辆" align="center" prop="robotNo" />
      <el-table-column label="告警/异常信息" align="center" prop="warnMsg" />
      <el-table-column label="发生时间" align="center" prop="createTime">
        <template #default="scope">
          {{ formatDate(scope.row.createTime, 'YYYY-MM-DD HH:mm:ss') }}
        </template>
      </el-table-column>

      <!-- <el-table-column label="操作" align="center">
        <template #default="scope">
          <el-button
            link
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['system:user-bank:update']"
          >
            编辑
          </el-button>
          <el-button
            link
            type="danger"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['system:user-bank:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
</template>

<script setup lang="ts">
import { useRoute } from 'vue-router';
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import * as CarErrorApi from '@/api/carError'
import { formatDate } from '@/utils/formatTime'
import router from '@/router'
const route = useRoute();
defineOptions({ name: 'SystemCarError' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(false) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  createTime: [],
  warnCode: undefined,
  robotNo: undefined
})
const monthList = ref([
  { label: '1月', value: 1 },
  { label: '2月', value: 2 },
  { label: '3月', value: 3 },
  { label: '4月', value: 4 },
  { label: '5月', value: 5 },
  { label: '6月', value: 6 },
  { label: '7月', value: 7 },
  { label: '8月', value: 8 },
  { label: '9月', value: 9 },
  { label: '10月', value: 10 },
  { label: '11月', value: 11 },
  { label: '12月', value: 12 }
])
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const deptList = ref([])

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await CarErrorApi.getRobotWarnMsgPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 关联操作 */
const formBank = ref()
const openBankForm = () => {
  formBank.value.open()
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await UserBankApi.deleteUserBank(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await UserBankApi.exportUserBank(queryParams)
    download.excel(data, '用户银行卡信息.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  // console.log(route.query.robotNo)
  if(route.query.robotNo){
    queryParams.robotNo = route.query.robotNo
  }
  getList()
})
</script>
<style scoped>
::v-deep .table-row-class {
  background-color: #fff;
}
::v-deep .el-table__body .cell {
  font-weight: 400;
  font-size: 14px;
  color: #536387;
  padding: 4px 12px;
}
::v-deep .el-table thead {
  font-weight: 500;
  font-size: 14px;
  color: #0d162a;
}
</style>
