<template>
    <div>
        <div class="divborder newSearchDiv">
            <div class="filter">
                <label>储位：</label>
                <Select v-model="location" clearable class="searchSelect">
                    <Option
                        v-for="item in locations1"
                        :value="item.id"
                        :key="item.id"
                    >{{ item.name }}</Option>
                </Select>
            </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" icon="ios-build-outline" @click="change()">变更储位</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 {
            location: '', // 储位
            supplier: 0, // 供应商
            // 模态框变量
            stores: [],
            materials: [],
            locations: [],
            locations1: [],
            changedLocation: '',
            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: "batch_number",
                // },
                // {
                //     title: "序号",
                //     key: "serial_number",
                // },
                {
                    title: '名称',
                    key: 'material_name'
                },
                {
                    title: '规格',
                    key: 'material_spec'
                },
                {
                    title: '数量',
                    key: 'quantity'
                }
            ],
            modal: false,
            selection: [],
            tableData1: [],
            total: 0,
            page: 1,
            pagesize: 10,
            detailobj: {}
        };
    },
    created () {
        this.columns1 = this.$time.initTableTitle(this.columns1)
        this.search(1, 10);

        this.getLocation();
    },
    methods: {
        search (page, pagesize) {
            let data = {
                type: 0, // 0:根据储位变更1:批次变更2:序号变更3:批量变更
                pn: '', // 料号
                filter: '',
                locationid: this.location, // 储位
                page: page,
                pageSize: pagesize
            };
            // this.page = page;
            this.request('/wms/Stock/GetStockList', 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) => {
                if (res.data.ret == 1) {
                    this.locations = res.data.data;
                }
            });
        },
        getLocation () {
            this.request(
                '/wms/Location/GetLastLocation',
                { locationid: 0 },
                'get'
            ).then((res) => {
                if (res.data.ret == 1) {
                    this.locations1 = 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) => {
                    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;
        },
        handleRowChange (currentRow, oldCurrentRow) {
            this.detailobj = currentRow;
            // 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>
