<script setup name="DeviceInfo">
import {
  getDeviceFindPageList,
  getDeviceID,
  addDevice,
  updateDevice,
  deleteDevice
} from '@/api/device/deviceInfo.js'

import { getElectricityMeterList } from '@/api/device/electricityMeter.js'

//路由
// const router = useRouter()
//this
const { proxy } = getCurrentInstance()
const { device_status } = proxy.useDict('device_status')
// const { net_status } = proxy.useDict('net_status')

const deviceList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
//表格多选
const ids = ref([])
//不等于-1  为true
const single = ref(true)
//大于1   为true
const multiple = ref(true)
const total = ref(0)
const title = ref('')
//时间范围
const dateRange = ref([])

const data = reactive({
  form: {},
  queryParams: {
    currentPage: 1,
    size: 10,
    deviceID: undefined
  },
  rules: {
    deviceName: [
      { required: true, message: '设备名称不能为空', trigger: 'blur' }
    ],
    imei: [{ required: true, message: 'IMEI号不能为空', trigger: 'blur' }]
    // longitude: [{ required: true, message: '经度不能为空', trigger: 'blur' }],
    // latitude: [{ required: true, message: '纬度不能为空', trigger: 'blur' }]
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询电表列表 */
function getList(id) {
  loading.value = true
  if (id) {
    getDeviceID(id).then((response) => {
      if (response.data) {
        deviceList.value = [response.data]
        total.value = 1
      } else {
        deviceList.value = []
        total.value = 0
      }
      loading.value = false
      proxy.$modal.msgSuccess('数据查询成功')
    })
  } else {
    getElectricityMeterList(queryParams.value).then(({ data: { result } }) => {
      deviceList.value = result.list
      total.value = result.total
    })
    loading.value = false
  }
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.currentPage = 1
  getList(queryParams.value.deviceID)
}
/** 重置按钮操作 */
function resetQuery() {
  dateRange.value = []
  proxy.resetForm('queryRef')
  handleQuery()
}
/** 删除按钮操作 */
function handleDelete(row) {
  const deviceIds = row.id ? [row.id] : ids.value
  proxy.$modal
    .confirm('是否确认删除设备编号为"' + deviceIds + '"的数据项?')
    .then(() => {
      return deleteDevice(deviceIds)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess('删除成功')
    })
    .catch(() => {})
}

/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map((item) => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 重置新增的表单以及其他数据  */
function reset() {
  form.value = {
    deviceName: undefined,
    imei: undefined,
    longitude: '',
    latitude: ''
  }
  //表单重置
  proxy.resetForm('deviceRef')
}
/** 新增按钮点击事件 */
function handleAdd() {
  //重置表单
  reset()
  open.value = true
  title.value = '添加电表'
}
/** 修改电表 */
function handleUpdate(row) {
  reset()
  getDeviceID(row.id).then((res) => {
    form.value = res.data
  })
  title.value = '修改电表'
  open.value = true

  // })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs['deviceRef'].validate((valid) => {
    if (valid) {
      try {
        if (form.value.id) {
          updateDevice(form.value).then((res) => {
            if (res) {
              proxy.$modal.msgSuccess('设备修改成功')
              getList()
              open.value = false
            }
          })
        } else {
          addDevice(form.value).then((res) => {
            if (res) {
              proxy.$modal.msgSuccess('设备新增成功')
              getList()
              open.value = false
            }
          })
        }
      } catch {
        proxy.$modal.msgError('失败')
      }
    }
  })
}
/** 取消按钮 */
function cancel() {
  open.value = false
  reset()
}

getList()
</script>

<template>
  <div class="app-container">
    <!-- 搜索功能 -->
    <!-- <el-form
      :model="queryParams"
      ref="queryRef"
      v-show="showSearch"
      v-hasPermi="['device:deviceInfo:query']"
      :inline="true"
    >
      <el-form-item label="设备ID" prop="deviceID">
        <el-input
          v-model.number="queryParams.deviceID"
          placeholder="请输入设备ID"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form> -->

    <!-- 新增,修改 -->
    <!-- <el-row :gutter="10" class="mb8" style="height: 32px"> -->
    <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['device:deviceInfo:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['device:deviceInfo:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete({})"
          v-hasPermi="['device:deviceInfo:remove']"
          >删除</el-button
        >
      </el-col> -->
    <!-- <right-toolbar
        v-model:showSearch="showSearch"
        v-hasPermi="['device:deviceInfo:query']"
        @queryTable="getList"
      ></right-toolbar> -->
    <!-- </el-row> -->

    <!-- 表格数据 -->
    <el-table
      v-loading="loading"
      :data="deviceList"
      @selection-change="handleSelectionChange"
    >
      <!-- 多选框 -->
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="电表编号" prop="id" width="80" align="center" />
      <el-table-column
        label="电表名称"
        prop="deviceName"
        :show-overflow-tooltip="true"
        width="150"
      />
      <el-table-column label="状态" align="center" width="100">
        <template #default="{ row }">
          <dict-tag :options="device_status" :value="row.deviceStatus" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="在线状态" align="center" width="100">
        <template #default="{ row }">
          <dict-tag :options="net_status" :value="row.netStatus" />
        </template>
      </el-table-column> -->
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template #default="{ row }">
          <span>{{ parseTime(row.createTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column label="更新时间" align="center" prop="createTime">
        <template #default="{ row }">
          <span>{{ parseTime(row.updateTime) }}</span>
        </template>
      </el-table-column>

      <!-- <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="{ row }">
          <el-tooltip content="修改" placement="top" v-if="row.roleId !== 1">
            <el-button
              type="text"
              icon="Edit"
              @click="handleUpdate(row)"
              v-hasPermi="['device:deviceInfo:edit']"
            ></el-button>
          </el-tooltip>
          <el-tooltip content="删除" placement="top" v-if="row.roleId !== 1">
            <el-button
              type="text"
              icon="Delete"
              @click="handleDelete(row)"
              v-hasPermi="['device:deviceInfo:remove']"
            ></el-button>
          </el-tooltip>
        </template>
      </el-table-column> -->
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total"
      :total="total"
      v-model:page="queryParams.currentPage"
      v-model:limit="queryParams.size"
      @pagination="getList()"
    />

    <!-- 添加或修改设备配置对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="deviceRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="设备名称" prop="deviceName">
          <el-input v-model="form.deviceName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="IMEI" prop="imei">
          <el-input v-model.number="form.imei" placeholder="请输入IMEI号" />
        </el-form-item>

        <el-form-item label="经度" prop="longitude">
          <el-input
            v-model.number="form.longitude"
            placeholder="请输入经度坐标"
          />
        </el-form-item>
        <el-form-item label="纬度" prop="latitude">
          <el-input
            v-model.number="form.latitude"
            placeholder="请输入纬度坐标"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>
