<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>
          <ai-picker
            :ops="{label: 'girdName'}"
            dialogTitle="选择所属网格"
            action="/app/appgirdinfo/girdList"
            :instance="instance"
            @change="search.current = 1, getList()"
            @pick="onGirdChange"
            v-model="search.girdId">
            <div class="userSelcet">
              <span style="color: #606266;" v-if="search.girdId.length">{{ search.girdName }}</span>
              <span v-else>请选择网格</span>
              <i class="el-icon-arrow-up"  v-if="!search.girdId.length"></i>
              <i class="el-icon-circle-close" v-if="search.girdId.length" @click.stop="search.girdId = [], search.girdName, search.current = 1, getList()"></i>
            </div>
          </ai-picker>
          <el-button size="small" type="primary" icon="iconfont iconAdd" @click="toAdd('')">添加</el-button>
          <ai-import
            :instance="instance"
            :dict="dict"
            type="appintegralpublicityinfo" name="积分公示"
            @success="search.current = 1, getList()">
          </ai-import>
        </template>
        <template #right>
        </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="options" width="120px" fixed="right" label="操作" align="center">
          <template slot-scope="{ row }">
            <div class="table-options">
              <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,
          girdId: [],
          girdName: ''
        },
        total: 0,
        colConfigs: [
          { prop: 'girdName',  label: '所属网格', align: 'left' },
          { prop: 'classOne',  label: '一类', align: 'center' },
          { prop: 'classTwo',  label: '二类', align: 'center' },
          { prop: 'classThree',  label: '三类', align: 'center' },
          { prop: 'integral',  label: '分值', align: 'center' },
          { slot: 'options'},
        ],
        tableData: []
      }
    },

    created () {
      this.getList()
    },

    methods: {
      onGirdChange (e) {
        if (e.length) {
          this.search.girdName = e[0].girdName
        }

        this.search.current = 1
        this.getList()
      },

      getList() {
        this.instance.post(`/app/appintegralpublicityinfo/list`, null, {
          params: {
            ...this.search,
            girdName: '',
            girdId: this.search.girdId.length ? this.search.girdId[0] : ''
          }
        }).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/appintegralpublicityinfo/delete?ids=${id}`).then(res => {
            if (res.code == 0) {
              this.$message.success('删除成功！')
              this.getList()
            }
          })
        })
      },

      toDetail (id) {
        this.$emit('change', {
          type: 'Detail',
          params: {
            id: id || ''
          }
        })
      },

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

<style lang="scss" scoped>
  .notice .userSelcet {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 215px;
    height: 32px;
    line-height: 32px;
    border-radius: 4px;
    border: 1px solid #d0d4dc;
    overflow: hidden;
    cursor: pointer;
    transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);

    &:hover {
      border-color: $placeholderColor;
    }

    i {
      display: flex;
      position: relative;
      align-items: center;
      justify-content: center;
      width: 30px;
      height: 100%;
      line-height: 32px;
      font-size: 14px;
      text-align: center;
      color: #d0d4dc;
      transform: rotateZ(180deg);
    }

    .el-icon-circle-close:hover {
      opacity: 0.6;
    }

    span {
      flex: 1;
      padding: 0 15px;
      font-size: 12px;
      color: $placeholderColor;
    }
  }
</style>
