<!--
 * @Descripttion: 当前组件信息
 * @version: 1.0.0
 * @Author: 无尘
 * @Date: 2019-03-20 14:36:37
 * @LastEditors  : 无尘
 * @LastEditTime : 2019-12-23 17:25:49
 -->

<!--

 -->
<template>
  <div class="app-detail-wrap">
    <div>
      <common-detail-top :topMenuData="topMenuData" :activeId="activeId"></common-detail-top>
      <div class="app-detail-pane border-box p-20">
        <div class="m-b-20">
          <div class="switch-wrap">
            <div class="flex flex-space-between">
              <div class="flex">
                <el-input placeholder="请输入考勤组/人员名称" v-model="conditionObj.attenceGroupName" class="w-264" style="width: 264px;" prefix-icon="el-icon-search" @keyup.native="value => toInput(value, conditionObj.attenceGroupName)" clearable @clear="clearInput"></el-input>
                <el-date-picker class="search-date m-l-10" @change="changeDate" 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>
              </div>
              <div><el-button type="primary" @click="toTableSet">设置报表</el-button><el-button class="p-l-10" type="primary" @click="toUpdateSet">更新报表</el-button></div>
            </div>
          </div>
        </div>
        <div>
          <el-table class="select-table" ref="multipleTable" :data="tableData" tooltip-effect="dark" :style="{ width: '100%' }">
            <el-table-column :fixed="pItem.categoryName == '基本信息' ? true : false" v-for="(pItem, pIndex) in tableHeader" :label="pItem.categoryName" :key="pIndex + 'cate'">
              <el-table-column v-for="(item, index) in pItem.fieldList" :label="item.fieldName" :key="index + 'field'" :prop="item.relateId"> </el-table-column>
            </el-table-column>
          </el-table>
          <div class="block common-wrap__page text-right m-t-24" v-if="tableData.length != 0">
            <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="currentPage" :page-sizes="[20, 40, 60, 80]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total"> </el-pagination>
          </div>
        </div>
      </div>
    </div>
    <day-field-select :showDayDialog="showDayDialog" @hideDayDialog="hideDayDialog"> </day-field-select>
  </div>
