<template>
    <div v-loading="loading" element-loading-text="跳转中...">
        <div class="communal-title">
            <div>添加车公司资料</div>
        </div>
        <template>
            <el-form :model="addData" :rules="rules" ref="addData" label-width="100px" class="demo-ruleForm">
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="服务评分" prop="Score">
                            <el-select v-model="addData.Score" placeholder="服务评分">
                                <el-option key="1" label="1分" value="1"></el-option>
                                <el-option key="2" label="2分" value="2"></el-option>
                                <el-option key="3" label="3分" value="3"></el-option>
                                <el-option key="4" label="4分" value="4"></el-option>
                                <el-option key="5" label="5分" value="5"></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="单位地区" prop="UnitArea">
                            <el-autocomplete class="inline-input" v-model="addData.UnitArea"
                                :fetch-suggestions="querySearch" placeholder="请输入单位地区" @select="handleSelect"
                                style="width: 290px;"></el-autocomplete>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="单位名称" prop="UnitName">
                            <el-input placeholder="单位名称" v-model="addData.UnitName">
                            </el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="24">
                        <el-form-item label="单位地址" prop="Address">
                            <el-input placeholder="单位地址" v-model="addData.Address">
                            </el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="联系人" prop="Contact">
                            <el-input placeholder="单位地址" v-model="addData.Contact">
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="联系方式" prop="ContactTel">
                            <el-input placeholder="联系方式" v-model="addData.ContactTel">
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col :span="8">
                        <el-form-item label="电子邮箱" prop="ContactEmail">
                            <el-input placeholder="电子邮箱" v-model="addData.ContactEmail">
                            </el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="8">
                        <el-form-item label="传 真" prop="ContactFax">
                            <el-input placeholder="传 真" v-model="addData.ContactFax">
                            </el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="23">
                        <el-form-item label="其他信息" prop="OtherInfo">
                            <el-input type="textarea" :rows="3" placeholder="请输入内容" v-model="addData.OtherInfo"
                                style="width: 80%;"></el-input>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="6">
                        <el-form-item label="有相关的车辆资质" prop="QualificationScore" label-width="130px">
                            <template>
                                <el-radio v-model="addData.QualificationScore" label="A"
                                    style="margin-right: 10px">A</el-radio>
                                <el-radio v-model="addData.QualificationScore" label="B"
                                    style="margin-right: 10px">B</el-radio>
                                <el-radio v-model="addData.QualificationScore" label="C"
                                    style="margin-right: 10px">C</el-radio>
                            </template>
                        </el-form-item>
                    </el-col>
                    <el-col :span="9">
                        <el-form-item label="车辆2-4年新，VIP及以上需要2年新" prop="CarAgeScore" label-width="260px">
                            <template>
                                <el-radio v-model="addData.CarAgeScore" label="A" style="margin-right: 10px">A</el-radio>
                                <el-radio v-model="addData.CarAgeScore" label="B" style="margin-right: 10px">B</el-radio>
                                <el-radio v-model="addData.CarAgeScore" label="C" style="margin-right: 10px">C</el-radio>
                            </template>
                        </el-form-item>
                    </el-col>
                    <el-col :span="9">
                        <el-form-item label="车身干净，无文字图片等" prop="CleanImgScore" label-width="170px">
                            <template>
                                <el-radio v-model="addData.CleanImgScore" label="A" style="margin-right: 10px">A</el-radio>
                                <el-radio v-model="addData.CleanImgScore" label="B" style="margin-right: 10px">B</el-radio>
                                <el-radio v-model="addData.CleanImgScore" label="C" style="margin-right: 10px">C</el-radio>
                            </template>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="6">
                        <el-form-item label="车内整洁、无异味" prop="SmellScore" label-width="130px">
                            <template>
                                <el-radio v-model="addData.SmellScore" label="A" style="margin-right: 10px">A</el-radio>
                                <el-radio v-model="addData.SmellScore" label="B" style="margin-right: 10px">B</el-radio>
                                <el-radio v-model="addData.SmellScore" label="C" style="margin-right: 10px">C</el-radio>
                            </template>
                        </el-form-item>
                    </el-col>
                    <el-col :span="9">
                        <el-form-item label="提前备水，纸巾等" prop="WaterPaperScore" label-width="260px">
                            <template>
                                <el-radio v-model="addData.WaterPaperScore" label="A"
                                    style="margin-right: 10px">A</el-radio>
                                <el-radio v-model="addData.WaterPaperScore" label="B"
                                    style="margin-right: 10px">B</el-radio>
                                <el-radio v-model="addData.WaterPaperScore" label="C"
                                    style="margin-right: 10px">C</el-radio>
                            </template>
                        </el-form-item>
                    </el-col>
                    <el-col :span="9">
                        <el-form-item label="车辆配置高（皮座椅等）" prop="HardwareScore" label-width="170px">
                            <template>
                                <el-radio v-model="addData.HardwareScore" label="A" style="margin-right: 10px">A</el-radio>
                                <el-radio v-model="addData.HardwareScore" label="B" style="margin-right: 10px">B</el-radio>
                                <el-radio v-model="addData.HardwareScore" label="C" style="margin-right: 10px">C</el-radio>
                            </template>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row>
                    <el-col :span="6">
                        <el-form-item label="时间概念强" prop="TimeScore" label-width="130px">
                            <template>
                                <el-radio v-model="addData.TimeScore" label="A" style="margin-right: 10px">A</el-radio>
                                <el-radio v-model="addData.TimeScore" label="B" style="margin-right: 10px">B</el-radio>
                                <el-radio v-model="addData.TimeScore" label="C" style="margin-right: 10px">C</el-radio>
                            </template>
                        </el-form-item>
                    </el-col>
                    <el-col :span="9">
                        <el-form-item label="安全意识高" prop="SafetyScore" label-width="260px">
                            <template>
                                <el-radio v-model="addData.SafetyScore" label="A" style="margin-right: 10px">A</el-radio>
                                <el-radio v-model="addData.SafetyScore" label="B" style="margin-right: 10px">B</el-radio>
                                <el-radio v-model="addData.SafetyScore" label="C" style="margin-right: 10px">C</el-radio>
                            </template>
                        </el-form-item>
                    </el-col>
                    <el-col :span="9">
                        <el-form-item label="司机驾龄时间长，提前熟悉路线" prop="DrivingAgeScore" label-width="210px">
                            <template>
                                <el-radio v-model="addData.DrivingAgeScore" label="A"
                                    style="margin-right: 10px">A</el-radio>
                                <el-radio v-model="addData.DrivingAgeScore" label="B"
                                    style="margin-right: 10px">B</el-radio>
                                <el-radio v-model="addData.DrivingAgeScore" label="C"
                                    style="margin-right: 10px">C</el-radio>
                            </template>
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-form-item>
                    <div style="margin-left: 60%;">
                        <el-button type="primary" @click="addBtn">立即保存</el-button>
                        <router-link to='/home/CarData'>
                            <el-button>取消</el-button>
                        </router-link>
                    </div>


                </el-form-item>
            </el-form>
        </template>
    </div>
