<template>
  <el-form ref="deviceCreate" :model="data" :rules="deviceRules" label-position="top">
    <el-row :gutter="20">
      <el-col :lg="8" :md="12" :span="12" :xl="6" :xs="24">
        <el-form-item label="设备ID(6位)" prop="deviceId">
          <el-input v-model="data.deviceId"></el-input>
        </el-form-item>
      </el-col>

      <el-col :lg="8" :md="12" :span="12" :xl="6" :xs="24">
        <el-form-item label="站点名称" prop="address">
          <el-input v-model="data.address"></el-input>
        </el-form-item>
      </el-col>

      <el-col :lg="8" :md="12" :span="12" :xl="6" :xs="24">
        <el-form-item label="设备完整ID" prop="deviceFullId">
          <el-input v-model="data.deviceFullId"></el-input>
        </el-form-item>
      </el-col>

      <el-col :lg="8" :md="12" :span="12" :xl="6" :xs="24">
        <el-form-item label="地区" prop="area">
          <el-input v-model="data.area"></el-input>
        </el-form-item>
      </el-col>

      <el-col :lg="8" :md="12" :span="12" :xl="6" :xs="24">
        <el-form-item label="联系人" prop="contact">
          <el-input v-model="data.contact"></el-input>
        </el-form-item>
      </el-col>
      <el-col :lg="8" :md="12" :span="12" :xl="6" :xs="24">
        <el-form-item label="电话" prop="phone">
          <el-input v-model="data.phone"></el-input>
        </el-form-item>
      </el-col>

      <el-col :lg="8" :md="12" :span="12" :xl="6" :xs="24">
        <el-form-item label="经度" prop="lon">
          <el-input v-model="data.lon"></el-input>
        </el-form-item>
      </el-col>
      <el-col :lg="8" :md="12" :span="12" :xl="6" :xs="24">
        <el-form-item label="纬度" prop="lat">
          <el-input v-model="data.lat"></el-input>
        </el-form-item>
      </el-col>

      <el-col :lg="8" :md="12" :span="12" :xl="6" :xs="24">
        <el-form-item label="负责人" prop="head">
          <el-input v-model="data.head"></el-input>
        </el-form-item>
      </el-col>
      <el-col :lg="8" :md="12" :span="12" :xl="6" :xs="24">
        <el-form-item label="安装位置" prop="installLocation">
          <el-input v-model="data.installLocation"></el-input>
        </el-form-item>
      </el-col>

      <el-col :lg="8" :md="12" :span="12" :xl="6" :xs="24">
        <el-form-item label="安装时间" prop="installTime">
          <el-date-picker v-model="data.installTime" placeholder="安装时间" style="width: 100%" type="datetime"></el-date-picker>
        </el-form-item>
      </el-col>
      <el-col :lg="8" :md="12" :span="12" :xl="6" :xs="24">
        <el-form-item label="正式运行时间" prop="runTime">
          <el-date-picker v-model="data.runTime" placeholder="正式运行时间" style="width: 100%" type="datetime"></el-date-picker>
        </el-form-item>
      </el-col>

      <el-col :lg="8" :md="12" :span="12" :xl="6" :xs="24">
        <el-form-item label="链路地址" prop="linkAddress">
          <el-input v-model="data.linkAddress"></el-input>
        </el-form-item>
      </el-col>
      <el-col :lg="8" :md="12" :span="12" :xl="6" :xs="24">
        <el-form-item label="探头编号" prop="probNo">
          <el-input v-model="data.probNo"></el-input>
        </el-form-item>
      </el-col>

      <el-col :lg="8" :md="12" :span="12" :xl="6" :xs="24">
        <el-form-item label="试样" prop="simple">
          <el-input v-model="data.simple"></el-input>
        </el-form-item>
      </el-col>
      <el-col :lg="8" :md="12" :span="12" :xl="6" :xs="24">
        <el-form-item label="sim卡1" prop="sim1">
          <el-input v-model="data.sim1"></el-input>
        </el-form-item>
      </el-col>

      <el-col :lg="8" :md="12" :span="12" :xl="6" :xs="24">
        <el-form-item label="设备朝向" prop="deviceDirection">
          <el-input v-model="data.deviceDirection"></el-input>
        </el-form-item>
      </el-col>
      <el-col :lg="8" :md="12" :span="12" :xl="6" :xs="24">
        <el-form-item label="协议版本" prop="protocolVersion">
          <el-input v-model="data.protocolVersion"></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row :gutter="20">
      <el-col :lg="8" :md="12" :span="12" :xl="6" :xs="24">
        <el-form-item label="与主站后台联调情况" prop="joint">
          <el-input v-model="data.joint" type="textarea"></el-input>
        </el-form-item>
      </el-col>
      <el-col :lg="8" :md="12" :span="12" :xl="6" :xs="24">
        <el-form-item label="维修记录" prop="operationRecord">
          <el-input v-model="data.operationRecord" type="textarea"></el-input>
        </el-form-item>
      </el-col>
      <el-col :lg="8" :md="12" :span="12" :xl="6" :xs="24">
        <el-form-item label="备注" prop="remark">
          <el-input v-model="data.remark" type="textarea"></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row :gutter="20" class="mt-5 pl-2">
      <el-form-item>
        <el-button type="primary" @click="onSubmit">提交</el-button>
        <el-button @click="onReset">重置</el-button>
      </el-form-item>
    </el-row>
  </el-form>
</template>

<script setup>
import { defineEmits, reactive, ref } from 'vue';
import { useStore } from 'vuex';
import { ElMessage, ElMessageBox } from 'element-plus';
import { createDevice } from 'apis';
import { deviceData, deviceRules } from '@/config/config';

const data = reactive(deviceData);
const deviceCreate = ref(null); // form
const store = useStore();
const emit = defineEmits(['hide']);

// 重置表单
const onReset = () => {
  deviceCreate.value.resetFields();
};

// 提交表单
const onSubmit = () => {
  deviceCreate.value.validate(async valid => {
    if (!valid) return;
    if (data.installTime) {
      data.installTime = new Date(data.installTime).getTime();
    }
    if (data.runTime) {
      data.runTime = new Date(data.runTime).getTime();
    }
    try {
      await createDevice({ ...data });
      store.commit('device/unshiftDevice', { ...data });
      ElMessageBox.confirm('添加成功，是否继续添加', '提示', {
        confirmButtonText: '继续添加',
        cancelButtonText: '关闭',
        type: 'success',
      })
        .then(() => {
          // 确认继续添加
          onReset();
        })
        .catch(() => {
          onReset();
          emit('on-hide');
        });
      await store.dispatch('device/getDevices'); // 更新站点列表
    } catch (error) {
      ElMessage.error(error);
      throw new Error(error);
    }
  });
};
</script>
