<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备编码" prop="deviceCode">
        <el-input
            v-model="queryParams.deviceCode"
            placeholder="请输入设备编码"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="deviceName">
        <el-input
            v-model="queryParams.deviceName"
            placeholder="请输入设备名称"
            clearable
            @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">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['driver:basic:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['driver:basic:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['driver:basic:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="Download"
            @click="handleExport"
            v-hasPermi="['driver:basic:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" stripe border :data="deviceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="设备ID" align="center" prop="id" />-->
      <el-table-column label="设备编码" align="center" prop="deviceCode" width="90"/>
      <el-table-column label="设备名称" align="center" prop="deviceName" width="120"/>
      <el-table-column label="设备类型" align="center" prop="deviceType" />
      <el-table-column label="子系统类型" align="center" prop="subsystemType" width="120"/>
      <el-table-column label="所属空间ID" align="center" prop="spaceId" width="100"/>
      <el-table-column label="所属空间名称" align="center" prop="spaceName" width="110"/>
      <el-table-column label="品牌" align="center" prop="brand" />
      <el-table-column label="型号" align="center" prop="model" width="90"/>
      <el-table-column label="序列号" align="center" prop="serialNumber" width="120"/>
      <el-table-column label="生产日期" align="center" prop="manufactureDate" width="110" sortable
      >
        <template #default="scope">
          <span>{{ parseTime(scope.row.manufactureDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="安装日期" align="center" prop="installDate" width="110">
        <template #default="scope">
          <span>{{ parseTime(scope.row.installDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="保修期(月)" align="center" prop="warrantyPeriod" width="90"/>
      <el-table-column label="状态" align="center" prop="status">
        <template #default="scope">
          <dict-tag :options="sys_drivce_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="是否在线" align="center" prop="isOnline">
        <template #default="scope">
          <dict-tag :options="sys_yes_no_tf" :value="scope.row.isOnline"/>
        </template>
      </el-table-column>
      <el-table-column label="设备描述" align="center" prop="description" width="140"/>
      <el-table-column label="操作" min-width="140" fixed="right" width="200px" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="View" @click="handleDetail(scope.row)" v-hasPermi="['driver:basic:detail']">详情</el-button>
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['driver:basic:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['driver:basic:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改设备基础信息对话框 -->
    <el-dialog :title="title" v-model="open" width="1000px" append-to-body>
      <el-form ref="deviceRef" :model="form" :rules="rules" label-width="180px">
        <el-form-item label="设备编码" prop="deviceCode">
          <el-input v-model="form.deviceCode" placeholder="请输入设备编码" />
        </el-form-item>
        <el-form-item label="设备名称" prop="deviceName">
          <el-input v-model="form.deviceName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="设备类型" prop="deviceType">
          <el-select
              v-model="form.deviceType"
              placeholder="请选择设备类型"
              filterable
              clearable
          >
            <el-option
                v-for="dict in sys_device_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
<!--        <el-form-item label="设备类型" prop="deviceType">
          <el-input v-model="form.deviceType" placeholder="请输入设备类型" />
        </el-form-item>-->
<!--        <el-form-item label="子系统类型" prop="subsystemType">
          <el-input v-model="form.subsystemType" placeholder="请输入子系统类型" />
        </el-form-item>-->
        <el-form-item label="子系统类型" prop="subsystemType">
          <el-select
              v-model="form.subsystemType"
              placeholder="请选择子系统类型"
              filterable
              clearable
          >
            <el-option
                v-for="dict in sys_subsystem_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属空间ID" prop="spaceId">
          <el-input v-model="form.spaceId" placeholder="请输入所属空间ID" />
        </el-form-item>
        <el-form-item label="所属空间名称" prop="spaceName">
          <el-input v-model="form.spaceName" placeholder="请输入所属空间名称" />
        </el-form-item>
        <el-form-item label="品牌" prop="brand">
          <el-input v-model="form.brand" placeholder="请输入品牌" />
        </el-form-item>
        <el-form-item label="型号" prop="model">
          <el-input v-model="form.model" placeholder="请输入型号" />
        </el-form-item>
        <el-form-item label="序列号" prop="serialNumber">
          <el-input v-model="form.serialNumber" placeholder="请输入序列号" />
        </el-form-item>
        <el-form-item label="生产日期" prop="manufactureDate">
          <el-date-picker clearable
                          v-model="form.manufactureDate"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择生产日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="安装日期" prop="installDate">
          <el-date-picker clearable
                          v-model="form.installDate"
                          type="date"
                          value-format="YYYY-MM-DD"
                          placeholder="请选择安装日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="保修期(月)" prop="warrantyPeriod">
          <el-input v-model="form.warrantyPeriod" placeholder="请输入保修期(月)" />
        </el-form-item>
        <el-form-item label="设备状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
                v-for="dict in sys_drivce_status"
                :key="dict.value"
                :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否在线" prop="isOnline">
         <el-select v-model="form.isOnline" placeholder="请选择">
            <el-option v-for="dict in sys_yes_no_tf" :key="dict.value" :label="dict.label" :value="parseInt(dict.value)"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="设备描述" prop="description">
          <el-input v-model="form.description" type="textarea" 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>
    <el-drawer
        v-model="detailVisible"
        title="设备监控"
        direction="rtl"
        size="50%"
    >
      <el-tabs v-model="activeTab">
        <el-tab-pane label="设备详情" name="detail">
          <el-descriptions :column="2" border>
            <el-descriptions-item label="设备编码">{{currentDevice.deviceCode}}</el-descriptions-item>
            <el-descriptions-item label="设备名称">{{currentDevice.deviceName}}</el-descriptions-item>
            <el-descriptions-item label="设备类型">
              {{ sys_device_type.find(item => item.value === currentDevice.deviceType)?.label || currentDevice.deviceType }}
            </el-descriptions-item>
            <el-descriptions-item label="子系统类型">
              {{ sys_subsystem_type.find(item => item.value === currentDevice.subsystemType)?.label || currentDevice.subsystemType }}
            </el-descriptions-item>
            <el-descriptions-item label="所属空间">{{currentDevice.spaceName}}</el-descriptions-item>
            <el-descriptions-item label="品牌">{{currentDevice.brand}}</el-descriptions-item>
            <el-descriptions-item label="型号">{{currentDevice.model}}</el-descriptions-item>
            <el-descriptions-item label="序列号">{{currentDevice.serialNumber}}</el-descriptions-item>
          </el-descriptions>
        </el-tab-pane>
        <el-tab-pane label="设备组态" name="configuration">
          <!-- 设备组态内容 -->
        </el-tab-pane>
        <el-tab-pane label="设备告警" name="alarm">
          <!-- 设备告警内容 -->
        </el-tab-pane>
        <el-tab-pane label="控制纪录" name="control">
          <!-- 控制纪录内容 -->
        </el-tab-pane>
        <el-tab-pane label="实时数据" name="realtime">
          <!-- 实时数据内容 -->
        </el-tab-pane>
        <el-tab-pane label="历史数据" name="instant">
          <!-- 历史数据内容 -->
        </el-tab-pane>
        <el-tab-pane label="自动抄表" name="meter">
          <!-- 自动抄表内容 -->
        </el-tab-pane>
        <el-tab-pane label="事件记录" name="event">
          <!-- 事件记录内容 -->
        </el-tab-pane>
      </el-tabs>
    </el-drawer>
  </div>
</template>

<script setup name="Device">
import {listBasic, getBasic, delBasic, addBasic, updateBasic} from "@/api/device/basic";
import {show} from "@/api/system/notice.js";

const {proxy} = getCurrentInstance();
const {sys_drivce_status,sys_yes_no_tf,sys_device_type,sys_subsystem_type} = proxy.useDict('sys_drivce_status','sys_yes_no_tf','sys_device_type','sys_subsystem_type');

const deviceList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const detailVisible = ref(false);
const activeTab = ref('detail'); // 默认选中第一个标签页
const currentDevice = ref({});

function handleDetail(row) {
  currentDevice.value = row;
  activeTab.value = 'detail'; // 每次点击详情时重置为第一个标签页
  detailVisible.value = true;
}


const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    deviceCode: null,
    deviceName: null,
    deviceType: null,
  },
  rules: {
    deviceCode: [
      {required: true, message: "设备编码不能为空", trigger: "blur"}
    ],
    deviceName: [
      {required: true, message: "设备名称不能为空", trigger: "blur"}
    ],
    device_type: [
      {required: true, message: "设备类型不能为空", trigger: "blur"}
    ],
    deviceType: [
      {required: true, message: "设备类型(空调/新风/配电等)不能为空", trigger: "change"}
    ],
    subsystemType: [
      {required: true, message: "子系统类型不能为空", trigger: "change"}
    ],
    createTime: [
      {required: true, message: "创建时间不能为空", trigger: "blur"}
    ],
    updateTime: [
      {required: true, message: "更新时间不能为空", trigger: "blur"}
    ]
  }
});

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

/** 查询设备基础信息列表 */
function getList() {
  loading.value = true;
  listBasic(queryParams.value).then(response => {
    deviceList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    deviceCode: null,
    deviceName: null,
    deviceType: null,
    subsystemType: null,
    spaceId: null,
    spaceName: null,
    brand: null,
    model: null,
    serialNumber: null,
    manufactureDate: null,
    installDate: null,
    warrantyPeriod: null,
    status: null,
    isOnline: null,
    description: null,
    createTime: null,
    updateTime: null
  };
  proxy.resetForm("deviceRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

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

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加设备基础信息";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getBasic(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改设备基础信息";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["deviceRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateBasic(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addBasic(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除设备基础信息编号为"' + _ids + '"的数据项？').then(function () {
    return delBasic(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('device/basic/export', {
    ...queryParams.value
  }, `device_${new Date().getTime()}.xlsx`)
}

getList();
</script>
