<!-- 到期提醒 -->
<script lang="ts" setup name="RemindList">
import { reactive, ref } from 'vue'
import { ElLoading, ElMessage, ElMessageBox } from 'element-plus'
import trend from './components/trend.vue'
import { exportRemind, getRemindList, remindRow } from '@/api/eqpt/device/remind'
import { getDictByCode } from '@/api/system/dict'
import { exportFile } from '@/utils/exportUtils'
import { getDeviceNameList, getModelAllList, getModelList } from '@/api/eqpt/device/model'
import { getTaskList } from '@/api/eqpt/device/task'
const { proxy } = getCurrentInstance() as any
const listQuery = reactive({
  certificateValidEnd: '',
  certificateValidStart: '',
  equipmentName: '',
  equipmentNo: '',
  usePosition: '',
  usageStatus: '',
  manufactureNo: '',
  tastEndTime: '',
  taskStartTime: '',
  taskId: '',
  model: '',
  offset: 1,
  limit: 20,
})
const columns = ref([
  {
    text: '统一编号',
    value: 'equipmentNo',
    align: 'center',
  },
  {
    text: '设备名称',
    value: 'equipmentName',
    align: 'center',
  },
  {
    text: '规格型号',
    value: 'model',
    align: 'center',
  },
  {
    text: '出厂编号',
    value: 'manufactureNo',
    align: 'center',
  },
  {
    text: '使用岗位',
    value: 'usePosition',
    align: 'center',
  },
  {
    text: '计量标识',
    value: 'meterIdentifyName',
    align: 'center',
  },
  {
    text: '检定周期',
    value: 'checkCycle',
    align: 'center',
  },
  {
    text: '证书有效期',
    value: 'certificateValid',
    align: 'center',
  },
  {
    text: '使用状态',
    value: 'usageStatusName',
    align: 'center',
  },
  // {
  //   text: '备注',
  //   value: 'remark',
  //   align: 'center',
  // },
  {
    text: '参试任务',
    value: 'taskNames',
    align: 'center',
  },
  {
    text: '参试任务时间',
    value: 'taskTimes',
    align: 'center',
  },
])
const list = ref([])
const total = ref(0)
const listLoading = ref(true)

// 获取数据
const fetchData = (isNowPage = true) => {
  listLoading.value = true
  if (!isNowPage) {
    // 是否显示当前页，否则跳转第一页
    listQuery.offset = 1
  }
  getRemindList(listQuery).then((response) => {
    list.value = response.data.rows
    total.value = parseInt(response.data.total)
    listLoading.value = false
  })
}
fetchData()
// 查询数据
const search = () => {
  fetchData(false)
}
const range = ref()
watch(() => range.value, (newVal) => {
  listQuery.certificateValidStart = ''
  listQuery.certificateValidEnd = ''
  if (Array.isArray(newVal)) {
    if (newVal.length) {
      listQuery.certificateValidStart = `${newVal[0]} 00:00:00`
      listQuery.certificateValidEnd = `${newVal[1]} 23:59:59`
    }
  }
}, {
  deep: true,
})
const datetimerange1 = ref()
watch(() => datetimerange1.value, (newVal) => {
  listQuery.taskStartTime = ''
  listQuery.tastEndTime = ''
  if (Array.isArray(newVal)) {
    if (newVal.length) {
      listQuery.taskStartTime = `${newVal[0]} 00:00:00`
      listQuery.tastEndTime = `${newVal[1]} 23:59:59`
    }
  }
}, {
  deep: true,
})
const reset = () => {
  range.value = []
  datetimerange1.value = []
  listQuery.tastEndTime = ''
  listQuery.taskStartTime = ''
  listQuery.certificateValidEnd = ''
  listQuery.certificateValidStart = ''
  listQuery.equipmentName = ''
  listQuery.manufactureNo = ''
  listQuery.equipmentNo = ''
  listQuery.usePosition = ''
  listQuery.usageStatus = ''
  listQuery.model = ''
  listQuery.taskId = ''
  listQuery.limit = 20
  listQuery.offset = 1
  search()
}
// 页数发生变化后的操作，可能是页码变化，可能是每页容量变化，此函数必写
const changePage = (val: { size: number; page: number }) => {
  if (val && val.size) {
    listQuery.limit = val.size
  }
  if (val && val.page) {
    listQuery.offset = val.page
  }
  fetchData()
}
// 表格被选中的行
const selectList = ref<any[]>([])
// 表格多选
const multiSelect = (row: any[]) => {
  selectList.value = row
}
const $router = useRouter()
const detail = (row: any) => {
  $router.push({
    path: '/dremindlist/detail',
    query: {
      row: JSON.stringify(row),
      id: row.id,
      statusName: '全部',
      equipmentType: '1',
    },
  })
}
// 提醒
const remind = (row: any) => {
  ElMessageBox.confirm(
    '确认提醒吗?',
    '确认',
    {
      confirmButtonText: '确认',
      cancelButtonText: '取消',
      type: 'warning',
    },
  ).then((res) => {
    remindRow({ equipmentId: row.id }).then(() => {
      ElMessage.success('操作成功')
      search()
    })
  })
}
// 导出列表
const exportList = () => {
  if (list.value.length) {
    const loading = ElLoading.service({
      lock: true,
      text: 'Loading',
      background: 'rgba(255, 255, 255, 0.8)',
    })
    const data = {
      ...listQuery,
      offset: undefined,
      limit: undefined,
      ids: selectList.value.map(item => item.id),
    }
    exportRemind(data).then((res) => {
      exportFile(res.data, '到期提醒')
      loading.close()
    })
      .catch((_) => {
        loading.close()
      })
  }
  else {
    ElMessage.warning('无可导出内容')
  }
}
// 设备使用状态
const useStatusList = ref<{ id: string; value: string; name: string }[]>() // 使用状态
// 任务列表
const taskList = ref<{ id: string; value: string; name: string }[]>([])
// 设备名称
const deviceNameList = ref<{ id: string; value: string; name: string }[]>([])
// 规格型号
const modelList = ref<any[]>([])
const allList = ref<any[]>([])
const fetchStatusList = () => {
  getDictByCode('eqptDeviceUseStatus').then((res) => {
    useStatusList.value = res.data
  })
  // 设备名称
  getDeviceNameList().then((res) => {
    deviceNameList.value = res.data
  })
  // 规格型号
  getModelAllList({}).then((res) => {
    allList.value = res.data
    modelList.value = res.data
  })
  // 参试任务
  getTaskList({ limit: 9999, offset: 1 }).then((res) => {
    taskList.value = res.data.rows.map((item: any) => ({ name: item.taskName, value: item.id, id: item.id }))
  })
}
fetchStatusList()
// 列表图表切换
const showTable = ref(true)
const changeTable = () => {
  showTable.value = !showTable.value
}
</script>