</template>
<script>
export default {
    data() {
        var isnumber = (rule, value, callback) => {
            debugger
            var reg = /^1[3456789]\d{9}$/;//小数点左边最高16位，小数点右边最多4位
            if (reg.test(value)) {
                callback();
            } else
                callback(new Error("请输入正确的手机号"));


        };
        return {
            rules: {
                UnitArea: [
                    { required: true, message: '请输入单位地区', trigger: 'blur' },
                    { required: true, message: '请输入单位地区', trigger: 'change' },
                ],
                UnitName: [
                    { required: true, message: '请输入单位名称', trigger: 'blur' },
                ],
                Contact: [
                    { required: true, message: '请输入单位联系人', trigger: 'blur' },
                ],
                ContactTel: [
                    { required: true, message: '请输入单位联系人电话', trigger: 'blur' },
                    { validator: isnumber, message: '请输入正确的手机号', trigger: 'change' }
                ],
            },
            loading: false,
            tableData: [],
            currentPage: 1, // 当前页码
            pageSize: 15,// 每页的数据条数
            input: '',
            token: '',
            userId: 0,
            addData: {
                UnitArea: '',
                UnitName: '',
                Address: '',
                Contact: '',
                ContactTel: '',
                ContactEmail: '',
                ContactFax: '',
                CarDes: '',
                CarPicPaths: '',
                OtherInfo: '',
                Score: '5',
                QualificationScore: 'A',
                CarAgeScore: 'A',
                CleanImgScore: 'A',
                SmellScore: 'A',
                WaterPaperScore: 'A',
                HardwareScore: 'A',
                TimeScore: 'A',
                SafetyScore: 'A',
                DrivingAgeScore: 'A',
                CreateUserId: 0,
                Remark: ''
            },
            unitAreas: [],
            restaurants: [],

        }
    },
    methods: {

        carData() {
            var url = "http://localhost:5256/api/Resource/QuerCarData"
            var that = this
            this.$axios({
                method: 'post',
                url: url,
                headers: {
                    Authorization: 'Bearer ' + this.token
                },
                data: {
                    portType: 1,
                }
            }).then(function (res) {
                console.log(res)
                debugger
                if (res.data.code == 200) {
                    debugger
                    that.tableData = res.data.data;
                    that.tableData.forEach(function (item, index) {
                        that.unitAreas.push({
                            value: item.unitArea,
                        });
                    });
                }
            })
        },
        handleSelect(item) {
            console.log(item);
        },
        addBtn() {
            const that = this;
            debugger
            this.addData.CreateUserId = JSON.parse(localStorage.getItem('userinif')).userInfo.userId
            this.addData.Remark = this.addData.OtherInfo
            that.$refs.addData.validate((valid) => {
                if (valid) {
                    var url = "http://localhost:5256/api/Resource/AddCarData"

                    that.$axios({
                        method: 'post',
                        url: url,
                        headers: {
                            Authorization: 'Bearer ' + that.token
                        },
                        data: that.addData
                    }).then(function (res) {
                        console.log(res)
                        debugger
                        if (res.data.code == 200) {
                            that.$message({
                                message: '添加成功',
                                type: 'success'
                            });
                            that.loading = true;
                            setTimeout(() => {
                                that.$router.push('/home/CarData')
                            }, 3000);
                        } else {
                            that.$message.error('添加失败！');
                        }
                    })
                } else {
                    this.$message.error('请完善信息在保存！');
                    return false;
                }
            })
        },


        querySearch(queryString, cb) {
            var arr = this.unitAreas;
            for (var i = 0; i < arr.length - 1; i++) {
                for (var j = i + 1; j < arr.length; j++) {
                    if (arr[i].value == arr[j].value) {
                        arr.splice(j, 1);
                        //因为数组长度减小1，所以直接 j++ 会漏掉一个元素，所以要 j--
                        j--;
                    }
                }
            }
            var restaurants = arr;
            var results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
            // 调用 callback 返回建议列表的数据
            cb(results);
        },
        createFilter(queryString) {
            return (restaurant) => {
                return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
            };
        },
    },
    mounted() {
        debugger
        this.token = JSON.parse(localStorage.getItem('userinif')).token;
        this.userId = JSON.parse(localStorage.getItem('userinif')).userInfo.userId
        this.carData();
    }
}
</script>
<style>
.communal-list {
    background-color: #fff;
    padding: 10px;
    box-shadow: 0 0 5px #0005;
    border-radius: 10px;
}

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

.communal-box {
    display: flex;
}

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