<template>
  <a-card :bordered="false" title="预警管理">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="24">
          <a-col :md="6" :sm="8">
            <a-form-item label="媒体">
              <a-radio-group v-model="mediaType">
                <a-radio-button value=""> 全部 </a-radio-button>
                <a-radio-button value="快手"> 快手 </a-radio-button>
                <a-radio-button value="头条"> 头条 </a-radio-button>
              </a-radio-group>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item label="运营名称">
              <a-input placeholder="请输入运营名称" v-model="userId"></a-input>
            </a-form-item>
          </a-col>
          <template>
            <a-col :md="6" :sm="8">
              <a-form-item label="账户ID">
                <Treeselect :appId.sync="accountId" :multiple="false" ref="treeSelect" style="width: 100%" />
              </a-form-item>
            </a-col>
          </template>
          <a-col :md="6" :sm="8">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <!-- <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">新增</a-button>
    </div>-->

    <!-- table区域-begin -->
    <!-- 供应商 -->
    <div>
      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        style="word-break: break-all"
      >
        <span slot="userId" slot-scope="text">{{ text.split('_')[0] }}</span>
        <div slot="alarmDetail" slot-scope="text" style="text-align: left; width: 100%; padding: 0 10px">
          {{ text }}
        </div>
      </a-table>
    </div>
  </a-card>
</template>

<script>
import JTreeSelect from '@/components/jeecg/JTreeSelect'
import { httpAction, getAction } from '@/api/manage'
import Treeselect from './pushTreeSelect.vue'
import { mapActions, mapGetters } from 'vuex'
export default {
  name: 'ProjectList',
  components: {
    JTreeSelect,
    Treeselect,
  },
  data() {
    return {
      dataSource: [],
      mediaType: '',
      userId: '',
      accountId: '',
      description: '项目管理页面',
      // 表头
      columns: [
        {
          title: '媒体',
          align: 'center',
          dataIndex: 'mediaType',
          width: 100,
        },
        {
          title: '账户ID',
          align: 'center',
          dataIndex: 'accountId',
          width: 200,
        },
        {
          title: '运营',
          align: 'center',
          dataIndex: 'userId',
          scopedSlots: {
            customRender: 'userId',
          },
          width: 100,
        },
        {
          title: '预警字段',
          align: 'center',
          dataIndex: 'groupName',
          scopedSlots: {
            customRender: 'groupName',
          },
          width: 100,
        },
        {
          title: '预警时间',
          align: 'center',
          dataIndex: 'createTime',
          scopedSlots: {
            customRender: 'createTime',
          },
          width: 200,
        },
        {
          title: '预警信息',
          align: 'center',
          dataIndex: 'alarmDetail',
          scopedSlots: {
            customRender: 'alarmDetail',
          },
        },
      ],
      list: [],
      options: [],
      loading: false,
      ipagination: {
        current: 1,
        pageSize: 10,
        //   pageSizeOptions: ['10', '20', '30'],
        showTotal: (total, range) => {
          return range[0] + '-' + range[1] + ' 共' + total + '条'
        },
        showQuickJumper: true,
        //   showSizeChanger: true,
        total: 0,
        onChange: (current) => {
          // 切换分页时的回调，
          // 当在页面定义change事件时，切记要把此处的事件清除，因为这两个事件重叠了，可能到时候会导致一些莫名的bug
          this.ipagination.current = current
        },
      },
    }
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  watch: {
    'ipagination.current': function (n, o) {
      if (n != o) {
        this.getHistoryList()
      }
    },
  },
  methods: {
    searchQuery() {
      this.ipagination.current = 1
      this.getHistoryList()
    },
    searchReset() {
      this.mediaType = ''
      this.userId = ''
      this.accountId = ''
      this.ipagination.current = 1
      this.getHistoryList()
    },
    getHistoryList() {
      this.loading = true
      var params = {}
      params.mediaType = this.mediaType
      params.userId = this.userId
      params.accountId = this.accountId
      params.pageNo = this.ipagination.current
      params.pageSize = this.ipagination.pageSize

      getAction('/alarm/alarmEventSend/pageList', params).then((res) => {
        this.loading = false
        if (res.success) {
          this.dataSource = res.result.list.map((item, index) => {
            return { ...item, key: index }
          })
          this.ipagination.total = res.result.total
        } else {
          this.$message.error(res.message)
        }
      })
    },
    ...mapGetters(['nickname', 'avatar', 'userInfo']),
    getList() {
      var params = {}
      params.userId = this.userInfo().id
      params.pageSize = 1000
      params.pageNo = 1
      getAction('/ctop/advertiser/list', params)
        .then((res) => {
          if (res.success) {
            this.list = res.result.records
          } else {
            this.$message.warning(res.message)
          }
        })
        .finally(() => {})
    },
    filterOption(input, option) {
      return option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
    },
    getAllUserList(value) {
      this.options = []
      getAction('/sys/user/getAllUserList', { pageSize: 50, pageNo: 1, userName: value }).then((res) => {
        if (res.success) {
          this.options = res.result.list
        }
      })
    },
    search(value) {
      this.options = []
      if (value != '') {
        // this.options = this.list.filter(item => {
        //   var data = item.realName + ' ' + item.roleName
        //   return data.toLowerCase().indexOf(value.toLowerCase()) > -1
        // })
        // this.options = [...this.options]
        this.getAllUserList(value)
      } else {
        this.getAllUserList()
      }
    },
  },
  created() {},
  mounted() {
    this.getHistoryList()
    // this.getAllUserList()
    // this.getList()
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>