<template>
  <app-container>
    <!-- 筛选条件 -->
    <search-area :need-clear="true" @search="search" @clear="reset">
      <search-item>
        <el-input v-model.trim="listQuery.equipmentNo" placeholder="统一编号" clearable />
      </search-item>
      <search-item>
        <el-select v-model.trim="listQuery.equipmentName" placeholder="设备名称" clearable>
          <el-option v-for="(item, index) in deviceNameList" :key="index" :label="item" :value="item" />
        </el-select>
      </search-item>
      <search-item>
        <el-select v-model.trim="listQuery.model" placeholder="规格型号" clearable>
          <el-option v-for="item in modelList" :key="item.id" :label="item.model" :value="item.model" />
        </el-select>
      </search-item>
      <search-item>
        <el-input v-model.trim="listQuery.manufactureNo" placeholder="出厂编号" clearable />
      </search-item>
      <!-- <search-item>
        <el-input v-model.trim="listQuery.equipmentName" placeholder="设备名称" clearable />
      </search-item> -->
      <search-item>
        <el-input v-model.trim="listQuery.usePosition" placeholder="所属岗位" clearable />
      </search-item>
      <search-item>
        <el-date-picker
          v-model="range" type="daterange" value-format="YYYY-MM-DD"
          format="YYYY-MM-DD" range-separator="至" start-placeholder="证书有效期开始时间" end-placeholder="证书有效期结束时间"
        />
      </search-item>
      <search-item>
        <!-- <el-input v-model.trim="listQuery.usageStatus" placeholder="使用状态" clearable /> -->
        <el-select v-model="listQuery.usageStatus" placeholder="使用状态" clearable style="width: 100%;">
          <el-option v-for="item in useStatusList" :key="item.id" :label="item.name" :value="item.value" />
        </el-select>
      </search-item>
      <search-item>
        <el-select v-model="listQuery.taskId" placeholder="参试任务" clearable>
          <el-option v-for="item in taskList" :key="item.value" :label="item.name" :value="item.value" />
        </el-select>
      </search-item>
      <search-item>
        <el-date-picker
          v-model="datetimerange1" type="daterange" value-format="YYYY-MM-DD"
          format="YYYY-MM-DD" range-separator="至" start-placeholder="参试任务开始时间" end-placeholder="参试任务结束时间"
        />
      </search-item>
    </search-area>
    <table-container>
      <template #btns-right>
        <icon-button icon="icon-change" title="切换" @click="changeTable" />
        <icon-button v-if="showTable" icon="icon-export" title="导出" @click="exportList" />
      </template>
      <!-- 普通表格 -->
      <normal-table
        v-if="showTable"
        :data="list" :total="total" :columns="columns" :query="listQuery" :list-loading="listLoading"
        :is-showmulti-select="true" :is-multi="true" @change="changePage" @multi-select="multiSelect"
      >
        <template #columns>
          <el-table-column label="操作" width="140" align="center">
            <template #default="scope">
              <el-button link type="primary" size="small" @click="detail(scope.row)">
                查看
              </el-button>
              <el-button link type="primary" size="small" @click="remind(scope.row)">
                提醒
              </el-button>
            </template>
          </el-table-column>
        </template>
      </normal-table>
      <trend v-else />
    </table-container>
  </app-container>
</template>

