<template>
    <div class="car_add">
        <div>
            <div class="communal-title">
                <div>{{ title }}</div>
            </div>
        </div>
        <hr style='background-color:#5555; height:1px; border:none;margin: 10px 0;' />
        <div>
            <el-form :model="OpAskData" ref="OpAskData" :rules="OpAskDataRules" label-width="100px" class="demo-ruleForm">
                <div style="display: flex;">
                    <div style="width: 25%;">
                        <el-form-item label="国家:" prop="country" label-width="160px">
                            <el-input clearable placeholder="国家" v-model="OpAskData.country">
                            </el-input>
                        </el-form-item>
                    </div>
                    <div style="width: 25%;">
                        <el-form-item label="地区:" prop="area" label-width="160px">
                            <el-input clearable placeholder="地区" v-model="OpAskData.area">
                            </el-input>
                        </el-form-item>
                    </div>
                    <div style="width: 25%;">
                        <el-form-item label="公务方名称:" prop="unitName" label-width="160px">
                            <el-input clearable placeholder="公务方名称" v-model="OpAskData.unitName">
                            </el-input>
                        </el-form-item>
                    </div>
                    <div style="width: 25%;">
                        <el-form-item label="涉及领域:" prop="field" label-width="160px">
                            <el-input clearable placeholder="涉及领域" v-model="OpAskData.field">
                            </el-input>
                        </el-form-item>
                    </div>
                </div>

                <div style="display: flex;">
                    <div style="width: 80%;">
                        <el-form-item label="请示范例:" prop="talkCase" label-width="160px">
                            <el-input type="textarea" :rows="3" placeholder="请示范例" v-model="OpAskData.talkCase"></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/AskData'>
                            <el-button>取消</el-button>
                        </router-link>
                    </div>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>
<script>
export default {
    data() {
        return {
            title: "新增请示资料",
            token: '',
            OpAskData: {
                status: 1,
                id: 0,
                diId: 0,
                deleName: '',
                country: '',
                area: '',
                unitName: '',
                field: '',
                talkCase: '',
                createUserId: 0,
                remark: ''
            },
            OpAskDataRules: {
                deleName: [
                    { required: true, message: '该信息为必填信息', trigger: 'blur' },
                    { required: true, message: '该信息为必填信息', trigger: 'change' },
                ],
                country: [
                    { required: true, message: '该信息为必填信息', trigger: 'blur' },
                    { required: true, message: '该信息为必填信息', trigger: 'change' },
                ],
                area: [
                    { required: true, message: '该信息为必填信息', trigger: 'blur' },
                    { required: true, message: '该信息为必填信息', trigger: 'change' },
                ],
                unitName: [
                    { required: true, message: '该信息为必填信息', trigger: 'blur' },
                    { required: true, message: '该信息为必填信息', trigger: 'change' },
                ],
                field: [
                    { required: true, message: '该信息为必填信息', trigger: 'blur' },
                    { required: true, message: '该信息为必填信息', trigger: 'change' },
                ],
                talkCase: [
                    { required: true, message: '该信息为必填信息', trigger: 'blur' },
                    { required: true, message: '该信息为必填信息', trigger: 'change' },
                ],
            },
        }
    },

    methods: {

        QueryAskDataById() {
            var url = "/api/Resource/QueryAskDataById"
            var that = this
            this.$axios({
                method: 'post',
                url: url,
                headers: {
                    Authorization: 'Bearer ' + this.token
                },
                data: {
                    Id: that.OpAskData.id,
                }
            }).then(function (res) {

                if (res.data.code == 200) {
                    var dataList = res.data.data
                    that.OpAskData.id = dataList.id
                    that.OpAskData.country = dataList.country
                    that.OpAskData.area = dataList.area
                    that.OpAskData.unitName = dataList.unitName
                    that.OpAskData.field = dataList.field
                    that.OpAskData.talkCase = dataList.talkCase
                    that.OpAskData.remark = dataList.remark
                }

            })
        },
        addBtn() {
            const that = this;
            that.$refs.OpAskData.validate((valid) => {
                if (valid) {
                    var url = "/api/Resource/OpAskData"
                    that.$axios({
                        method: 'post',
                        url: url,
                        headers: {
                            Authorization: 'Bearer ' + that.token
                        },
                        data: that.OpAskData
                    }).then(function (res) {

                        if (res.data.code == 200) {
                            that.$message({
                                message: res.data.msg,
                                type: 'success'
                            });
                            setTimeout(() => {
                                that.$router.push('/home/AskData')
                            }, 3000);
                            that.loading = true;
                        } else {
                            that.$message.error(res.data.msg);
                        }
                    })
                } else {
                    this.$message.error('请完善信息在保存！');
                    return false;
                }
            })
        },
    },

    mounted() {
        debugger
        this.token = JSON.parse(localStorage.getItem('userinif')).token;
        this.OpAskData.createUserId = JSON.parse(localStorage.getItem('userinif')).userInfo.userId
        this.OpAskData.id = parseInt(this.$route.query.id)
        if (this.OpAskData.id != null && this.OpAskData.id != undefined && this.OpAskData.id != 0) {
            this.QueryAskDataById();
            this.title = "修改请示资料";
            this.OpAskData.status = 2
        } else {
            this.OpAskData.status = 1
            this.title = "新增请示资料"
        }

    }
}
</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>