<template>
  <div class="testdiv">
    <div class="divborder" style="margin:0px;">
        <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="daterange" :value="dateTime" placeholder="Select date" style="width: 300px;"  @on-change="selectdate"></DatePicker>
        </div>
         <div class="filter">
         <label >{{$t('950288')}}:</label>
          <Select v-model="state" label-in-value clearable style="width:160px;" >
              <Option v-for="item in statelist" :value="item.id" :key="item.id">{{ item.title }}</Option>
          </Select>
        </div>
          <Button type="primary" style="margin:0 10px" icon="ios-search" @click="search(1,10,0)">{{$t('1001')}}</Button>
          <Button type="primary" style="margin:0 10px" icon="ios-cloud-download" @click="search(1,10,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 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>
  </div>
</template>
<script>
import moment from 'moment'
import axios from '@/libs/api.request'
export default {
    name: 'producetime', // 组件不刷新
    data () {
        return {
            tableData1: [],
            total: 0,
            page: 1,
            pagesize: 10,
            detailobj: {},
            treeContents: [],
            sectionlist: [],
            section: 0,
            group: 0,
            grouplist: [],
            user: 0,
            userlist: [],
            state: 0,
            statelist: [],
            dateTime: '',
            tableColumns1: [
                {
                    type: 'index2',
                    title: this.$t('1008'),
                    key: 'xuhao',
                    render: (h, params) => {
                        return h(
                            'span',
                            params.index + (this.page - 1) * this.pagesize + 1
                        )
                    },
                    width: '60px'
                },
                {
                    title: this.$t('200003'),
                    key: 'location',
                    width: '120px'
                },
                {
                    title: this.$t('950288'),
                    key: 'state_name',
                    width: '120px'
                },
                {
                    title: this.$t('100088'),
                    key: 'product_name',
                    width: '100px'
                },
                {
                    title: this.$t('950289'),
                    key: 'workgroup_name'
                },
                {
                    title: this.$t('100002'),
                    key: 'user_name'
                },
                {
                    title: this.$t('1049'),
                    key: 'start_time'
                },
                {
                    title: this.$t('1050'),
                    key: 'end_time'
                },
                {
                    title: this.$t('950290'),
                    key: 'seconds'
                }
            ]
        }
    },
    created () {
        this.getsections()
        this.getstates()
        this.inittime()
        this.search(1, 10, 0)
    },
    methods: {
        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
            })
        },
        getstates () {
            axios.request({
                url: '/cnc/WorkingState/GetAllState',
                method: 'get'
            }).then(res => {
                this.statelist = res.data.data
            })
        },
        sectionchange () {
            this.getgroups()
            this.getusers()
        },
        search (page, pagesize, toexcel) {
            var params = {
                sectionId: this.section,
                userId: this.user,
                workgroupId: this.group,
                state: this.state,
                page: this.page,
                pagesize: this.pagesize,
                timerange: moment(this.dateTime[0]).format('YYYY-MM-DD HH:mm:ss') + ' - ' + moment(this.dateTime[1]).format('YYYY-MM-DD HH:mm:ss'),
                toexcel: toexcel
            }
            axios
                .request({
                    url: '/cnc/WorkingState/MachineStateRecord',
                    params,
                    method: 'get'
                })
                .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.total = res.data.total
                        this.mockTableData1(res.data.data)
                    }
                })
        },
        mockTableData1 (tablelist) {
            let data = []
            tablelist.forEach(item => {
                if (item) {
                    data.push({
                        location: item.location,
                        state_name: item.state_name,
                        product_name: item.product_name,
                        workgroup_name: item.workgroup_name,
                        user_name: item.user_name,
                        start_time: item.start_time,
                        end_time: item.end_time,
                        seconds: this.timechange(item.seconds),
                        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)
        },
        Export () {
            let params = {
            }
            axios.request({
                url: '/config/BeatSetDownLoad/GetTemplates',
                params,
                method: 'get'
            }).then(res => {
                if (res.data.ret === 1) {
                    window.location.href = axios.publicPath + '' + res.data.data
                } else {
                    this.$Message.error(this.$t(res.data.msg));
                }
            })
        },
        timechange (num) {
            var str = ''
            if (num > 86400) {
                let d = Math.floor(num / 86400)
                let h = Math.floor((num % 86400) / 3600)
                let m = Math.floor(((num % 86400) % 3600) / 60)
                let s = ((num % 86400) % 3600) % 60
                str = d + this.$t('1101') + h + this.$t('1102') + m + this.$t('1103') + s + this.$t('1104')
                return str
            } else if (num > 3600) {
                let h = Math.floor(num / 3600)
                let m = Math.floor((num % 3600) / 60)
                let s = (num % 24) % 60
                str = h + this.$t('1102') + m + this.$t('1103') + s + this.$t('1104')
                return str
            } else if (num > 60) {
                let m = Math.floor(num / 60)
                let s = Math.floor(num % 60)
                str = '' + m + this.$t('1103') + s + this.$t('1104')
                return str
            } else {
                str = num + this.$t('1104')
                return str
            }
        }
    }
}
</script>
<style lang="less">
    .treeSelect{
      span{
        background:#2D8CF1;
        color:#fff;
        margin: 0;
        padding: 0 4px;
        border-radius: 3px;
        cursor: pointer;
        vertical-align: top;
      }
}
</style>
