<template>
  <div>
    <div class="gva-search-box">
      <el-form
          ref="searchForm"
          :inline="true"
          :model="searchInfo"
      >
        <el-form-item label="名称或编码">
          <el-input
              v-model="searchInfo.keyword"
              placeholder="搜索名称或编码"
          />
        </el-form-item>
        <el-form-item label="所属分路">
          <el-select v-model="searchInfo.shunt" placeholder="所属分路">
            <el-option label="主路" value=1></el-option>
            <el-option label="支路" value=2></el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button
              type="primary"
              icon="search"
              @click="onSubmit"
          >查询</el-button>
          <el-button
              icon="refresh"
              @click="onReset"
          >重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="gva-table-box">
      <div class="gva-btn-list">
        <el-button
            type="primary"
            icon="plus"
            @click="add()"
        >新增</el-button>
      </div>
      <el-table
          :data="tableData"
          style="width: 100%; margin-bottom: 20px"
          row-key="id"
          border
          :table-layout="'fixed'"
          :row-class-name="tableRowClassName"
      >
        <el-table-column prop="ID" label="#" :width="80"/>
        <el-table-column prop="shunt" label="所属分路" :width="100">
          <template #default="scope">
            {{ scope.row.shunt===1?"主路":scope.row.shunt===2?"支路":"未知" }}
          </template>
        </el-table-column>
        <el-table-column prop="radarName" label="名称" />
        <el-table-column prop="radarCode" label="编码">
          <template #default="scope">
            <el-tag effect="light">{{ scope.row.radarCode }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="radarBrand" label="品牌" />
        <el-table-column prop="radarModel" label="型号"/>
        <el-table-column prop="GateWay.gatewayName" label="所属网关"/>
<!--        <el-table-column prop="status" label="在线状态">-->
<!--          <template #default="scope">-->
<!--            <div class="onlinebox" :style="{'background': scope.row.status===1 ? 'red':'green' }"></div>-->
<!--            <span :style="{ color: scope.row.status === 1 ? 'red' : 'green' }">{{ scope.row.status === 1 ? '离线' : '在线' }}</span>-->
<!--          </template>-->
<!--        </el-table-column>-->
        <el-table-column
            align="left"
            fixed="right"
            label="操作"
            :width="300"
        >
          <template #default="scope">
            <el-button
                type="primary"
                link
                icon="view"
                @click="view(scope.row)"
            >查看</el-button>
            <el-button
                type="primary"
                link
                icon="edit"
                @click="editRadar(scope.row)"
            >编辑</el-button>
            <el-button
                type="primary"
                link
                icon="delete"
                @click="deleteRadar(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="gva-pagination">
        <el-pagination
            :current-page="page"
            :page-size="pageSize"
            :page-sizes="[10, 30, 50, 100]"
            :total="total"
            layout="total, sizes, prev, pager, next, jumper"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"
        />
      </div>

      <el-dialog v-model="addRadarDialog" title="雷达" width="900">
        <el-form
            ref="radarForm"
            :rules="rules"
            :model="radarInfo"
            label-width="100px"
            style="padding: 15px"
        >
          <el-row>
            <el-col :span="12">
              <el-form-item label="雷达名称：" :inline="false" prop="radarName">
                <el-input v-model="radarInfo.radarName"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="编码：" :inline="false">
                <el-input disabled v-model="radarInfo.radarCode"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="品牌：" :inline="false" prop="radarBrand">
                <el-input v-model="radarInfo.radarBrand"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="型号：" :inline="false" prop="radarModel">
                <el-input v-model="radarInfo.radarModel"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="ip地址：" :inline="false" prop="ipAddress">
                <el-input v-model="radarInfo.ipAddress"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="所属网关：" :inline="false" prop="gatewayId">
                <el-select v-model="radarInfo.gatewayId" placeholder="所属网关">
                  <el-option v-for=" (item) in radarParams.gatewayList" :label="item.gatewayName" :value="item.id"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="所属分路：" :inline="false">
                <el-select v-model="radarInfo.shunt" placeholder="请选择所属分路">
                  <el-option label="主路" :value=1></el-option>
                  <el-option label="支路" :value=2></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="分路描述：" :inline="false">
                <el-input v-model="radarInfo.shuntDescribe"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="探测距离：" :inline="false">
                <el-select v-model="radarInfo.detectionRange" placeholder="请选择探测距离">
                  <el-option label="50米" :value="'50米'"></el-option>
                  <el-option label="100米" :value="'100米'"></el-option>
                  <el-option label="150米" :value="'150米'"></el-option>
                  <el-option label="200米" :value="'200米'"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="安装时间：" :inline="false">
                <el-date-picker
                    v-model="radarInfo.installTime"
                    type="date"
                    placeholder="Pick a day"
                    size="default"
                />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="closeAddRadarDialog">取消</el-button>
            <el-button type="primary" @click="enterAddRadarDialog">
              确定
            </el-button>
          </div>
        </template>
      </el-dialog>

      <el-dialog v-model="viewRadarDialog" title="详情" style="padding: 35px">
        <el-form
            :model="radarInfo"
            label-width="100px"
            style="padding: 20px"
        >
          <el-row>
            <el-col :span="12">
              <el-form-item label="雷达名称：" :inline="false">
                {{radarInfo.radarName }}
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="编码：" :inline="false">
                {{radarInfo.radarCode }}
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="品牌：" :inline="false">
                {{radarInfo.radarBrand }}
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="型号：" :inline="false">
                {{radarInfo.radarModel }}
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="ip地址：" :inline="false">
                {{radarInfo.ipAddress }}
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="所属网关：" :inline="false">
                {{radarInfo.GateWay.gatewayName }}
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="所属分路：" :inline="false">
                <el-tag effect="dark">{{radarInfo.shunt===1?"主路":radarInfo.shunt===2?"支路":"未知"}}</el-tag>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="分路描述：" :inline="false">
                {{radarInfo.shuntDescribe }}
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="安装时间：" :inline="false">
                {{radarInfo.installTime }}
              </el-form-item>
            </el-col>
<!--            <el-col :span="12">-->
<!--              <el-form-item label="在线状态：" :inline="false">-->
<!--                <div class="onlinebox" :style="{'background': radarInfo.status===1 ? 'red':'green' }"></div>-->
<!--                <span :style="{ color: radarInfo.status === 1 ? 'red' : 'green' }">{{ radarInfo.status === 1 ? '离线' : '在线' }}</span>-->
<!--              </el-form-item>-->
<!--            </el-col>-->
            <el-col :span="12">
              <el-form-item label="探测距离：" :inline="false">
                {{radarInfo.detectionRange }}
              </el-form-item>
            </el-col>
          </el-row>
<!--          <el-row>-->
<!--            -->
<!--          </el-row>-->
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>
<script setup>
import {deleteBaseRadar, getRadarList, setRadarInfo, addRadar, getPublicGateway} from "@/api/radar";
import { ref,reactive} from 'vue'
import {ElMessage, ElMessageBox} from "element-plus";
const page = ref(1)
const total = ref(0)
const pageSize = ref(10)
const tableData = ref([])
const searchInfo = ref({})
const radarInfo = ref({})
const radarParams = reactive({
  gatewayList:[]
})
const radarForm = ref(null)
const dialogFlag = ref('add')
const isFlag = ref('')
const addRadarDialog = ref(false)
const viewRadarDialog = ref(false)

const rules = ref({
  radarName: [
    { required: true, message: '请输入名称',trigger: 'blur'},
    { min: 5, message: '最低5位字符', trigger: 'blur' }
  ],
  radarBrand: [
    { required: true, message: '请填写品牌',trigger: 'blur'},
  ],
  radarModel: [
    { required: true, message: '请填写型号',trigger: 'blur'},
  ],
  ipAddress: [
    { required: true,  message:'请输入ip地址',trigger: 'blur'},
    { pattern:/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/,message: '请输入正确的ip地址',trigger: 'blur'}
  ],
  gatewayId: [
    { required: true,  message:'请选择网关',trigger: 'blur'},
  ]
})

const getNowDay = ()=>{
  let timestamp = Date.now(); // 获取当前时间戳，单位是毫秒
  radarInfo.value.installTime = new Date(timestamp)
}

const getRandomCode = (prex)=>{
  let str1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
  let arr1 = str1.split("");
  radarInfo.value.radarCode = prex
  for(let i = 0; i < 10;i++){
    let n = Math.round(Math.random()*(arr1.length -1));
    radarInfo.value.radarCode += arr1[n]
  }
}

const tableRowClassName = ({row, rowIndex})=>{
  if ((rowIndex+1) % 2  === 0) {
    return 'success-row';
  }
  return '';
}
const getTableData = async() => {
  const table = await getRadarList({page: page.value, pageSize: pageSize.value,...searchInfo.value})
  if (table.code === 0) {
    tableData.value = table.data.list
    total.value = table.data.total
    page.value = table.data.page
    pageSize.value = table.data.pageSize
  }
}
const getRadarParms = async() => {
  const table = await getPublicGateway()
  if (table.code === 0) {
    radarParams.gatewayList = table.data.pubGateway
  }
}

getRadarParms()
getTableData()

const view = (row)=>{
  viewRadarDialog.value = true
  radarInfo.value = JSON.parse(JSON.stringify(row))
  radarInfo.value.installTime = new Date(radarInfo.value.installTime).toLocaleDateString()
}



const handleSizeChange = (val) => {
  pageSize.value = val
  getTableData()
}

const handleCurrentChange = (val) => {
  page.value = val
  getTableData()
}

const add = () => {
  radarInfo.value={GateWay:{},shunt:1,detectionRange:'50米'}
  dialogFlag.value = 'add'
  addRadarDialog.value = true
  getNowDay()
  getRandomCode("LD-")
}

const closeAddRadarDialog = () => {
  radarForm.value.resetFields()
  addRadarDialog.value = false
  isFlag.value=''
}

// 弹窗相关
const onSubmit = () => {
  page.value = 1
  pageSize.value = 10
  getTableData()
}
const onReset = () => {
  searchInfo.value = {}
}

const enterAddRadarDialog = async() => {
  radarForm.value.validate(async valid => {
    if (valid) {
      const req = {
        ...radarInfo.value
      }
      if (dialogFlag.value === 'add') {
        const res = await addRadar(req)
        if (res.code === 0) {
          ElMessage({ type: 'success', message: '创建成功' })
          await getTableData()
          closeAddRadarDialog()
        }
      }
      if (dialogFlag.value === 'edit') {
        const res = await setRadarInfo(req)
        if (res.code === 0) {
          ElMessage({ type: 'success', message: '编辑成功' })
          await getTableData()
          closeAddRadarDialog()
        }
      }
    }
  })
}

const editRadar = (row) => {
  dialogFlag.value = 'edit'
  radarInfo.value = JSON.parse(JSON.stringify(row))
  addRadarDialog.value = true
}
const deleteRadar = (obj) => {
  ElMessageBox.confirm('您确定要删除吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
      .then(async() => {
        console.log(obj)
        const res = await deleteBaseRadar({ ID:obj.ID })
        if (res.code === 0) {
          ElMessage({
            type: 'success',
            message: '删除成功!',
          })
          getTableData()
        }
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '已取消删除',
        })
      })
}

</script>
<style>
.el-table .success-row {
  background: #f3fdef;
}

.onlinebox{
  width: 12px; height: 12px;border-radius: 50%; display: inline-block; margin-right: 4px; position: relative; top: 1px;
}

.el-dialog__header{
  border-bottom: 1px solid #e8eaec;
}
.el-dialog__footer {
  border-top: 1px solid #e8eaec;
}
</style>
