<template>
  <div class="testdiv">
    <div class="divborder" style="margin:0px;">
        <div class="filter">
          <label >{{$t('1132')}}：</label>
          <Cascader :data="datalevel" :value="leveldata" trigger="click" change-on-select style="display:inline-block;width:200px;" @on-change="getlevel"></Cascader>
      </div>
        <div class="filter">
         <label >{{$t('200207')}}</label>
        <Select v-model="section" label-in-value @on-change="sectionchange" clearable style="width:160px;" >
              <Option v-for="item in sectionlist" :value="item.id" :key="item.id">{{ item.title }}</Option>
          </Select>
        </div>
         <div class="filter">
         <label >{{$t('200179')}}:</label>
         <Select v-model="group" label-in-value clearable style="width:160px;" >
              <Option v-for="item in grouplist" :value="item.id" :key="item.id">{{ item.name }}</Option>
          </Select>
        </div>
         <div class="filter">
         <label >{{$t('200208')}}</label>
         <Select v-model="user" label-in-value clearable style="width:160px;" >
              <Option v-for="item in userlist" :value="item.id" :key="item.id">{{ item.name }}</Option>
          </Select>
        </div>
         <div class="filter">
         <label >{{$t('900908')}}:</label>
         <DatePicker type="datetimerange" :value="dateTime" placeholder="Select date" style="width: 300px;"  @on-change="selectdate"></DatePicker>
        </div>
          <Button type="primary" style="margin:0 10px" icon="ios-search" @click="search(0)">{{$t('1001')}}</Button>
          <Button type="primary" style="margin:0 10px" icon="ios-cloud-download" @click="search(1)">{{$t('1007')}}</Button>
    </div>
    <div class="divborder">
      <Table
        style="margin-top:10px"
        @on-current-change="handleRowChange"
        highlight-row
        :data="tableData1"
        @on-selection-change="handleSelectRow"
        :columns="tableColumns1"></Table>
    </div>
    <div style="margin: 10px;overflow: hidden">
      <div style="float: right;">
        <Page
          :total="total"
          :current="page"
          @on-change="changePage"
          show-elevator
          show-sizer
          show-total
          @on-page-size-change="handlePageSize"
          :page-size-opts="[10,20,30,40,100]"
        ></Page>
      </div>
    </div>
  </div>
