<template>
    <div class="edit-view">
        <page-title>
            <el-button @click="$router.go(-1)" :disabled="saving">取消</el-button>
            <el-button @click="onDelete" :disabled="saving" type="danger" v-if="formData.id"> 删除 </el-button>
            <el-button @click="onSave" :loading="saving" type="primary">保存</el-button>
        </page-title>
        <div class="edit-view__content-wrapper">
            <div class="edit-view__content-section">
                <el-form
                    :model="formData"
                    :rules="rules"
                    ref="form"
                    label-width="156px"
                    label-position="right"
                    size="small"
                    style="max-width: 700px"
                >
                    <el-form-item prop="name" label="专利名称">
                        <el-input v-model="formData.name"></el-input>
                    </el-form-item>
                    <el-form-item prop="code" label="申请号">
                        <el-input v-model="formData.code"></el-input>
                    </el-form-item>
                    <el-form-item prop="patentTypeId" label="专利类型" v-if="formData.caseType == 'GENERAL'">
                        <el-radio-group v-model="formData.patentTypeId">
                            <el-radio v-for="item in patentTypeOptions" :key="item.value" :label="item.value">
                                {{ item.label }}
                            </el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item prop="ownerType" label="持有类型">
                        <el-radio-group v-model="formData.ownerType">
                            <el-radio v-for="item in ownerTypeOptions" :key="item.value" :label="item.value">
                                {{ item.label }}
                            </el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item prop="applyTime" label="申请日期">
                        <el-date-picker
                            v-model="formData.applyTime"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="选择日期"
                        >
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item prop="industryClass" label="所属领域">
                        <el-select
                            v-model="formData.industryClass"
                            clearable
                            filterable
                            placeholder="请选择"
                            style="width: 100%"
                        >
                            <el-option
                                v-for="item in industryOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item prop="ipc" label="IPC分类号">
                        <el-input v-model="formData.ipc"></el-input>
                    </el-form-item>
                    <el-form-item prop="lawStatusId" label="法律状态" v-if="formData.caseType == 'GENERAL'">
                        <el-select
                            v-model="formData.lawStatusId"
                            clearable
                            filterable
                            placeholder="请选择"
                            style="width: 100%"
                        >
                            <el-option
                                v-for="item in lawStatusOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item prop="owner" label="专利权人">
                        <!-- <el-input v-model="formData.owner"></el-input> -->
                        <el-select
                            v-model="formData.owner"
                            multiple
                            filterable
                            allow-create
                            default-first-option
                            placeholder="请输入，按回车分割"
                            clearable
                            popper-class="hideSelect"
                            style="width: 100%"
                        >
                        </el-select>
                    </el-form-item>
                    <el-form-item prop="applicant" label="申请人" v-if="formData.caseType == 'GENERAL'">
                        <el-select
                            v-model="formData.applicant"
                            multiple
                            filterable
                            allow-create
                            default-first-option
                            placeholder="请输入，按回车分割"
                            clearable
                            popper-class="hideSelect"
                            style="width: 100%"
                        >
                        </el-select>
                    </el-form-item>
                    <el-form-item prop="inventor" label="发明人">
                        <!-- <el-input v-model="formData.inventor"></el-input> -->
                        <!-- <el-tag
                            :key="tag"
                            size="medium"
                            v-for="tag in formData.inventor"
                            closable
                            :disable-transitions="false"
                            @close="handleClose(tag)"
                        >
                            {{ tag }}
                        </el-tag>
                        <el-input
                            class="input-new-tag"
                            v-model="inputValue"
                            ref="saveTagInput"
                            @keyup.enter.native="handleInputConfirm"
                            @blur="handleInputConfirm"
                        >
                        </el-input> -->
                        <el-select
                            v-model="formData.inventor"
                            multiple
                            class="suffix"
                            filterable
                            allow-create
                            default-first-option
                            placeholder="请输入发明人，按回车分割"
                            clearable
                            style="width: 100%"
                            popper-class="hideSelect"
                        >
                            <div slot="empty"></div>
                        </el-select>
                    </el-form-item>
                    <el-form-item prop="img" label="专利附图">
                        <multi-upload v-model="formData.img"></multi-upload>
                    </el-form-item>
                    <el-form-item prop="digest" label="摘要">
                        <el-input type="textarea" :rows="10" v-model="formData.digest"></el-input>
                    </el-form-item>
                    <el-form-item prop="manual" label="权利要求书">
                        <rich-text v-model="formData.manual"></rich-text>
                    </el-form-item>
                    <el-form-item prop="tradingMethodId" label="交易方式">
                        <el-select
                            v-model="formData.tradingMethodId"
                            clearable
                            filterable
                            placeholder="请选择"
                            style="width: 100%"
                        >
                            <el-option
                                v-for="item in tradingMethodOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item prop="expectedPrice" label="期望价（万元）">
                        <el-input-number
                            v-model="formData.expectedPrice"
                            @change="Price"
                            :disabled="formData.negotiateDirectly"
                            :precision="1"
                            :controls="false"
                            style="width: 75%; margin-right: 15px"
                        >
                        </el-input-number>
                        <el-checkbox @change="Directly" v-model="formData.negotiateDirectly">面议</el-checkbox>
                    </el-form-item>
                    <el-form-item prop="basePrice" label="底价（万元）">
                        <el-input-number
                            v-model="formData.basePrice"
                            :min="0"
                            :precision="1"
                            :controls="false"
                            style="width: 75%; margin-right: 15px"
                        >
                        </el-input-number>
                    </el-form-item>
                    <el-form-item prop="commissionType" label="佣金类型">
                        <el-radio-group v-model="formData.commissionType">
                            <el-radio v-for="item in commissionTypeOptions" :key="item.value" :label="item.value">
                                {{ item.label }}
                            </el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item
                        prop="commission"
                        :label="formData.commissionType == 'COMMISSION' ? '佣金（万元）' : '佣金（%）'"
                    >
                        <el-input v-model="formData.commission" :max="100"></el-input>
                    </el-form-item>
                    <el-form-item prop="contact" label="联系人">
                        <el-input v-model="formData.contact"></el-input>
                    </el-form-item>
                    <el-form-item prop="phone" label="电话">
                        <el-input v-model="formData.phone"></el-input>
                    </el-form-item>
                    <el-form-item prop="email" label="邮箱">
                        <el-input v-model="formData.email"></el-input>
                    </el-form-item>
                    <el-form-item prop="address" label="所在地区">
                        <el-input v-model="formData.address"></el-input>
                    </el-form-item>
                    <!-- <el-form-item class="form-submit">
                        <el-button @click="onSave" :loading="saving" type="primary">
                            保存
                        </el-button>
                        <el-button @click="onDelete" :disabled="saving" type="danger" v-if="formData.id">
                            删除
                        </el-button>
                        <el-button @click="$router.go(-1)" :disabled="saving">取消</el-button>
                    </el-form-item> -->
                </el-form>
            </div>
        </div>
    </div>
