<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="136px"
                    label-position="right"
                    size="small"
                    style="max-width: 700px;"
                >
                    <el-form-item prop="name" :label="formData.tpType == 'TECHNOLOGY' ? '技术名称' : '产品名称'">
                        <el-input v-model="formData.name"></el-input>
                    </el-form-item>
                    <el-form-item prop="industry" label="所属领域">
                        <el-select
                            v-model="formData.industry"
                            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="isPatented"
                        :label="formData.tpType == 'TECHNOLOGY' ? '是否需要专利技术' : '是否需要专利产品'"
                    >
                        <el-radio-group v-model="formData.isPatented">
                            <el-radio :label="true">是</el-radio>
                            <el-radio :label="false">否</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item prop="patentType" label="专利类型" v-if="formData.isPatented">
                        <el-radio-group v-model="formData.patentType">
                            <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="description" label="需求描述">
                        <el-input type="textarea" :rows="4" v-model="formData.description"></el-input>
                    </el-form-item>
                    <el-form-item prop="stage" label="所处阶段">
                        <el-select
                            v-model="formData.stage"
                            clearable
                            filterable
                            placeholder="请选择"
                            style="width: 100%"
                        >
                            <el-option
                                v-for="item in stageOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item prop="modeId" label="交易方式">
                        <!-- <el-select
                            v-model="formData.modeId"
                            clearable
                            filterable
                            placeholder="请选择"
                            style="width: 100%"
                        >
                            <el-option
                                v-for="item in modeOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            >
                            </el-option>
                        </el-select> -->
                        <el-radio-group v-model="formData.modeId" v-if="formData.tpType == 'TECHNOLOGY'">
                            <el-radio v-for="item in modeOptions" :key="item.value" :label="item.value">
                                {{ item.label }}
                            </el-radio>
                        </el-radio-group>
                        <el-radio-group v-model="formData.modeId" v-else>
                            <el-radio v-for="item in modeOptions1" :key="item.value" :label="item.value">
                                {{ item.label }}
                            </el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item prop="expectedPrice" label="预算(万元)">
                        <el-input-number
                            v-model="formData.expectedPrice"
                            :disabled="formData.negotiateDirectly"
                            @change="Price"
                            :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="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 { phonePattern } from '../../utils/variables';
import tradingMethod from '@/mixins/tradingMethod';
export default {
    name: 'TechProductDemandEdit',
    mixins: [tradingMethod],
    created() {
        if (this.$route.query.id) {
            this.$http
                .get('techProductDemand/get/' + this.$route.query.id)
                .then(res => {
                    this.formData = res;
                })
                .catch(e => {
                    console.log(e);
                    this.$message.error(e.error);
                });
        }
        if (this.$route.query.type) {
            this.formData.tpType = this.$route.query.type;
        }
        this.$http
            .post('/setting/byFlag', { flag: 1 })
            .then(res => {
                if (res.length > 0) {
                    res.forEach(item => {
                        this.industryOptions.push({
                            label: item.name,
                            value: item.id
                        });
                    });
                }
            })
            .catch(e => {
                console.log(e);
                this.$message.error(e.error);
            });
        this.getSetting(29, 'modeOptions1');
    },
    data() {
        return {
            isMode: true,
            flag: 30,
            saving: false,
            formData: {},
            industryOptions: [],
            patentTypeOptions: [
                { label: '发明', value: 'INVENTION' },
                { label: '实用新型', value: 'UTILITY_MODEL' },
                { label: '外观设计', value: 'APPEARANCE_DESIGN' }
            ],
            stageOptions: [
                { label: '研发阶段', value: 'RESEARCH_AND_DEVELOPMENT' },
                { label: '样品阶段', value: 'SAMPLE' },
                { label: '小试阶段', value: 'SMALL_TEST' },
                { label: '中试阶段', value: 'PILOT_TEST' },
                { label: '量产阶段', value: 'MASS_PRODUCTION' }
            ],
            modeOptions: [
                { label: '技术转让', value: 'TRANSFER' },
                { label: '技术许可', value: 'PERMIT' },
                { label: '技术入股', value: 'INVEST' },
                { label: '合作生产', value: 'CO_PRODUCTION' },
                { label: '其他', value: 'OTHER' }
            ],
            modeOptions1: [
                { label: '转让', value: 'TRANSFER' },
                { label: '许可', value: 'PERMIT' },
                { label: '作价入股', value: 'INVEST' },
                { label: '合作生产', value: 'CO_PRODUCTION' },
                { label: '其他', value: 'OTHER' }
            ],
            tpTypeOptions: [
                { label: '技术', value: 'TECHNOLOGY' },
                { label: '产品', value: 'PRODUCT' }
            ]
        };
    },
    computed: {
        types() {
            return this.$route.query.tpType;
        },
        rules() {
            return {
                name: [
                    {
                        required: true,
                        message: `请输入需求${this.types === 'TECHNOLOGY' ? '技术成果' : '产品'}名称`
                    }
                ]
                // industry: [{ required: true, message: '请选择所属领域' }],
                // stage: [{ required: true, message: '请选择所处阶段' }],
                // patentType: [{ required: true, message: '请选择专利类型', trigger: 'blur' }],
                // description: [{ required: true, message: '请填写需求描述' }],
                // isPatented: [
                //     {
                //         required: true,
                //         message: `请选择是否需要专利${this.types === 'TECHNOLOGY' ? '技术' : '产品'}`,
                //         trigger: 'blur'
                //     }
                // ],
                // modeId: [{ 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();
                //             }
                //         }
                //     }
                // ],
                // contact: [{ required: true, message: '请输入联系人', trigger: 'blur' }],
                // phone: [{ required: true, pattern: phonePattern, message: '请输入联系电话', trigger: 'blur' }],
                // email: [{ type: 'email', 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.submit();
            // 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('/techProductDemand/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(`/techProductDemand/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>
