<template>
    <div class="edit-view">
        <el-form
            :model="formData"
            :rules="rules"
            ref="form"
            label-width="100px"
            label-position="right"
            size="small"
            style="max-width: 600px;"
        >
            <el-collapse v-model="activeNames" accordion>
                <el-collapse-item title="投诉内容" name="1">
                    <div>
                        <el-form-item prop="source" label="来源渠道">
                            <el-input v-model="formData.source" class="input1" :readonly="!!formData.id"></el-input>
                        </el-form-item>
                        <el-form-item prop="complainAt" label="来电时间">
                            <el-date-picker
                                class="input1"
                                v-model="formData.complainAt"
                                type="datetime"
                                value-format="yyyy-MM-dd HH:mm:ss"
                                placeholder="选择日期时间"
                                :readonly="!!formData.id"
                            >
                            </el-date-picker>
                        </el-form-item>
                        <el-form-item prop="name" label="服务对象">
                            <el-input class="input1" v-model="formData.name" :readonly="!!formData.id"></el-input>
                        </el-form-item>
                        <el-form-item prop="sex" label="性别">
                            <el-select class="input1" v-model="formData.sex" :disabled="!!formData.id">
                                <el-option label="男" value="男"></el-option>
                                <el-option label="女" value="女"></el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item prop="phone" label="联系方式">
                            <el-input class="input1" v-model="formData.phone" :readonly="!!formData.id"></el-input>
                        </el-form-item>
                        <el-form-item prop="content" label="投诉内容">
                            <el-input
                                type="textarea"
                                :rows="5"
                                :readonly="!!formData.id"
                                v-model="formData.content"
                            ></el-input>
                        </el-form-item>
                        <el-form-item prop="title" label="投诉目的">
                            <el-input
                                type="textarea"
                                :rows="3"
                                :readonly="!!formData.id"
                                v-model="formData.title"
                            ></el-input>
                        </el-form-item>
                        <el-form-item prop="file" label="附件">
                            <file-upload v-model="formData.file" :readonly="!!formData.id"></file-upload>
                        </el-form-item>

                        <el-form-item v-if="!formData.id">
                            <el-button @click="onSave(true)" :loading="saving" type="primary" v-if="formData.id"
                                >提交</el-button
                            >
                            <el-button @click="onSave(false)" :loading="saving" type="success">保存</el-button>
                            <el-button @click="onDelete" :loading="saving" type="danger" v-if="formData.id"
                                >删除
                            </el-button>
                            <el-button @click="$router.go(-1)">取消</el-button>
                        </el-form-item>
                    </div>
                </el-collapse-item>
                <el-collapse-item title="处理" name="2" v-if="formData.id">
                    <div>
                        <el-form-item prop="nature" label="问题性质" v-if="formData.id">
                            <el-input class="input1" v-model="formData.nature"></el-input>
                        </el-form-item>
                        <el-form-item prop="unitType" label="涉及的单位" v-if="formData.id">
                            <el-row>
                                <el-col :span="11" :offset="0">
                                    <el-select
                                        v-model="formData.unitType"
                                        clearable
                                        class="input1"
                                        filterable
                                        placeholder="请选择单位类型"
                                    >
                                        <el-option
                                            v-for="item in unitTypeOptions"
                                            :key="item.value"
                                            :label="item.label"
                                            :value="item.value"
                                        >
                                        </el-option>
                                    </el-select>
                                </el-col>
                                <el-col :span="13" :offset="0">
                                    <el-select
                                        style="width:100%"
                                        v-model="formData.unitName"
                                        clearable
                                        filterable
                                        placeholder="请选择"
                                        v-if="formData.unitType == 'ORGANIZATION'"
                                    >
                                        <el-option
                                            v-for="item in organizations"
                                            :key="item.value"
                                            :label="item.label"
                                            :value="item.value"
                                        >
                                        </el-option>
                                    </el-select>
                                    <el-select
                                        v-model="formData.unitName"
                                        clearable
                                        style="width:100%"
                                        filterable
                                        placeholder="请选择"
                                        v-if="formData.unitType == 'GRADING_ORGANIZATION'"
                                    >
                                        <el-option
                                            v-for="item in gradingOrganizations"
                                            :key="item.value"
                                            :label="item.label"
                                            :value="item.value"
                                        >
                                        </el-option>
                                    </el-select>
                                </el-col>
                            </el-row>

                            <!-- <el-input v-model="formData.unitName" style="width: 68%"></el-input> -->
                        </el-form-item>

                        <el-form-item prop="processing" label="办理情况" v-if="formData.id">
                            <el-input type="textarea" :rows="5" v-model="formData.processing"></el-input>
                        </el-form-item>
                        <el-form-item prop="situationIsReal" label="情况属实" v-if="formData.id">
                            <el-radio-group v-model="formData.situationIsReal">
                                <el-radio :label="true">是</el-radio>
                                <el-radio :label="false">否</el-radio>
                            </el-radio-group>
                        </el-form-item>
                        <el-form-item prop="reviewFile" label="审查附件" v-if="formData.id">
                            <file-upload v-model="formData.reviewFile"></file-upload>
                        </el-form-item>
                        <el-form-item prop="reviewAt" label="审查时间" v-if="formData.id">
                            <el-date-picker
                                v-model="formData.reviewAt"
                                type="datetime"
                                value-format="yyyy-MM-dd HH:mm:ss"
                                placeholder="选择日期时间"
                            >
                            </el-date-picker>
                        </el-form-item>

                        <el-form-item>
                            <el-button @click="onSave(true)" :loading="saving" type="primary" v-if="formData.id"
                                >提交</el-button
                            >
                            <el-button @click="onSave(false)" :loading="saving" type="success">保存</el-button>
                            <el-button @click="onDelete" :loading="saving" type="danger" v-if="formData.id"
                                >删除
                            </el-button>
                            <el-button @click="$router.go(-1)">返回</el-button>
                        </el-form-item>
                    </div>
                </el-collapse-item>
            </el-collapse>
        </el-form>
    </div>
