<!-- 用户管理 -->
<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="list">
        <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>
    </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" disabled 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"
                disabled
                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>
import DeviceApi from "@/api/device";

const route = useRoute();
const router = useRouter();

const form = ref({});
const formRef = ref(ElForm); // 表单
const loading = ref(false); //  加载状态
const list = 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 handleSubmit = useThrottleFn(() => {
  formRef.value.validate((valid) => {
    if (valid) {
      loading.value = true;
      DeviceApi.updateDevices(form.value.device_id,form.value)
        .then((res) => {
          if (res.data.code == 0) {
            ElMessage.success("保存成功");
            closeBack();
          }
        })
        .finally(() => {
          loading.value = false;
        });
    }
  });
}, 3000);

/** 初始化数据 */
function initPage() {
  let id = route.query.id;
  loading.value = true;
  DeviceApi.devicesInfo(id)
    .then((res) => {
      let device_info = res.data.data.device_info;
      if (res.data.code == 0) {
        let data = {};
        data.uuid = device_info.uuid;
        data.pcie_index = device_info.pcie_index;
        data.manufacturer = device_info.manufacturer;
        data.hardware_chip = device_info.hardware_chip;
        data.hardware_model = device_info.hardware_model;
        data.software_version = device_info.software_version;
        data.cmpt_hardware_type = device_info.cmpt_hardware_type;
        data.cmpt_interface_type = device_info.cmpt_interface_type;
        list.value.push(data);
      }
      form.value.device_id = res.data.data.device_id || device_info.device_id;
      form.value.device_name = device_info.device_name;
      form.value.device_desc = device_info.device_desc;
      form.value.infomation = device_info.infomation;
    })
    .finally(() => {
      loading.value = false;
    });
}

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

onMounted(() => {
  initPage();
});
</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>
