<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="142px"
                    label-position="right"
                    size="small"
                    style="max-width: 600px;"
                >
                    <el-form-item prop="name" :label="formData.type == 'COPY' ? '作品名称' : '软著名称'">
                        <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="registrationTime" label="登记日期">
                        <el-date-picker
                            v-model="formData.registrationTime"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="选择日期"
                        >
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item prop="workType" :label="formData.type == 'COPY' ? '作品类型' : '软著类型'">
                        <el-select v-model="formData.workType" clearable filterable placeholder="请选择">
                            <el-option
                                v-for="item in workTypeOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item class="block" prop="author" label="作者" v-if="formData.type == 'COPY'">
                        <el-select
                            v-model="formData.author"
                            class="suffix"
                            multiple
                            style="width: 100%"
                            filterable
                            allow-create
                            default-first-option
                            placeholder="请输入作者，按回车分割"
                            clearable
                            popper-class="hideSelect"
                        >
                        </el-select>
                    </el-form-item>
                    <el-form-item prop="owner" class="block" label="著作权人">
                        <el-select
                            v-model="formData.owner"
                            class="suffix"
                            multiple
                            filterable
                            allow-create
                            default-first-option
                            placeholder="请输入著作权人，按回车分割"
                            clearable
                            style="width: 100%"
                            popper-class="hideSelect"
                        >
                        </el-select>
                    </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="img" label="附图">
                        <multi-upload v-model="formData.img"></multi-upload>
                    </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"></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';
import { phonePattern } from '../../utils/variables';
export default {
    name: 'CopyrightEdit',
    mixins: [tradingMethod],
    created() {
        if (this.$route.query.id) {
            this.$http
                .get('copyright/get/' + this.$route.query.id)
                .then(res => {
                    this.formData = res;
                    if (res.type == 'COPY') {
                        this.getSetting(3, 'workTypeOptions');
                    } else {
                        this.getSetting(4, 'workTypeOptions');
                    }
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
        }
        if (this.$route.query.type) {
            this.formData.type = this.$route.query.type;
            if (this.formData.type == 'COPY') {
                this.getSetting(3, 'workTypeOptions');
            } else {
                this.getSetting(4, 'workTypeOptions');
            }
        }
    },
    data() {
        return {
            saving: false,
            formData: {},
            workTypeOptions: [],
            commissionTypeOptions: [
                { label: '佣金', value: 'COMMISSION' },
                { label: '百分比', value: 'PERCENTAGE' }
            ]
        };
    },
    computed: {
        types() {
            return this.$route.query.type;
        },
        rules() {
            return {
                name: [{ required: true, message: `请输入${this.types === 'COPY' ? '作品' : '软著'}名称` }]
                // code: [
                //     {
                //         required: true,
                //         message: `请输入${this.types === 'COPY' ? '版本' : '软著'}登记号`
                //     }
                // ],
                // workType: [{ required: true, message: `请选择${this.types === 'COPY' ? '作品' : '软著'}类型` }],
                // author: [{ required: true, message: '请输入作者', trigger: 'blur' }],
                // owner: [{ required: true, message: '请输入著作权人', trigger: 'blur' }],
                // img: [
                //     {
                //         required: true,
                //         message: `请上传${this.types === 'COPY' ? '作品' : '软著'}附图`
                //     }
                // ],
                // tradingMethodId: [{ required: true, message: '请选择交易方式' }],
                // expectedPrice: [
                //     {
                //         required: true,
                //         validator: (rule, value, callback) => {
                //             if (!this.formData.negotiateDirectly) {
                //                 if (!this.formData.expectedPrice) {
                //                     callback(new Error('请选择期望价方式'));
                //                 } else {
                //                     callback();
                //                 }
                //             } else {
                //                 callback();
                //             }
                //         }
                //     }
                // ],
                // basePrice: [{ required: true, message: '请输入底价', trigger: 'blur' }],
                // contact: [{ required: true, message: '请输入联系人' }],
                // phone: { required: true, pattern: phonePattern, 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: '请输入所在地区' }]
            };
        }
    },
    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('/copyright/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(`/copyright/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 || '删除失败');
                    }
                });
        }
        // getSetting(flag) {
        //     this.$http
        //         .post('/setting/byFlag', { flag: flag })
        //         .then(res => {
        //             if (res.length > 0) {
        //                 res.forEach(item => {
        //                     this.workTypeOptions.push({
        //                         label: item.name,
        //                         value: item.id
        //                     });
        //                 });
        //             }
        //         })
        //         .catch(e => {
        //             console.log(e);
        //             this.$message.error(e.error);
        //         });
        // }
    }
};
</script>
<style lang="less" scoped></style>
