<template>
    <div class="p-3">
        <div class="bg-fff flex1 ov-hd d-flex flex-cln">
            <div class="d-flex a-c pd-16 border-bottom">
                <div class="f-s-20 c-333 f-w-7 mr-10">新增品种概况</div>
                <el-button @click="router.go(-1)" type="primary" text>
                    <el-icon>
                        <Back />
                    </el-icon>
                    返回上一级
                </el-button>
            </div>
            <div class="flex1 over-auto">
                <el-form ref="formRef" label-width="auto" label-position="top" :model="form" :rules="rules" :disabled="disable">
                    <div class="pd-16 border-bottom ov-hd">
                        <el-row :gutter="90">
                            <el-col :span="8">
                                <el-form-item label="概况标题" prop="newsTitle">
                                    <el-input v-model="form.newsTitle" clearable placeholder="请输入概况标题"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item label="关联品种" prop="variety">
                                    <el-select v-model="form.variety" filterable multiple clearable placeholder="请选择关联品种">
                                        <el-option v-for="item in varietyOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="16">
                                <el-form-item label="概况内容" prop="newsContent">
                                    <div class="flex1">
                                        <Editor v-model="form.newsContent" placeholder="请输入概况内容" :readOnly="disable"></Editor>
                                    </div>
                                </el-form-item>
                            </el-col>
                        </el-row>
                    </div>
                </el-form>
            </div>
            <div class="d-flex a-c j-c pd-16">
                <el-button @click="router.go(-1)">取消</el-button>
                <el-button @click="save" color="#0079fe" :disabled="disable">暂存</el-button>
                <el-button @click="submitGround" type="primary" :disabled="disable">立即上架</el-button>
            </div>
        </div>
    </div>
</template>

<script setup name="MedicineNews-add" lang="ts">
import { ref, reactive, onMounted } from 'vue';
import { debounce } from 'lodash';
import { addNews, editNews, publishNews, getNewsInfo } from '@/api/dgtmedicine/news';
import { useRouter } from 'vue-router';
import { getMedicineList } from '@/api/dgtmedicine/variety';
const router = useRouter();
const form = ref({
    id: undefined,
    newsTitle: '',
    variety: [],
    newsContent: '',
    newsImg: '',
    newsType: '0'
});
const rules = reactive({
    newsTitle: [{ required: true, message: '请输入概况标题', trigger: 'blur' }],
    content: [{ required: true, message: '请输入概况内容', trigger: 'blur' }]
});
const formRef = ref();
const varietyOptions = ref([]);

const fetchVarietyOptions = async () => {
    const res = await getMedicineList({});
    if (res && res.code === 200) {
        varietyOptions.value = res.data.map((item) => ({
            label: item.varietyName,
            value: item.id
        }));
    }
};

const save = debounce(async () => {
    await formRef.value.validate();
    const res = form.value.id ? await editNews(form.value) : await addNews(form.value);
    if (res && res.code === 200) {
        router.go(-1);
    }
}, 500);

const submitGround = debounce(async () => {
    await formRef.value.validate();
    const res = form.value.id ? await editNews(form.value) : await addNews(form.value);
    if (res && res.code === 200) {
        await publishNews(res.data.id);
        router.go(-1);
    }
}, 500);
const fetchDetail = async (id) => {
    const res = await getNewsInfo(id);
    if (res && res.code === 200) {
        form.value = res.data;
    }
};
const disable = ref(false);
onMounted(() => {
    const id = router.currentRoute.value.query.id;
    disable.value = router.currentRoute.value.query.disable == '1';
    if (id) {
        // 编辑
        fetchDetail(id);
    }
    fetchVarietyOptions();
});
</script>
