<template>
  <div class="sui-slider">
    <div class="sui-slider-main">
      <div class="sui-slider-title" style="z-index: 1933">
        <h2>
          <span class="sui-slider-title-text">选择人员</span>
        </h2>
        <span class="sui-icons sui-icons-close sui-slider-title-x" @click="close"></span>
      </div>
      <div class="sui-slider-content" style="position: absolute; top: 0; left: 0; padding-top: 80px; padding-bottom: 70px; width: 100%; height: 100%;">
        <div class="oa-ulist-main">
          <div class="oa-ulist-l">
            <div class="oa-ulist-title clearfix">
              <div class="oa-ulist-title-r" @click="all(true)">全选</div>
              <div class="oa-ulist-title-l">
                <el-input v-model="keyWord" @on-enter="search" placeholder="请输入..." style="width: 215px" size="small">
                  <i style="cursor: pointer;" slot="suffix" @click="search" class="el-input__icon el-icon-search"></i>
                </el-input>
              </div>
            </div>

            <div class="oa-ulist-lcon">
              <ul class="oa-ulist-list">
                <template>
                  <li v-for="item in pageItems" :key="item.userId" :class="'oa-ulist-list-sed'" @click="add(item)">
                    <img v-if="item.profilePhoto" class="oa-ulist-list-p" :src="(store.getters.API !== '/' ? store.getters.API : '') + item.profilePhoto">
                    <img v-else class="oa-ulist-list-p" src="/src/static/img/photo-default.png" alt="头像">
                    <span class="st-icons st-icons-success oa-ulist-list-i" v-if="item.checked"></span>
                    <h3>{{ item.userName }}</h3>
                    <p class="st-ellipsis">{{ item.title }}<span v-if="item.duty">&nbsp;-&nbsp;{{item.duty}}</span></p>
                  </li>
                </template>
                <template v-if="dataAllItems.length < 1">
                  <li class="oa-ulist-liste">无数据</li>
                </template>
              </ul>
            </div>
          </div>
          <div class="oa-ulist-r">
            <div class="oa-ulist-title clearfix">
              <div class="oa-ulist-title-r" @click="all(false)">清空</div>
              <div class="oa-ulist-title-l">
                <span>已选人数：{{dataItems.length}}</span>
              </div>
            </div>
            <div class="oa-ulist-lcon">
              <ul class="oa-ulist-list">
                <template>
                  <li v-for="item in dataItems" :key="item.id" @click="del(item)">
                    <img v-if="item.profilePhoto" class="oa-ulist-list-p" :src="(store.getters.API !== '/' ? store.getters.API : '') + item.profilePhoto">
                    <img v-else class="oa-ulist-list-p" src="/src/static/img/photo-default.png" alt="头像">
                    <span class="st-icons st-icons-del oa-ulist-list-i2"></span>
                    <h3>{{ item.userName }}</h3>
                    <p class="st-ellipsis">{{ item.title }}<span v-if="item.duty">&nbsp;-&nbsp;{{item.duty}}</span></p>
                  </li>
                </template>
                <template v-if="dataItems.length < 1">
                  <li class="oa-ulist-liste">暂无选择</li>
                </template>
              </ul>
            </div>
          </div>
        </div>
        <div class="oa-ulist-bottom" style="bottom: 25px;">
          <div style="float: left; position: relative; margin-left: 20px;">
            <el-pagination
              :current-page = "page.pageIndex"
              :page-sizes = "[10, 20, 30, 40]"
              :page-size = "page.pageSize"
              layout = "total, sizes, prev, pager, next"
              :total = "page.pageTotal"
              @size-change = "pageSizeChange"
              @current-change = "pageCurrentChange"
            >
            </el-pagination>
          </div>
          <div style="float: right; margin-right: 20px;">
            <el-button type="primary" style="width: 200px;" @click="send" size="small">确&nbsp;&nbsp;定</el-button>
          </div>
        </div>
      </div>
    </div>
    <div class="sui-slider-cover" @click="close"></div>
    <!-- child slider -->
  </div>
