<template>
    <div>
        <div class="divborder newSearchDiv">
            <div class="filter">
                <label>{{ $t(800283) }}：</label>
                <Select v-model="type" clearable class="searchSelect">
                    <Option value="1">单号</Option>
                    <Option value="2">批次号</Option>
                    <Option value="3">料号</Option>
                    <Option value="4">序号</Option>
                </Select>
                <Input
                    type="text"
                    placeholder="关键词"
                    class="searchInput"
                    v-model="code"
                    clearable
                ></Input>
            </div>
            <div class="filter">
                <label>{{ $t(1035) }}</label>
                <DatePicker
                    type="datetimerange"
                    format="yyyy-MM-dd HH:mm:ss"
                    :clearable="false"
                    v-model="index_time"
                    @on-change="getTimeFormat"
                    class="searchInput MaxWidth"
                ></DatePicker>
            </div>
            <div class="searchBtn">
                <Button
                    type="primary"
                    class="twoWord"
                    icon="ios-search"
                    @click="search(1, 10)"
                    >{{ $t("1001") }}</Button
                >
                <Button
                    type="success"
                    class="twoWord"
                    @click="downExcel()"
                    icon="md-add-circle"
                    >导出</Button
                >
            </div>
        </div>
        <div class="divborder newTableDiv">
            <Table
                class="tableClass"
                @on-current-change="handleRowChange"
                highlight-row
                :data="tableData1"
                :columns="columns1"
            ></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 {
    data() {
        return {
            action:
                axios.publicPath +
                "tlm/Upload/Submit?templateName=TechToolList",
            headers: {
                token: localStorage.getItem("token"),
                timestamp: localStorage.getItem("timestamp"),
                nonce: localStorage.getItem("nonce"),
                sign: localStorage.getItem("sign"),
            },
            type: "",
            code: "",
            index_time: [],
            columns1: [
                {
                    type: "index2",
                    title: "ID",
                    key: "xuhao",
                    render: (h, params) => {
                        return h(
                            "span",
                            params.index + (this.page - 1) * this.pagesize + 1
                        );
                    },
                    width: "70px",
                },
                {
                    title: "单号",
                    key: "billID",
                },
                {
                    title: "批次号",
                    key: "batch",
                },
                {
                    title: "料号",
                    key: "inventorySN",
                },
                {
                    title: "序号",
                    key: "no",
                },
                {
                    title: "动作",
                    key: "action",
                },
                {
                    title: "操作人",
                    key: "username",
                },
                {
                    title: "操作时间",
                    key: "updatetimeStr",
                },
            ],
            tableData1: [],
            total: 0,
            page: 1,
            pagesize: 10,
            detailobj: {},
        };
    },
    created() {
        this.search(1, 10);
        this.columns1 = this.$time.initTableTitle(this.columns1);
        this.index_time = [
            moment(new Date().getTime() - 3600 * 1000 * 24 * 7).format(
                "YYYY-MM-DD "
            ) + "00:00:00",
            moment(new Date().getTime()).format("YYYY-MM-DD ") + "23:59:59",
        ];
    },
    methods: {
        search(page, pagesize) {
            let starttime = this.index_time[0];
            let endtime = this.index_time[1];
            let data = {
                type: this.type,
                filter: this.code,
                start: starttime,
                end: endtime,
                page: page,
                pageSize: pagesize,
            };
            // this.page = page;
            this.request("/wms/StockHistory/GetStockDetails", data, "get").then(
                (res) => {
                    if (res.data.ret == 1) {
                        this.total = res.data.total;
                        this.tableData1 = res.data.data;
                    }
                }
            );
        },
        getTimeFormat(val) {
            this.index_time = val;
        },
        downExcel() {
            // let data = {
            //     invantorysn: this.material_pn, // 物料编号
            //     storageid: this.storageid, // 仓库id
            //     locationid: this.location, // 储位
            //     businessid: this.supplier,
            // };
            let starttime = this.index_time[0];
            let endtime = this.index_time[1];
            this.request(
                "/wms/StockHistory/ExportStockDetails?type=" +
                    this.type +
                    "&filter=" +
                    this.code +
                    "&start=" +
                    starttime +
                    "&end=" +
                    endtime,
                "",
                "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));
                }
            });
        },
        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);
        },
        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>
