<template>
  <div class="app-container">
    <div class="Pd20">
      <el-row type="flex" :gutter="10">
        <el-col :span="6">
          <el-button type="primary" plain size="mini" @click="append">
            <i class="el-icon-plus el-icon--left"></i>
            创建视频分析任务
          </el-button>
        </el-col>
        <el-col :span="6"> </el-col>
        <el-col :span="6"> </el-col>
        <el-col :span="6" :offset="6">
          <el-input
            v-model="searchValue"
            size="mini"
            style="width: 160px"
            clearable
            placeholder="请输入内容"
          ></el-input>
          <el-button
            type="primary"
            size="mini"
            @click="search"
            style="margin-left: 10px"
          >
            <i class="el-icon-search el-icon--left"></i>
            搜索
          </el-button>
        </el-col>
      </el-row>
      <el-table :data="tableList" class="Mt20">
        <el-table-column
          label="任务ID"
          align="center"
          prop="taskId"
        ></el-table-column>
        <el-table-column
          label="任务名称"
          align="center"
          prop="taskName"
        ></el-table-column>
        <el-table-column label="关联算法" align="center">
          <template slot-scope="scope">
            <span>
              {{ scope.row.algorithmNames | ftNames }}
            </span>
          </template>
        </el-table-column>
        <el-table-column label="任务启用状态" align="center">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.taskStatus"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-value="1"
              inactive-value="0"
              @change="(value) => handleSwitchChange(scope.row, value)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="mini"
              @click="() => editBut(scope.row)"
            >
              编辑
            </el-button>
            <el-button
              type="text"
              class="danger"
              size="mini"
              @click="() => deleteBut(scope.row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
      <el-drawer :visible.sync="drawer" size="80%" :wrapperClosable="false">
        <span slot="title">创建视频分析任务</span>
        <add-video-anlysis-tasks
          :editData="currentRow"
          @close-drawer="closeDrawer()"
        ></add-video-anlysis-tasks>
      </el-drawer>
    </div>
  </div>
</template>

<script>
import addVideoAnlysisTasks from "./modules/addVideoAnlysisTasks.vue";
import {
  getVideoAnalysisTaskList,
  addVideoAnalysisTask,
  updateVideoAnalysisTask,
  delVideoAnalysisTask,
} from "@/api/business/videoanalysistasks.js";
export default {
  name: "VideoAnalysisTasks",
  props: {},
  components: { "add-video-anlysis-tasks": addVideoAnlysisTasks },
  data() {
    return {
      searchValue: "", // 搜索框的值
      tableList: [],
      queryParams: {
        pageNum: 1, // 当前页码
        pageSize: 10, // 每页显示的数量
      },
      total: 0, // 总数量
      /* 创建摄像头相关 */
      drawer: false, // 控制弹窗的显示
      currentRow: null, // 当前行数据存储
    };
  },
  computed: {},
  watch: {},
  created() {
    this.getList();
  },
  mounted() {},
  filters: {
    // 过滤算法名称
    ftNames(algorithmNames) {
      if (algorithmNames) {
        return algorithmNames.join(", "); // 将数组转换为逗号分隔的字符串
      } else {
        return "无关联算法"; // 如果数组为空，返回空字符串
      }
    },
  },
  methods: {
    // 新增
    append() {
      this.currentRow = null; // 存储当前行数据
      this.drawer = true; // 打开抽屉
    },
    // 搜索
    search() {},
    // 分页
    getList() {
      getVideoAnalysisTaskList(this.queryParams).then((res) => {
        if (res.code !== 200) {
          this.$modal.msgError(res.msg);
          return;
        }
        this.tableList = res.rows; // 表格数据
        this.total = res.total; // 总数量
      });
    },
    // 删除
    deleteBut(row) {
      delVideoAnalysisTask(row.taskId).then((res) => {
        if (res.code !== 200) {
          this.$modal.msgError(res.msg);
          return;
        } else {
          this.$modal.msgSuccess(res.msg);
          this.getList(); // 重新获取列表数据
        }
      });
    },
    // 编辑
    editBut(row) {
      if (row.taskStatus == 1) {
        this.$modal
          .confirm("启用状态下无法编辑，是否关闭任务？")
          .then((res) => {
            updateVideoAnalysisTask({ taskId: row.taskId, taskStatus: 0 }).then(
              (res) => {
                if (res.code !== 200) {
                  this.$modal.msgError(res.msg);
                  return;
                } else {
                  this.currentRow = row; // 存储当前行数据
                  this.$nextTick(() => {
                    this.drawer = true;
                  });
                }
              }
            ); // 切换状态为禁用
          });
      } else {
        this.currentRow = row; // 存储当前行数据
        this.drawer = true; // 打开抽屉
      }
    },
    // 关闭抽屉
    closeDrawer() {
      this.drawer = false; // 关闭抽屉
      //需要重新获取列表数据
      this.getList();
    },
    // 切换状态
    handleSwitchChange(row, value) {
      updateVideoAnalysisTask({ taskId: row.taskId, taskStatus: value }).then(
        (res) => {
          if (res.code !== 200) {
            this.$modal.msgError(res.msg);
            return;
          } else {
            this.$modal.msgSuccess(res.msg);
            this.getList(); // 重新获取列表数据
          }
        }
      );
    },
  },
  // 在beforeDestroy生命周期添加
  beforeDestroy() {
    // 清理图表实例
    this.chartInstance?.dispose();
    // 释放图片资源
    document.querySelectorAll(".content-top, .content-bottom").forEach((el) => {
      el.style.backgroundImage = "none";
    });
  },
};
</script>

<style scoped lang="scss">
.Pd20 {
  padding: 20px;
  background: #fff;
  position: relative;
  height: calc(100vh - 125px);
}
.Mt20 {
  margin-top: 20px;
}
.danger {
  color: #f5222d;
}
.active {
  color: #52c41a;
}
::v-deep .el-drawer__header {
  margin-bottom: 0;
}
.content-top {
  background: url("../../assets/images/contenttop.png") no-repeat center;
  will-change: transform; // 提示浏览器优化
}
.chart-box {
  transform: translateZ(0);
}
</style>