</template>
<script>
import commonDetailTop from '@/components/app/common-detail-top.vue';
import dayFieldSelect from '@/components/app/dailyAttendance/day-field-select.vue';
// import showMsg from '@/common/js/showmsg';
import errMsg from '@/common/js/error';
import { _debounce } from '@/common/js/public';
import { postRequest } from '@/api/api';
export default {
  name: 'app-detail',
  components: {
    commonDetailTop,
    dayFieldSelect
  },
  props: {
    brandId: {
      type: String,
      default() {
        return '';
      }
    },
    tabType: {
      type: String,
      default() {
        return '1';
      }
    },
    activeGroupId: {
      type: String,
      default() {
        return '';
      }
    }
  },
  data() {
    return {
      projectName: '', // 当前项目名=
      activeBrand: this.brandId,
      activeGroup: this.activeGroupId, // 商户(品牌) groupId
      enterpriseId: JSON.parse(localStorage.getItem('userInfo')).enterpriseId,
      userId: JSON.parse(localStorage.getItem('userInfo')).userId,
      activeId: '1',
      topMenuData: [
        {
          id: '1',
          name: '每日统计'
        }
      ],
      conditionObj: {
        attenceGroupName: '',
        choiceDate: ''
      },
      tableHeader: [],
      tableData: [], // 列表数据
      currentPage: 1,
      pageSize: 20,
      total: 0,
      // 新建班次
      showDayDialog: false,
      choiceDateCopy: [],
      pickerOptions: {
        onPick: ({ maxDate, minDate }) => {
          this.choiceDateCopy = [minDate.getTime()];
          if (maxDate) {
            this.choiceDateCopy = [];
          }
        },
        disabledDate: time => {
          if (this.choiceDateCopy.length) {
            const one = 30 * 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;
        }
      }
    };
  },

  methods: {
    /* eslint-disable */
    // 选择日期
    changeDate(val) {
      let that = this;
      if (!val) {
        that.choiceDateCopy = [];
        that.conditionObj.choiceDate = [];
      }
      that.currentPage = 1;
      that.getTableList();
    },

    /**
     * 编辑字段
     */
    toTableSet() {
      const that = this;
      that.showDayDialog = true;
    },
    /*
     *
     */
    hideDayDialog() {
      const that = this;
      that.showDayDialog = false;
    },
    /**
     * 输入
     */
    toInput: _debounce(function(e, value) {
      const that = this;
      that.currentPage = 1;
      that.getTableList(that.brandId);
    }, 500),
    // 搜索清除
    clearInput() {
      const that = this;
      that.currentPage = 1;
      that.getTableList(that.brandId);
    },
    changeCondition() {
      const that = this;
      that.currentPage = 1;
      that.getTableList(that.brandId);
    },

    /**
     * 获取日统计字段
     */
    getDayFields(brandId) {
      const that = this;
      let para = {
        brandId: brandId || that.activeBrand,
        enterpriseId: that.enterpriseId,
        userId: that.userId,
        showType: 1
      };
      postRequest('/haoban-app-attence-web/attence-show-field-list', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
            that.tableHeader = resData.result || [];
            that.getTableList();
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },

    /**
     * 分页---页码变化
     */
    handleSizeChange(val) {
      const that = this;
      that.pageSize = val;
      that.getTableList(that.brandId);
    },

    /**
     * 分页---当前页变化
     */
    handleCurrentChange(val) {
      const that = this;
      that.currentPage = val;
      that.getTableList(that.brandId);
    },

    /**
     * 更新列表数据
     */
    toUpdateSet() {
      const that = this;
      that.currentPage = 1;
      that.getTableList(that.brandId);
    },

    /**
     * 获取列表数据
     */
    getTableList(brandId) {
      const that = this;
      if (!that.conditionObj.choiceDate) {
        that.conditionObj.choiceDate = []
      }
      let para = {
        brandId: brandId || that.activeBrand,
        enterpriseId: that.enterpriseId,
        userName: that.conditionObj.attenceGroupName,
        startDate: that.conditionObj.choiceDate[0],
        endDate: that.conditionObj.choiceDate[1],
        pageNum: that.currentPage,
        pageSize: that.pageSize
      };
      postRequest('/haoban-app-attence-web/attence-total-day', para)
        .then(res => {
          let resData = res.data;
          if (resData.errorCode == 1) {
              that.tableData = resData.result || [];
              that.total = resData.result.total;
            return;
          }
          errMsg.errorMsg(resData);
        })
        .catch(function(error) {
          that.$message.error({
            duration: 1000,
            message: error.message
          });
        });
    },
  },
  watch: {
    brandId: function(newData, oldData) {
      const that = this;
      if(!!newData) {
        that.activeBrand = newData;
      }
    },
    activeGroupId: function(newData, oldData) {
      let that = this;
      if (!!newData) {
        that.activeGroup = newData;
      }
    },
    tabType: function(newData, oldData) {
      const that = this;

    }
  },
   mounted() {
    const that = this;
    that.$emit('showTab', '11');
    if(that.brandId && that.brandId != 1) {
      that.activeBrand = that.brandId;
    }
    that.getDayFields(that.brandId);
  },

};
</script>
<style lang="less" scoped>
.w-500 {
  width: 500px;
}

.w-195 {
  width: 195px;
}

.color-1890ff {
  color: #1890ff;
}

.p-20 {
  padding: 20px;
}

.p-l-38 {
  padding-left: 38px;
}

.app-detail-wrap {
  height: 100%;
  background: #fff;
  .el-tabs {
    background: #fff;
    /deep/ .el-tabs__nav-wrap {
      /* height: 48px;
      line-height: 48px; */
      &::after {
        height: 1px;
      }
      /* .el-tabs__nav-scroll {
        padding-left: 20px;
      } */
    }
  }
  .condition-tip {
    width: 740px;
    .el-alert--info {
      background: #E6F7FF;
      border: 1px solid rgba(145,213,255,1);
      .el-alert__icon {
        font-size: 12px;
      }
    }
  }
}
</style>
