<template>
  <div class='app-container'>
    <!-- 搜索 -->
    <el-form inline size="mini">
      <el-form-item label="弹窗名称：">
        <el-input v-model="form.name" placeholder="请输入弹窗名称" clearable />
      </el-form-item>
      <el-form-item label="弹窗类型：">
        <el-select v-model="form.type" placeholder="请选择弹窗类型" clearable>
          <el-option v-for="item in dialogTypeOptions" :key="item.value" :value="item.value" :label="item.label" />
        </el-select>
      </el-form-item>
      <el-form-item label="当前状态：">
        <el-select v-model="form.effectiveState" placeholder="请选择当前状态" clearable>
          <el-option v-for="item in currentOptions" :key="item.value" :value="item.value" :label="item.label" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间：">
        <el-date-picker v-model="form.listDate" type="datetimerange" start-placeholder="开始日期" end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="getSearch">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="getRefresh">重置</el-button>
        <el-button type="primary" icon="el-icon-plus" plain @click="getDetail()"
          v-hasPermi="['push:dialog:add']">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table :data="tableData" v-loading="loading">
      <el-table-column label="序号" type="index" align="center" />
      <el-table-column label="弹窗ID" prop="id" align="center" show-overflow-tooltip />
      <el-table-column label="弹窗名称" prop="name" align="center" show-overflow-tooltip />
      <el-table-column label="弹窗图片" align="center" width="100px">
        <template slot-scope="scope">
          <el-image :src="scope.row.pic" />
        </template>
      </el-table-column>
      <el-table-column label="弹窗类型" prop="type" align="center" :formatter="typeFormatter" />
      <el-table-column label="优先级" prop="level" align="center" />
      <el-table-column label="创建时间" prop="createTime" align="center" show-overflow-tooltip />
      <el-table-column label="生效时间" align="center" width="310px">
        <template slot-scope="scope">
          <span>{{ scope.row.startTime }} 至 {{ scope.row.endTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="当前状态" prop="effectiveState" align="center" :formatter="effectiveFormatter" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="getDetail(scope.row.id, true)">查看</el-button>
          <el-button type="text" @click="getDetail(scope.row.id)" v-hasPermi="['push:dialog:edit']">编辑</el-button>
          <el-button type="delete" @click="getDelete(scope.row)" v-hasPermi="['push:dialog:delete']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total > 0" :total="total" :page.sync="form.pageNum" :limit.sync="form.pageSize"
      @pagination="getList" />
  </div>
</template>

<script>
import { list, remove } from '@/api/push/dialog'
import { currentMixin } from '@/mixin/index'
export default {
  mixins: [currentMixin],
  data() {
    return {
      // 遮罩层
      loading: false,
      // 表单
      form: {
        pageNum: 1,
        pageSize: 10
      },
      // 列表
      tableData: [],
      // 总数据
      total: 0,
      // 弹窗类型
      dialogTypeOptions: [{
        value: 0,
        label: '当天弹窗'
      }, {
        value: 1,
        label: '启动弹窗'
      }, {
        value: 2,
        label: '新用户弹窗'
      }]
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 列表
    getList() {
      this.loading = true
      list(this.form).then(res => {
        if (res.code === 0) {
          this.tableData = res.data.records
          this.total = res.data.total
          this.loading = false
        }
      })
    },

    // 搜索
    getSearch() {
      this.form.pageNum = 1
      this.getList()
    },

    // 重置
    getRefresh() {
      this.form = {
        pageNum: 1,
        pageSize: 10
      }
      this.getList()
    },

    // 详情
    getDetail(id, boolean) {
      this.$router.push({
        path: `/push/pushDialog/detail`,
        query: {
          id: id,
          boolean: boolean
        }
      })
    },

    // 删除
    getDelete(row) {
      this.$confirm(`是否删除${row.name}?`, '提示', {
        type: 'warning'
      }).then(() => {
        remove(row.id, 2).then(res => {
          if (res.code === 0) {
            this.$message.success(`删除成功！`)
            this.getList()
          }
        })
      }).catch(() => { })
    },

    // 字典翻译
    effectiveFormatter(row) {
      return this.selectDictLabel(this.currentOptions, row.effectiveState)
    },

    typeFormatter(row) {
      return this.selectDictLabel(this.dialogTypeOptions, row.type)
    }
  }
}
</script>