<template>
    <div class="edit-view">
        <el-form
            :model="formData"
            :rules="rules"
            ref="form"
            label-width="80px"
            label-position="right"
            size="small"
            style="max-width: 820px;"
        >
            <el-timeline>
                <el-timeline-item placement="top" size="normal" timestamp="基本信息">
                    <el-card shadow="hover" :body-style="{ padding: '20px' }">
                        <el-form-item prop="performanceId" label="活动名称">
                            <el-select v-model="formData.performanceId" filterable placeholder="请选择" class="width">
                                <el-option
                                    v-for="item in performances"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                >
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item prop="name" label="节目名称">
                            <el-input v-model="formData.name" class="width"></el-input>
                        </el-form-item>
                        <el-form-item prop="specialtyId" label="参赛专业">
                            <el-cascader
                                ref="artCascader"
                                class="width"
                                v-model="formData.specialtyId"
                                :props="optionProps"
                                @change="change"
                                :options="artTypes"
                                :show-all-levels="false"
                                placeholder="请选择专业"
                            >
                            </el-cascader>
                        </el-form-item>
                        <el-form-item prop="competitionGroup" label="参赛组别">
                            <el-select
                                v-model="formData.competitionGroup"
                                clearable
                                filterable
                                placeholder="请选择"
                                class="width"
                            >
                                <el-option
                                    v-for="item in competitionGroupOptions"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                >
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item prop="levelSettingId" label="参赛级别" v-if="formData.competitionGroup">
                            <el-select
                                v-model="formData.levelSettingId"
                                clearable
                                filterable
                                placeholder="请选择"
                                class="width"
                                v-if="formData.competitionGroup == 'SINGLE'"
                            >
                                <el-option
                                    v-for="item in levelSingleOptions"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                >
                                    <span style="float: left">{{ item.label }}</span>
                                    <span style="float: right; color: #8492a6; font-size: 13px">{{ item.desc }}</span>
                                </el-option>
                            </el-select>
                            <el-select
                                v-model="formData.levelSettingId"
                                clearable
                                filterable
                                placeholder="请选择"
                                class="width"
                                v-else
                            >
                                <el-option
                                    v-for="item in levelCollectiveOptions"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                >
                                    <span style="float: left">{{ item.label }}</span>
                                    <span style="float: right; color: #8492a6; font-size: 13px">{{ item.desc }}</span>
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item
                            prop="durationOfWork"
                            label="作品时长"
                            v-if="
                                code !== 187 &&
                                    code !== 188 &&
                                    code !== 189 &&
                                    code !== 190 &&
                                    code !== 191 &&
                                    code !== 192 &&
                                    code !== 193 &&
                                    code !== 194 &&
                                    code !== 589 &&
                                    code !== 590 &&
                                    code !== 591 &&
                                    code !== 592 &&
                                    code !== 593 &&
                                    code !== 594 &&
                                    code !== 595 &&
                                    code !== 596 &&
                                    code !== 597 &&
                                    code !== 598 &&
                                    code !== 599 &&
                                    code !== 600 &&
                                    code !== 601 &&
                                    code !== 602 &&
                                    code !== 603 &&
                                    code !== 604 &&
                                    code !== 605 &&
                                    formData.specialtyId !== 187 &&
                                    formData.specialtyId !== 188 &&
                                    formData.specialtyId !== 189 &&
                                    formData.specialtyId !== 190 &&
                                    formData.specialtyId !== 191 &&
                                    formData.specialtyId !== 192 &&
                                    formData.specialtyId !== 193 &&
                                    formData.specialtyId !== 194 &&
                                    formData.specialtyId !== 589 &&
                                    formData.specialtyId !== 590 &&
                                    formData.specialtyId !== 591 &&
                                    formData.specialtyId !== 592 &&
                                    formData.specialtyId !== 593 &&
                                    formData.specialtyId !== 594 &&
                                    formData.specialtyId !== 595 &&
                                    formData.specialtyId !== 596 &&
                                    formData.specialtyId !== 597 &&
                                    formData.specialtyId !== 598 &&
                                    formData.specialtyId !== 599 &&
                                    formData.specialtyId !== 600 &&
                                    formData.specialtyId !== 601 &&
                                    formData.specialtyId !== 602 &&
                                    formData.specialtyId !== 603 &&
                                    formData.specialtyId !== 604 &&
                                    formData.specialtyId !== 605
                            "
                        >
                            <el-input-number
                                type="number"
                                v-model="formData.durationOfWork"
                                class="width"
                            ></el-input-number>
                        </el-form-item>
                        <el-form-item prop="instructor" label="指导老师">
                            <el-input v-model="formData.instructor" class="width"></el-input>
                        </el-form-item>
                        <el-form-item prop="instructorPhone" label="联系电话">
                            <el-input v-model="formData.instructorPhone" class="width"></el-input>
                        </el-form-item>
                        <el-form-item
                            prop="annex"
                            label="作品图片"
                            v-if="
                                code == 187 ||
                                    code == 188 ||
                                    code == 189 ||
                                    code == 190 ||
                                    code == 191 ||
                                    code == 192 ||
                                    code == 193 ||
                                    code == 194 ||
                                    code == 589 ||
                                    code == 590 ||
                                    code == 591 ||
                                    code == 592 ||
                                    code == 593 ||
                                    code == 594 ||
                                    code == 595 ||
                                    code == 596 ||
                                    code == 597 ||
                                    code == 598 ||
                                    code == 599 ||
                                    code == 600 ||
                                    code == 601 ||
                                    code == 602 ||
                                    code == 603 ||
                                    code == 604 ||
                                    code == 605 ||
                                    formData.specialtyId == 187 ||
                                    formData.specialtyId == 188 ||
                                    formData.specialtyId == 189 ||
                                    formData.specialtyId == 190 ||
                                    formData.specialtyId == 191 ||
                                    formData.specialtyId == 192 ||
                                    formData.specialtyId == 193 ||
                                    formData.specialtyId == 194 ||
                                    formData.specialtyId == 589 ||
                                    formData.specialtyId == 590 ||
                                    formData.specialtyId == 591 ||
                                    formData.specialtyId == 592 ||
                                    formData.specialtyId == 593 ||
                                    formData.specialtyId == 594 ||
                                    formData.specialtyId == 595 ||
                                    formData.specialtyId == 596 ||
                                    formData.specialtyId == 597 ||
                                    formData.specialtyId == 598 ||
                                    formData.specialtyId == 599 ||
                                    formData.specialtyId == 600 ||
                                    formData.specialtyId == 601 ||
                                    formData.specialtyId == 602 ||
                                    formData.specialtyId == 603 ||
                                    formData.specialtyId == 604 ||
                                    formData.specialtyId == 605
                            "
                        >
                            <single-upload2 v-model="formData.annex"></single-upload2>
                        </el-form-item>
                        <el-form-item prop="video" label="作品视频" v-else>
                            <video-upload v-model="formData.video" class="width"></video-upload>
                        </el-form-item>
                    </el-card>
                </el-timeline-item>

                <el-timeline-item placement="top" size="normal" timestamp="单位信息">
                    <el-card shadow="hover" :body-style="{ padding: '20px' }">
                        <el-form-item prop="gradingOrganizationId" label="考级机构">
                            <el-select
                                v-model="formData.gradingOrganizationId"
                                clearable
                                filterable
                                placeholder="请选择"
                                class="width"
                            >
                                <el-option
                                    v-for="item in gradingOrganizationIdOptions"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                >
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item prop="organizationId" label="承办单位">
                            <el-select
                                v-model="formData.organizationId"
                                clearable
                                filterable
                                placeholder="请选择"
                                class="width"
                            >
                                <el-option
                                    v-for="item in organizationIdOptions"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                >
                                </el-option>
                            </el-select>
                        </el-form-item>
                        <el-form-item prop="examPoint" label="考级点">
                            <el-input v-model="formData.examPoint" class="width"></el-input>
                        </el-form-item>
                        <el-form-item prop="contact" label="联系人">
                            <el-input v-model="formData.contact" class="width"></el-input>
                        </el-form-item>
                        <el-form-item prop="phone" label="联系电话">
                            <el-input v-model="formData.phone" class="width"></el-input>
                        </el-form-item>
                    </el-card>
                </el-timeline-item>

                <el-timeline-item placement="top" size="normal" timestamp="参演人员">
                    <el-card shadow="hover" :body-style="{ padding: '20px' }">
                        <div class="add-bottom">
                            <div class="name">
                                <div class="label">姓名</div>
                            </div>
                            <div class="name">
                                <div class="label">出生年月</div>
                            </div>
                            <div class="name">
                                <div class="label label1">性别</div>
                            </div>
                            <div class="name">
                                <div class="label label1">联系方式</div>
                            </div>
                            <div class="name">
                                <div class="label label2">身份证</div>
                            </div>
                            <div class="name imgBox">
                                <div class="label label1">证件照</div>
                            </div>
                            <div class="name imgBox">
                                <div class="label label3">考级证书</div>
                            </div>
                        </div>
                        <div v-for="(item, index) in participants" :key="index">
                            <div class="add-con" v-if="!item.del">
                                <el-input class="name" v-model="item.name"></el-input>
                                <el-date-picker
                                    style="width: 17.5%"
                                    v-model="item.birthday"
                                    type="date"
                                    value-format="yyyy-MM-dd"
                                    placeholder="选择日期"
                                >
                                </el-date-picker>

                                <el-select class="sex" v-model="item.sex">
                                    <el-option label="男" value="男"></el-option>
                                    <el-option label="女" value="女"></el-option>
                                </el-select>
                                <el-input class="phone" v-model="item.phone"></el-input>
                                <el-input class="focus" v-model="item.idNo"></el-input>
                                <single-upload class="img" v-model="item.img"></single-upload>
                                <single-upload v-model="item.certificate"></single-upload>
                                <div>
                                    <el-button
                                        class="del"
                                        type="danger"
                                        size="small"
                                        icon="el-icon-delete"
                                        @click="onDeleteOtherForm(item, index)"
                                    ></el-button>
                                </div>
                            </div>
                        </div>
                        <el-button type="primary" size="mini" @click="onAddOtherForm()" style="margin: 10px" plain
                            >添加参演人员</el-button
                        >
                    </el-card>
                </el-timeline-item>
            </el-timeline>
        </el-form>
        <div class="btn">
            <el-button @click="onSave(true)" :loading="saving" type="primary">提交</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>
        </div>
    </div>
