<template>
    <div class="testdiv">
        <div class="divborder newSearchDiv">
            <div class="filter">
                <label>
                    <span>姓名</span>：
                </label>
                <Input class="searchInput" type="text" v-model="name" />
            </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="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: '',
            page: 1,
            pagesize: 10,
            total: 0,
            tableData: [],
            tableColumns: [
                {
                    type: 'index2',
                    title: this.$t(1008),
                    key: 'xuhao',
                    render: (h, params) => {
                        return h(
                            'span',
                            params.index + (this.page - 1) * this.pagesize + 1
                        );
                    }
                },
                {
                    title: '姓名',
                    key: 'name'
                },
                {
                    title: '工号',
                    key: 'work_code'
                },
                {
                    title: '领用数量',
                    key: 'collar_number'
                },
                {
                    title: '刀具柜领用数量',
                    key: 'cabinet_number'
                },
                {
                    title: '安装数量',
                    key: 'install_number'
                },
                {
                    title: '安装领用比',
                    key: 'proportion'
                }
            ],
            index_time: []
        };
    },
    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.search();
    },
    methods: {
        search () {
            let starttime = '',
                endtime = '';
            starttime = this.index_time[0] ? this.index_time[0] : '';
            endtime = this.index_time[1] ? this.index_time[1] : '';
            console.log(starttime)
            console.log(endtime);

            var data = {
                starttime: starttime,
                endtime: endtime,
                name: this.name,
                page: this.page,
                pagesize: this.pagesize,
                toexcel: 0
            };
            this.request(
                '/ims/ToolReplaceReturn/GetToolUseCount',
                data,
                'post'
            ).then((res) => {
                // console.log(res.data);
                if (res.data.ret == 1) {
                    this.total = res.data.total;
                    this.tableData = res.data.data;
                }
            });
        },
        Export () {
            let starttime = '',
                endtime = '';
            starttime = this.index_time[0] ? this.index_time[0] : '';
            endtime = this.index_time[1] ? this.index_time[1] : '';

            var data = {
                starttime: starttime,
                endtime: endtime,
                name: this.name,
                page: this.page,
                pagesize: this.pagesize,
                toexcel: 1
            };
            this.request(
                '/ims/ToolReplaceReturn/GetToolUseCount',
                data,
                'post'
            ).then((res) => {
                if (res.data.ret === 1) {
                    window.location.href =
                        axios.publicPath + '' + res.data.data;
                } else {
                    this.$Message.error(this.$t(res.data.msg));
                }
            });
        },
        getTime (val) {
            this.index_time = val;
        },
        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>
