<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.running"
                        disabled
                        @click="handleSelect"
                        placeholder="请选择设备"
                        type="select"
                    />

                     <u-select
            v-model="haltState"
            :list="runningList"
            @confirm="runningSubmit"
          ></u-select> -->
                </u-form-item>
                <common-popup :list="runningList" @select="runningSubmit" ref="popup" />
                <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>
                <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.storeName" disabled />
                </u-form-item>
                <u-form-item label="库存价格" :label-width="150">
                    <u-input v-model="form.price" disabled/>
                </u-form-item>
                <u-form-item label="库存数量" :label-width="150">
                    <u-input v-model="form.storeNum" disabled/>
                </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: '',
                storeNum: ''
            },
            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']
                    }
                ]
            },
            // #ifdef MP-WEIXIN
            cursorSpacing: 50,
            // #endif
            // #ifndef MP-WEIXIN
            cursorSpacing: 0,
            // #endif
            sbBackId: '',
            aShow: false,
            address: '',
            sbId: null,
            aList: [
            ]
        };
    },
    onShow() {
    },
    onLoad(options) {
       
        this.form.spareId = options.spareId;
        this.form.spareName = options.spareName;
        this.form.spareNo = options.spareNo;
        this.form.storeId = options.storeId;
        this.form.storeNum = options.storeNum;
        this.form.price = options.price;
        this.form.storeName = options.storeName;
        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);
    },
    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'
            })
        },
        handleSubmit() {
            const params = {
                storeId: this.form.storeId,
                detailList:[{
                    price: this.form.price,
                    spareId: this.form.spareId,
                    storeId: this.form.storeId,
                    num: this.form.num
                }]
            };

            // this.form.photo = a.join(',');
            var that = this;
            this.$refs.uForm.validate((valid) => {
                if (valid) {
                    this.$Http.addInStoreForm(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>
