<template>
  <div class="notice-list-wrap">
    <div class="notice-list">
      <common-detail-top :topMenuData="topMenuData" :activeId="activeId"></common-detail-top>
      <div class="notice-list-table">
        <div class="search-wrap">
          <el-input @keyup.native="value => searchPeople(value)" clearable class="search-input" placeholder="请输入姓名/员工代码" prefix-icon="el-icon-search" v-model="conditionObj.keyword" @clear="clearInput"> </el-input>
          <store-tree-select :brandId="brandId" :activeGroupId="activeGroupId" @checkStoreGroupIds="checkStoreGroupIds"> </store-tree-select>
          <el-select v-model="conditionObj.signStatus" placeholder="请选择" class="w-131 m-l-10" @change="changeSelect"> <el-option v-for="item in signStatusOptions" :key="item.value" :label="item.label" :value="item.value"> </el-option> </el-select>
          <el-date-picker @change="changeDate" class="search-date" format="yyyy-MM-dd" value-format="yyyy-MM-dd" :editable="false" v-model="conditionObj.choiceDate" :picker-options="pickerOptions" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期"> </el-date-picker>
          <el-button @click="exportData" class="fr" type="primary">导出</el-button>
        </div>
        <el-table :data="tableData" style="width: 100%" v-loading="loading">
          <el-table-column prop="title" label="门店名称" show-overflow-tooltip> </el-table-column>
          <el-table-column label="姓名" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.content }}
            </template>
          </el-table-column>
          <el-table-column label="员工代码" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.content }}
            </template>
          </el-table-column>
          <el-table-column label="职位" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.readCount }}
            </template>
          </el-table-column>
          <el-table-column label="状态" show-overflow-tooltip>
            <template slot-scope="scope">
              <div v-if="scope.row.sendTime" class="time-wrap">
                <p>{{ scope.row.sendTime | formatTimeYMD }}</p>
                <p>{{ scope.row.sendTime | formatTimeHMS }}</p>
              </div>
              <div v-else>--</div>
            </template>
          </el-table-column>
          <el-table-column label="时间" show-overflow-tooltip>
            <template slot-scope="scope">
              <div v-if="scope.row.sendTime" class="time-wrap">
                <p>{{ scope.row.sendTime | formatTimeYMD }}</p>
                <p>{{ scope.row.sendTime | formatTimeHMS }}</p>
              </div>
              <div v-else>--</div>
            </template>
          </el-table-column>
          <el-table-column label="打卡坐标" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.sendTime }}
            </template>
          </el-table-column>
          <el-table-column label="门店坐标" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.sendTime }}
            </template>
          </el-table-column>
          <el-table-column label="距离" show-overflow-tooltip>
            <template slot-scope="scope">
              {{ scope.row.sendTime }}
            </template>
          </el-table-column>
        </el-table>
        <div class="pagination" v-if="totalCount > 0">
          <el-pagination background :page-sizes="[10, 20, 30, 40]" :page-size="pageSize" :current-page="currentPage" layout="total, sizes, prev, pager, next" :total="totalCount" @size-change="handleSizeChange" @current-change="handleCurrentChange"> </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import storeTreeSelect from '@/components/app/store-tree-select.vue';
