<template>
    <div class="edit-view">
        <el-form
            :model="formData"
            :rules="rules"
            ref="form"
            label-width="100px"
            label-position="right"
            size="small"
            style="max-width: 630px;"
        >
            <el-timeline>
                <el-timeline-item
                    :timestamp="formData.complainAt"
                    placement="top"
                    icon="el-icon-check"
                    type="success"
                    size="normal"
                >
                    <el-collapse v-model="activeNames">
                        <el-collapse-item title="发起投诉" name="1">
                            <div>
                                <el-form-item prop="source" label="来源渠道">
                                    <el-input v-model="formData.source" class="input1" readonly></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
                                    >
                                    </el-date-picker>
                                </el-form-item>
                                <el-form-item prop="name" label="服务对象">
                                    <el-input class="input1" v-model="formData.name" readonly></el-input>
                                </el-form-item>
                                <el-form-item prop="sex" label="性别">
                                    <el-select class="input1" v-model="formData.sex" disabled>
                                        <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></el-input>
                                </el-form-item>
                                <el-form-item prop="content" label="投诉内容">
                                    <el-input type="textarea" :rows="5" readonly v-model="formData.content"></el-input>
                                </el-form-item>
                                <el-form-item prop="title" label="投诉目的">
                                    <el-input type="textarea" :rows="3" readonly v-model="formData.title"></el-input>
                                </el-form-item>
                                <el-form-item prop="file" label="附件">
                                    <file-upload v-model="formData.file" readonly></file-upload>
                                </el-form-item>

                                <el-form-item v-if="!formData.finish">
                                    <el-button @click="$router.go(-1)">返回</el-button>
                                </el-form-item>
                            </div>
                        </el-collapse-item>
                    </el-collapse>
                </el-timeline-item>

                <el-timeline-item
                    :timestamp="formData.reviewAt || formData.complainAt"
                    placement="top"
                    :type="formData.finish ? 'success' : 'primary'"
                    size="normal"
                    :icon="formData.finish ? 'el-icon-check' : 'el-icon-more'"
                >
                    <el-collapse v-model="activeNames">
                        <el-collapse-item :title="formData.finish ? '处理完成' : '处理中'" name="2">
                            <div v-if="formData.finish">
                                <el-form-item prop="nature" label="问题性质">
                                    <el-input class="input1" readonly v-model="formData.nature"></el-input>
                                </el-form-item>
                                <el-form-item prop="unitType" label="涉及的单位">
                                    <el-row>
                                        <el-col :span="11" :offset="0">
                                            <el-select
                                                disabled
                                                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="请选择"
                                                disabled
                                                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
                                                disabled
                                                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="办理情况">
                                    <el-input
                                        type="textarea"
                                        readonly
                                        :rows="5"
                                        v-model="formData.processing"
                                    ></el-input>
                                </el-form-item>
                                <el-form-item prop="situationIsReal" label="情况属实">
                                    <el-radio-group disabled 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="审查附件">
                                    <file-upload readonly v-model="formData.reviewFile"></file-upload>
                                </el-form-item>
                                <el-form-item prop="reviewAt" label="审查时间">
                                    <el-date-picker
                                        disabled
                                        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="$router.go(-1)">返回</el-button>
                                </el-form-item>
                            </div>
                        </el-collapse-item>
                    </el-collapse>
                </el-timeline-item>
            </el-timeline>
        </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;
                    this.$nextTick(() => {
                        if (res.finish) {
                            this.activeNames = '2';
                        } else {
                            this.activeNames = '1';
                        }
                    });
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
        }
        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: ''
        };
    },
    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>
