<template>
  <section class="AppTvMsg">
    <ai-list>
      <ai-title slot="title" title="极光推送"/>
      <template #content>
        <ai-search-bar class="mar-t8">
          <template #left>
            <el-button type="primary" icon="iconfont iconAdd" @click="dialog=true">添加</el-button>
            <el-button icon="iconfont iconDelete" :disabled="!search.ids" @click="handleDelete(search.ids)">删除</el-button>
          </template>
          <template #right>
            <ai-select v-model="search.type" :selectList="typeList" placeholder="请选择推送分类" @change="page.current=1,getTableData()"/>
          </template>
        </ai-search-bar>
        <ai-table :tableData="tableData" :total="page.total" :current.sync="page.current" :size.sync="page.size" @getList="getTableData"
          :col-configs="colConfigs" :dict="dict" @selection-change="v=>search.ids=v.map(e=>e.id).toString()">
          <el-table-column slot="type" label="推送分类"  align="center" width="200">
            <template slot-scope="{row}">
              <span v-if="row.type == 15" style="color:#42D784;">核酸监测通知</span>
              <span v-else style="color:#4E8EEE;">主动报备提醒</span>
            </template>
          </el-table-column>
          <el-table-column slot="options" label="操作" align="center" width="200">
            <template slot-scope="{row}">
              <el-button type="text" @click="handleDelete(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </ai-table>
      </template>

    </ai-list>
    <ai-dialog title="推送信息" :visible.sync="dialog" @closed="formInit" @onConfirm="submit" width="600px">
        <el-form :model="form" size="small" ref="DialogForm" :rules="rules" label-width="80px">
          <el-form-item label="推送分类" prop="type">
            <ai-select v-model="form.type" :selectList="typeList" placeholder="请选择推送分类" @change="changeType"/>
          </el-form-item>
          <el-form-item label="消息内容" prop="msgContent">
            <el-input type="textarea" :rows="2" placeholder="请输入消息内容" v-model="form.msgContent" maxlength="200" show-word-limit></el-input>
          </el-form-item>
          <template v-if="form.type == 15">
            <el-form-item label="时间" prop="msgTime">
              <el-input type="text" v-model="form.msgTime" maxlength="30" />
            </el-form-item>
            <el-form-item label="地点" prop="msgPlace">
              <el-input type="text" v-model="form.msgPlace" maxlength="100" />
            </el-form-item>
            <el-form-item label="按钮标题" prop="msgBtnText">
              <el-input type="text" v-model="form.msgBtnText" maxlength="20" />
            </el-form-item>
          </template>
        </el-form>
      </ai-dialog>
  </section>
</template>

<script>

export default {
  name: "AppTvMsg",
  label: "极光推送",
  props: {
    instance: Function,
    dict: Object,
    permissions: Function
  },
  computed: {
    colConfigs() {
      return [
        {type: 'selection'},
        {slot: "type"},
        {label: "消息内容", prop: "content", align:'center'},
        {slot: "options"}
      ]
    }
  },
  data() {
    return {
      page: {current: 1, size: 10, total: 0},
      tableData: [],
      dialog: false,
      form: {
        type: '',
        msgContent: '',
        msgTime: '',
        msgPlace: '',
        msgBtnText: '',
      },
      rules: {
        type: {required: true, message: "请选择推送分类"},
        msgContent: {required: true, message: "请输入消息内容"},
        msgTime: {required: true, message: "请选择时间"},
        msgPlace: {required: true, message: "请输入地点"},
        msgBtnText: {required: true, message: "请输入按钮标题"},
      },
      typeList: [{dictName: '核酸监测通知', dictValue: '15' }, {dictName: '主动报备提醒', dictValue: '10' }],
      search: {ids: [], type: ''}
    }
  },
  methods: {
    changeType() {
      this.$refs['DialogForm'].clearValidate()
    },
    getTableData() {
      this.instance.post("/app/appiptvjpush/list", null, {
        params: {...this.page, ...this.search}
      }).then(res => {
        if (res?.data) {
          this.tableData = res.data?.records
          this.page.total = res.data.total
        }
      })
    },
    handleDelete(ids) {
      this.$confirm("是否要删除推送消息?").then(() => {
        this.instance.post("/app/appiptvjpush/delete", null, {
          params: {ids}
        }).then(res => {
          if (res?.code == 0) {
            this.$message.success("删除成功!")
            this.getTableData()
          }
        })
      }).catch(() => 0)
    },
    submit() {
      this.$refs.DialogForm.validate(v => {
        if (v) {
          let loading = this.$loading({text: "提交中..."})
          this.instance.post("/app/appiptvjpush/addOrUpdate", this.form).then(res => {
            loading.close()
            if (res?.code == 0) {
              this.$message.success("提交成功!")
              this.dialog = false
              this.page.current = 1
              this.getTableData()
            }
          }).catch(() => loading.close())
        }
      })
    },
    formInit() {
      for(let key in this.form) {
        this.form[key] = ''
      }
    }
  },
  created() {
    this.getTableData()
  }
}
</script>

<style lang="scss" scoped>
.AppTvMsg {
  height: 100%;

  :deep(.mar-t16 ){
    margin-top: 16px;
  }

  :deep(.mar-t8 ){
    margin-top: 8px;
  }
}
</style>
