<template>
  <div class="page">
    <el-form size="small" :inline="true" class="query-form" ref="searchForm" :model="searchForm" @keyup.enter.native="refreshList()" @submit.native.prevent>
      <!-- 搜索框-->
      <el-form-item label="活动" prop="eventName">
        <el-input size="small" v-model="searchForm.eventName" placeholder="请输入活动名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="姓名" prop="name">
        <el-input size="small" v-model="searchForm.name" placeholder="请输入姓名" clearable></el-input>
      </el-form-item>
      <el-form-item v-if="showHideItem" label="手机号" prop="phone">
        <el-input size="small" v-model="searchForm.phone" placeholder="请输入手机号" clearable></el-input>
      </el-form-item>
      <el-form-item v-if="showHideItem" label="部门" prop="officeName">
        <el-select v-model="searchForm.officeName" placeholder="请选择部门" clearable>
          <el-option label="工程一部" value="工程一部"></el-option>
          <el-option label="工程二部" value="工程二部"></el-option>
          <el-option label="工程三部" value="工程三部"></el-option>
          <el-option label="工程四部" value="工程四部"></el-option>
          <el-option label="工程五部" value="工程五部"></el-option>
          <el-option label="招标代理部" value="招标代理部"></el-option>
          <el-option label="兴光评估" value="兴光评估"></el-option>
          <el-option label="其他" value="其他"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="default" @click="showHide" size="small" :icon="showHideIcon">{{showHideName}}</el-button>
        <el-button type="primary" @click="refreshList()" size="small" icon="el-icon-search">查询</el-button>
        <el-button @click="resetSearch()" size="small" icon="el-icon-refresh-right">重置</el-button>
      </el-form-item>
    </el-form>
    <div class="bg-white top" style="">
      <vxe-toolbar   :refresh="{query: refreshList}" custom>
        <template #buttons>
          <el-button v-if="hasPermission('luckyDraw:member:del')" icon="el-icon-delete" type="danger"  size="small"  @click="del()" :disabled="$refs.luckyTable && $refs.luckyTable.getCheckboxRecords().length === 0" plain>删除</el-button>
        </template>
      </vxe-toolbar>
      <div style="height: calc(100% - 90px)">
        <vxe-table
          :key="tableKey"
          border="inner"
          auto-resize
          resizable
          height="auto"
          :loading="loading"
          size="small"
          ref="clientTable"
          show-header-overflow
          show-overflow
          highlight-hover-row
          :menu-config="{}"
          @sort-change="sortChangeHandle"
          :sort-config="{remote:true}"
          :data="dataList"
          :checkbox-config="{}">
          <vxe-column type="checkbox"  width="40px"></vxe-column>
          <vxe-column type="seq" width="60" title="序号"></vxe-column>
          <vxe-column min-width="160" align="center" title="活动" field="eventName"></vxe-column>
          <vxe-column min-width="160" align="center" title="姓名" field="name"></vxe-column>
          <vxe-column min-width="160" align="center" title="手机号" field="phone"></vxe-column>
          <vxe-column min-width="160" align="center" title="部门" field="officeName">
<!--            <template slot-scope="scope">-->
<!--              <span v-if="scope.row.officeName === '1'">工程一部</span>-->
<!--              <span v-if="scope.row.officeName === '2'">工程二部</span>-->
<!--              <span v-if="scope.row.officeName === '3'">工程三部</span>-->
<!--              <span v-if="scope.row.officeName === '4'">工程四部</span>-->
<!--              <span v-if="scope.row.officeName === '5'">工程五部</span>-->
<!--              <span v-if="scope.row.officeName === '6'">其他</span>-->
<!--            </template>-->
          </vxe-column>
          <vxe-column title="操作" width="120px" fixed="right" align="center">
            <template  slot-scope="scope">
              <el-button v-if="hasPermission('luckyDraw:member:edit')" type="text"  size="small" @click="edit(scope.row.id)">修改</el-button>
              <el-button v-if="hasPermission('luckyDraw:member:del')" type="text"   size="small" @click="del(scope.row.id)">删除</el-button>
            </template>
          </vxe-column>
        </vxe-table>
        <vxe-pager
          background
          size="small"
          :current-page="tablePage.currentPage"
          :page-size="tablePage.pageSize"
          :total="tablePage.total"
          :page-sizes="[10, 20, 100, 1000, {label: '全量数据', value: 1000000}]"
          :layouts="['PrevPage', 'JumpNumber', 'NextPage', 'FullJump', 'Sizes', 'Total']"
          @page-change="currentChangeHandle">
        </vxe-pager>
        <LuckyDrawMemberUpdateForm ref="luckyDrawMemberUpdateForm" @refreshList="refreshList"></LuckyDrawMemberUpdateForm>
      </div>
    </div>
  </div>
