<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form inline size="mini">
      <el-form-item label="消息类型：">
        <el-select v-model="form.messageType" placeholder="请选择消息类型" clearable>
          <el-option v-for="item in typeOptions" :key="item.value" :label="item.label" :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="推送状态：">
        <el-select v-model="form.status" placeholder="请选择推送状态" clearable>
          <el-option v-for="item in statusOptions" :key="item.value" :label="item.label"
            :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="推送平台：">
        <el-select v-model="form.systemType" placeholder="请选择推送平台" clearable>
          <el-option v-for="item in systemOptions" :key="item.value" :label="item.label"
            :value="item.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="推送内容：">
        <el-input v-model="form.content" placeholder="请输入推送内容" clearable />
      </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="getDialog()"
          v-hasPermi="['push:message:add']">新增</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
    <el-table :data="tableData">
      <el-table-column label="消息类型" prop="messageType" align="center" :formatter="typeFormatter" />
      <el-table-column label="推送标题" prop="title" align="center" />
      <el-table-column label="推送内容" prop="content" align="center" />
      <el-table-column label="推送日期" prop="pushTime" align="center" />
      <el-table-column label="推送状态" prop="status" align="center" :formatter="statusFormatter" />
      <el-table-column label="推送平台" prop="systemType" align="center" :formatter="systemFormatter" />
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="text" @click="getDialog(scope.row.id)" v-hasPermi="['push:message:edit']">编辑
          </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" />
    <!-- 弹窗 -->
    <el-dialog :visible.sync="dialogVisible" title="新增推送" width="500px" :before-close="cancel">
      <el-form label-width="auto" :model="dialogForm" :rules="rules" ref="dialogForm">
        <el-form-item label="推送类型：" prop="messageType">
          <el-select v-model="dialogForm.messageType" placeholder="请选择推送类型">
            <el-option v-for="item in typeOptions" :key="item.value" :label="item.label"
              :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="推送标题：" prop="title">
          <el-input v-model="dialogForm.title" placeholder="请输入推送标题" />
        </el-form-item>
        <el-form-item label="推送内容：" prop="content">
          <el-input v-model="dialogForm.content" type="textarea" rows="4" placeholder="请输入推送内容" />
        </el-form-item>
        <el-form-item v-if="dialogForm.messageType === 4" label="打开方式：" prop="jumpType">
          <el-select v-model="dialogForm.jumpType" placeholder="请选择打开方式">
            <el-option v-for="item in jumpOptions" :key="item.value" :label="item.label"
              :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="dialogForm.messageType === 4" label="参数：" prop="jumpContent">
          <el-input v-model="dialogForm.jumpContent" placeholder="请输入URL或App参数" />
        </el-form-item>
        <el-form-item label="推送日期：" prop="pushTime">
          <el-date-picker v-model="dialogForm.pushTime" type="datetime" placeholder="选择推送日期"
            value-format="yyyy-MM-dd HH:mm:ss" />
        </el-form-item>
        <el-form-item label="推送平台：" prop="systemType">
          <el-select v-model="dialogForm.systemType" placeholder="请选择推送平台">
            <el-option v-for="item in systemOptions" :key="item.value" :label="item.label"
              :value="item.value" />
          </el-select>
        </el-form-item>
        <el-form-item v-if="dialogForm.id">
          <el-button type="danger" @click="dialogForm.status = 4, getSubmit()">强制过期</el-button>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="cancel">取消</el-button>
        <el-button type="primary" @click="getSubmit">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { list, create, detail, edit } from '@/api/push/message'
export default {
  data() {
    return {
      form: {
        pageNum: 1,
        pageSize: 10
      },
      // 列表
      tableData: [],
      total: 0,
      // 弹窗
      dialogVisible: false,
      // 推送类型
      typeOptions: [{
        value: 1,
        label: '通知/活动'
      }, {
        value: 2,
        label: '通知/内容'
      }, {
        value: 3,
        label: '通知/系统'
      }, {
        value: 4,
        label: 'push'
      }],
      // 推送平台
      systemOptions: [{
        value: 1,
        label: '全部'
      }, {
        value: 2,
        label: 'Android'
      }, {
        value: 3,
        label: 'iOS'
      }],
      // 打开方式
      jumpOptions: [{
        value: 1,
        label: '指定页面'
      }, {
        value: 2,
        label: 'URL'
      }],
      // 推送状态
      statusOptions: [{
        value: 1,
        label: '未开始'
      }, {
        value: 2,
        label: '已推送'
      }, {
        value: 4,
        label: '已过期'
      }],
      // 表单
      title: '',
      dialogForm: {},
      // 校验
      rules: {
        messageType: [{
          required: true, message: '请选择推送类型', trigger: 'change'
        }],
        title: [{
          required: true, message: '请输入推送标题', trigger: 'blur'
        }],
        content: [{
          required: true, message: '请输入推送内容', trigger: 'blur'
        }],
        jumpType: [{
          required: true, message: '请选择打开方式', trigger: 'change'
        }],
        jumpContent: [{
          required: true, message: '请输入参数', trigger: 'blur'
        }],
        pushTime: [{
          required: true, message: '请选择推送日期', trigger: 'change'
        }],
        systemType: [{
          required: true, message: '请选择推送平台', trigger: 'change'
        }]
      }
    }
  },
  watch: {
    'dialogForm.messageType'(val) {
      if (val !== 4) {
        delete this.dialogForm.jumpType
        delete this.dialogForm.jumpContent
      }
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    // 列表
    getList() {
      list(this.form).then(res => {
        if (res.code === 0) {
          this.tableData = res.data.records
          this.total = res.data.total
        }
      })
    },

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

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

    // 详情
    getDialog(id) {
      this.dialogVisible = true
      if (id) {
        detail(id).then(res => {
          if (res.code === 0) {
            this.dialogForm = res.data
          }
        })
      }
    },

    // 推送
    getSubmit() {
      this.$refs.dialogForm.validate((valid) => {
        if (valid) {
          if (this.dialogForm.id) {
            edit(this.dialogForm).then(res => {
              if (res.code === 0) {
                this.$message.success('新增成功！')
                this.dialogVisible = false
                this.getList()
              }
            })
          } else {
            create(this.dialogForm).then(res => {
              if (res.code === 0) {
                this.$message.success('新增成功！')
                this.dialogVisible = false
                this.getList()
              }
            })
          }
        } else {
          return false
        }
      })
    },

    // 取消
    cancel() {
      this.dialogForm = {}
      this.$refs.dialogForm.resetFields()
      this.dialogVisible = false
    },

    // 字典翻译
    statusFormatter(row) {
      return this.selectDictLabel(this.statusOptions, row.status)
    },

    systemFormatter(row) {
      return this.selectDictLabel(this.systemOptions, row.systemType)
    },

    typeFormatter(row) {
      return this.selectDictLabel(this.typeOptions, row.messageType)
    }
  }
}
</script>