<template>
    <div class="app-container">
       <TabTitle :text="nowText" />

       <el-card class="left-list" :style="{height:globalScreenHeight+'px'}">
        <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
          <el-form-item label="设备名称" prop="deviceName">
             <el-input
                v-model="queryParams.deviceName"
                placeholder="请输入设备名称"
                clearable
                style="width: 200px"
                @keyup.enter="handleQuery"
             />
          </el-form-item>
          <!-- <el-form-item label="设备是否有效" prop="isEffective">
             <el-select v-model="queryParams.isEffective" placeholder="请选择是否有效" clearable style="width: 200px">
                <el-option :label="'有效'" :value="'0'" />
                <el-option :label="'无效'" :value="'1'" />
             </el-select>
          </el-form-item> -->
          <el-form-item label="状态" prop="isOpen">
             <el-select v-model="queryParams.isOpen" placeholder="请选择状态" clearable style="width: 200px">
                <el-option :label="'运行中'" :value="'0'" />
                <el-option :label="'已禁用'" :value="'1'" />
             </el-select>
          </el-form-item>
          <el-form-item label="创建时间" >
            <el-date-picker
               v-model="dateRange"
               value-format="YYYY-MM-DD"
               type="daterange"
               range-separator="-"
               start-placeholder="开始日期"
               end-placeholder="结束日期"
            ></el-date-picker>
          </el-form-item>
          <el-form-item>
             <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
             <el-button icon="Refresh" @click="resetQuery">重置</el-button>
          </el-form-item>
       </el-form>
 
       <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
             <el-button
                type="primary"
                plain
                icon="Plus"
                @click="handleAdd"
             >新增</el-button>
          </el-col>
          <!-- <el-col :span="1.5">
             <el-button
                type="success"
                plain
                icon="Edit"
                :disabled="single"
                @click="handleUpdate"
                v-hasPermi="['monitor:job:edit']"
             >修改</el-button>
          </el-col>
          <el-col :span="1.5">
             <el-button
                type="danger"
                plain
                icon="Delete"
                :disabled="multiple"
                @click="handleDelete"
                v-hasPermi="['monitor:job:remove']"
             >删除</el-button>
          </el-col> -->
          <!-- <el-col :span="1.5">
             <el-button
                type="warning"
                plain
                icon="Download"
                @click="handleExport"
                v-hasPermi="['monitor:job:export']"
             >导入</el-button>
          </el-col> -->
          <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
       </el-row>
 
       <el-table v-loading="loading" :data="deviceList" @selection-change="handleSelectionChange">
          <el-table-column label="设备名称" width="200" align="center" prop="deviceName" :show-overflow-tooltip="true" />
          <el-table-column label="设备编码" align="center" prop="deviceNo" :show-overflow-tooltip="true" />
          <el-table-column label="设备视频流地址" align="center" prop="videoUrl" :show-overflow-tooltip="true"  />
          <el-table-column label="创建人" align="center" prop="createBy" :show-overflow-tooltip="true" />
          <el-table-column label="创建时间" align="center" prop="createTime" :show-overflow-tooltip="true" />
          <!-- <el-table-column label="设备是否有效" width="150" align="center" prop="isEffective" :show-overflow-tooltip="true" >
             <template #default="scope">
                <el-tag
                   :type="scope.row.isEffective === '0'?'success' : 'danger'"
                   effect="dark"
                >
                   {{scope.row.isEffective === '0'? '有效' : '无效'}}
                </el-tag>
             </template>
          </el-table-column> -->
          <el-table-column label="状态" width="150"  align="center" prop="isOpen" :show-overflow-tooltip="true" >
             <template #default="scope">
                <el-tag
                   :type="scope.row.isOpen === '0'?'success' : 'danger'"
                   effect="plain"
                >
                   {{scope.row.isOpen === '0'? '运行中' : '已禁用'}}
                </el-tag>
             </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
             <template #default="scope">
                <el-tooltip :content="scope.row.isOpen === '0'? '禁用' : '启用'" placement="top">
                   <el-button link type="primary" :icon="scope.row.isOpen === '0'? 'Close' : 'Check'" @click="handleStatusChange(scope.row)" v-hasPermi="['monitor:job:edit']"></el-button>
                </el-tooltip>
                <el-tooltip content="修改" placement="top">
                   <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['monitor:job:edit']"></el-button>
                </el-tooltip>
                <el-tooltip content="删除" placement="top">
                   <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['monitor:job:remove']"></el-button>
                </el-tooltip>
             </template>
          </el-table-column>
       </el-table>
 
       <pagination
          v-show="total > 0"
          :total="total"
          v-model:page="queryParams.pageNum"
          v-model:limit="queryParams.pageSize"
          @pagination="getList"
       />
       </el-card>
 
 
       <!-- 添加或修改定时任务对话框 -->
       <el-dialog :title="title" v-model="open" width="800px" append-to-body>
         <template v-slot:header>
            <div class="cleartitle" style="display: flex;justify-content: flex-start;align-items: center;">
               <img src="@/assets/images/logo_video.png" width="25px" alt=""> <span>{{title}}</span>
            </div>
         </template>
          <el-form ref="deviceRef" :model="form" :rules="rules" label-width="120px">
             <el-row>
                <el-col :span="24">
                   <el-form-item label="设备名称" prop="deviceName">
                      <el-input v-model="form.deviceName" placeholder="请输入设备名称" />
                   </el-form-item>
                </el-col>
                <el-col :span="24">
                   <el-form-item label="设备编码" prop="deviceNo">
                     <el-input v-model="form.deviceNo" placeholder="请输入设备编码" />
                   </el-form-item>
                </el-col>
                <el-col :span="24">
                   <el-form-item label="视频流类型" prop="videoUrlType">
                      <el-select v-model="form.videoUrlType" placeholder="请选择视频流类型" clearable >
                        <el-option :label="'RTSP类型'" :value="'rtsp'" />
                        <el-option :label="'RTMP类型'" :value="'rtmp'" />
                     </el-select>
                   </el-form-item>
                </el-col>
                <el-col :span="24">
                  <el-form-item label="视频流地址" prop="videoUrl">
                      <el-input v-model="form.videoUrl" placeholder="请输入视频流地址" />
                   </el-form-item>
                </el-col>
      
             </el-row>
          </el-form>
          <template #footer>
             <div class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
             </div>
          </template>
       </el-dialog>
    </div>
 </template>
 
 <script setup name="Job">
 import { listDevice, deleteDevice, addDevice, updateDevice, detailDevice,updateStatusDevice } from "@/api/yunshou/device";
 const router = useRouter();
 const { proxy } = getCurrentInstance();
 const { sys_job_group, sys_job_status } = proxy.useDict("sys_job_group", "sys_job_status");
 const globalScreenHeight = inject('globalScreenHeight');
 const nowText = ref("设备管理");
 const deviceList = ref([]);
 const open = ref(false);
 const loading = ref(true);
 const showSearch = ref(true);
 const ids = ref([]);
 const dateRange = ref([]);
 const single = ref(true);
 const multiple = ref(true);
 const total = ref(0);
 const title = ref("");
 
 const data = reactive({
   form: {},
   queryParams: {
     pageNum: 1,
     pageSize: 10,
     jobName: undefined,
     jobGroup: undefined,
     status: undefined
   },
   rules: {
      deviceName: [{ required: true, message: "设备名称不能为空", trigger: "blur" }],
      deviceNo: [{ required: true, message: "设备编码不能为空", trigger: "blur" }],
      videoUrl: [{ required: true, message: "视频流地址不能为空", trigger: "blur" }]
   }
 });
 
 const { queryParams, form, rules } = toRefs(data);
 
 /** 查询定时任务列表 */
 function getList() {
   loading.value = true;
   listDevice(proxy.addDateRange(queryParams.value, dateRange.value)).then(response => {
     deviceList.value = response.rows;
     total.value = response.total;
     loading.value = false;
   });
 }
 
 /** 任务组名字典翻译 */
 function jobGroupFormat(row, column) {
   return proxy.selectDictLabel(sys_job_group.value, row.jobGroup);
 }
 
 /** 取消按钮 */
 function cancel() {
   open.value = false;
   reset();
 }
 
 /** 表单重置 */
 function reset() {
   form.value = {
      videoUrlType: 'rtsp',
      videoUrl: undefined,
      deviceNo: undefined,
      deviceName: undefined,
      isOpen: '0',
      isEffective: "0"
   };
   proxy.resetForm("deviceRef");
 }
 
 /** 搜索按钮操作 */
 function handleQuery() {
   queryParams.value.pageNum = 1;
   getList();
 }
 
 /** 重置按钮操作 */
 function resetQuery() {
   proxy.resetForm("queryRef");
   handleQuery();
 }
 
 // 多选框选中数据
 function handleSelectionChange(selection) {
   ids.value = selection.map(item => item.id);
   single.value = selection.length != 1;
   multiple.value = !selection.length;
 }
 
 // 任务状态修改
 function handleStatusChange(row) {
   let text = row.isOpen === "0" ? "禁用" : "启用";
   proxy.$modal.confirm('确认要"' + text + '""' + row.deviceName + '"设备吗?').then(function () {
     return updateStatusDevice({id:row.id,status:row.isOpen === "0" ? "1" : "0"});
   }).then(() => {
      getList() 
     proxy.$modal.msgSuccess(text + "成功");
   }).catch(function () {
     row.status = row.status === "0" ? "1" : "0";
   });
 }

 /** 新增按钮操作 */
 function handleAdd() {
   reset();
   open.value = true;
   title.value = "添加设备";
 }
 
 /** 修改按钮操作 */
 function handleUpdate(row) {
   reset();
   const id = row.id || ids.value;
   detailDevice(id).then(response => {
     form.value = response.data;
     open.value = true;
     title.value = "修改设备";
   });
 }
 
 /** 提交按钮 */
 function submitForm() {
   proxy.$refs["deviceRef"].validate(valid => {
     if (valid) {
       if (form.value.id != undefined) {
         updateDevice(form.value).then(response => {
           proxy.$modal.msgSuccess("修改成功");
           open.value = false;
           getList();
         });
       } else {
         addDevice(form.value).then(response => {
           proxy.$modal.msgSuccess("新增成功");
           open.value = false;
           getList();
         });
       }
     }
   });
 }
 
 /** 删除按钮操作 */
 function handleDelete(row) {
   const ids = row.id || ids.value;
   proxy.$modal.confirm('是否确认删除定时任务编号为"' + ids + '"的数据项?').then(function () {
     return deleteDevice(ids);
   }).then(() => {
     getList();
     proxy.$modal.msgSuccess("删除成功");
   }).catch(() => {});
 }
 
 /** 导出按钮操作 */
 function handleExport() {
   proxy.download("monitor/job/export", {
     ...queryParams.value,
   }, `job_${new Date().getTime()}.xlsx`);
 }
 
 getList();
 </script>
 <style scoped lang="scss">
 .left-list{
  min-height: 700px;
 }
</style>

