<template>
  <el-card id="all-card" class="box-card" shadow="hover">
    <div style="position: relative;height: 100%;width: 100%;overflow-y: scroll;padding-bottom:20px">
      <div class="search-tab">
        <div>
          <el-date-picker @change="changeDateRange" v-model="modalSelect.dateRange" value-format="yyyy-MM-dd" format="yyyy-MM-dd"
            type="daterange" align="right" unlink-panels range-separator="至" start-placeholder="开始日期"
            end-placeholder="结束日期" :picker-options="pickerOptions">
          </el-date-picker>
        </div>
        <div>
          <el-select v-model="modalSelect.searchTag" @change="changeTag" placeholder="请选择类型">
            <el-option v-for="item in tagList" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </div>
        <div>
          <el-select style="width:250px" v-model="modalSelect.searchDepartmentId" placeholder="请选择科室">
            <el-option v-for="item in departments" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </div>
        <div>
          <el-button type="primary" @click="getChartOverallList">查询</el-button>
        </div>
      </div>
      <!-- :style="{'height':defaultModalSize.height}" -->
      <div class="search-table">
        <div  v-loading="loading" class="progress-card" element-loading-text="拼命加载中"
      element-loading-spinner="el-icon-loading" element-loading-background="rgba(255, 255, 255, 0.8)">
          <el-table style="width: 100%;" size="mini" :header-cell-style="{
              'font-weight': '600',
              'background': '#fff',
              'color': '#515a6e',
               'text-align':'left'
            }" :height="400" :data="list">
            <el-table-column v-for="(item, index) in table" :key="index" :width="item.width" :label="item.title"
              :prop="item.key" :show-overflow-tooltip="item['show-overflow-tooltip']" header-align="center"
              :align="item.align" :formatter="item.formatter" />
          </el-table>
          <el-pagination
            style="margin-top: 10px;text-align: right;"
            :current-page.sync="modalSelect.page"
            :page-sizes="[20, 30, 40, 50]"
            :page-size.sync="modalSelect.page_size"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            @size-change="e => {
              modalSelect.page_size = e;
              modalSelect.page = 1;
              getChartOverallList();
            }"
            @current-change="e => {
              modalSelect.page = e;
              getChartOverallList();
            }"
          />
        </div>
      </div>

    </div>
  </el-card>
</template>