</template>
<script>
import tradingMethod from '@/mixins/tradingMethod';
export default {
    name: 'PatentEdit',
    mixins: [tradingMethod],
    created() {
        if (this.$route.query.id) {
            this.$http
                .get('patent/get/' + this.$route.query.id)
                .then(res => {
                    this.formData = res;
                    if (res.caseType == 'GENERAL') {
                        this.getSetting(1, 'industryOptions');
                        this.getSetting(25, 'patentTypeOptions');
                        this.getSetting(26, 'lawStatusOptions');
                    } else {
                        this.getSetting(13, 'industryOptions');
                    }
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
        }
        if (this.$route.query.type) {
            let type = this.$route.query.type;
            this.formData.caseType = this.$route.query.type;
            if (type == 'GENERAL') {
                this.getSetting(1, 'industryOptions');
                this.getSetting(25, 'patentTypeOptions');
                this.getSetting(26, 'lawStatusOptions');
            } else {
                this.getSetting(13, 'industryOptions');
            }
        }
    },
    data() {
        var validatePrice = (rule, value, callback) => {
            if (!this.formData.negotiateDirectly) {
                if (!this.formData.expectedPrice) {
                    callback(new Error('请选择期望价方式'));
                } else {
                    callback();
                }
            } else {
                callback();
            }
        };
        return {
            saving: false,
            formData: {
                inventor: []
            },
            rules: {
                name: [{ required: true, message: '请输入专利名称' }]
                // code: [{ required: true, message: '请输入专利申请号' }],
                // patentType: [{ required: true, message: '请选择专利类型' }],
                // ownerType: [{ required: true, message: '请输入专利权人类型', trigger: 'blur' }],
                // industryClass: [{ required: true, message: '请选择所属领域' }],
                // lawStatus: [{ required: true, message: '请选择法律状态' }],
                // applicant: [{ required: true, message: '请输入申请人', trigger: 'blur' }],
                // owner: [{ required: true, message: '请输入专利权人', trigger: 'blur' }],
                // inventor: [{ required: true, message: '请输入发明人' }],
                // img: [{ required: true, message: '请上传专利附图' }],
                // digest: [{ required: true, message: '请填写专利摘要' }],
                // manual: [{ required: true, message: '请输入权利要求书' }],
                // expectedPrice: [{ required: true, validator: validatePrice, message: '请选择期望价方式' }],
                // tradingMethod: [{ required: true, message: '请选择交易方式' }],
                // contact: [{ required: true, message: '请输入联系人' }],
                // basePrice: [{ required: true, message: '请输入底价', trigger: 'blur' }],
                // phone: { required: true, message: '请输入联系电话', trigger: 'blur' },
                // commissionType: [{ required: true, message: '请选择佣金类型' }],
                // email: [{ type: 'email', required: true, message: '请输入邮箱', trigger: 'blur' }],
                // commission: [{ required: true, message: '请输入佣金', trigger: 'blur' }],
                // address: [{ required: true, message: '请输入所在地区' }]
            },
            industryOptions: [],
            patentTypeOptions: [
                { label: '发明', value: 'INVENTION' },
                { label: '实用新型', value: 'UTILITY_MODEL' },
                { label: '外观设计', value: 'APPEARANCE_DESIGN' }
            ],
            lawStatusOptions: [
                { label: '申请中', value: 'APPLY' },
                { label: '已授权未期满', value: 'AUTHORISED' }
            ],
            commissionTypeOptions: [
                { label: '佣金', value: 'COMMISSION' },
                { label: '百分比', value: 'PERCENTAGE' }
            ],
            caseTypeOptions: [
                { label: '脱密专利', value: 'DECLASSIFICATION' },
                { label: '普通专利', value: 'GENERAL' }
            ],
            ownerTypeOptions: [
                { label: '企业', value: 'ENTERPRISES_AND_INSTITUTIONS' },
                { label: '高校', value: 'UNIVERSITIES_AND_INSTITUTES' },
                { label: '研究院', value: 'RESEARCH' },
                { label: '个人', value: 'PERSONAL' }
            ],
            inputValue: ''
        };
    },
    methods: {
        Price() {
            if (this.formData.expectedPrice) {
                this.formData.negotiateDirectly = false;
            }
        },
        Directly() {
            if (this.formData.negotiateDirectly == true) {
                this.$set(this.formData, 'expectedPrice', undefined);
            }
            this.$forceUpdate();
        },
        onSave() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.submit();
                } else {
                    return false;
                }
            });
        },
        submit() {
            let data = { ...this.formData, status: 'PENDING' };

            this.saving = true;
            this.$http
                .post('/patent/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(`/patent/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 || '删除失败');
                    }
                });
        },
        handleClose(tag) {
            this.formData.inventor.splice(this.formData.inventor.indexOf(tag), 1);
        },
        showInput() {
            this.inputVisible = true;
            this.$nextTick(_ => {
                this.$refs.saveTagInput.$refs.input.focus();
            });
        },
        handleInputConfirm() {
            let inputValue = this.inputValue;
            if (inputValue) {
                this.formData.inventor.push(inputValue);
            }
            this.inputVisible = false;
            this.inputValue = '';
        }
    }
};
</script>
<style lang="less" scoped>
.el-tag {
    margin-bottom: 11px;
}
.el-tag + .el-tag {
    margin-left: 10px;
}
.input-new-tag {
    width: 100%;
    vertical-align: bottom;
}
</style>
