<template>
    <view class="container">
        <view class="content-container">
            <view class="shop-container">
                <view class="form-label">{{$t('转移到')}}</view>
                <view style="width: 100%" @tap="bindSelectShop">
                    <text v-if="selectedShop">{{ selectedShop.shop_name }}</text>
                    <text v-else style="color: #999">{{$t('请选择转移用户')}}</text>
                </view>
                <text class="btn-transfer" @tap="bindTransfer">{{$t('转移')}}</text>
            </view>
        </view>

        <view class="device-list-container">
            <view class="block-head">
                {{$t('设备信息')}}
                <text style="font-size: 24rpx">({{ $t('共') + devicelist.length }})</text>
            </view>
            <view class="table">
                <view class="tb-head">
                    <view class="tb-row">
                        <view class="tb-column col-1">{{$t('电池编号')}}</view>
                        <view class="tb-column col-2">{{$t('型号')}}</view>
                        <view class="tb-column col-3">{{$t('操作')}}</view>
                    </view>
                </view>
                <view class="tb-body">
                    <view class="tb-row" v-for="(item, index) in devicelist" :key="index">
                        <view class="tb-column col-1">{{ item.battery_sn }}</view>

                        <view class="tb-column col-2">{{ item.dev_name }}</view>

                        <view class="tb-column col-3">
                            <text class="del-btn" @tap="bindDelDevice" :data-index="index">{{$t('删除')}}</text>
                        </view>
                    </view>
                </view>
            </view>
        </view>
    </view>
</template>

<script>
// myPages/pages/deviceTransfer/deviceTransfer.js
var config = require('../../../common/config.js');
var http = require('../../../common/http.js');
var common = require('../../../common/common.js');
var storage = require('../../../common/storage.js');
export default {
    data() {
        return {
            devicelist: [],
            selectedShop: null
        };
    }
    /**
     * 生命周期函数--监听页面加载
     */,
    onLoad: function (options) {
		uni.setNavigationBarTitle({
			title: this.$t('设备转移')
		});
        storage.setSelectedShop(null);
        const devicelist = storage.getBatchSelectedDeviceList();
        if (!devicelist || devicelist.length === 0) {
            uni.navigateBack({
                delta: 1
            });
        }
        this.setData({
            devicelist: devicelist
        });
    },
    onShow: function () {
        const selectedShop = storage.getSelectedShop();
        this.setData({
            selectedShop: selectedShop
        });
    },
    methods: {
        bindSelectShop: function () {
            uni.navigateTo({
                url: '/myPages/pages/selectShop/selectShop'
            });
        },

        bindTransfer: function () {
			const that = this
            if (!this.selectedShop) {
                common.simpleToast(that,that.$t('请选择转移用户'));
                return;
            }
            let batteryList = [];
            this.devicelist.forEach((item) => {
                batteryList.push(item.battery_id);
            });
            const pData = {
                shop_id: this.selectedShop.shop_id,
                batteryList: JSON.stringify(batteryList)
            };
            http.postApi(config.API_BATTERY_TRANSFER, pData, function (resp) {
                if (resp.data.code === 200) {
                    storage.setRefreshDeviceoPage(true);
                    common.simpleToast(that,that.$t('转移成功'));
                    setTimeout(function () {
                        uni.navigateBack({
                            delta: 1
                        });
                    }, 1500);
                } else {
                    common.simpleToast(that,resp.data.msg);
                }
            });
        },

        bindDelDevice: function (e) {
            const index = e.currentTarget.dataset.index;
            let devicelist = [];
            this.devicelist.forEach((item, idx) => {
                if (idx != index) {
                    devicelist.push(item);
                }
            });
            this.setData({
                devicelist: devicelist
            });
        }
    }
};
</script>
<style>
@import './deviceTransfer.css';
</style>
