<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="108px"
                    label-position="right"
                    size="small"
                    style="max-width: 600px;"
                >
                    <el-form-item prop="name" label="项目名称">
                        <el-input v-model="formData.name"></el-input>
                    </el-form-item>
                    <el-form-item prop="purchaseType" label="采购类型">
                        <el-radio-group v-model="formData.purchaseType">
                            <el-radio v-for="item in purchaseTypeOptions" :key="item.value" :label="item.value">
                                {{ item.label }}
                            </el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item prop="purchaseItem" label="采购项目">
                        <el-radio-group v-model="formData.purchaseItem">
                            <el-radio v-for="item in purchaseItemOptions" :key="item.value" :label="item.value">
                                {{ item.label }}
                            </el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item prop="registerDeadline" label="报名截止日期">
                        <el-date-picker
                            v-model="formData.registerDeadline"
                            type="date"
                            value-format="yyyy-MM-dd"
                            placeholder="选择日期"
                            style="width: 100%"
                        >
                        </el-date-picker>
                    </el-form-item>
                    <el-form-item prop="field" label="所属领域">
                        <el-select
                            v-model="formData.field"
                            clearable
                            filterable
                            placeholder="请选择"
                            style="width: 100%"
                        >
                            <el-option
                                v-for="item in fieldOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item prop="address" label="地点">
                        <el-input :rows="3" type="textarea" v-model="formData.address"></el-input>
                    </el-form-item>
                    <el-form-item prop="expectedPrice" label="预算（万元）">
                        <el-input-number
                            type="number"
                            v-model="formData.expectedPrice"
                            style="width: 50%"
                        ></el-input-number>
                    </el-form-item>
                    <el-form-item prop="img" label="图片">
                        <multi-upload v-model="formData.img"></multi-upload>
                    </el-form-item>
                    <el-form-item prop="content" label="采购详情">
                        <rich-text v-model="formData.content"></rich-text>
                    </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>
export default {
    name: 'PurchaseEdit',
    created() {
        if (this.$route.query.id) {
            this.$http
                .get('purchase/get/' + this.$route.query.id)
                .then(res => {
                    this.formData = res;
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
        }
        this.$http
            .post('/setting/byFlag', { flag: 1 })
            .then(res => {
                if (res.length > 0) {
                    res.forEach(item => {
                        this.fieldOptions.push({
                            label: item.name,
                            value: item.id
                        });
                    });
                }
            })
            .catch(e => {
                console.log(e);
                this.$message.error(e.error);
            });
    },
    data() {
        return {
            saving: false,
            formData: {},
            rules: {
                name: [{ required: true, message: '请输入名称' }],
                purchaseType: [{ required: true, message: '请选择类型' }],
                purchaseItem: [{ required: true, message: '请选择项目' }],
                registerDeadline: [{ required: true, message: '请选择报名截止时间', trigger: 'blur' }],
                field: [{ required: true, message: '请选择所属领域' }],
                address: [{ required: true, message: '请填写地点' }],
                img: [{ required: true, message: '请添加图片', trigger: 'blur' }],
                expectedPrice: [{ required: true, message: '请输入预算', trigger: 'blur' }],
                content: [{ required: true, message: '请填写采购详情' }]
            },
            purchaseTypeOptions: [
                { label: '公开招标', value: 'PUBLIC_TENDER' },
                { label: '邀请招标', value: 'INVITATION_TO_TENDER' },
                { label: '竞争性谈判', value: 'COMPETITIVE_NEGOTIATION' },
                { label: '询价', value: 'INQUIRY' }
            ],
            purchaseItemOptions: [
                { label: '工程', value: 'ENGINEERING' },
                { label: '货物', value: 'CARGO' },
                { label: '服务', value: 'SERVICE' }
            ],
            fieldOptions: []
        };
    },
    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('/purchase/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(`/purchase/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>
