<template>
    <div class="edit-view">
        <page-title>
            <el-button @click="$router.go(-1)">取消</el-button>
            <el-button @click="del" :loading="$store.state.fetchingData" type="danger" v-if="formData.id">
                删除
            </el-button>
            <el-button @click="onSave" :loading="$store.state.fetchingData" type="primary">保存</el-button>
        </page-title>
        <div class="edit-view__content-wrapper">
            <div class="edit-view__content-section">
                <divider />
                <el-form
                    :model="formData"
                    :rules="rules"
                    ref="form"
                    label-width="136px"
                    label-position="right"
                    size="small"
                    style="max-width: 500px;"
                >
                    <el-form-item prop="userId" label="创建申请人">
                        <el-input-number type="number" v-model="formData.userId"></el-input-number>
                    </el-form-item>
                    <el-form-item prop="name" label="名称">
                        <el-input v-model="formData.name"></el-input>
                    </el-form-item>
                    <el-form-item prop="clientPartnerId" label="客户">
                        <el-select v-model="formData.clientPartnerId" clearable filterable placeholder="请选择">
                            <el-option
                                v-for="item in clientPartnerIdOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item prop="applyStatus" label="案件状态">
                        <el-select v-model="formData.applyStatus" clearable filterable placeholder="请选择">
                            <el-option
                                v-for="item in applyStatusOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item prop="logoWorkflow" label="当前流程">
                        <el-select v-model="formData.logoWorkflow" clearable filterable placeholder="请选择">
                            <el-option
                                v-for="item in logoWorkflowOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item prop="supplierPartnerId" label="供应商">
                        <el-select v-model="formData.supplierPartnerId" clearable filterable placeholder="请选择">
                            <el-option
                                v-for="item in supplierPartnerIdOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item prop="uwNo" label="寰球案号">
                        <el-input v-model="formData.uwNo"></el-input>
                    </el-form-item>
                    <el-form-item prop="applyNo" label="商标申请号">
                        <el-input v-model="formData.applyNo"></el-input>
                    </el-form-item>
                    <el-form-item prop="applyDate" label="商标申请日">
                        <el-date-picker
                            v-model="formData.applyDate"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="选择日期"
                        >
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item prop="payRatio" label="支付比例">
                        <el-select v-model="formData.payRatio" clearable filterable placeholder="请选择">
                            <el-option
                                v-for="item in payRatioOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item prop="contract" label="是否签约">
                        <el-input v-model="formData.contract"></el-input>
                    </el-form-item>
                    <el-form-item prop="accept" label="是否受理">
                        <el-input v-model="formData.accept"></el-input>
                    </el-form-item>
                    <el-form-item prop="correctionPeriod" label="回文补正期限">
                        <el-date-picker
                            v-model="formData.correctionPeriod"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="选择日期"
                        >
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item prop="payment" label="是否缴费完成">
                        <el-input v-model="formData.payment"></el-input>
                    </el-form-item>
                    <el-form-item prop="reviewRequire" label="是否符合审查要求">
                        <el-input v-model="formData.reviewRequire"></el-input>
                    </el-form-item>
                    <el-form-item prop="rejectAll" label="是否全部驳回">
                        <el-input v-model="formData.rejectAll"></el-input>
                    </el-form-item>
                    <el-form-item prop="rejectContent" label="驳回意见">
                        <el-input v-model="formData.rejectContent"></el-input>
                    </el-form-item>
                    <el-form-item prop="rehear" label="是否复审">
                        <el-input v-model="formData.rehear"></el-input>
                    </el-form-item>
                    <el-form-item prop="reHearPayment" label="复审缴费信息">
                        <el-input v-model="formData.reHearPayment"></el-input>
                    </el-form-item>
                    <el-form-item prop="rehearResult" label="复审是否通过">
                        <el-input v-model="formData.rehearResult"></el-input>
                    </el-form-item>
                    <el-form-item class="form-submit">
                        <el-button @click="onSave" :loading="saving" size="default" type="primary">保存 </el-button>
                        <el-button @click="onDelete" :loading="saving" size="default" type="danger" v-if="formData.id"
                            >删除
                        </el-button>
                        <el-button @click="$router.go(-1)" size="default">取消</el-button>
                    </el-form-item>
                </el-form>
            </div>
        </div>
    </div>
</template>
<script>
export default {
    name: 'LogoPatentEdit',
    created() {
        if (this.$route.query.id) {
            this.$http
                .get('logoPatent/get/' + this.$route.query.id)
                .then(res => {
                    this.formData = res;
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
        }
        this.$http
            .post('/partner/all', { size: 1000, query: { del: false } }, { body: 'json' })
            .then(res => {
                if (res.content.length > 0) {
                    res.content.forEach(item => {
                        this.clientPartnerIdOptions.push({
                            label: item.name,
                            value: item.id
                        });
                    });
                }
            })
            .catch(e => {
                console.log(e);
                this.$message.error(e.error);
            });
        this.$http
            .post('/partner/all', { size: 1000, query: { del: false } }, { body: 'json' })
            .then(res => {
                if (res.content.length > 0) {
                    res.content.forEach(item => {
                        this.supplierPartnerIdOptions.push({
                            label: item.name,
                            value: item.id
                        });
                    });
                }
            })
            .catch(e => {
                console.log(e);
                this.$message.error(e.error);
            });
    },
    data() {
        return {
            saving: false,
            formData: {},
            rules: {},
            clientPartnerIdOptions: [],
            applyStatusOptions: [
                { label: '申请阶段', value: 'APPLY_STAGE' },
                { label: '初审阶段', value: 'FIRST_REVIEW_STAGE' },
                { label: '实审阶段', value: 'SUBSTANTIVE_STAGE' },
                { label: '复审阶段', value: 'REVIEW_STAGE' },
                { label: '授权阶段', value: 'GRANT_STAGE' },
                { label: '已完成', value: 'COMPLETED' }
            ],
            logoWorkflowOptions: [
                { label: '是否决定签约', value: 'IS_CONTRACT' },
                { label: '待维护案件', value: 'TO_BE_MAINTAINED' },
                { label: '待受理', value: 'PENDING' },
                { label: '待审查', value: 'PENDING_REVIEW' },
                { label: '驳回处理', value: 'DISMISS' },
                { label: '公告初审', value: 'ANNOUNCEMENTS' },
                { label: '证件办理', value: 'HANDLE' }
            ],
            supplierPartnerIdOptions: [],
            payRatioOptions: [
                { label: '全款', value: 'FULL_AMOUNT' },
                { label: '首付80%', value: 'DOWN_PAYMENT_80' },
                { label: '首付50%', value: 'DOWN_PAYMENT_50' }
            ]
        };
    },
    methods: {
        onSave() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.submit();
                } else {
                    return false;
                }
            });
        },
        submit() {
            let data = { ...this.formData };

            this.saving = true;
            this.$http
                .post('/logoPatent/save', data, { body: 'json' })
                .then(res => {
                    this.saving = false;
                    this.$message.success('成功');
                    this.$router.go(-1);
                })
                .catch(e => {
                    console.log(e);
                    this.saving = false;
                    this.$message.error(e.error);
                });
        },
        onDelete() {
            this.$alert('删除将无法恢复，确认要删除么？', '警告', { type: 'error' })
                .then(() => {
                    return this.$http.post(`/logoPatent/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></style>