</template>
<script>
import { allPagination } from '@/utils/util.js';
import store from '@/store/index.js';
export default {
  name: 'userSelector',
  props: {
    //  全部用户
    allItems: {
      type: Array,
      default: () => { return []; }
    },
    //  已选中用户
    items: {
      type: Array,
      default: () => { return []; }
    }
  },
  data () {
    return {
      store,
      //  源数组
      allData: [],
      //  当前用数组
      dataAllItems: [],
      //  当前选中数组
      dataItems: [],
      //  page items 分页数据
      pageItems: [],
      //  page
      page: {
        pageIndex: 1,
        pageSize: 20,
        pageTotal: 0
      },
      keyWord: ''
    }
  },
  computed: {
  },
  watch: {
    allData (newValue, oldValue) {
      this.dataAllItems = newValue;
    },
    allItems (newValue, oldValue) {
      this.dataAllItems = newValue;
    },
    items (newValue, oldValue) {
      for (let item of this.allData) {
        let bol = false;
        for (let item2 of newValue) {
          if (item.userId === item2.userId) {
            bol = true;
            break;
          }
        }
        item.checked = bol;
      }
      this.dataItems = newValue;
    },
    dataAllItems (newValue, oldValue) {
      this.initPage();
    }
  },
  mounted () {
    this.initData();
  },
  methods: {
    //  init
    initData () {
      //  是否有传数据
      if (this.allItems.length > 0) {
        return false;
      }
      //  取用户
      this.getAllUser();
    },
    getAllUser () {
      this.store.dispatch('system/usersManage/list', {pageNum: '1', pageSize: '1000000'}).then(res => {
        if (res.code === '0') {
          this.allData = res.data.list;
        } else {
          this.$message.error(res.message);
        }
        //
        this.initPage();
      });
    },
    initPage () {
      //  数据
      this.pageItems = allPagination(this.dataAllItems, this.page.pageIndex, this.page.pageSize);
      this.page.pageTotal = this.dataAllItems.length;
    },
    //  关键词搜索
    search () {
      //  筛选数据
      let data = this.allData.filter(item => {
        if (item.userName && item.userName.indexOf(this.keyWord) > -1) {
          return true;
        } else {
          return false;
        }
      });
      this.dataAllItems = data;
    },
    //  查询设置选中数组
    select () {
      let data = this.allData.filter(item => {
        if (item.checked) {
          return true;
        } else {
          return false;
        }
      });
      this.dataItems = data;
    },
    //  选中
    add (item) {
      item.checked = !item.checked;
      this.select();
    },
    //  del
    del (item) {
      item.checked = false;
      this.select();
    },
    //  全选
    all (bol) {
      for (let item of this.allData) {
        item.checked = bol;
      }
      this.select();
    },
    //
    pageSizeChange (size) {
      this.page.pageSize = size;
      this.initPage();
    },
    //
    pageCurrentChange (index) {
      this.page.pageIndex = index;
      this.initPage();
    },
    //  确定
    send () {
      //  发送
      this.$emit('on-select-user', this.dataItems);
      //
      this.close();
    },
    //  关闭
    close () {
      this.$emit('on-then', 'close', {});
    }
  }
}
</script>
<style scoped lang="less">
  .sui-slider{
    display: block;
  }
  .oa-ulist-main {
    display: flex;
    position: relative;
    height: 100%; width: 560px; margin: 0 auto;
  }
  .oa-ulist-l, .oa-ulist-r { height: 100%; position: relative; border: 1px solid #ccc; }
  .oa-ulist-l { width: 300px; }
  .oa-ulist-r { width: 300px; margin-left: 20px; }
  .oa-ulist-title {
    position: absolute; top: 0; left: 0; z-index: 2;
    width: 100%; height: 35px;
    border-bottom: 1px solid #ccc;
  }
  .oa-ulist-title-l {
    width: 210px;
    margin-right: 60px; padding-top: 1px;
    color: #333; text-align: left;
    .el-input input {
      border: none;
    }
  }

  .oa-ulist-title-l-single {
    width: 260px;
    margin-right: 60px; padding-top: 1px;
    color: #333; text-align: left;
  }
  .oa-ulist-title-l > span { display: inline-block; margin-top: 5px; padding-left: 10px; }
  .oa-ulist-title-r {
    float: right;
    width: 50px; height: 34px; line-height: 34px; vertical-align: middle;
    border-left: 1px solid #ccc;
    text-align: center; color: #333; cursor: pointer;
  }
  .oa-ulist-title .ivu-input {
    border: 0 !important;
    box-shadow: none !important;
  }
  .oa-ulist-lcon {
    position: absolute; top: 0; left: 0; z-index: 1;
    width: 100%; height: 100%; padding-top: 35px;
  }
  .oa-ulist-list { width: 100%; height: 100%; overflow-y: auto; overflow-x: hidden; }
  .oa-ulist-liste {
    padding: 10px 0;
    color: #666; text-align: center;
  }
  .oa-ulist-list > li {
    position: relative;
    padding: 5px 40px 0 60px;
    height: 50px;
    cursor: pointer; text-align: left;
  }
  .oa-ulist-list > li:hover { background-color: #fbfbfb; }
  .oa-ulist-list-p {
    position: absolute; top: 5px; left: 10px;
    width: 36px; height: 36px;
    border-radius: 18px;
  }
  .oa-ulist-list-sed { background-color: #f5f5f5; }
  .oa-ulist-list-i {
    display: none;
    position: absolute; top: 15px; right: 20px;
  }
  .oa-ulist-list-i2 { position: absolute; top: 15px; right: 20px; }
  .oa-ulist-list-sed .oa-ulist-list-i { display: block; }
  .oa-ulist-list > li > h2 { color: #333; }
  .oa-ulist-list > li > p { color: #666; font-size: 12px;
    height: 20px;
    line-height: 20px;
    vertical-align: middle; }
  .oa-ulist-bottom {
    position: absolute; bottom: 20px; left: 0;
    width: 100%;
    overflow: hidden;
  }
  .oa-ulist-bottom .el-pagination {
    float: none;
  }
  .oa-ulist-title-l-single .el-input__inner {
    border: none;
  }
</style>