</template>
<script>
export default {
    name: 'ComplainEdit',
    created() {
        if (this.$route.query.id) {
            this.$http
                .get('complain/get/' + this.$route.query.id)
                .then(res => {
                    this.formData = res;
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });

            this.activeNames = '2';
        }
        this.$http
            .post('/gradingOrganization/all', { size: 1000 }, { body: 'json' })
            .then(res => {
                if (res.content.length > 0) {
                    res.content.forEach(item => {
                        this.gradingOrganizations.push({
                            label: item.name,
                            value: item.id
                        });
                    });
                }
            })
            .catch(e => {
                console.log(e);
                this.$message.error(e.error);
            });
        this.$http
            .post('/organization/all', { size: 1000 }, { body: 'json' })
            .then(res => {
                if (res.content.length > 0) {
                    res.content.forEach(item => {
                        this.organizations.push({
                            label: item.name,
                            value: item.id
                        });
                    });
                }
            })
            .catch(e => {
                console.log(e);
                this.$message.error(e.error);
            });
    },
    data() {
        return {
            saving: false,
            formData: {},
            rules: {
                phone: [
                    {
                        pattern: /^1[3-9]\d{9}$/,
                        message: '请输入正确的手机号',
                        trigger: 'blur'
                    }
                ]
            },
            unitTypeOptions: [
                { label: '承办单位', value: 'ORGANIZATION' },
                { label: '考级机构', value: 'GRADING_ORGANIZATION' },
                { label: '考点', value: 'EXAMINATION' }
            ],
            gradingOrganizations: [],
            organizations: [],
            activeNames: '1'
        };
    },
    methods: {
        onSave(finish) {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.submit(finish);
                } else {
                    return false;
                }
            });
        },
        submit(finish) {
            let data = { ...this.formData, finish: finish };

            this.saving = true;
            this.$http
                .post('/complain/save', data, { body: 'json' })
                .then(res => {
                    this.saving = false;
                    this.$message.success('成功');
                    // this.$router.go(-1);
                    this.$router.replace({
                        query: {
                            id: res.id
                        }
                    });
                })
                .catch(e => {
                    console.log(e);
                    this.saving = false;
                    this.$message.error(e.error);
                });
        },
        onDelete() {
            this.$alert('删除将无法恢复，确认要删除么？', '警告', { type: 'error' })
                .then(() => {
                    return this.$http.post(`/complain/del/${this.formData.id}`);
                })
                .then(() => {
                    this.$message.success('删除成功');
                    this.$router.go(-1);
                })
                .catch(e => {
                    if (e !== 'cancel') {
                        console.log(e);
                        this.$message.error((e || {}).error || '删除失败');
                    }
                });
        }
    }
};
</script>
<style lang="less" scoped>
.edit-view {
    padding: 0;
    background-color: transparent;

    /deep/ .el-collapse-item__header {
        padding: 0 20px;
        background-color: transparent;
    }

    /deep/ .el-collapse-item__wrap {
        padding: 0 20px;
        background-color: transparent;
    }

    /deep/.el-collapse-item {
        background-color: #fff;
        border-radius: 4px;
        &:hover {
            box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
        }
        & + .el-collapse-item {
            margin-top: 20px;
        }
    }
}
</style>
