<template>
  <ai-list class="appform">
    <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>
        <template slot="right">
          <el-input
            v-model="search.applicationName"
            size="small"
            @keyup.enter.native="search.current = 1, getList()"
            placeholder="请输入菜单名称"
            clearable
            @clear="search.current = 1, search.applicationName = '', getList()"
            suffix-icon="iconfont iconSearch">
          </el-input>
        </template>
      </ai-search-bar>
      <ai-table
        :tableData="tableData"
        :col-configs="colConfigs"
        :total="total"
        v-loading="loading"
        style="margin-top: 6px;"
        :current.sync="search.current"
        :size.sync="search.size"
        @getList="getList">
        <el-table-column slot="options" width="180px" fixed="right" label="操作" align="center">
          <template slot-scope="{ row }">
            <div class="table-options">
              <el-button type="text" @click="push(row.id)" v-if="row.pushStatus === '0'">推送</el-button>
              <el-button type="text" @click="operate(row.id, row.status)">{{ row.status === '1' ? '停用' : '启用' }}</el-button>
              <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,
          applicationName: ''
        },
        loading: false,
        total: 10,
        colConfigs: [
          { prop: 'saasPlatformName', label: '所属平台' },
          { prop: 'menuLevel1Name', label: '一级菜单名称'},
          { prop: 'menuLevel2Name', label: '权限列表名称'},
          { prop: 'menuLevel2Lable', label: '二级菜单名称'},
          { prop: 'createUserName', label: '编辑人' },
          { prop: 'createTime', label: '编辑时间' },
          {
            prop: 'status',
            label: '状态',
            align: 'center',
            render (h, {row}) {
              return h('span',{
                style: {
                  color: row.status === '1' ? '#2EA222' : '#F46'
                }
              }, row.status === '1' ? '启用' : '停用')
            }
          },
          {
            prop: 'pushStatus',
            label: '推送状态',
            align: 'center',
            render (h, {row}) {
              return h('span',{
                style: {
                  color: row.pushStatus === '1' ? '#2EA222' : '#F46'
                }
              }, row.pushStatus === '1' ? '成功' : '失败')
            }
          }
        ],
        tableData: [],
        ids: ''
      }
    },

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

    mounted() {
      this.getList()
    },

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

            this.$nextTick(() => {
              this.loading = false
            })
          } else {
            this.loading = false
          }
        }).catch(() => {
          this.loading = false
        })
      },

      removeAll () {
        if (!this.ids) return

        this.remove(this.ids)
      },

      push (id) {
        this.instance.post(`/app/appapplicationinfo/push?id=${id}`).then(res => {
          if (res.code == 0) {
            this.$message.success(`推送成功`)
            this.getList()
          }
        })
      },

      operate (id, status) {
        this.instance.post(`/app/appapplicationinfo/enable?id=${id}`).then(res => {
          if (res.code == 0) {
            this.$message.success(`${status === '1' ? '停用成功' : '启用成功'}`)
            this.getList()
          }
        })
      },

      remove (id) {
        this.$confirm('确定删除该数据？').then(() => {
          this.instance.post(`/app/appapplicationinfo/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>
</style>
