<template>
  <div class="ele-body">
    <el-card shadow="never" v-loading="loading">
      <PollingRulesSearch @search="handleQuery" @reset="reload"> </PollingRulesSearch>
      <!-- 数据表格 -->
      <ele-pro-table
        ref="table"
        :pageSizes="tablePageSizes"
        :columns="columns"
        :datasource="datasource"
        :selection.sync="selection"
        :cache-key="cacheKeyUrl"
      >
        <!-- 表头工具栏 -->
        <template v-slot:toolbar>
          <el-button
            size="small"
            type="primary"
            icon="el-icon-plus"
            class="ele-btn-icon"
            @click="openForm('create')"
          >
            新建
          </el-button>
          <el-button
            size="small"
            type="danger"
            icon="el-icon-delete"
            class="ele-btn-icon"
            v-if="selection.length"
            @click="handleBatchDelete"
          >
            批量删除
          </el-button>
        </template>

        <!-- 是否重复列 -->
        <template v-slot:repeated="{ row }">
          <el-switch
            v-model="row.repeated"
            @change="handleRepeatedChange(row)"
          />
        </template>

        <!-- 操作列 -->
        <template v-slot:action="{ row }">
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-view"
            @click="openDetailForm(row.id)"
          >
            详情
          </el-link>
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-edit"
            @click="openForm('update', row.id)"
          >
            编辑
          </el-link>
          <el-link
            type="primary"
            :underline="false"
            icon="el-icon-delete"
            :style="{ color: delColor }"
            @click="handleDelete(row.id)"
          >
            删除
          </el-link>
        </template>
      </ele-pro-table>
    </el-card>

    <!-- 表单弹窗：添加/修改 -->
    <VideoPolingForm ref="formRef" @success="reload" />
    <!-- 详情弹窗 -->
    <Detail ref="detailRef" />
  </div>
</template>

<script>
import PollingRulesSearch from './components/PollingRulesSearch.vue'
import VideoPolingForm from './components/videoPolingForm.vue'
import Detail from './components/detail.vue'

import * as VideoPollingApi from '@/api/config/videoPolling'

export default {
  name: 'VideoPolling',
  components: {
    PollingRulesSearch,
    VideoPolingForm,
    Detail
  },
  data() {
    return {
      cacheKeyUrl: 'videoPollingTable',
      delColor: '#F56C6C',
      selection: [],
      tablePageSizes: [10, 20, 50, 100],
      // 表格列配置
      columns: [
        {
          width: 45,
          type: 'selection',
          columnKey: 'selection',
          align: 'center',
          fixed: 'left'
        },
        {
          columnKey: 'index',
          label: '序号',
          type: 'index',
          width: 55,
          align: 'center',
          showOverflowTooltip: true,
          fixed: 'left'
        },
        {
          prop: 'ruleCode',
          label: '规则编码',
          headerAlign: 'center',
          align: 'center',
          showOverflowTooltip: true,
         
        },
        {
          prop: 'ruleName',
          label: '规则名称',
          headerAlign: 'center',
          align: 'center',
          showOverflowTooltip: true,

        },
        {
          prop: 'forInterval',
          label: '时间间隔',
          align: 'center',
          showOverflowTooltip: true,
          width: 150
        },
        {
          prop: 'repeated',
          label: '是否重复',
          align: 'center',
          showOverflowTooltip: true,
          width: 100,
          slot: 'repeated'
        },
        {
          prop: 'indexNo',
          label: '排序号',
          align: 'center',
          showOverflowTooltip: true,
          width: 150
        },
        {
          columnKey: 'action',
          fixed: 'right',
          label: '操作',
          width: 220,
          align: 'center',
          resizable: false,
          slot: 'action',
          showOverflowTooltip: true
        }
      ],
      loading: false
    }
  },
  methods: {
    /* 表格数据源 */
    datasource({ page, limit, where, order }) {
      return VideoPollingApi.getVideoRulePage({
        pageNo: page,
        pageSize: limit,
        ...where
      })
    },
    /* 刷新表格 */
    reload(where) {
      this.$refs.table.reload({ page: 1, where })
    },

    /* 搜索 */
    handleQuery(where) {
      this.reload(where)
    },

    /** 添加/修改操作 */
    openForm(type, id) {
      this.$refs.formRef.openForm(type, id)
    },

    /** 详情 */
    openDetailForm(id) {
      this.$refs.detailRef.openForm(id)
    },

    /** 删除按钮操作 */
    handleDelete(id) {
      this.$confirm('是否确认删除数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await VideoPollingApi.deleteVideoRule(id)
          this.$message.success('删除成功')
          this.reload()
        })
        .catch(() => {})
    },

    /** 批量删除按钮操作 */
    handleBatchDelete() {
      if (this.selection.length == 0) {
        this.$message.warning('请选择要删除的数据项')
        return
      }
      this.$confirm('是否确认删除选中的数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          const ids = this.selection.map((item) => item.id)
          // 循环删除
          for (const id of ids) {
            await VideoPollingApi.deleteVideoRule(id)
          }
          this.$message.success('删除成功')
          this.reload()
        })
        .catch(() => {})
    },

    /** 是否重复切换 */
    handleRepeatedChange(row) {
      const text = row.repeated ? '启用重复' : '取消重复'
      this.$confirm('确认要' + text + '?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await VideoPollingApi.updateVideoRule({ ...row, repeated: row.repeated })
          this.$message.success(text + '成功')
          this.reload()
        })
        .catch(() => {
          row.repeated = !row.repeated
        })
    }
  }
}
</script>

<style lang="scss" scoped></style>
