<template>
    <view class="mx3" style="padding-bottom: 100rpx">
        <u-toast ref="uToast" />
        <view class="main-color-bg" style="height: 100rpx; margin: 0 -30rpx"></view>
        <view
            class="p3 bg-color-white"
            style="margin-top: -50rpx; border-radius: 20rpx"
        >
            <u-form :model="form" ref="uForm" >
                <!--<u-form-item label="选择仓库" required prop="running" :label-width="150">
                    <u-input
                        v-model="form.storeName"
                        disabled
                        @click="handleSelect"
                        placeholder="请选择仓库"
                        type="select"
                    />
                </u-form-item>
                <view class="mb-2">
                    <view class="d-flex a-center py2">
                        <image src="/static/images/type.svg" style="width: 36rpx;height: 36rpx;" mode=""></image>
                        <text class="ml-2 f-size-28 f-weight-6">仓库</text>
                    </view>
                    <view class="d-flex j-between a-center border1">
                        <u-input v-model="form.storeName" placeholder="请选择部门" disabled @click="industryTypeShow = true" style="flex: 1;" />
                    </view>
                </view>-->
                <u-form-item label="选择仓库" :label-width="150">
                   <u-input v-model="form.storeName" placeholder="请选择仓库" disabled @click="industryTypeShow = true" style="flex: 1;" />
                </u-form-item>
                <u-select
                    v-model="industryTypeShow"
                    value-name="id"
                    label-name="title"
                    child-name="children"
                    :list="storeTree"
                    mode="mutil-column-auto"
                    @confirm="industryTypeConfirm">
                </u-select>
                <view class="main-color-text f-size-32 f-weight-6 mt-2">备件信息</view>
                <u-form-item label="备件名称" :label-width="150">
                    <u-input v-model="form.spareName" disabled />
                </u-form-item>
                <u-form-item label="备件编号" :label-width="150">
                    <u-input v-model="form.spareNo" disabled />
                </u-form-item>
                <u-form-item label="库存价格" prop="price" required :label-width="150">
                    <u-input v-model="form.price"/>
                </u-form-item>
                <u-form-item label="库存数量" prop="num" required :label-width="150">
                    <u-input v-model="form.num" :cursor-spacing="cursorSpacing"/>
                </u-form-item>
                <!--<u-form-item label="上传图片" :label-width="150">
                    <u-upload
                        width="180"
                        height="180"
                        max-count="6"
                        :action="action"
                        :file-list="fileList"
                        @on-success="uploadSuccess"
                        @on-remove="uploadRemove"></u-upload>
                </u-form-item>-->
            </u-form>

            <!-- <button class="main-color-bg text-color-white" @tap='handleSubmit'>提交</button> -->
            <view class="p-fixed d-flex j-around a-center footer">
                <view
                    class="main-color-bg text-color-white common"
                    @tap="handleSubmit"
                >提交</view
                >
            </view>
        </view>
    </view>
</template>