</template>
<script>
import delChild from '@/mixins/delChild';
import { mapState } from 'vuex';
export default {
    name: 'ProgrammeEdit',
    mixins: [delChild],
    created() {
        if (this.$route.query.id) {
            this.$http
                .get('programme/get/' + this.$route.query.id)
                .then(res => {
                    this.formData = res;
                    console.log(res);
                    this.$http
                        .post(
                            '/participant/all',
                            { size: 100, query: { programmeId: this.$route.query.id } },
                            { body: 'json' }
                        )
                        .then(res => {
                            this.participants = res.content;
                        });
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
        } else if (this.organization) {
            this.formData.organizationId = this.organization.id;
            this.formData.contact = this.organization.owner;
            this.formData.phone = this.organization.ownerPhone;
            if (this.organization.gradingOrganizationId.length > 0) {
                this.formData.gradingOrganizationId = this.organization.gradingOrganizationId[0];
            }
        }
        this.$http
            .post('/setting/byFlag', { flag: 3 })
            .then(res => {
                if (res.length > 0) {
                    res.forEach(item => {
                        this.levelSingleOptions.push({
                            label: item.name,
                            value: item.id,
                            desc: item.code
                        });
                    });
                }
            })
            .catch(e => {
                console.log(e);
                this.$message.error(e.error);
            });
        this.$http
            .post('/setting/byFlag', { flag: 4 })
            .then(res => {
                if (res.length > 0) {
                    res.forEach(item => {
                        this.levelCollectiveOptions.push({
                            label: item.name,
                            value: item.id,
                            desc: item.code
                        });
                    });
                }
            })
            .catch(e => {
                console.log(e);
                this.$message.error(e.error);
            });
        this.$http
            .post('/gradingOrganization/all', { size: 1000, query: { del: false } }, { body: 'json' })
            .then(res => {
                if (res.content.length > 0) {
                    res.content.forEach(item => {
                        this.gradingOrganizationIdOptions.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.organizationIdOptions.push({
                            label: item.name,
                            value: item.id
                        });
                    });
                }
            })
            .catch(e => {
                console.log(e);
                this.$message.error(e.error);
            });
        this.$http
            .post('/artType/allList')
            .then(res => {
                this.artTypes = this.delChild(res);
            })
            .catch(e => {
                console.log(e);
            });
        this.$http
            .post(
                '/performance/all',
                {
                    size: 1000,
                    sort: 'year,desc',
                    query: {
                        publish: true
                    }
                },
                { body: 'json' }
            )
            .then(res => {
                if (res.content.length > 0) {
                    res.content.forEach(item => {
                        this.performances.push({
                            label: item.name,
                            value: item.id
                        });
                    });
                }
            })
            .catch(e => {
                console.log(e);
                this.$message.error(e.error);
            });
    },
    data() {
        return {
            saving: false,
            formData: {},
            code: '',
            rules: {
                phone: [
                    {
                        required: true,
                        pattern: /^1[3-9]\d{9}$/,
                        message: '请输入正确的手机号',
                        trigger: 'blur'
                    }
                ],
                performanceId: [{ required: true, message: '请选择活动', trigger: 'blur' }],
                name: [{ required: true, message: '请输入节目名称', trigger: 'blur' }],
                specialtyId: [{ required: true, message: '请选择参赛专业', trigger: 'blur' }],
                competitionGroup: [{ required: true, message: '请选择参赛组别', trigger: 'blur' }],
                levelSettingId: [{ required: true, message: '请选择参赛级别', trigger: 'blur' }],
                instructor: [{ required: true, message: '请输入指导老师', trigger: 'blur' }],
                gradingOrganizationId: [{ required: true, message: '请选择考级机构', trigger: 'blur' }],
                organizationId: [{ required: true, message: '请选择承办单位', trigger: 'blur' }],
                annex: [{ required: true, message: '请上传作品图片', trigger: 'blur' }],
                video: [{ required: true, message: '请上传作品视频', trigger: 'blur' }],
                contact: [{ required: true, message: '请输入联系人', trigger: 'blur' }]
            },
            competitionGroupOptions: [
                { label: '个人', value: 'SINGLE' },
                { label: '集体', value: 'COLLECTIVE' }
            ],
            levelSingleOptions: [],
            levelCollectiveOptions: [],
            gradingOrganizationIdOptions: [],
            organizationIdOptions: [],
            artTypes: [],
            optionProps: {
                value: 'id',
                label: 'name',
                children: 'children',
                multiple: false,
                emitPath: false,
                checkStrictly: true,
                expandTrigger: 'hover'
            },
            participants: [],
            programmeId: '',
            performances: []
        };
    },
    computed: {
        ...mapState(['organization']),
        saveOtherJson() {
            return [...this.participants]
                .filter(item => {
                    return !!item.name || !!item.sex || !!item.phone;
                })
                .map(item => {
                    return {
                        ...item,
                        programmeId: this.programmeId,
                        performanceId: this.formData.performanceId
                    };
                });

            // return JSON.stringify(participants);
        }
    },
    methods: {
        change(e) {
            this.code = e;
            console.log(this.code);
        },
        onSave(isSubmit) {
            if (isSubmit) {
                this.$refs.form.validate(valid => {
                    if (valid) {
                        this.$confirm('确定提交吗？提交后节目信息不能修改。', '提示', {
                            confirmButtonText: '继续',
                            cancelButtonText: '退出',
                            type: 'warning'
                        }).then(() => {
                            this.submit(isSubmit);
                        });
                    } else {
                        return false;
                    }
                });
            } else {
                this.submit(false);
            }
        },
        submit(isSubmit) {
            let lenght = this.participants.filter(item => !item.del).length;
            let data = { ...this.formData, quantity: lenght };
            if (isSubmit) {
                data.programmeStatus = 'SUBMIT';
            } else {
                data.programmeStatus = 'INITIAL';
            }
            this.saving = true;
            this.$http
                .post('/programme/save', data, { body: 'json' })
                .then(res => {
                    this.saving = false;

                    this.programmeId = res.id;
                    this.$nextTick(() => {
                        this.$http
                            .post('/participant/batchSave', this.saveOtherJson, { body: 'json' })
                            .then(() => {
                                this.$message.success('成功');
                                this.$router.go(-1);
                            })
                            .catch(e => {
                                console.log(e);
                                this.saving = false;
                                this.$message.error(e.error);
                            });
                    });
                })
                .catch(e => {
                    console.log(e);
                    this.saving = false;
                    this.$message.error(e.error);
                });
        },
        onDelete() {
            this.$alert('删除将无法恢复，确认要删除么？', '警告', { type: 'error' })
                .then(() => {
                    return this.$http.post(`/programme/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 || '删除失败');
                    }
                });
        },
        onAddOtherForm() {
            let lenght = this.participants.filter(item => !item.del).length;
            if (this.formData.competitionGroup == 'SINGLE' && lenght > 0) {
                this.$alert('单人组别只能添加一个参赛人员信息', '提示', {
                    confirmButtonText: '确定',
                    callback: action => {
                        this.$message({
                            type: 'info',
                            message: '添加失败'
                        });
                    }
                });
            } else {
                this.participants.push({
                    name: '',
                    sex: '',
                    birthday: '',
                    phone: '',
                    img: ''
                });
            }
        },
        onDeleteOtherForm(info, index) {
            this.$alert('删除将无法恢复，确认要删除么？', '警告', { type: 'warning' })
                .then(() => {
                    const participants = [...this.participants];
                    if (info.id) {
                        info.del = true;
                        participants[index] = info;
                    } else {
                        participants.splice(index, 1);
                    }
                    this.participants = participants;
                    this.$message.success('删除成功');
                })
                .catch(e => {});
        }
    }
};
</script>
<style lang="less" scoped>
.edit-view {
    padding: 0 0;
    background-color: transparent;
}
/deep/ .el-form-item__label {
    font-weight: bold;
}
/deep/ .el-input__inner {
    border: 1px solid #00000069 !important;
}
/deep/ .el-timeline-item {
    padding-bottom: 35px;
}

.btn {
    position: fixed;
    bottom: 0;
    width: 100%;
    background: #ffffff;
    height: 50px;
    line-height: 50px;
    z-index: 999;
    margin-left: -20px;
    padding-left: 20px;
}
.label {
    text-align: center;
    height: 40px;
    width: 70px;
    line-height: 40px;
    font-weight: bold;
    font-size: 13px;
    &.label1 {
        width: 100px;
    }
    &.label2 {
        width: 110px;
    }
    &.label3 {
        width: 60px;
    }
}
.add-bottom {
    display: flex;
    align-items: center;
    .name {
        width: 100%;
        background: #c9c9c9;
    }
}
.add-con {
    margin-top: 7px;
    display: flex;
    align-items: center;

    .name {
        width: 70px;
        padding-right: 10px;
    }
    .focus {
        width: 118px;
        padding: 0 10px;
    }
    .phone {
        width: 110px;
        padding-left: 10px;
    }
    .del {
        margin-left: 10px;
        // text-align: right;
    }
    .sex {
        width: 90px;
        margin-left: 10px;
    }
    .img {
        margin-right: 10px;
    }
    /deep/ .el-input__inner {
        text-align: center;
    }
    /deep/ .el-form-item__label {
        float: none;
    }
}
.width {
    width: 260px;
}
/deep/.el-timeline-item__timestamp {
    color: #0561d9;
    line-height: 1;
    font-size: 14px;
}
/deep/.el-timeline-item__node {
    background-color: #5b9bed;
}
/deep/.el-timeline-item__tail {
    border-left: 2px solid #7cace9;
}
</style>
