<template>
    <div class="testdiv">
        <div class="divborder newSearchDiv">
            <div class="filter">
                <label>
                    <span>安装人员</span>：
                </label>
                <Select v-model="name" label-in-value clearable class="searchSelect">
                    <Option v-for="item in userList" :value="item.id" :key="item.id">{{item.name}}</Option>
                </Select>
            </div>
            <div class="filter">
                <label>
                    <span>{{$t(8098)}}</span>：
                </label>
                <DatePicker
                    type="datetimerange"
                    format="yyyy-MM-dd HH:mm:ss"
                    v-model="index_time"
                    @on-change="getTime"
                    class="searchInput MaxWidth"
                ></DatePicker>
            </div>
            <div class="filter">
                <label>
                    <span>序号</span>：
                </label>
                <Input class="searchInput" type="text" v-model="serials" clearable />
            </div>
            <div class="searchBtn">
                <Button
                    type="primary"
                    class="twoWord"
                    icon="ios-search"
                    @click="search()"
                >{{$t('1001')}}</Button>
                <Button
                    type="primary"
                    class="fourWord"
                    icon="ios-cloud-download"
                    @click="Export()"
                >{{$t('1007')}}</Button>
            </div>
        </div>
        <div class="divborder newTableDiv">
            <Table
                class="tableClass"
                @on-current-change="handleRowChange"
                highlight-row
                :data="tableData"
                :columns="tableColumns"
            ></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 moment from 'moment';
import axios from '@/libs/api.request';

export default {
    data () {
        return {
            name: 0,
            userList: [],
            serials: '',
            index_time: [],
            page: 1,
            pagesize: 10,
            total: 0,
            tableData: [],
            tableColumns: [
                {
                    type: 'index2',
                    title: 'ID',
                    key: 'xuhao',
                    render: (h, params) => {
                        return h(
                            'span',
                            params.index + (this.page - 1) * this.pagesize + 1
                        );
                    }
                },
                {
                    title: this.$t(1008),
                    key: 'serialNo'
                },
                {
                    title: '刀具名称',
                    key: 'name'
                },
                {
                    title: '料号',
                    key: 'materialCode'
                },
                {
                    title: '供应商',
                    key: 'supplier'
                },
                {
                    title: '安装位置',
                    key: 'location'
                },
                {
                    title: '刀位号',
                    key: 'position'
                },
                {
                    title: '安装人员',
                    key: 'installer'
                },
                {
                    title: '安装时间',
                    key: 'installTime'
                },
                {
                    title: '加工件数',
                    key: 'processCount'
                },
                {
                    title: '卸载时间',
                    key: 'uninstallTime'
                },
                {
                    title: '卸载原因',
                    key: 'reason'
                }
            ]
        };
    },
    created () {
        this.index_time = [
            moment(new Date().getTime() - 3600 * 24 * 1000 * 7).format(
                'yyyy-MM-DD '
            ) + '00:00:00',
            moment(new Date().getTime()).format('yyyy-MM-DD ') + '23:59:59'
        ];
        this.getUser();
        this.search();
        this.tableColumns = this.$time.initTableTitle(this.tableColumns);

    },
    methods: {
        search () {
            let starttime = '',
                endtime = '';
            starttime = this.index_time[0] ? this.index_time[0] : '';
            endtime = this.index_time[1] ? this.index_time[1] : '';

            var data = {
                Code: this.serials,
                Begin: starttime,
                End: endtime,
                Uid: this.name,
                page: this.page,
                pagesize: this.pagesize
            };
            this.request('/tlm/ToolInstall/GetToolUseData', data, 'post').then(
                (res) => {
                    console.log(res.data);
                    if (res.data.ret == 1) {
                        this.total = res.data.total;
                        this.tableData = res.data.data;
                    }
                }
            );
        },
        getUser () {
            let data = {
                page: 1,
                pagesize: 9999
            };
            this.request('/config/user/GetUsers', data, 'get').then((res) => {
                if (res.data.ret == 1) {
                    this.userList = res.data.data;
                }
            });
        },
        getTime (val) {
            this.index_time = val;
        },
        Export () {
            let starttime = '',
                endtime = '';
            starttime = this.index_time[0] ? this.index_time[0] : '';
            endtime = this.index_time[1] ? this.index_time[1] : '';

            var data = {
                Code: this.serials,
                Begin: starttime,
                End: endtime,
                Uid: this.name,
                page: this.page,
                pagesize: this.pagesize
            };
            this.request(
                '/tlm/ToolInstall/ExportToolUseData',
                data,
                'post'
            ).then((res) => {
                // console.log(res.data);
                if (res.data.ret == 1) {
                    window.location.href =
                        axios.publicPath + '' + res.data.data;
                } else {
                    this.$Message.error(this.$t(res.data.msg));
                }
            });
        },
        handleRowChange (currentRow, oldCurrentRow) {},
        handlePageSize (value) {
            this.pagesize = value;
            this.search();
        },
        changePage (value) {
            this.page = value;
            this.search();
        },
        request (url, data, type) {
            if (type == 'get') {
                return this.axios
                    .request({
                        url: url,
                        params: data,
                        method: type
                    })
                    .then((res) => {
                        return res;
                    })
                    .catch((error) => error);
            } else {
                return this.axios
                    .request({
                        url: url,
                        data,
                        method: type
                    })
                    .then((res) => {
                        return res;
                    })
                    .catch((error) => error);
            }
        }
    }
};
</script>

<style>
</style>
