<template>
  <ai-list class="notice">
    <template slot="title">
      <ai-title title="小程序公告" isShowBottomBorder></ai-title>
    </template>
    <template slot="content">
      <ai-search-bar class="search-bar">
        <template #left>
          <el-button size="small" type="primary" icon="iconfont iconAdd" @click="toAdd('')">添加</el-button>
        </template>
      </ai-search-bar>
      <ai-table
        :tableData="tableData"
        :col-configs="colConfigs"
        :total="total"
        style="margin-top: 6px;"
        :current.sync="search.current"
        :size.sync="search.size"
        @getList="getList">
        <el-table-column slot="tags" label="标签">
          <template slot-scope="{ row }">
            <div class="table-tags">
              <el-tag type="info" v-for="(item, index) in row.tags" size="small" :key="index">{{ item }}</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column slot="options" width="180px" fixed="right" label="操作" align="center">
          <template slot-scope="{ row }">
            <div class="table-options">
              <el-popover
                  placement="bottom"
                  width="160"
                  :visible-arrow="false"
                  popper-class="wechat-message__container"
                  trigger="hover">
                <el-button type="text" slot="reference">二维码</el-button>
                <div style="font-size: 0;">
                  <img class="message-info__img" :src="row.codeUrl">
                </div>
              </el-popover>
              <el-button type="text" @click="toAdd(row.id)">编辑</el-button>
              <el-button type="text" @click="remove(row.id)">删除</el-button>
            </div>
          </template>
        </el-table-column>
      </ai-table>
    </template>
  </ai-list>
</template>

<script>
  import { mapState } from 'vuex'
  export default {
    name: 'List',

    props: {
      instance: Function,
      dict: Object
    },

    data() {
      return {
        search: {
          current: 1,
          size: 10,
          status: 0,
          title: ''
        },
        currIndex: -1,
        areaList: [],
        total: 10,
        colConfigs: [
          {prop: 'codeName', label: '名称', align: 'left'},
          {prop: 'type', label: '二维码类型', align: 'left', formart: v => v === '0' ? '群二维码' : '个人二维码'},
          {prop: 'createUserName', label: '创建人'},
          {prop: 'createTime', label: '创建时间'},
          {slot: 'options', label: '操作'}
        ],
        areaName: '',
        unitName: '',
        tableData: []
      }
    },

    computed: {
      ...mapState(['user'])
    },

    mounted() {
      this.getList()
    },

    methods: {
      getList() {
        this.instance.post(`/app/appeveryvillagecode/list`, null, {
          params: {
            ...this.search
          }
        }).then(res => {
          if (res.code == 0) {
            this.tableData = res.data.records
            this.total = res.data.total
          }
        })
      },

      remove(id) {
        this.$confirm('确定删除该数据？').then(() => {
          this.instance.post(`/app/appeveryvillagecode/delete?ids=${id}`).then(res => {
            if (res.code == 0) {
              this.$message.success('删除成功！')
              this.getList()
            }
          })
        })
      },

      toAdd(id) {
        this.$emit('change', {
          type: 'Add',
          params: {
            id: id || ''
          }
        })
      }
    }
  }
</script>

<style lang="scss" scoped>
  .notice {
  }
</style>