</template>

<script>
  import LuckyDrawMemberUpdateForm from './LuckyDrawMemberUpdateForm'
  import UserSelect from '@/components/userSelect'
  import InputNumber from '@/views/modules/cw/workContract/InputNumber.vue'
  import SelectTree from '@/components/treeSelect/treeSelect.vue'
  import LuckyDrawMemberService from '@/api/luckyDraw/LuckyDrawMemberService'
  import UserService from '@/api/sys/UserService'
  import SelectUserTree from '@/views/modules/utils/treeUserSelect'
  export default {
    data () {
      return {
        showHideItem: false,
        showHideIcon: 'el-icon-arrow-down',
        showHideName: '展示',
        num: 0,
        searchForm: {
          eventId: '',
          eventName: '',
          officeName: '',
          phone: '',
          name: ''
        },
        dataList: [],
        tablePage: {
          total: 0,
          currentPage: 1,
          pageSize: 10,
          orders: []
        },
        tableKey: '',
        loading: false,
        processDefinitionAuditId: '',
        procDefAuditKey: '',
        isAdmin: false,
        create: ''
      }
    },
    luckyDrawMemberService: null,
    userService: null,
    created () {
      this.luckyDrawMemberService = new LuckyDrawMemberService()
      this.userService = new UserService()
    },
    components: {
      UserSelect,
      InputNumber,
      SelectUserTree,
      SelectTree,
      LuckyDrawMemberUpdateForm
    },
    computed: {
      userName () {
        return JSON.parse(localStorage.getItem('user')).name
      },
      user () {
        this.createName = JSON.parse(localStorage.getItem('user')).name
        return JSON.parse(localStorage.getItem('user'))
      }
    },
    mounted () {
      this.refreshList()
    },
    activated () {
      this.refreshList()
    },
    methods: {
      showHide () {
        if (this.showHideItem === false) {
          this.showHideItem = true
          this.showHideIcon = 'el-icon-arrow-up'
          this.showHideName = '隐藏'
        } else {
          this.showHideItem = false
          this.showHideIcon = 'el-icon-arrow-down'
          this.showHideName = '展示'
        }
      },
      // 查询当前用户是否是管理员用户
      checkIsAdmin () {
        this.userService.is().then(({data}) => {
          this.isAdmin = data
        })
      },
      // 获取数据列表
      refreshList () {
        this.loading = true
        this.luckyDrawMemberService.list({
          'current': this.tablePage.currentPage,
          'size': this.tablePage.pageSize,
          'orders': this.tablePage.orders,
          ...this.searchForm
        }).then(({data}) => {
          this.dataList = data.records
          this.tablePage.total = data.total
          this.tableKey = Math.random()
          this.loading = false
        })
        this.checkIsAdmin()
      },
      // 当前页
      currentChangeHandle ({ currentPage, pageSize }) {
        this.tablePage.currentPage = currentPage
        this.tablePage.pageSize = pageSize
        this.refreshList()
      },
      // 排序
      sortChangeHandle (column) {
        this.tablePage.orders = []
        if (column.order != null) {
          this.tablePage.orders.push({column: this.$utils.toLine(column.property), asc: column.order === 'asc'})
        }
        this.refreshList()
      },
      resetSearch () {
        this.$refs.searchForm.resetFields()
        this.refreshList()
      },
      // 修改
      edit (id) {
        id = id || this.$refs.clientTable.getCheckboxRecords().map(item => {
          return item.id
        })[0]
        this.$refs.luckyDrawMemberUpdateForm.init('edit', id)
      },
      // 删除
      del (id) {
        console.log(123)
        let ids = id || this.$refs.clientTable.getCheckboxRecords().map(item => {
          return item.id
        }).join(',')
        this.$confirm(`确定删除所选项吗?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.loading = true
          this.luckyDrawMemberService.remove(ids).then(({data}) => {
            this.$message.success(data)
            this.refreshList()
            this.loading = false
          })
        })
      }
    }
  }
</script>
