<template>
    <el-dialog :title="typeTitle" :visible.sync="dialogCarVisible" width="500px" class="dialog-info">
        <div class="info-wrp">
            <div class="title">车辆信息</div>
            <div class="info-item">
                <span>车牌号：</span>
                <span v-show="actionType=='check'">{{ bike.plateNumber }}</span>
                <span v-show="actionType!='check'">
                    <el-input placeholder="车牌号" v-model="bike.plateNumber" size="small" style="width: 200px;" />
                    <span v-show="type='add'" style="color:red">(选填)</span>
                </span>
            </div>
            <!--<div class="info-item">-->
                <!--<span>电池电压：</span>-->
                <!--<span v-show="actionType=='check'">{{ bike.batteryVoltage }}</span>-->
                <!--<span v-show="actionType!='check'">-->
                    <!--<el-select v-model="bike.batteryVoltage" size="small" placeholder="请选择" style="width: 100px;">-->
                        <!--<el-option v-for="item in voltageOptions" :key="item" :label="item" :value="item"/>-->
                    <!--</el-select>-->
                    <!--V-->
                <!--</span>-->
            <!--</div>-->
            <div class="info-item" v-show="actionType!='add'">
                <span>剩余电量：</span>
                <span>{{ parseFloat(bike.batteryPower).toFixed(0) }}%</span>
            </div>
            <div class="info-item" v-show="actionType!='add'">
                <span>故障状态：</span>
                <span>{{ bike.troublePart || '无故障' }}</span>
            </div>
            <div class="info-item">
                <span>备注：</span>
                <span v-show="actionType=='check'">{{ bike.remark }}</span>
                <span v-show="actionType!='check'">
                    <el-input placeholder="车牌号" v-model="bike.remark" size="small" style="width: 200px;" />
                    <span v-show="type='add'" style="color:red">(选填)</span>
                </span>
            </div>
            <div class="title">定位设备信息</div>
            <div class="info-item">
                <span>设备编号：</span>
                <span v-show="actionType=='check'">{{ bike.boxCode }}</span>
                <span v-show="actionType!='check'">
                    <el-input placeholder="设备编码" v-model="bike.boxCode" size="small" style="width: 200px;" />
                </span>
            </div>
            <div class="title">投放信息</div>
            <div class="info-item">
                <span>投放状态：</span>
                <span v-show="actionType=='check'">{{ bike.putStatus==1 ? '已投放' : '未投放' }}</span>
                <span v-show="actionType!='check'">
                    <el-radio-group v-model="bike.putStatus">
                        <el-radio label="1">已投放</el-radio>
                        <el-radio label="0">未投放</el-radio>
                    </el-radio-group>
                </span>
            </div>
            <div class="info-item" v-show="bike.putStatus==1">
                <span>所属行政区：</span>
                <span v-show="actionType=='check'">{{ bike.putAreaName }}</span>
                <span v-show="actionType!='check'">
                    <select-place ref="SelectPlace" size="small" @submit="setCode"></select-place>
                </span>
            </div>
            <div class="info-item" v-show="bike.putStatus==1">
                <span>投放区域：</span>
                <span v-show="actionType=='check'">{{ bike.manageAreaName }}</span>
                <span v-show="actionType!='check'">
                    <el-select v-model="bike.manageAreaId" size="small" placeholder="请选择" style="width: 200px;">
                        <el-option v-for="item in $root.regionOptions" :key="item.id" :label="item.name" :value="item.id" v-if="item.id!=''"/>
                    </el-select>
                </span>
            </div>
            <div class="info-item" v-show="bike.putStatus==1">
                <span>投放时间：</span>
                <span v-show="actionType=='check'" v-dateformat="bike.putTime"></span>
                <span v-show="actionType!='check'">
                    <el-date-picker v-model="bike.putTime" type="datetime" placeholder="选择日期" value-format="yyyy-MM-dd HH:mm:ss">
                    </el-date-picker>
                </span>
            </div>
            <div class="btn-group">
                <el-row>
                    <el-button size=small @click="dialogCarVisible = false" v-show="actionType!='check'">取消</el-button>
                    <el-button size=small type="primary" v-show="actionType=='edit'" @click="updateBike">保存</el-button>
                    <el-button size=small type="primary" v-show="actionType=='add'" @click="addBike">确定</el-button>
                </el-row>
            </div>
        </div>
    </el-dialog>
