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

<!--

 -->
<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" :style="{ width: 'calc(100% - 20px)' }">
          <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: 'calc(100% - 20px)' }">
            <template v-for="(pItem, pIndex) in tableHeader">
              <el-table-column v-if="pItem.fieldList.length" :fixed="pItem.categoryName == '基本信息' ? true : false" :label="pItem.categoryName" :key="pIndex + 'cate'">
                <template v-for="(item, index) in pItem.fieldList">
                  <el-table-column v-if="item.relateId != 'endClock' && item.relateId != 'startClock' && item.relateId != 'clockLL'" :label="item.fieldName" :key="index + 'field'" :prop="item.relateId" min-width="112"> </el-table-column>
                  <el-table-column v-if="item.relateId == 'startClock'" :label="item.fieldName" :key="index + 'field'" :prop="item.relateId" min-width="112">
                    <template slot-scope="scope">
                      <span v-for="(ele, key) in scope.row.startClock" :key="key + 'clcok'">{{ ele.clockStartTime || '--' }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column v-if="item.relateId == 'endClock'" :label="item.fieldName" :key="index + 'field'" :prop="item.relateId" min-width="112">
                    <template slot-scope="scope">
                      <span v-for="(ele, key) in scope.row.endClock" :key="key + 'clcok'">{{ ele.clockEndTime || '--' }}</span>
                    </template>
                  </el-table-column>
                  <el-table-column v-if="item.relateId == 'clockLL'" :label="item.fieldName" :key="index + 'field'" :prop="item.relateId" min-width="112">
                    <template slot-scope="scope">
                      <span v-for="(ele, key) in scope.row.clockLL" :key="key + 'clcok'"> {{ key == 0 ? '上班：' : '下班：' }}{{ !!ele.longitude ? ele.longitude : '--' }}{{ !!ele.latitude ? ele.latitude : '--' }}<br /></span>
                    </template>
                  </el-table-column>
                </template>
              </el-table-column>
            </template>
          </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: [
        {
          /* clerkCode: '42LjXlS7uoti9Aq',
          groupName: '测试1-1-1-1',
          clockAddress: [],
          userName: '545',
          clockLL: '[]',
          endClock: '[]',
          startClock: '[]',
          times: 1,
          earlyTime: '0',
          lateTime: '0',
          needWorkTime: '120.0',
          overtime: '0',
          workTime: '0.0',
          attenceGroupName: '测试考勤组1',
          attenceType: '1',
          classesTimes: '1',
          workDate: '2019-12-25' */
        }
      ], // 列表数据
      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) {
            let arr = []
            resData.result.forEach(ele => {
              arr.push({
                categoryCode: ele.categoryCode,
                categoryName: ele.categoryName,
                fieldList : ele.fieldList ? ele.fieldList.filter(child => child.selectFlag == 1) : []
              })
            })
            that.tableHeader = arr || [];
            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.getDayFields(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) {
            if (!!resData.result.list && !!resData.result.list.length) {
              resData.result.list.forEach(ele => {
                // ele.clockAddress = !!ele.clockAddress ? JSON.parse(ele.clockAddress) : [];
                ele.clockLL = !!ele.clockLL ? JSON.parse(ele.clockLL) : [];
                ele.endClock = !!ele.endClock ? JSON.parse(ele.endClock) : [];
                ele.startClock = !!ele.startClock ? JSON.parse(ele.startClock) : [];
              })
            }
              that.tableData = resData.result.list || [];
              that.total = resData.result.total;
              console.log(that.tableData)
            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;
}

.el-table{
  /deep/ td.is-hidden > * {
    visibility: unset;
  }
}

.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>
