<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="'十字路口'"></el-option>
            <el-option label="T型路口" :value="'T型路口'"></el-option>
            <el-option label="弯道" :value="'弯道'"></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="intersectName" label="名称" />
        <el-table-column prop="intersectType" label="类型"/>
        <el-table-column prop="address" label="所在地址"/>
        <el-table-column prop="status" label="运行状态">
          <template #default="scope">
            <div class="onlinebox" :style="{'background': scope.row.status===0 ? 'red':'green' }"></div>
            <span :style="{ color: scope.row.status === 0 ? 'red' : 'green' }">{{ scope.row.status === 0 ? '异常' : '正常' }}</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="editIntersect(scope.row)"
            >编辑</el-button>
            <el-button
                type="primary"
                link
                icon="delete"
                @click="deleteIntersect(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="addIntersectDialog" title="摄像头" width="900">
        <el-form
            ref="intersectForm"
            :rules="rules"
            :model="intersectInfo"
            label-width="110px"
            style="padding: 15px"
        >
          <el-row>
            <el-col :span="24">
              <el-form-item label="路口名称：" :inline="false" prop="intersectName">
                <el-input v-model="intersectInfo.intersectName"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="路口类型：" :inline="false" prop="intersectType">
                <el-select v-model="intersectInfo.intersectType" placeholder="路口类型">
                  <el-option label="十字路口" :value="'十字路口'"></el-option>
                  <el-option label="T型路口" :value="'T型路口'"></el-option>
                  <el-option label="弯道" :value="'弯道'"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="路口地址：" :inline="false">
                <el-input type="textarea" v-model="intersectInfo.address"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button @click="closeAddIntersectDialog">取消</el-button>
            <el-button type="primary" @click="enterAddIntersectDialog">
              确定
            </el-button>
          </div>
        </template>
      </el-dialog>

      <el-dialog v-model="viewIntersectDialog" title="详情" style="padding: 35px">
        <el-form
            :model="intersectInfo"
            label-width="100px"
            style="padding: 20px"
        >
          <el-row>
            <el-col :span="12">
              <el-form-item label="路口名称：" :inline="false">
                {{intersectInfo.intersectName }}
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="路口类型：" :inline="false">
                {{intersectInfo.intersectType }}
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="安装时间：" :inline="false">
                {{intersectInfo.createTime }}
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="在线状态：" :inline="false">
                <div class="onlinebox" :style="{'background': intersectInfo.status===0 ? 'red':'green' }"></div>
                <span :style="{ color: intersectInfo.status === 0 ? 'red' : 'green' }">{{ intersectInfo.status === 0 ? '异常' : '正常' }}</span>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="所在地址：" :inline="false">
                {{intersectInfo.address }}
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>
<script setup>
import {deleteBaseIntersect, getIntersectList,setIntersectInfo,addIntersect} from "@/api/intersect";
import { ref} 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 intersectInfo = ref({})
const intersectForm = ref(null)
const dialogFlag = ref('add')
const isFlag = ref('')
const addIntersectDialog = ref(false)
const viewIntersectDialog = ref(false)

const rules = ref({
  intersectName: [
    { required: true, message: '请输入名称',trigger: 'blur'}
  ],
  intersectType: [
    {required: true, message: '请选择类型',trigger: 'blur' }
  ]
})
const tableRowClassName = ({row, rowIndex})=>{
  if ((rowIndex+1) % 2  === 0) {
    return 'success-row';
  }
  return '';
}
const getTableData = async() => {
  const table = await getIntersectList({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
  }
}
getTableData()

const view = (row)=>{
  viewIntersectDialog.value = true
  intersectInfo.value = JSON.parse(JSON.stringify(row))
  intersectInfo.value.createTime = new Date(intersectInfo.value.createTime).toLocaleDateString()
}



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

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

const add = () => {
  intersectInfo.value={}
  dialogFlag.value = 'add'
  addIntersectDialog.value = true
}

const closeAddIntersectDialog = () => {
  intersectForm.value.resetFields()
  addIntersectDialog.value = false
  isFlag.value=''
}

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

const enterAddIntersectDialog = async() => {
  intersectForm.value.validate(async valid => {
    if (valid) {
      const req = {
        ...intersectInfo.value
      }
      if (dialogFlag.value === 'add') {
        const res = await addIntersect(req)
        if (res.code === 0) {
          ElMessage({type: 'success', message: '创建成功'})
          await getTableData()
          closeAddIntersectDialog()
        }
      }
      if (dialogFlag.value === 'edit') {
        const res = await setIntersectInfo(req)
        if (res.code === 0) {
          ElMessage({type: 'success', message: '编辑成功'})
          await getTableData()
          closeAddIntersectDialog()
        }
      }
    }
  })
}

const editIntersect = (row) => {
  dialogFlag.value = 'edit'
  intersectInfo.value = JSON.parse(JSON.stringify(row))
  addIntersectDialog.value = true
}
const deleteIntersect = (obj) => {
  ElMessageBox.confirm('您确定要删除吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  })
      .then(async () => {
        const res = await deleteBaseIntersect({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>