</template>

<script>
    import SelectPlace from '@/components/SelectPlace'
    import api from '@/api/bike'
    export default {
        name: "user-details",
        components: {
            SelectPlace
        },
        filters: {
            // troubleFilter(status) {
            //     let troubleMap = ['无故障', '有故障']
            //     return troubleMap[status]
            // }
        },
        data() {
            return {
                dialogCarVisible: false,
                bike: {
                    plateNumber: "",
                    boxCode: "",
                    putStatus: "1",
                    putAreaName: "",
                    putArea: "",
                    putTime: "",
                    batteryVoltage: "",
                    troubleStatus: "0",
                    batteryPower: "0",
                    manageAreaName: "",
                    manageAreaId: ""
                },
                bikeArra: {},
                voltageOptions: ['36', '48', '60', '72'],
                actionType: 'check',
                typeTitle: ''
            }
        },
        methods: {
            open(actionType, data) {
                console.log(data)
                this.dialogCarVisible = true
                this.actionType = actionType
                console.log(this.actionType)
                let textMap = {
                    check: '查看详情',
                    edit: '编辑车辆',
                    add: '添加车辆'
                }
                this.typeTitle = textMap[actionType]

                if(data) {
                    console.log(data)
                    this.bike = data
                    this.bike.putStatus = this.bike.putStatus+''
                    if(!this.bike.batteryPower) this.bike.batteryPower = '0%'
                    setTimeout(() => {
                        this.$refs.SelectPlace.openCode(this.bike.putArea)
                    }, 0)
                } else {
                    this.bike = {
                        plateNumber: "",
                        boxCode: "",
                        putStatus: "1",
                        putArea: "",
                        putTime: "",
                        manageAreaId: ""
                    }
                    // 440000,440100,440106
                    setTimeout(() => {
                        this.$refs.SelectPlace.openCode()
                    }, 0)
                }
            },
            setCode(code) {
                console.log(code)
                this.bike.putArea = code
                // const arr = code.split(',')
                // this.bike.provinceCode = arr[0]
                // this.bike.cityCode = arr[1]
                // this.bike.areaCode = arr[2]
                // console.log(code)
            },
            addBike() {
                api.addBike(this.bike).then(res => {
                    console.log(res)
                    this.$message({
                        type: 'success',
                        message: '添加成功!'
                    });
                    this.dialogCarVisible = false
                    this.bike = {
                        plateNumber: "",
                        boxCode: "",
                        putStatus: "1",
                        putAreaName: "",
                        putArea: "",
                        putTime: "",
                        batteryVoltage: "",
                        troubleStatus: "0",
                        batteryPower: "0"
                    }
                    this.$emit('updateList')
                }).catch(err => {
                    console.log(err)
                })
            },
            updateBike() {
                api.updateBike(this.bike).then(res => {
                    console.log(res)
                    this.$message({
                        type: 'success',
                        message: '修改成功!'
                    });
                    this.dialogCarVisible = false
                    this.$emit('updateList')
                }).catch(err => {
                    console.log(err)
                })
            }
        },
        created() {
            // console.log(this.type)
        }
    }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
    .dialog-info {
        top: -5vh;
    }
    .info-wrp {
        margin-top: -50px;
        .info-item {
            margin-left: 20px;
            margin-bottom: 10px;
            span:first-child {
                display: inline-block;
                width: 85px;
                text-align: right;
            }
            span:last-child {
                /*padding-left: 10px;*/
            }
        }
        .title {
            font-size: 16px;
            margin: 20px 0 10px;
            padding-top: 20px;
            border-top: 1px solid #ccc;
        }
        .btn-group {
            text-align: center;
            margin-top: 20px;
        }
    }
</style>
