<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="screensName" label="名称" />
        <el-table-column prop="screensCode" label="编码">
          <template #default="scope">
            <el-tag effect="light">{{ scope.row.screensCode }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="screensBrand" label="品牌" />
        <el-table-column prop="screensModel" 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===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="editScreens(scope.row)"
            >编辑</el-button>
            <el-button
                type="primary"
                link
                icon="delete"
                @click="deleteScreens(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="addScreensDialog" title="LED显示屏" width="900">
        <el-form
            ref="screensForm"
            :rules="rules"
            :model="screensInfo"
            label-width="100px"
            style="padding: 15px"
        >
          <el-row>
            <el-col :span="12">
              <el-form-item label="屏幕名称：" :inline="false" prop="screensName">
                <el-input v-model="screensInfo.screensName"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="编码：" :inline="false">
                <el-input disabled v-model="screensInfo.screensCode"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="品牌：" :inline="false" prop="screensBrand">
                <el-input v-model="screensInfo.screensBrand"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="型号：" :inline="false" prop="screensModel">
                <el-input v-model="screensInfo.screensModel"></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="screensInfo.ipAddress"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="所属网关：" :inline="false" prop="gatewayId">
                <el-select v-model="screensInfo.gatewayId" placeholder="所属网关">
                  <el-option v-for=" (item) in screensParams.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="screensInfo.resolutionId" placeholder="请选择分辨率">
                  <el-option v-for=" (item) in screensParams.resolution" :label="item.price" :value="item.ID"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="屏幕尺寸：" :inline="false">
                <el-select v-model="screensInfo.screensSizeId" placeholder="请选择屏幕尺寸">
                  <el-option v-for=" (item) in screensParams.screensSize" :label="item.size" :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="screensInfo.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="screensInfo.shuntDescribe"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="安装时间：" :inline="false">
                <el-date-picker
                    v-model="screensInfo.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="closeAddScreensDialog">取消</el-button>
            <el-button type="primary" @click="enterAddScreensDialog">
              确定
            </el-button>
          </div>
        </template>
      </el-dialog>

      <el-dialog v-model="viewScreensDialog" title="详情" style="padding: 35px">
        <el-form
            :model="screensInfo"
            label-width="100px"
            style="padding: 20px"
        >
          <el-row>
            <el-col :span="12">
              <el-form-item label="屏幕名称：" :inline="false">
                {{screensInfo.screensName }}
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="编码：" :inline="false">
                {{screensInfo.screensCode }}
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="品牌：" :inline="false">
                {{screensInfo.screensBrand }}
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="型号：" :inline="false">
                {{screensInfo.screensModel }}
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="ip地址：" :inline="false">
                {{screensInfo.ipAddress }}
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="所属网关：" :inline="false">
                {{screensInfo.GateWay.gatewayName }}
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="分辨率：" :inline="false">
                {{screensInfo.Resolution.price }}
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="屏幕尺寸：" :inline="false">
                {{screensInfo.ScreensSize.size }}
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="所属分路：" :inline="false">
                <el-tag effect="dark">{{screensInfo.shunt===1?"主路":screensInfo.shunt===2?"支路":"未知"}}</el-tag>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="分路描述：" :inline="false">
                {{screensInfo.shuntDescribe }}
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="安装时间：" :inline="false">
                {{screensInfo.installTime }}
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="在线状态：" :inline="false">
                <div class="onlinebox" :style="{'background': screensInfo.status===0 ? 'red':'green' }"></div>
                <span :style="{ color: screensInfo.status === 0 ? 'red' : 'green' }">{{ screensInfo.status === 0 ? '离线' : '在线' }}</span>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="播放状态：" :inline="false">
                <div class="onlinebox" :style="{'background': screensInfo.playStatus===0 ? 'red':'green' }"></div>
                <span :style="{ color: screensInfo.playStatus === 0 ? 'red' : 'green' }">{{ screensInfo.playStatus === 0 ? '异常' : '正常' }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="模板状态：" :inline="false">
                <div class="onlinebox" :style="{'background': screensInfo.displayStatus===0 ? 'red':'green' }"></div>
                <span :style="{ color: screensInfo.displayStatus === 0 ? 'red' : 'green' }">{{ screensInfo.displayStatus === 0 ? '异常' : '正常' }}</span>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="素材状态：" :inline="false">
                <div class="onlinebox" :style="{'background': screensInfo.sourceStatus===0 ? 'red':'green' }"></div>
                <span :style="{ color: screensInfo.sourceStatus === 0 ? 'red' : 'green' }">{{ screensInfo.sourceStatus === 0 ? '异常' : '正常' }}</span>
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
      </el-dialog>
    </div>
  </div>
</template>
<script setup>
import {deleteBaseScreens, getScreensList, queryscreensParms,setScreensInfo,addScreens} from "@/api/screens";
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 screensInfo = ref({})
const screensParams = reactive({
  resolution:[],
  screensSize:[],
  gatewayList:[]
})
const screensForm = ref(null)
const dialogFlag = ref('add')
const isFlag = ref('')
const addScreensDialog = ref(false)
const viewScreensDialog = ref(false)

const rules = ref({
  screensName: [
    { required: true, message: '请输入名称',trigger: 'blur'},
    { min: 5, message: '最低5位字符', trigger: 'blur' }
  ],
  screensBrand: [
    { required: true, message: '请填写品牌',trigger: 'blur'},
  ],
  screensModel: [
    { 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(); // 获取当前时间戳，单位是毫秒
  screensInfo.value.installTime = new Date(timestamp)
}

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

const tableRowClassName = ({row, rowIndex})=>{
  if ((rowIndex+1) % 2  === 0) {
    return 'success-row';
  }
  return '';
}
const getTableData = async() => {
  const table = await getScreensList({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 getScreensParms = async() => {
  const table = await queryscreensParms()
  if (table.code === 0) {
    screensParams.screensSize = table.data.screensSize
    screensParams.resolution = table.data.resolution
    screensParams.gatewayList = table.data.pubGateway
  }
}

getScreensParms()
getTableData()

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



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

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

const add = () => {
  screensInfo.value={GateWay:{},Resolution:{},ScreensSize:{},resolutionId:1,screensSizeId:1,shunt:1}
  dialogFlag.value = 'add'
  addScreensDialog.value = true
  getNowDay()
  getRandomCode("XSP-")
}

const closeAddScreensDialog = () => {
  screensForm.value.resetFields()
  addScreensDialog.value = false
  isFlag.value=''
}

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

const enterAddScreensDialog = async() => {
  screensForm.value.validate(async valid => {
    if (valid) {
      const req = {
        ...screensInfo.value
      }
      if (dialogFlag.value === 'add') {
        const res = await addScreens(req)
        if (res.code === 0) {
          ElMessage({ type: 'success', message: '创建成功' })
          await getTableData()
          closeAddScreensDialog()
        }
      }
      if (dialogFlag.value === 'edit') {
        const res = await setScreensInfo(req)
        if (res.code === 0) {
          ElMessage({ type: 'success', message: '编辑成功' })
          await getTableData()
          closeAddScreensDialog()
        }
      }
    }
  })
}

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