<template>
  <div class="testdiv">
    <div class="divborder newSearchDiv" style="margin:0px;">
      <div class="filter">
          <label >{{$t('1132')}}：</label>
          <Cascader :data="datalevel" :value="leveldata" trigger="click" change-on-select class="searchInput" @on-change="getlevel"></Cascader>
      </div>
      <div class="filter">
          <label >{{$t('900908')}}：</label>
          <DatePicker type="datetimerange" :value="dateTime" placeholder="" class="searchInput MaxWidth" @on-change="selectdate"></DatePicker>
      </div>
      <div class="filter">
          <label >{{$t('500070')}}：</label>
          <Input class="searchInput" type="text" v-model="Alarmcode" />
      </div>
      <div class="filter">
          <label >{{$t('500071')}}：</label>
          <Input class="searchInput" type="text"  v-model="AlarmContent" />
      </div>
      <div class="searchBtn">
      <Button type="primary" class="twoWord intervalRight" icon="ios-search" @click="tosearch(0)">{{$t('1001')}}</Button>
      <Button type="primary" class="fourWord" icon="ios-cloud-download" @click="tosearch(1)">{{$t('1007')}}</Button>
      </div>
    </div>

    <div class="divborder newTableDiv">
     <Table
       class="tableClass"
      @on-current-change="handleRowChange"
      highlight-row
      :data="tableData1"
      :columns="tableColumns1"
    ></Table>
    <div class="pageDiv">
      <div class="pageDirection">
        <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 axios from '@/libs/api.request'
