<template>
  <div class="m20">
    <el-row>
      <div class="leftBox">
        <el-input class="w260 mr10" placeholder="请输入门店名称/code" v-model="pageParam.search1" prefix-icon="el-icon-search"></el-input>
        <el-input class="w260 mr10" placeholder="请输入导购名称/code" v-model="pageParam.search2" prefix-icon="el-icon-search"></el-input>
      </div>

      <div class="rightBox">
        <ul class="datebox" v-for="(lis, index) in dateLis" :key="lis.key">
          <li :class="active == index ? 'activeClass' : 'noActive'" class="dateLisBox" @click="btnChange(index)">{{ lis.value }}</li>
        </ul>
        <el-date-picker class="w256" v-model="pageParam.dateDefault" type="daterange" range-separator="~" start-placeholder="创建开始日期" end-placeholder="创建结束日期" :default-time="['00:00:00', '23:59:59']" :picker-options="pickerOptions()" @change="onDateChange"> </el-date-picker>
      </div>
    </el-row>
    <el-table class="mt18" :data="tableData" @sort-change="sortChange">
      <el-table-column label="导购信息">
        <template slot-scope="{ row }">
          <p class="overflow-ellipsis" :title="row.guideMsg">{{ row.guideMsg }}</p>
          <p>{{ row.guideMsgs }}</p>
        </template>
      </el-table-column>
      <el-table-column label="导购所属门店">
        <template slot-scope="{ row }">
          <p class="overflow-ellipsis" :title="row.guideStore">{{ row.guideStore }}</p>
          <p>{{ row.guideStoreCode }}</p>
        </template>
      </el-table-column>
      <el-table-column label="企微号">
        <template slot-scope="{ row }">
          <p class="overflow-ellipsis" :title="row.qw">{{ row.qw }}</p>
          <p>{{ row.qwCode }}</p>
        </template>
      </el-table-column>
      <el-table-column label="新增总人数" prop="addTotal" sortable></el-table-column>
      <el-table-column label="今日新增总人数" prop="todayAddTotal" sortable></el-table-column>
      <el-table-column label="今日流失人数" prop="toadyLess" sortable></el-table-column>
      <el-table-column label="流失总人数" prop="lessTotal" sortable></el-table-column>
      <el-table-column label="今日已达上限" prop="status">
        <template slot-scope="{ row }">
          <p>{{ row.status == 1 ? '是' : '否' }}</p>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="{ row }">
          <el-button type="text" @change="looks(row)">查看记录</el-button>
        </template>
      </el-table-column>
    </el-table>
    <dm-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageParam.currentPage" :page-sizes="[20, 40, 60, 80]" :page-size="pageParam.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="totalCount"> </dm-pagination>
  </div>
</template>

