<template>
    <div>
        <div class="divborder newSearchDiv">
            <div class="filter">
                <label>{{$t(1008)}}：</label>
                 <Input type="text" class="searchInput" v-model="batch_number" />
            </div>
            <div class="searchBtn">
                <Button
                    type="primary"
                    class="twoWord intervalRight"
                    icon="ios-search"
                    @click="search(1,10)"
                >{{$t('1001')}}</Button>
                <Button
                    type="success"
                    class="fourWord"
                    @click="change()"
                    icon="ios-build-outline"
                >变更储位</Button>
            </div>
        </div>
        <div class="divborder newTableDiv">
            <Table
                class="tableClass"
                @on-current-change="handleRowChange"
                highlight-row
                :data="tableData1"
                :columns="columns1"
                @on-selection-change="handleSelect"
            ></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>
        <Modal class="newModalClass" v-model="modal" title="变更储位" :styles="{width:'400px'}">
                <div class="filter">
                    <label>
                        <i>*</i>变更后储位：
                    </label>
                    <Select v-model="changedLocation" clearable class="searchSelect">
                        <Option
                            v-for="item in locations"
                            :value="item.id"
                            :key="item.id"
                        >{{ item.name }}</Option>
                    </Select>
                </div>

            <div slot="footer">
                <Button type="text" size="large" @click="modal = false">{{$t('1033')}}</Button>
                <Button type="primary" size="large" @click="ok()">{{$t('1011')}}</Button>
            </div>
        </Modal>
    </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')
            },
            batch_number: '', // 仓库
            // 模态框变量
            stores: [],
            materials: [],
            locations: [],
            suppliers: [],
            columns1: [
                {
                    type: 'selection',
                    width: 60,
                    align: 'center'
                },
                {
                    type: 'index2',
                    title: 'ID',
                    key: 'xuhao',
                    render: (h, params) => {
                        return h(
                            'span',
                            params.index + (this.page - 1) * this.pagesize + 1
                        );
                    },
                    width: '70px'
                },
                {
                    title: '储位',
                    key: 'storage_location_name',
                    width: 400
                },
                {
                    title: '供应商',
                    key: 'businessName'
                },
                {
                    title: '料号',
                    key: 'material_pn'
                },
                {
                    title: '序号',
                    key: 'serial_number'
                },
                {
                    title: '名称',
                    key: 'material_name'
                },
                {
                    title: '规格',
                    key: 'material_spec'
                },
                {
                    title: '数量',
                    key: 'quantity'
                }
            ],
            modal: false,
            selection: [],
            testData: [],
            returnData: [],
            location1: [],
            location2: [],
            tableData1: [],
            total: 0,
            page: 1,
            pagesize: 10,
            total1: 0,
            page1: 1,
            pagesize1: 10,
            enterData: [],
            type: 0,
            storeArr: [],
            detailobj: {},
            changedLocation: ''
        };
    },
    created () {
        this.search(1, 10);
        this.columns1 = this.$time.initTableTitle(this.columns1)
    },
    methods: {
        search (page, pagesize) {
            let data = {
                type: 2, // 0:根据储位变更1:批次变更2:序号变更3:批量变更
                pn: '', // 料号
                filter: this.batch_number,
                locationid: '', // 储位
                page: page,
                pageSize: pagesize
            };
            // this.page = page;
            this.request('/wms/Location/GetInventory', data, 'get').then(
                (res) => {
                    if (res.data.ret == 1) {
                        this.total = res.data.total;
                        this.tableData1 = res.data.data;
                    }
                }
            );
        },
        change () {
            if (this.selection.length == 0) {
                this.$Message.info('请先选择数据');
                return false;
            }
            let bool = false, id = 0
            for (let i = 0; i < this.selection.length; i++) {
                if (this.selection[i].storageID != this.selection[0].storageID) {
                    bool = true;
                } else {
                    id = this.selection[i].storageID
                }
            }
            if (bool) {
                this.$Message.error('所选物料必须是同一仓库');
                return false;
            }
            this.getLocations(id);
            this.changedLocation = '';
            this.modal = true;
        },
        getLocations (cid) {
            this.request(
                '/wms/Location/GetLastLocation',
                { storageid: cid,
                    locationid: 0 },
                'get'
            ).then((res) => {
                console.log(res.data.data)
                if (res.data.ret == 1) {
                    this.locations = res.data.data;
                }
            });
        },
        ok () {
            if (
                (this.changedLocation == '' ||
                this.changedLocation == undefined ||
                this.changedLocation == 0)
            ) {
                this.$Message.error('请选择变更后储位');
                return false;
            }
            let arr = []
            this.selection.forEach((ele) => {
                arr.push(ele.id);
            });
            let data = {
                locationidafter: this.changedLocation,
                stockIDs: arr
            };
            this.request('/wms/Location/ChangeLocation', data, 'post').then((res) => {
                console.log(res.data.data);
                if (res.data.ret == 1) {
                    this.$Message.success('变更成功');
                    this.modal = false;
                    this.search(1, 10);
                } else {
                    this.$Message.error(this.$t(res.data.msg))
                }
            });
        },
        handleSelect (val) {
            this.selection = val;
            console.log(val);
        },
        handleRowChange (currentRow, oldCurrentRow) {
            this.detailobj = currentRow;
            console.log(this.detailobj);
            // this.getDefendList();
        },
        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>
