<template>
    <div v-loading="loading" element-loading-text="跳转中..." class="car_add">
        <div class="communal-title">
            <div>机场三字码操作</div>
        </div>
        <div>
            <el-form :model="addData" :rules="rules" ref="addData" label-width="100px" class="demo-ruleForm">
                <div style="display: flex;">
                    <div style="width: 49%;">
                        <el-form-item label="三字码" prop="Three">
                            <el-input placeholder="三字码" v-model="addData.Three">
                            </el-input>
                        </el-form-item>
                    </div>
                    <div style="width: 49%;">
                        <el-form-item label="四字码" prop="Four">
                            <el-input placeholder="四字码" v-model="addData.Four">
                            </el-input>
                        </el-form-item>
                    </div>
                </div>
                <div style="display: flex;">
                    <div style="width: 49%;">
                        <el-form-item label="国家" prop="Country">
                            <el-input placeholder="国家" v-model="addData.Country">
                            </el-input>
                        </el-form-item>
                    </div>

                    <div style="width: 49%;">
                        <el-form-item label="城市" prop="City">
                            <el-input placeholder="城市" v-model="addData.City">
                            </el-input>
                        </el-form-item>
                    </div>
                </div>
                <div style="display: flex;">
                    <div style="width: 49%;">
                        <el-form-item label="机场名" prop="AirPort">
                            <el-input placeholder="机场名" v-model="addData.AirPort">
                            </el-input>
                        </el-form-item>
                    </div>
                    <div style="width: 49%;">
                        <el-form-item label="机场英文名" prop="AirPort_En">
                            <el-input placeholder="机场英文名" v-model="addData.AirPort_En">
                            </el-input>
                        </el-form-item>
                    </div>
                </div>
                <div style="display: flex;">
                    <div style="width: 98%;">
                        <el-form-item label="备注" prop="Remark">
                            <el-input type="textarea" :rows="3" placeholder="请输入内容" v-model="addData.Remark"></el-input>
                        </el-form-item>
                    </div>
                </div>

                <el-form-item>
                    <div style="margin-left: 60%;">
                        <el-button type="primary" @click="addBtn">保存</el-button>
                        <router-link to='/home/ThreeCode'>
                            <el-button>取消</el-button>
                        </router-link>
                    </div>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>
<script>
export default {
    data() {
        return {
            rules: {
                Three: [
                    { required: true, message: '请输入三字码', trigger: 'blur' },
                ],
                Country: [
                    { required: true, message: '请输入国家', trigger: 'blur' },
                ],
                City: [
                    { required: true, message: '请输入城市', trigger: 'blur' },
                ],
                AirPort: [
                    { required: true, message: '请输入机场名', trigger: 'blur' },
                ],
                AirPort_En: [
                    { required: true, message: '请输入机场英文名', trigger: 'blur' },
                ],
            },
            loading: false,
            tableData: [],
            currentPage: 1, // 当前页码
            pageSize: 15,// 每页的数据条数
            input: '',
            token: '',
            userId: 0,
            addData: {
                Status: '',
                Id: 0,
                Three: '',
                Four: '',
                Country: '',
                City: '',
                AirPort: '',
                AirPort_En: '',
                CreateUserId: '',
                Remark: ''
            },
            ThreeCodeData: {}

        }
    },
    methods: {
        ThreeCode() {
            var url = "/api/Resource/QueryThreeCode"
            var that = this
            this.$axios({
                method: 'post',
                url: url,
                headers: {
                    Authorization: 'Bearer ' + this.token
                },
                data: {
                    portType: 1,
                    Three: '',
                    City: '',
                    PageIndex:'0',
                    PageSize:'0'
                }
            }).then(function (res) {
                if (res.data.code == 200) {

                    that.tableData = res.data.data;
                    that.addData.Id = that.$route.query.id;
                    if (that.addData.Id == undefined || that.addData.Id == null) {
                        //添加
                        that.addData.Status = 1
                    } else {
                        //编辑
                        that.upData();
                    }
                }
            }).catch(function (error) {
                that.$message.error("网络错误，请稍后重试");
            });
        },

        handleSelect(item) {
            console.log(item);
        },
        addBtn() {
            const that = this;

            that.addData.CreateUserId = that.userId
            that.$refs.addData.validate((valid) => {
                if (valid) {

                    console.log(that.addData)
                    var url = "/api/Resource/OperationThreeCode"
                    that.$axios({
                        method: 'post',
                        url: url,
                        headers: {
                            Authorization: 'Bearer ' + that.token
                        },
                        data: that.addData
                    }).then(function (res) {
                        console.log(res)

                        if (res.data.code == 200) {
                            that.$message({
                                message: res.data.msg,
                                type: 'success'
                            });
                            that.loading = true;
                            setTimeout(() => {
                                that.$router.push('/home/ThreeCode')
                            }, 3000);
                        } else {
                            that.$message.error(res.data.msg);
                        }
                    })
                } else {
                    this.$message.error('请完善信息在保存！');
                    return false;
                }
            })
        },
        upData() {
            for (var i = 0; i < this.tableData.length; i++) {
                if (this.tableData[i].id == parseInt(this.addData.Id)) {
                    this.ThreeCodeData = this.tableData[i];
                    break;
                }
            }
            this.addData.Status = 2;

            this.addData.Id = this.ThreeCodeData.id;
            this.addData.Three = this.ThreeCodeData.three;
            this.addData.Four = this.ThreeCodeData.four;
            this.addData.Country = this.ThreeCodeData.country;
            this.addData.City = this.ThreeCodeData.city;
            this.addData.AirPort = this.ThreeCodeData.airPort;
            this.addData.AirPort_En = this.ThreeCodeData.airPort_En;
            this.addData.CreateUserId = this.ThreeCodeData.createUserId;
            this.addData.Remark = this.ThreeCodeData.remark;
        },
    },

    mounted() {

        this.token = JSON.parse(localStorage.getItem('userinif')).token;
        this.userId = JSON.parse(localStorage.getItem('userinif')).userInfo.userId
        this.ThreeCode();

    }
}
</script>
<style>
.communal-list {
    background-color: #fff;
    padding: 10px;
    box-shadow: 0 0 5px #0005;
    border-radius: 10px;
}

.car_add .communal-title {
    display: flex;
    font-size: 17px;
    font-weight: 600;
    color: #555;
    margin-bottom: 20px;
    justify-content: space-between;
    align-items: center;
}

.appraise-box {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    margin: 50px 0;
}

.appraise-box>div {
    width: 30%;
}

.communal-box {
    display: flex;
}

.communal-box>button {
    margin-left: 10px;
    padding: 8px 20px;
}

.car_add {
    background-color: #fff;
    padding: 20px;
    box-shadow: 0 0 5px #0005;
    border-radius: 10px;
}

@media screen and (max-width: 1700px) {
    .appraise-box>div {
        width: 48%;
    }

    .appraise-box>div el-form-item__content {
        width: 260px !important;
    }
}
</style>
