<!-- 用户管理 -->
<template>
  <div class="app-container model-detail">
    <!-- 用户新增/编辑表单 -->
    <el-card v-loading="loading">
      <template #header
        ><svg-icon
          icon-class="pause"
          style="width: 20px; height: 20px"
        />添加网络设备</template
      >
      <el-table :data="unregistedList" stripe>
        <el-table-column label="选择" width="60" align="center">
          <template #default="scope">
            <el-radio
              v-model="form.unregisted"
              :label="scope.row.uuid"
              @change.native="getCurrentRow(scope.row)"
              >{{ "" }}</el-radio
            >
          </template>
        </el-table-column>
        <el-table-column prop="hardware_chip" label="芯片信息" align="left" />
        <el-table-column prop="hardware_model" label="设备型号" align="left" />
        <el-table-column prop="cmpt_hardware_type" label="设备类型" align="left" />
        <el-table-column prop="manufacturer" label="厂商名称" align="left" />
        <el-table-column prop="software_version" label="服务程序版本" align="left" />
        <el-table-column
          prop="connection_state"
          label="连接状态"
          align="left"
          v-if="false"
        />
      </el-table>
    </el-card>
    <el-card v-loading="loading" style="margin-top: 12px">
      <el-row>
        <el-col :lg="12" :xs="24">
          <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
            <el-form-item label="设备ID" prop="device_id">
              <el-input v-model="form.device_id" placeholder="请输入设备ID" />
            </el-form-item>
            <el-form-item label="设备名称" prop="device_name">
              <el-input v-model="form.device_name" placeholder="请输入设备名称" />
            </el-form-item>
            <el-form-item label="设备描述" prop="device_desc">
              <el-input
                v-model="form.device_desc"
                :rows="2"
                type="textarea"
                placeholder="请输入设备描述"
              />
            </el-form-item>
            <el-form-item label="详细信息" prop="infomation">
              <el-input
                v-model="form.infomation"
                :rows="3"
                type="textarea"
                placeholder="请输入详细信息"
              />
            </el-form-item>
          </el-form>
        </el-col>
        <el-col :lg="12" :xs="24"> </el-col>
      </el-row>
    </el-card>
    <el-card class="card-footer">
      <el-button type="primary" @click="handleSubmit"><i-ep-check />确 定</el-button>
      <el-button @click="closeBack"><i-ep-close />取 消</el-button>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import DeviceApi from "@/api/device";

const router = useRouter();

const form = reactive({
  unregisted: null,
});
const formRef = ref(ElForm); // 表单
const loading = ref(false); //  加载状态
const unregistedList = ref([]); // 未注册设备
// 校验规则
const rules = reactive({
  device_id: [{ required: true, message: "设备ID不能为空", trigger: "blur" }],
  device_name: [{ required: true, message: "设备名称不能为空", trigger: "blur" }],
  device_desc: [{ required: false, message: "设备描述不能为空", trigger: "blur" }],
  infomation: [{ required: false, message: "详细信息不能为空", trigger: "blur" }],
});
const queryParams = reactive({
  cmpt_interface_type: "net",
});

/** 选中未注册设备 */
function getCurrentRow(row: { [key: string]: any }) {
  form.uuid = row.uuid;
  form.manufacturer = row.manufacturer;
  form.hardware_chip = row.hardware_chip;
  form.hardware_model = row.hardware_model;
  form.software_version = row.software_version;
  form.cmpt_hardware_type = row.cmpt_hardware_type;
  form.cmpt_interface_type = row.cmpt_interface_type;
}

/** 表单提交 */
const handleSubmit = useThrottleFn(() => {
  formRef.value.validate((valid: any) => {
    if (valid) {
      if (!form.unregisted) {
        ElMessage.error("请选择要注册的设备");
        return false;
      }
      loading.value = true;
      DeviceApi.add(form)
        .then((res) => {
          if (res.data.code == 0) {
            ElMessage.success("保存成功");
            closeBack();
          }
        })
        .finally(() => {
          loading.value = false;
        });
    }
  });
}, 3000);

/** 初始化未注册设备列表 */
function initUnregistedList() {
  DeviceApi.unregisted(queryParams).then((res) => {
    if (res.data.code == 0) {
      unregistedList.value = res.data.data.UnregistedList;
    }
  });
}

/** 返回列表页面 */
function closeBack() {
  router.push({ path: "/tester/equipment" });
}

onMounted(() => {
  initUnregistedList();
});
</script>
<style scoped lang="scss">
.model-detail {
  :deep(.el-card__header) {
    padding: 8px 4px;
    display: flex;
    align-items: center;
  }
}
.sp-file {
  color: #29d;
}
.tb-base-info {
  line-height: 30px;
}
:deep(.svg-icon) {
  margin-right: 8px;
}
.card-footer {
  position: fixed;
  width: calc(100% - 215px);
  bottom: 0px;
  :deep(.el-card__body) {
    padding: 10px;
    .el-pagination {
      justify-content: end;
    }
  }
}
</style>