<script>
export default {
  name: 'DateDetail',
  props: {
    id: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      active: 0,
      dateLis: [
        { key: 1, value: '近7天' },
        { key: 2, value: '近30天' },
        { key: 3, value: '近三个月' },
        { key: 4, value: '近半年' }
      ],
      tableData: [],
      pageParam: {
        search1: '',
        search2: '',
        dateDefault: [],
        currentPage: 1,
        pageSize: 20
      },
      totalCount: 0,
      sortColumn: '',
      sortType: '',
      pickerOptions() {
        let pickerMinDate;
        return {
          onPick(obj) {
            pickerMinDate = obj.minDate;
            // console.log(pickerMinDate);
          },
          disabledDate(time) {
            // const date = new Date();
            // const year = date.getFullYear();
            // return time.getTime() > Date.now() || time.getTime() < date.setFullYear(year - 2);
            let start = new Date();
            start = new Date(`${start.getFullYear()}-${start.getMonth() + 1}-${start.getDate()}`);
            let start2 = new Date(pickerMinDate);
            let end = new Date();
            end = new Date(`${end.getFullYear()}-${end.getMonth() + 1}-${end.getDate()} 23:59:59`);
            let end2 = new Date(pickerMinDate);
            if (pickerMinDate) {
              start = start.setFullYear(start.getFullYear() - 2);
              start2 = start2.setDate(start2.getDate() - 60);
              start = start > start2 ? start : start2;
              end2 = end2.setDate(end2.getDate() + 60);
              end = end.getTime() > end2 ? end2 : end.getTime();
              // console.log('end2', end2, end);
            } else {
              start = start.setFullYear(start.getFullYear() - 2);
              end = end.getTime();
            }
            return time.getTime() < start || time.getTime() > end;
          }
        };
      }
    };
  },
  created() {
    this.getTableList();
    // 默认显示近七天
    this.btnChange(0);
  },
  methods: {
    getDateRange(setEndDate) {
      const start = new Date();
      let end = new Date();
      end = setEndDate(end);
      function getDate(date) {
        const result = [];
        result.push(date.getFullYear());
        result.push((date.getMonth() + 1).toString().padStart(2, '0'));
        result.push(
          date
            .getDate()
            .toString()
            .padStart(2, '0')
        );
        return result.join('-');
      }
      // yyyy-MM-dd
      return [getDate(start), getDate(end)];
    },
    btnChange(index) {
      //把index值赋给active，点击改变样式
      this.active = index;
      this.pageParam.dateDefault = [];
      let setDate;
      if (index == 0) {
        // 近7天
        setDate = endDate => {
          endDate.setDate(endDate.getDate() - 6);
          return new Date(endDate);
        };
      } else if (index == 1) {
        // 近30天
        setDate = endDate => {
          endDate.setDate(endDate.getDate() - 29);
          return new Date(endDate);
        };
      } else if (index == 2) {
        // 近三个月
        setDate = endDate => {
          endDate.setMonth(endDate.getMonth() - 3);
          return new Date(endDate);
        };
      } else if (index == 3) {
        // 近半年
        setDate = endDate => {
          endDate.setMonth(endDate.getMonth() - 6);
          return new Date(endDate);
        };
      }
      const [start, end] = this.getDateRange(setDate);
      this.pageParam.dateDefault.push(end);
      this.pageParam.dateDefault.push(start);
    },
    onDateChange() {
      this.active = -1;
    },
    // 当前页变化
    handleCurrentChange(val) {
      this.pageParam.currentPage = val;
      sessionStorage.setItem('currentPage', val);
    },
    // 页码
    handleSizeChange(val) {
      this.pageParam.currentPage = 1;
      this.pageParam.pageSize = val;
      this.getTableList();
    },
    // 排序
    sortChange(column, prop, order) {
      const that = this;
      that.sortColumn = column.prop;
      that.sortType = column.order == 'ascending' ? 'asc' : 'desc';
      that.getTableList();
    },
    // 列表数据
    getTableList() {
      this.tableData = [
        {
          guideMsg: '导购信息1索尼大法根据历史的快乐风格问题',
          guideMsgs: '2364756qre',
          guideStore: '门店名称1哈金斯的看法和大家说法可接受的垡健康是的发送到',
          guideStoreCode: '4564564',
          qw: '趣味',
          qwCode: '23674726',
          addTotal: 1,
          todayAddTotal: 2,
          toadyLess: 89,
          lessTotal: 23,
          staus: 1
        },
        {
          guideMsg: '导购信息1索尼大法根据历史的快乐风格问题',
          guideMsgs: '2364756qre',
          guideStore: '门店名称1哈金斯的看法和大家说法可接受的垡健康是的发送到',
          guideStoreCode: '4564564',
          qw: '趣味',
          qwCode: '23674726',
          addTotal: 9,
          todayAddTotal: 5,
          toadyLess: 77,
          lessTotal: 66,
          staus: 0
        }
      ];
    }
  }
};
</script>
<style lang="scss" scoped>
.activeClass {
  color: #2f54eb;
}
.noActive {
  color: #303133;
}

.m20 {
  margin: 20px;
}
.mb16 {
  margin-bottom: 16px;
}
.w260 {
  width: 260px;
}
.w256 {
  width: 256px;
}
.mr10 {
  margin-right: 10px;
}
.mr24 {
  margin-right: 24px;
}
.mr12 {
  margin-right: 12px;
}
.mt18 {
  margin-top: 18px;
}
.leftBox {
  float: left;
}
.rightBox {
  float: right;
  line-height: 32px;
  .datebox {
    display: inline-block;
    .dateLisBox {
      font-size: 14px;
      margin-right: 24px;
      cursor: pointer;
    }
  }
}
.title {
  width: 100%;
  display: flex;
  justify-content: space-between;
  .info-content {
    color: #2f54eb;
    font-size: 14px;
  }
  .info-icon {
    margin-right: 8px;
  }
}
.cur-po {
  cursor: pointer;
}
.el-button--text {
  color: #2f54eb;
}
</style>
