<template>
    <el-dialog title="审核" :visible.sync="dialogVisible" width="50%" :before-close="close">
        <el-form :model="formData" ref="ruleForm" :rules="formRules" label-width="100px">
            <el-row>
                <el-col :span="12">
                    <el-form-item label="姓名:" prop="name">
                        <el-input v-model="formData.name" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="年龄:" prop="age">
                        <el-input v-model="formData.age" />
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row>
                <el-col :span="12">
                    <el-form-item label="性别:" prop="gender">
                        <el-select v-model="formData.gender">
                            <el-option label="男" value="1"></el-option>
                            <el-option label="女" value="2"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="手机号:" prop="mobile">
                        <el-input v-model="formData.mobile" />
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row>
                <el-col :span="12">
                    <el-form-item label="身份证号:" prop="idNumber">
                        <el-input v-model="formData.idNumber" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="学历:">
                        <el-select v-model="formData.eduLevel">
                            <el-option label="小学" value="1"></el-option>
                            <el-option label="初中" value="2"></el-option>
                            <el-option label="中专" value="3"></el-option>
                            <el-option label="高中" value="4"></el-option>
                            <el-option label="大专" value="5"></el-option>
                            <el-option label="本科" value="6"></el-option>
                            <el-option label="硕士研究生" value="7"></el-option>
                            <el-option label="博士研究生" value="8"></el-option>
                        </el-select>
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row>
                <el-col :span="12">
                    <el-form-item label="专业:" prop="professional">
                        <el-input v-model="formData.professional" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="从业时长:" prop="workDate">
                        <el-input v-model="formData.workDate" />年
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row>
                <el-col :span="12">
                    <el-form-item label="公司:" prop="company">
                        <el-input v-model="formData.company" />
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="工作岗位:" prop="job">
                        <el-input v-model="formData.job" />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="12">
                    <el-form-item label="擅长品牌:" prop="proficiencyBrand">
                        <multipleBrand v-model="formData.proficiencyBrand" ref="brand"></multipleBrand>
                    </el-form-item>
                </el-col>
                <el-col :span="12">
                    <el-form-item label="擅长电气:" prop="goodElectrical">
                        <el-input v-model="formData.goodElectrical" />
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row>
                <el-col :span="12">
                    <el-form-item label="擅长机械:" prop="goodMechanical">
                        <el-input v-model="formData.goodMechanical" />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col>
                    <el-form-item label="常驻地点:" prop="residentPlace">
                        <el-input v-model="formData.residentPlace" />
                    </el-form-item>
                </el-col>
            </el-row>

            <el-row>
                <el-col>
                    <el-form-item label="工作经历:" prop="workExperience">
                        <el-input v-model="formData.workExperience" />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col>
                    <el-form-item label="项目经历:" prop="projectExperience">
                        <el-input v-model="formData.projectExperience" />
                    </el-form-item>
                </el-col>
            </el-row>
            <el-form-item label="专家服务费" prop="serviceFee">
                <el-input v-model="formData.serviceFee" maxLength="20" style="width:240px"></el-input>
            </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
            <el-button @click="close">取 消</el-button>
            <el-button type="primary" @click="submit" :disabled="submitBtndisabled">确 定</el-button>
        </span>
    </el-dialog>
</template>

<script>
import multipleBrand from '@/apps/mobile/components/multipleBrands';
import { expertApproval } from '@/apps/mobile/api/expert/index';
import { validPrice } from '@/apps/mobile/utils/validate';
export default {
    components: { multipleBrand },
    data() {
        // 验证费用
        var checkServiceFee = (rule, value, callback) => {
            if (value && !validPrice(value)) {
                callback(new Error('格式不正确'));
            } else {
                callback();
            }
        };
        return {
            dialogVisible: false,
            formRules: {
                serviceFee: [
                    {
                        required: true,
                        message: '请输入专家服务费',
                        trigger: 'blur',
                    },
                    {
                        required: true,
                        trigger: 'blur',
                        validator: checkServiceFee,
                        message: '请输入正确的服务费，保留两位小数',
                    },
                ],
            },
            submitBtndisabled: false,
            formData: {},
        };
    },
    methods: {
        changeBrands(val) {
            this.formData.proficiencyBrand = val;
        },
        open(data) {
            this.dialogVisible = true;
            this.submitBtndisabled = false;
            let tempData = JSON.parse(JSON.stringify(data));
            if (tempData.proficiencyBrand) {
                tempData.proficiencyBrand = tempData.proficiencyBrand.split(',').map((item) => {
                    return Number(item);
                });
            }
            this.formData = tempData;
        },
        close() {
            this.dialogVisible = false;
            this.submitBtndisabled = true;
            this.$refs['ruleForm'].resetFields();
            this.formData = {};
        },
        submit() {
            console.log(this.$refs.brand.getSelectedItem())
            let brandName = this.$refs.brand.getSelectedItem().map(item=>{
                return item.name;
            })
            let brandIds = this.formData.proficiencyBrand.join(",");
            this.$refs['ruleForm'].validate((valid) => {
                if (valid) {
                    let obj = {
                        ...this.formData,
                        checkFlag: 1,
                        proficiencyBrand: brandIds,
                        proficiencyBrandName: brandName.join(",")
                    };
                    let {createBy,createTime,updateBy,updateTime, ...newObj} = obj
                    expertApproval(newObj)
                        .then((res) => {
                            if (res.statusCode === '1') {
                                this.$message.success(res.message);
                            } else {
                                this.$message.warning(res.message);
                            }
                            this.$emit('queryListData');
                            this.close();
                        })
                        .catch(() => {
                            this.submitBtndisabled = false;
                        });
                } else {
                    return false;
                }
            });
        },
    },
};
</script>