<script>
  import ElementResize from 'element-resize-detector'
  import {
    defaultModalSize
  } from '@/settings'
  import axios from 'axios'
  import {
    getToken
  } from '@/utils/auth'

  export default {
    name: 'AllTable',
    components: {},
    props: {
      departments: {
        type: Array,
        default: []
      },
      modalSelect: {
        type: Object,
        default: () => {
          return {}
        }
      },

    },
    data() {
      return {
        defaultModalSize,
        tableHeight: 400,
        loading:false,
        list: [],
        total: 0,
        table: [],
        tableList: {
          jiaban: [{
              title: '人员',
              key: 'flow.creator.name',
              width: 100,
              align: 'left',
            },
            {
              title: '原因',
              key: 'yuanyinshuoming',
              align: 'left',
              'show-overflow-tooltip': true,
              minWidth: 200
            },
            {
              title: '时间',
              key: 'kaishiriqi',
              align: 'left',
              'show-overflow-tooltip': true,
              width: 180
            }
          ],
          yongche: [{
              title: '车辆',
              key: 'chepaihao',
              width: 120,
              align: 'left',
            },
            {
              title: '驾驶员',
              key: 'jiashiyuan',
              align: 'left',
              'show-overflow-tooltip': true,
              width: 100,
            },
            {
              title: '目的地',
              key: 'daodadidian',
              align: 'left',
              'show-overflow-tooltip': true,
              minWidth: 200,
            },
            {
              title: '用车时间',
              key: 'yongchekaishiriqi',
              align: 'left',
              'show-overflow-tooltip': true,
              width: 180,
            }
          ],
          qingjia: [{
              title: '人员',
              key: 'flow.creator.name',
              width: 100,
              align: 'left',
            },
            {
              title: '事由',
              key: 'qingjiashiyou',
              align: 'left',
              'show-overflow-tooltip': true,
              minWidth: 200
            },
            {
              title: '时间',
              key: 'kaishishijian',
              align: 'left',
              'show-overflow-tooltip': true,
              width: 180,
            },
          ],
          chuchai: [{
              title: '人员',
              key: 'flow.creator.name',
              width: 120,
              align: 'left',
            },
            {
              title: '去向',
              key: 'mudidi',
              align: 'left',
              'show-overflow-tooltip': true,
              minWidth: 120
            },
            {
              title: '交通方式',
              key: 'jiaotongfangshi',
              align: 'left',
              'show-overflow-tooltip': true,
              minWidth: 120
            },
            {
              title: '出差说明',
              key: 'chuchaishiyou',
              align: 'left',
              'show-overflow-tooltip': true,
            },
            {
              title: '开始日期',
              key: 'kaishi',
              align: 'left',
              'show-overflow-tooltip': true,
              width: 120
            },
            {
              title: '结束日期',
              key: '结束',
              align: 'left',
              'show-overflow-tooltip': true,
              width: 120
            }
          ]
        },
        // 弹窗选择
        tagList: [{
          name: '出差',
          id: 'chuchai',
          type:3
        }, {
          name: '用车',
          id: 'yongche',
          type:3
        }, {
          name: '加班',
          id: 'jiaban',
          type:1
        }, {
          name: '请假',
          id: 'qingjia',
          type:2
        }],



        dateRange: '',
        pickerOptions: {
          shortcuts: [{
            text: '今日',
            onClick(picker) {
              const start = new Date();
              start.setTime(start.getTime());
              picker.$emit('pick', [start, start]);
            }
          }, {
            text: '明日',
            onClick(picker) {
              const start = new Date();
              start.setTime(start.getTime() + 3600 * 1000 * 24);
              picker.$emit('pick', [start, start]);
            }
          }, {
            text: '本周',
            onClick(picker) {
              const end = new Date();
              const now = new Date();
              const day = now.getDay(); // 获取当前是星期几，0 表示周日，1 - 6 表示周一到周六
              const diff = now.getDate() - day + (day === 0 ? -6 : 1);
              const start = new Date(now);
              start.setDate(diff);
              start.setHours(0, 0, 0, 0);
              end.setTime(start.getTime() + 3600 * 1000 * 24 * 6);
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '本月',
            onClick(picker) {
              const end = new Date()
              const now = new Date();
              const year = now.getFullYear();
              const month = now.getMonth() + 1; // getMonth 返回值是 0 - 11，所以加 1 得到实际月份
              const nextMonth = new Date(year, month, 0);
              const start = new Date(now.getFullYear(), now.getMonth(), 1)
              end.setTime(start.getTime() + 3600 * 1000 * 24 * (nextMonth.getDate() - 1));
              picker.$emit('pick', [start, end]);
            }
          }, {
            text: '本年',
            onClick(picker) {
              const now = new Date();
              const start = new Date(now.getFullYear(), 0, 1);
              const end = new Date(now.getFullYear(), 11, 31);
              picker.$emit('pick', [start, end]);
            }
          }]
        },

      }
    },
    computed: {},
    watch: {
      modalSelect(newval){
        console.log("isShowModal",newval)
        this.getChartOverallList()
      }
    },
    created() {
      this.getChartOverallList()
    },
    beforeDestroy() {},
    mounted() {
      this.table = this.tableList['chuchai']
      this.init()
    },
    methods: {

      init() {
        const cardDom = document.getElementById('all-card')
        const cardTitleH = 109
        const elementResize = ElementResize({
          strategy: 'scroll'
        })
        elementResize.listenTo(cardDom, (ele) => {
          this.tableHeight =
            cardDom.getBoundingClientRect().height -
            40 -
            cardTitleH
        })
      },
      // 弹出的总览数据
      async getChartOverallList() {
        try {
          this.loading = true
          this.list = []
          this.total = 0
          const res = await axios.get(`${process.env.VUE_APP_BASE_API}/api/oa/chart/overall-list`, {
            headers: {
              'Authorization': `Bearer ${getToken()}`
            },
            params: {
              star_date: this.modalSelect.start_date,
              end_date: this.modalSelect.end_date,
              department_id: this.modalSelect.searchDepartmentId,
              type: this.modalSelect.type,
              page: this.modalSelect.page,
              page_size: this.modalSelect.page_size
            }
          })
          if (res.status === 200) {
            console.log("res",res)

            this.table = this.tableList[this.modalSelect.searchTag]
            let list = res.data.data.data
            this.total = res.data.data.total
            if(this.modalSelect.searchTag==='yongche'){
              let carList = []
              list.map(a=>{
                if(a.chucheqingkuang){
                  carList.push(...JSON.parse(a.chucheqingkuang))
                }
              })
              this.list = carList
            }else{
              this.list = list
            }

            // this.list.map(item => {
            //   if (item.name == '今日') {
            //     this.progress = item
            //   }
            // })
          }
          this.loading = false
        } catch (err) {
          this.loading = false
        }
      },
      changeDateRange(e) {
        console.log("e", e)
        if (e) {
          this.modalSelect.start_date = e[0] + ' 00:00:00'
          this.modalSelect.end_date = e[1] + ' 23:59:59'
        } else {
          this.modalSelect.start_date = ''
          this.modalSelect.end_date = ''
        }
      },
      changeTag(e){
        this.tagList.map(item=>{
          if(item.id==e){
            this.modalSelect.type = item.type
          }
        })
      }


    }
  }
</script>

<style scoped lang="scss">
  .box-card {
    height: 100%;
  }

  ::v-deep .el-card__body {
    width: 100%;
    height: calc(100% - 58px);
  }

  ::v-deep .el-table th,
  ::v-deep .el-table td {
    border-bottom-style: dashed;
  }

  .clearfix {
    display: flex;
    align-items: center;
  }

  ::v-deep .vxe-modal--content {
    padding: 0;
  }

  .search-tab {
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
    margin-bottom: 10px;

    &>div {
      margin: 5px;
    }
  }
</style>