import moment from 'moment'
export default {
    // name: 'tree_select_page',
    components: {

    },
    data () {
        return {
            leveldata: [], // 产线层级集合
            levelid: 0, // 产线层级id
            tableData1: [], // 列表数据
            dateTime: '', // 时间范围
            startTime: '', // 开始时间
            endTime: '', // 结束时间
            Alarmcode: '', // 报警代码
            AlarmContent: '', // 报警内容
            page: 1,
            pagesize: 10,
            total: 0,
            obj: {},
            treedata1: [],
            datalevel: [],
            tableColumns1: [
                {
                    type: 'index2',
                    title: this.$t('1008'),
                    key: 'xuhao',
                    render: (h, params) => {
                        return h('span', params.index + (this.page - 1) * this.pagesize + 1)
                    },
                    width: 80
                },
                {
                    title: this.$t('200003'),
                    key: 'location',
                    width: '180px'
                },
                {
                    title: this.$t('500070'),
                    key: 'faultcode',
                    width: '90px'
                },
                {
                    title: this.$t('500071'),
                    key: 'message',
                    width: '200px'
                },
                {
                    title: this.$t('200005'),
                    key: 'faultContent',
                    width: '160px'
                },
                {
                    title: this.$t('200010'),
                    key: 'faultReason',
                    width: '150px'
                },
                {
                    title: this.$t('800145'),
                    key: 'faultSolution',
                    width: '150px'
                },
                {
                    title: this.$t('1049'),
                    key: 'startTime',
                    width: '150px'
                },

                {
                    title: this.$t('1050'),
                    key: 'endTime',
                    width: '150px'
                },
                {
                    title: this.$t('500083'),
                    key: 'duration'
                },
                {
                    title: this.$t('400008'),
                    key: 'active'
                },
                {
                    title: this.$t('950000'),
                    key: 'isFault'
                }
            ],
            excel:0, //是否导出
        }
    },
    created () {
        this.inittime()// 初始化
        this.initlevel()
        this.tosearch(0)
        // 格式化表格样式
        this.tableColumns1 = this.$time.initTableTitle(this.tableColumns1)
    },
    methods: {
        inittime () {
            // 时间赋值
            var date = new Date()
            var start = this.timestampToTime(date.getTime() - 3600 * 1000 * 24 * 7)
            var end = this.timestampToTime(date.getTime())
            this.dateTime = [start, end]
        },
        selectdate (e) {
            this.dateTime = [e[0], e[1]];
            let dtStart = e[0];
            let dtEnd = e[1];
            let last_str = dtEnd.split(' ')[1];
            if (last_str == '00:00:00') {
                dtEnd = moment(dtEnd).format('YYYY-MM-DD') + ' 23:59:59';
                this.dateTime = [dtStart, dtEnd];
            }
            this.dateTime = [dtStart, dtEnd];
        },
        // 产线层级
        initlevel () {
            axios.request({
                url: 'config/ProjectLevelSection/GetSectionTree',
                method: 'get'
            }).then(res => {
                const arr = []
                this.datalevel = res.data.data
                let sdata = res.data.data
                if(sdata!=null&&sdata!=undefined&&sdata&&sdata.length >0){
                    arr.push(res.data.data[0].value)
                    this.levelid = res.data.data[0].value;
                }
                this.leveldata = arr
                this.tosearch()
            })
        },
        // 产线层级选择
        getlevel: function (data) {
            //  console.log(data[data.length-1])
            this.levelid = data[data.length - 1]
        },
        tosearch (excel) {
            this.excel = excel
            this.page = 1
            this.pagesize = 10
            this.search()
        },
        search () {
            if (this.dateTime !== '') {
                this.startTime = this.dateTime[0]
                this.endTime = this.dateTime[1]
            }
            var data = {
                SectionID: this.levelid,
                Start: this.startTime,
                End: this.endTime,
                Alarmcode: this.Alarmcode,
                AlarmContent: this.AlarmContent,
                pagesize: this.pagesize,
                page: this.page,
                toexcel:this.excel
            }
            axios.request({
                url: '/cnc/MachineAlarm/GetMachineAlarmList',
                data,
                method: 'post'
            }).then(res => {
                if(this.excel === 1){
                    document.location.href = axios.publicPath + "" + res.data.data;
                    return false;
                }
                
                this.total = 0
                this.tableData1 = []
                if (res.data.ret === 1 && res.data.data.length > 0) {
                    this.total = res.data.total
                    this.mockTableData1(res.data.data)
                }
            })
        },
        mockTableData1 (tablelist) {
            let data = []
            tablelist.forEach(item => {
                var active_str = item.active === 0 ? this.$t('800257') : this.$t('800258')
                var isFault_str = item.isFault === 1 ? this.$t('300030') : this.$t('300031')
                if (item) {
                    data.push({
                        location: item.machineCode,
                        faultcode: item.code,
                        message: item.message,
                        faultContent: item.faultContent,
                        faultReason: item.faultReason,
                        faultSolution: item.faultSolution,
                        startTime: item.startTime,
                        endTime: item.endTime,
                        duration: item.duration,
                        active: active_str,
                        isFault: isFault_str
                    })
                }
            })
            this.tableData1 = data
        },
        handleRowChange (currentRow, oldCurrentRow) {
            this.obj = currentRow
        },
        handlePageSize (value) {
            this.pagesize = value
            this.tableData1 = this.search()
        },
        changePage (value) {
            this.page = value
            this.tableData1 = this.search()
        },
        // 时间戳转换成时间
        timestampToTime (timeStampe) {
            var date = new Date(timeStampe)// 时间戳为10位需*1000，时间戳为13位的话不需乘1000
            var Y = date.getFullYear() + '-'
            var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-'
            var D = (date.getDate() < 10 ? '0' + (date.getDate()) : date.getDate()) + ' '
            var h = (date.getHours() < 10 ? '0' + (date.getHours()) : date.getHours()) + ':'
            var m = (date.getMinutes() < 10 ? '0' + (date.getMinutes()) : date.getMinutes()) + ':'
            var s = (date.getSeconds() < 10 ? '0' + (date.getSeconds()) : date.getSeconds())
            return Y + M + D + h + m + s
        }
    }

}
</script>
<style lang="less" scoped>

</style>
