<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="178px"
                    label-position="right"
                    size="small"
                    style="max-width: 500px;"
                >
                    <el-form-item prop="workflow" label="流程">
                        <el-select v-model="formData.workflow" clearable filterable placeholder="请选择">
                            <el-option
                                v-for="item in workflowOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item prop="applicantUscc" label="申请人统一社会信用代码">
                        <el-input v-model="formData.applicantUscc"></el-input>
                    </el-form-item>
                    <el-form-item prop="inventorIdno" label="第一发明人身份证">
                        <el-input v-model="formData.inventorIdno"></el-input>
                    </el-form-item>
                    <el-form-item prop="deduction" label="是否请求费减">
                        <el-switch v-model="formData.deduction"></el-switch>
                    </el-form-item>
                    <el-form-item prop="essentialReview" label="是否请求实质审查">
                        <el-switch v-model="formData.essentialReview"></el-switch>
                    </el-form-item>
                    <el-form-item prop="publicInAdvance" label="是否申请提前公开">
                        <el-switch v-model="formData.publicInAdvance"></el-switch>
                    </el-form-item>
                    <el-form-item prop="chapterAcceptanceNotice" label="是否申请红章受理通知书">
                        <el-switch v-model="formData.chapterAcceptanceNotice"></el-switch>
                    </el-form-item>
                    <el-form-item prop="chapterPaymentNotice" label="是否申请红章缴费通知书">
                        <el-switch v-model="formData.chapterPaymentNotice"></el-switch>
                    </el-form-item>
                    <el-form-item prop="decideReply" label="是否决定答复">
                        <el-switch v-model="formData.decideReply"></el-switch>
                    </el-form-item>
                    <el-form-item prop="isClientReady" label="准备答复意见">
                        <el-switch v-model="formData.isClientReady"></el-switch>
                    </el-form-item>
                    <el-form-item prop="replyPassed" label="是否通过答复">
                        <el-switch v-model="formData.replyPassed"></el-switch>
                    </el-form-item>
                    <el-form-item prop="registerNotice" label="办登通知日">
                        <el-date-picker
                            v-model="formData.registerNotice"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="选择日期"
                        >
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item prop="registerEndDate" label="办登截止日">
                        <el-date-picker
                            v-model="formData.registerEndDate"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="选择日期"
                        >
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item prop="feeRemark" label="费用备注">
                        <el-input v-model="formData.feeRemark"></el-input>
                    </el-form-item>
                    <el-form-item prop="payment" label="是否已缴费">
                        <el-switch v-model="formData.payment"></el-switch>
                    </el-form-item>
                    <el-form-item prop="annualFee" label="年费">
                        <el-input-number type="number" v-model="formData.annualFee"></el-input-number>
                    </el-form-item>
                    <el-form-item prop="feePaymentPeriod" label="年费支付期限">
                        <el-date-picker
                            v-model="formData.feePaymentPeriod"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="选择日期"
                        >
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item prop="bill" label="账单">
                        <el-input v-model="formData.bill"></el-input>
                    </el-form-item>

                    <el-form-item prop="name" label="专利名称">
                        <el-input v-model="formData.name"></el-input>
                    </el-form-item>
                    <el-form-item prop="status" label="状态">
                        <el-select v-model="formData.status" clearable filterable placeholder="请选择">
                            <el-option
                                v-for="item in statusOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item prop="type" label="专利类型">
                        <el-select v-model="formData.type" clearable filterable placeholder="请选择">
                            <el-option
                                v-for="item in typeOptions"
                                :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="clientPartnerId" label="客户id">
                        <el-input-number type="number" v-model="formData.clientPartnerId"></el-input-number>
                    </el-form-item>
                    <el-form-item prop="supplierPartnerId" label="供应商">
                        <el-input-number type="number" v-model="formData.supplierPartnerId"></el-input-number>
                    </el-form-item>
                    <el-form-item prop="supplierNo" label="供应商案号">
                        <el-input v-model="formData.supplierNo"></el-input>
                    </el-form-item>
                    <el-form-item prop="supplierSubmitPeriod" label="供应商提交期限">
                        <el-date-picker
                            v-model="formData.supplierSubmitPeriod"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="选择日期"
                        >
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item prop="applicantName" label="申请人名称">
                        <el-input v-model="formData.applicantName"></el-input>
                    </el-form-item>
                    <el-form-item prop="applicantEnName" label="申请人英文名称">
                        <el-input v-model="formData.applicantEnName"></el-input>
                    </el-form-item>
                    <el-form-item prop="applicantAddress" label="申请人地址">
                        <el-input v-model="formData.applicantAddress"></el-input>
                    </el-form-item>
                    <el-form-item prop="applicantEnAddress" label="申请人英文地址">
                        <el-input v-model="formData.applicantEnAddress"></el-input>
                    </el-form-item>
                    <el-form-item prop="inventorName" label="发明人名称">
                        <el-input v-model="formData.inventorName"></el-input>
                    </el-form-item>
                    <el-form-item prop="inventorEnName" label="发明人英文名称">
                        <el-input v-model="formData.inventorEnName"></el-input>
                    </el-form-item>
                    <el-form-item prop="priorityNo" label="优先权号">
                        <el-input v-model="formData.priorityNo"></el-input>
                    </el-form-item>
                    <el-form-item prop="priorityDate" label="优先权日">
                        <el-date-picker
                            v-model="formData.priorityDate"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="选择日期"
                        >
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item prop="priorityCountry" label="优先权国别">
                        <el-input v-model="formData.priorityCountry"></el-input>
                    </el-form-item>
                    <el-form-item prop="submitPeriod" label="提交期限/内部期限">
                        <el-date-picker
                            v-model="formData.submitPeriod"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="选择日期"
                        >
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item prop="officialPeriod" label="官方期限">
                        <el-date-picker
                            v-model="formData.officialPeriod"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="选择日期"
                        >
                        </el-date-picker>
                    </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 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: 'DomesticPatentEdit',
    created() {
        if (this.$route.query.id) {
            this.$http
                .get('domesticPatent/getDTO/' + this.$route.query.id)
                .then(res => {
                    this.formData = res;
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
        }
    },
    data() {
        return {
            saving: false,
            formData: {},
            rules: {},
            workflowOptions: [
                { label: '待添加供应商', value: 'ADD_SUPPLIERS' },
                { label: '待提交供应商材料', value: 'SUPPLIER_MATERIALS' },
                { label: '待维护案件', value: 'MAINTAIN_CASE' },
                { label: '待审查', value: 'PENDING_REVIEW' },
                { label: '待办登', value: 'PENDING_REGISTER' },
                { label: '办理登记', value: 'REGISTER' },
                { label: '待维护年费信息', value: 'ANNUAL_FEE' },
                { label: '已完成', value: 'COMPLETED' }
            ],
            statusOptions: [
                { label: '申请阶段', value: 'APPLY_STAGE' },
                { label: '审查阶段', value: 'REVIEW_STAGE' },
                { label: '授权阶段', value: 'GRANT_STAGE' },
                { label: '已完成', value: 'COMPLETED' }
            ],
            typeOptions: [
                { label: '发明专利', value: 'INVENTION' },
                { label: '实用新型专利', value: 'UTILITY_MODEL' },
                { label: '外观设计专利', value: 'APPEARANCE_DESIGN' }
            ]
        };
    },
    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('/domesticPatent/saveDTO', 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(`/domesticPatent/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>