</template>
<script>
import moment from 'moment'
import axios from '@/libs/api.request'
export default {
    name: 'producetime', // 组件不刷新
    data () {
        return {
            leveldata: [], // 产线层级集合
            levelid: 0, // 产线层级id
            datalevel: [],
            tableData1: [],
            detailobj: {},
            treeContents: [],
            sectionlist: [],
            section: 0,
            group: 0,
            grouplist: [],
            user: 0,
            userlist: [],
            dateTime: '',
            total: 0,
            page: 1,
            pagesize: 10,
            tableColumns1: [
                {
                    type: 'index',
                    title: this.$t('1008'),
                    key: 'xuhao',
                    render: (h, params) => {
                        return h(
                            'span',
                            params.index + 1
                        )
                    },
                    width: '60px'
                },
                {
                    title: this.$t('200003'),
                    key: 'location'
                },
                {
                    title: this.$t('900176'),
                    key: 'workcode'
                },
                {
                    title: this.$t('100002'),
                    key: 'username'
                },
                {
                    title: this.$t('100000'),
                    key: 'departname'
                },
                {
                    title: this.$t('101092'),
                    key: 'groupname'
                },
                {
                    title: this.$t('950289'),
                    key: 'stationname'
                },
                {
                    title: this.$t('950302'),
                    key: 'startworktime'
                },
                {
                    title: this.$t('100039'),
                    key: 'endworktime'
                },
                {
                    title: this.$t('950303') + '(h)',
                    key: 'totaltime'
                }
            ]
        }
    },
    created () {
        this.initlevel()
        this.getsections()
        this.inittime()
        this.search(0)
    },
    methods: {
    // 产线层级
        initlevel () {
            axios.request({
                url: 'config/ProjectLevelSection/GetSectionTree',
                method: 'get'
            }).then(res => {
                const arr = []
                this.datalevel = res.data.data
                arr.push(res.data.data[0].value)
                this.leveldata = arr
            })
        },
        // 产线层级选择
        getlevel: function (data) {
            this.levelid = data[data.length - 1]
        },
        getLevels () {
            this.xTypeList = []
            axios.request({
                url: '/config/ProjectLevelSection/GetLevelsBySection?sectionId=' + this.levelid,
                method: 'get'
            }).then(res => {
                // 产线层级
                if (res.data.ret === 1 && this.xtype === 1) {
                    if (res.data.data.length !== 0) {
                        res.data.data.forEach(item => {
                            this.xTypeList.push({ value: item.id, label: item.title })
                        })
                        this.type3 = this.xTypeList[0].value
                    } else {
                        this.showStr = false
                    }
                }
            })
        },
        inittime () {
            // 时间赋值
            var date = new Date()
            this.dateTime = [moment(date.getTime() - 3600 * 1000 * 24 * 15).format('YYYY-MM-DD ') + '00:00:00', moment(date.getTime()).format('YYYY-MM-DD HH:mm:ss')]
        },
        selectdate (e) {
            this.dateTime = [e[0], e[1]]
        },
        getsections () {
            axios.request({
                url: '/config/ProjectUser/getUserDepartment',
                method: 'get'
            }).then(res => {
                this.sectionlist = res.data.data
            })
        },
        getgroups () {
            axios.request({
                url: '/config/WorkingGroup/GetAllWorkGroup?departid=' + this.section,
                method: 'get'
            }).then(res => {
                this.grouplist = res.data.data
            })
        },
        getusers () {
            axios.request({
                url: '/config/ProjectUser/getUserListsBySectionId?sectionid=' + this.section,
                method: 'get'
            }).then(res => {
                this.userlist = res.data.data
            })
        },
        sectionchange () {
            this.getgroups()
            this.getusers()
        },
        search (toexcel) {
            var data = {
                sectionid: this.leveldata.length === 0 ? 0 : this.leveldata[this.leveldata.length - 1],
                userid: this.user,
                departid: this.section,
                groupid: this.group,
                starttime: moment(this.dateTime[0]).format('YYYY-MM-DD HH:mm:ss'),
                endtime: moment(this.dateTime[1]).format('YYYY-MM-DD HH:mm:ss'),
                page: this.page,
                pagesize: this.pagesize,
                toexcel: toexcel
            }
            axios
                .request({
                    url: '/cnc/WorkingSign/GetSignRecord',
                    data,
                    method: 'post'
                })
                .then(res => {
                    if (toexcel === 1) {
                        if (res.data.ret === 1) {
                            window.location.href = axios.publicPath + '' + res.data.data
                        } else {
                            this.$Message.error(this.$t(res.data.msg))
                        }
                    } else {
                        this.tableData1 = []

                        if (res.data.data != null && res.data.data.length > 1) {
                            this.total = res.data.total
                            this.mockTableData1(res.data.data)
                        }
                    }
                })
        },
        mockTableData1 (tablelist) {
            let data = []
            tablelist.forEach(item => {
                if (item) {
                    data.push({
                        location: item.location,
                        workcode: item.workcode,
                        username: item.username,
                        departname: item.departname,
                        groupname: item.groupname,
                        stationname: item.stationname,
                        startworktime: item.startworktime,
                        endworktime: item.endworktime,
                        totaltime: Math.round(item.totaltime / 60 * 100) / 100,
                        id: item.id
                    })
                }
            })
            this.tableData1 = data
        },
        handleSelectRow: function (data) {
            var ids = []
            for (var i in data) {
                ids.push(data[i].id)
            }
            this.selectids = ids
        },
        handleRowChange (currentRow, oldCurrentRow) {
            this.detailobj = currentRow
        },
        handlePageSize (value) {
            this.pagesize = value
            this.tableData1 = this.search(this.page, this.pagesize)
        },
        changePage (value) {
            this.page = value
            this.tableData1 = this.search(this.page, this.pagesize)
        }
    }
}
</script>
<style lang="less">

</style>
