<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="121px"
                    label-position="right"
                    size="small"
                    style="max-width: 500px;"
                >
                    <el-form-item prop="patentId" label="专利id">
                        <el-input-number type="number" v-model="formData.patentId"></el-input-number>
                    </el-form-item>
                    <el-form-item prop="feeType" label="费用类型">
                        <el-select v-model="formData.feeType" clearable filterable placeholder="请选择">
                            <el-option
                                v-for="item in feeMaintenanceIdOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item prop="feeName" label="费用名称">
                        <el-select v-model="formData.feeType" clearable filterable placeholder="请选择">
                            <el-option
                                v-for="item in feeMaintenanceIdOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            >
                            </el-option>
                        </el-select>
                    </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="patentPartnerId" label="专利权人">
                        <el-select v-model="formData.patentPartnerId" clearable filterable placeholder="请选择">
                            <el-option
                                v-for="item in patentPartnerIdOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item prop="payPartnerId" label="支付对象">
                        <el-select v-model="formData.payPartnerId" clearable filterable placeholder="请选择">
                            <el-option
                                v-for="item in patentPartnerIdOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item prop="amount" label="金额">
                        <el-input-number type="number" v-model="formData.amount"></el-input-number>
                    </el-form-item>
                    <el-form-item prop="currencyMaintenanceId" label="币种">
                        <el-select v-model="formData.currencyMaintenanceId" clearable filterable placeholder="请选择">
                            <el-option
                                v-for="item in currencyMaintenanceIdOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item prop="paymentDeadline" label="支付期限">
                        <el-date-picker
                            v-model="formData.paymentDeadline"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="选择日期"
                        >
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item prop="paymentDate" label="支付日期">
                        <el-date-picker
                            v-model="formData.paymentDate"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="选择日期"
                        >
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item prop="remark" label="备注">
                        <el-input type="textarea" v-model="formData.remark"></el-input>
                    </el-form-item>
                    <el-form-item prop="invoiceReturn" label="是否发票回传">
                        <el-switch v-model="formData.invoiceReturn"></el-switch>
                    </el-form-item>
                    <el-form-item prop="userId" label="userId">
                        <el-input-number type="number" v-model="formData.userId"></el-input-number>
                    </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: 'FeeEdit',
    created() {
        if (this.$route.query.id) {
            this.$http
                .get('fee/get/' + this.$route.query.id)
                .then(res => {
                    this.formData = res;
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
        }
        this.$http
            .post('/maintenance/byDesc', { description: 'FeeType' })
            .then(res => {
                if (res.content.length > 0) {
                    res.content.forEach(item => {
                        this.feeMaintenanceIdOptions.push({
                            label: item.name,
                            value: item.name
                        });
                    });
                }
            })
            .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.patentPartnerIdOptions.push({
                            label: item.name,
                            value: item.id
                        });
                    });
                }
            })
            .catch(e => {
                console.log(e);
                this.$message.error(e.error);
            });
        // this.$http
        //     .get('/partner/all', { size: 1000, query: { del: false } })
        //     .then(res => {
        //         if (res.content.length > 0) {
        //             res.content.forEach(item => {
        //                 this.payPartnerIdOptions.push({
        //                     label: item.name,
        //                     value: item.id
        //                 });
        //             });
        //         }
        //     })
        //     .catch(e => {
        //         console.log(e);
        //         this.$message.error(e.error);
        //     });
        this.$http
            .post('/maintenance/getTree')
            .then(res => {
                if (res.content.length > 0) {
                    res.content.forEach(item => {
                        this.currencyMaintenanceIdOptions.push({
                            label: item.name,
                            value: item.id
                        });
                    });
                }
            })
            .catch(e => {
                console.log(e);
                this.$message.error(e.error);
            });
    },
    data() {
        return {
            saving: false,
            formData: {},
            rules: {},
            feeMaintenanceIdOptions: [],
            statusOptions: [
                { label: '已支付', value: 'PAID' },
                { label: '未支付', value: 'UNPAID' }
            ],
            patentPartnerIdOptions: [],
            payPartnerIdOptions: [],
            currencyMaintenanceIdOptions: []
        };
    },
    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('/fee/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(`/fee/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>