<script>
export default {
    data() {
        return {
            form: {
                price: '',
                spareName: '',
                spareId: '',
                spareNo: '',
                storeId: '',
                storeName: '',
                num: ''
            },
            storeTree: [],
            industryTypeShow: false,
            data: [],
            badState: false,
            badList: [],
            runningState: false, // 设备选择
            runningList: [],
            haltState: false, // 是否停机
            haltList: [],
            repairState: false, // 报修来源
            repairList: [],
            levelState: false,
            levelList: [],
            imagesList: [],
            action: this.$BaseTool.UserUtil.getUploadUrlIgnores(),
            // action: 'http://39.103.133.141:8000/mock/7/upms/files/upload',
            /*header: {
                Authorization: 'Bearer ' + uni.getStorageSync('accessToken')
            },*/
            fileList: [],
            sourceMap: {},
            levelMap: {},
            statusMap: {},
            needStopMap: {},
            haltDefaultValue: [0],
            rules: {
                num: [
                    {
                        required: true,
                        message: '请填写出库数量',
                        trigger: ['blur', 'change']
                    }
                ],
                price: [
                    {
                        required: true,
                        message: '请填写库存价格',
                        trigger: ['blur', 'change']
                    }
                ]
            },
            // #ifdef MP-WEIXIN
            cursorSpacing: 50,
            // #endif
            // #ifndef MP-WEIXIN
            cursorSpacing: 0,
            // #endif
            backId: '',
            backName: '',
            aShow: false,
            address: '',
            sbId: null,
            aList: [
            ]
        };
    },
    onShow() {
        const pages = getCurrentPages();
        const currPage = pages[pages.length - 1];
        this.backId = currPage.backId;
        this.backName = currPage.backName;
        this.form.storeName = this.backName;
        this.form.storeId = this.backId;
        console.log('this.form.storeName：' + this.form.storeName)
        
    },
    onLoad(options) {
        this.form.spareId = options.spareId;
        this.form.spareName = options.spareName;
        this.form.spareNo = options.spareNo;
        this.sourceMap = this.$DictCache.getLabelByValueMapByType(
            this.$DictCache.TYPE.REPAIR_APPLICATION_FORM_SOURCE
        );
        for (const key in this.sourceMap) {
            this.repairList.push({
                label: this.sourceMap[key],
                value: key
            });
        }
        this.form.repair = this.repairList[0].label;
        this.form.source = this.repairList[0].value;
        this.levelMap = this.$DictCache.getLabelByValueMapByType(
            this.$DictCache.TYPE.REPAIR_APPLICATION_FORM_LEVEL
        );
        for (const key in this.levelMap) {
            this.levelList.push({
                label: this.levelMap[key],
                value: key
            });
        }
        this.form.level = this.levelList[0].label;
        this.form.levelValue = this.levelList[0].value;

        this.statusMap = this.$DictCache.getLabelByValueMapByType(
            this.$DictCache.TYPE.REPAIR_APPLICATION_FORM_STATUS
        );
        this.needStopMap = this.$DictCache.getLabelByValueMapByType(
            this.$DictCache.TYPE.YES_NO
        );
        for (const key in this.needStopMap) {
            this.haltList.push({
                label: this.needStopMap[key],
                value: key
            });
        }
        this.form.halt = this.haltList[this.haltDefaultValue].label;
        this.form.needStop = this.haltList[this.haltDefaultValue].value;
        this.getType()
    // 获取设备数据
    // this.initSb();
    },
    onReady() {
        this.$refs.uForm.setRules(this.rules);
    },
    created() {
        this.$Http.fetchStoreTree().then(res => {
            this.storeTree = res.data
            console.log(3333, this.storeTree)
        })
    },
    methods: {
        //
        confirm(e) {
            console.log(e);
            this.address = ''
            for (let i = 0; i < e.length; i++) {
                this.address += e[i].label
            }
        },
        badSubmit(data) {
            this.form.badType = data[0].label;
            this.form.repairErrorTypeId = data[0].value;
        },
        getType() {
            /* this.$Http
                .fetchErrorTypeTree({
                    id: this.detailId
                })
                .then((res) => {
                    console.log(res.data);
                    const temp = []
                    res.data.forEach(item => {
                        temp.push({
                            label: item.title,
                            value: item.id
                        })
                    })
                    this.badList = temp
                    console.log(this.badList);
                }) */
        },
        handleSelect() {
            uni.navigateTo({
                url: '/pages/search/search-store'
            })
        },
        // 选择仓库
        runningSubmit(data) {
            console.log(data);
            this.form.storeId = data.value;
            this.$Http
                .fetchStore({
                    id: data.value
                })
                .then((res) => {
                    this.form.storeName = res.data.name;
                    this.form.running = res.data.name;
                });
            this.form.running = data.label;
        },
        industryTypeConfirm(e) {
            this.form.storeName = e[e.length - 1].label
            this.form.storeId = e[e.length - 1].value
            console.log('this.form.storeName：' + this.form.storeName)
        },
        handleSubmit() {
            const params = {
                storeId: this.form.storeId,
                price: this.form.price, 
                spareId: this.form.spareId,
                num: this.form.num
            };

            // this.form.photo = a.join(',');
            var that = this;
            this.$refs.uForm.validate((valid) => {
                if (valid) {
                    this.$Http.addSpareStore(params).then((res) => {
                        uni.showToast({ icon: 'none', title: '已提交!', duration: 2000 });
                        setTimeout(function () {
                            uni.navigateTo({
                                url: '/pages/spare/spare-store?detailId=' + that.form.spareId
                            });
                        }, 2000)
                    });
                }
            });
        }
    }
};
</script>

<style lang="less">
.footer {
  bottom: 0;
  left: 0;
  right: 0;
  background-color: #fff;
  z-index: 10000;
  border-top: 1rpx solid #f4f4f4;
  height: 100rpx;

  .common {
    padding: 15rpx 200rpx;
    border-radius: 30rpx;
    background-color: #0082ff;
    color: #fff;
  }
}
</style>