import { getRequest } from '@/api/api';
import { _debounce } from '@/common/js/public';
export default {
  name: 'noticeList',
  props: {
    brandId: {
      type: String,
      default() {
        return '';
      }
    },
    activeGroupId: {
      type: String,
      default() {
        return '';
      }
    }
  },
  data() {
    return {
      topMenuData: [
        {
          id: '1',
          name: '企业公告列表'
        }
      ],
      activeId: '1',
      activeBrand: this.brandId, // 商户(品牌) id
      activeGroup: this.activeGroupId, // 商户(品牌) groupId
      conditionObj: {
        keyword: '',
        storeGroupIds: [],
        signStatus: '',
        choiceDate: []
      },
      signStatusOptions: [
        {
          value: '',
          label: '所有状态'
        },
        {
          value: '0',
          label: '签到'
        },
        {
          value: '1',
          label: '签退'
        }
      ],
      choiceDateCopy: [],
      pickerOptions: {
        onPick: ({ maxDate, minDate }) => {
          this.choiceDateCopy = [minDate.getTime()];
          if (maxDate) {
            this.choiceDateCopy = [];
          }
        },
        disabledDate: time => {
          if (this.choiceDateCopy.length) {
            const one = 31 * 24 * 3600 * 1000;
            const minTime = this.choiceDateCopy[0] - one;
            const maxTime = this.choiceDateCopy[0] + one;
            return time.getTime() < minTime || time.getTime() > maxTime || time.getTime() > Date.now() - 8.64e6;
          }
          return time.getTime() > Date.now() - 8.64e6;
        }
      },
      tableData: [],
      pageSize: 10,
      currentPage: 1,
      totalCount: 0
    };
  },
  filters: {
    formatTimeYMD(data) {
      let date = new Date(data);
      let month = date.getMonth() + 1;
      let day = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
      let newMonth = month < 10 ? '0' + month : month;
      return date.getFullYear() + '-' + newMonth + '-' + day;
    },
    formatTimeHMS(data) {
      let date = new Date(data);
      let hours = date.getHours() < 10 ? '0' + date.getHours() : date.getHours();
      let minutes = date.getMinutes() < 10 ? '0' + date.getMinutes() : date.getMinutes();
      return hours + ':' + minutes;
    }
  },
  watch: {
    brandId: function(newData, oldData) {
      let that = this;
      if (!!newData) {
        that.activeBrand = newData;
        that.getTableList();
      }
    },
    activeGroupId: function(newData, oldData) {
      let that = this;
      if (!!newData) {
        that.activeGroup = newData;
      }
    }
  },
  mounted() {
    this.activeBrand = this.brandId;
    if (!!this.brandId) {
      this.getTableList();
    }
  },
  methods: {
    // 获取公告列表
    getTableList() {
      let that = this;
      that.loading = true;
      let para = {
        startTime: that.startTime,
        brandId: that.activeBrand,
        title: that.searchTitle,
        endTime: that.endTime,
        pageNum: that.currentPage,
        pageSize: that.pageSize
      };
      getRequest('/haoban-app-announcement-web/announcement-list', para).then(res => {
        let data = res.data;
        that.loading = false;
        if (data.errorCode == 1) {
          that.tableData = data.result.list;
          that.totalCount = data.result.total;
        }
      });
    },
    /**
     *  选择 tree 节点,获取选择节点信息
     */
    checkStoreGroupIds: function(nodes) {
      var that = this;
      that.currentPage = 1;
      that.conditionObj.storeGroupIds = nodes;
      that.getTableList();
    },
    // 选择日期
    changeDate(val) {
      let that = this;
      if (!val) {
        that.choiceDateCopy = [];
        that.conditionObj.choiceDate = [];
      }
      that.currentPage = 1;
      that.getTableList();
    },
    // 搜索
    searchPeople: _debounce(function(value) {
      let that = this;
      that.currentPage == 1;
      that.getTableList();
    }, 500),

    // 搜索清除
    clearInput() {
      this.currentPage = 1;
      this.getTableList();
    },

    // 分页
    handleSizeChange(val) {
      this.pageSize = val;
      this.getTableList();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getTableList();
    },

    /**
     *  导出
     *
     */
    exportData() {
      let that = this;
      that.postExportData();
    },
    postExportData() {
      let that = this;
      that.a = '';
    }
  },
  components: {
    storeTreeSelect
  }
};
</script>
<style type="text/scss" lang="scss" scoped>
.notice-list-wrap {
  height: 100%;
  .notice-list {
    background: #fff;
    margin-bottom: 34px;
    // min-height: 100%;
    // min-height: calc(100% + 24px);
  }
}
.notice-list-table {
  padding: 22px 24px 24px;
  background-color: #fff;
}

.search-wrap {
  font-size: 0;
  margin-bottom: 22px;
  .search-input {
    width: 264px;
  }
  .search-date {
    width: 300px;
    margin-left: 10px;
  }
}
.pagination {
  margin-top: 24px;
  text-align: right;
}
</style>
