<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">{{ form.id ? '编辑' : '新增' }}{{ selectDictLabel(new_type, form.newsType) }}</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">
                        <template v-if="['23', '20'].includes(form.newsType)">
                            <el-row :gutter="90">
                                <el-col :span="8">
                                    <el-form-item label="中文名称：" prop="newsTitle" required :rules="[{ required: true, message: '请输入中文名称', trigger: 'blur' }]">
                                        <el-input v-model="form.newsTitle" clearable placeholder="请输入中文名称"></el-input>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="8">
                                    <el-form-item label="英文名称：" prop="extraInfo.pinyin" required :rules="[{ required: true, message: '请输入英文名称', trigger: 'blur' }]">
                                        <el-input v-model="form.extraInfo.pinyin" clearable placeholder="请输入中文名称"></el-input>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="8">
                                    <el-form-item label="序号：" prop="sort" required :rules="[{ required: true, message: '请输入序号', trigger: 'blur' }]">
                                        <el-input v-model="form.sort" clearable placeholder="请输入序号"></el-input>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="8">
                                    <el-form-item label="发布时间：" prop="publishTime">
                                        <el-date-picker v-model="form.publishTime" type="datetime" placeholder="请选择发布时间" format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss"></el-date-picker>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row :gutter="90">
                                <!-- <el-col :span="8">
                                <el-form-item label="来源：" prop="newsOrigin">
                                    <el-input v-model="form.newsOrigin" clearable placeholder="请输入来源"></el-input>
                                </el-form-item>
                            </el-col> -->
                                <el-col :span="8">
                                    <el-form-item label="封面图" prop="newsImg" required :rules="[{ required: true, message: '请上传封面图片', trigger: 'blur' }]">
                                        <imageUpload v-model="form.newsImg" :limit="1" isString :isShowTip="false"></imageUpload>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="24">
                                    <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>
                        </template>
                        <template v-if="['24', '25'].includes(form.newsType)">
                            <el-row :gutter="90">
                                <el-col :span="8">
                                    <el-form-item label="标题：" prop="newsTitle" required :rules="[{ required: true, message: '请输入中文名称', trigger: 'blur' }]">
                                        <el-input v-model="form.newsTitle" clearable placeholder="请输入中文名称"></el-input>
                                    </el-form-item>
                                </el-col>
                                <el-col :span="8">
                                    <el-form-item label="发布时间：" prop="publishTime">
                                        <el-date-picker v-model="form.publishTime" type="datetime" placeholder="请选择发布时间" format="YYYY-MM-DD HH:mm:ss" value-format="YYYY-MM-DD HH:mm:ss"></el-date-picker>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row :gutter="90">
                                <!-- <el-col :span="8">
                                <el-form-item label="来源：" prop="newsOrigin">
                                    <el-input v-model="form.newsOrigin" clearable placeholder="请输入来源"></el-input>
                                </el-form-item>
                            </el-col> -->
                                <el-col :span="8">
                                    <el-form-item label="封面图" prop="newsImg" required :rules="[{ required: true, message: '请上传封面图片', trigger: 'blur' }]">
                                        <imageUpload v-model="form.newsImg" :limit="1" isString :isShowTip="false"></imageUpload>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <el-row>
                                <el-col :span="24">
                                    <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>
                        </template>
                        <template v-if="['21', '26'].includes(form.newsType)">
                            <el-row :gutter="90">
                                <el-col :span="8">
                                    <el-form-item label="年份：" prop="newsTitle" required :rules="[{ required: true, message: '请输入年份', trigger: 'blur' }]">
                                        <el-date-picker v-model="form.newsTitle" type="year" format="YYYY年" value-format="YYYY年" placeholder="请输入年份" />
                                        <!-- <el-input v-model="form.newsTitle" clearable placeholder="请输入年份"></el-input> -->
                                    </el-form-item>
                                </el-col>
                                <el-col :span="8">
                                    <el-form-item label="发布时间：" prop="publishTime">
                                        <el-date-picker v-model="form.publishTime" type="datetime" placeholder="请选择发布时间" format="YYYY-MM-DD" value-format="YYYY-MM-DD"></el-date-picker>
                                    </el-form-item>
                                </el-col>
                            </el-row>
                            <template v-for="(item, index) in form.extraInfo?.dataList" :key="index">
                                <el-row :gutter="90" class="mb-16">
                                    <el-col :span="8">
                                        <el-form-item label="月份：" :prop="'extraInfo.dataList.' + index + '.month'" required :rules="[{ required: true, message: '请输入月份', trigger: 'blur' }]">
                                            <el-input v-model="item.month" clearable placeholder="请输入月份"></el-input>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="8">
                                        <el-form-item label="副标题：" :prop="'extraInfo.dataList.' + index + '.subtitle'" required :rules="[{ required: true, message: '请输入副标题', trigger: 'blur' }]">
                                            <el-input v-model="item.subtitle" clearable placeholder="请输入副标题"></el-input>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="8">
                                        <el-form-item label="是否标记：" prop="isMark">
                                            <el-radio-group v-model="item.isMark">
                                                <el-radio label="0">否</el-radio>
                                                <el-radio label="1">是</el-radio>
                                            </el-radio-group>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="8">
                                        <el-form-item label="内容：" prop="content">
                                            <el-input v-model="item.content" clearable placeholder="请输入内容"></el-input>
                                        </el-form-item>
                                    </el-col>
                                    <el-col :span="8">
                                        <el-form-item label=" ">
                                            <el-button @click="deleteList(index)" type="danger">删除</el-button>
                                        </el-form-item>
                                    </el-col>
                                </el-row>
                            </template>
                            <AddBtn content="新增历程" @click="addDataList"></AddBtn>
                        </template>
                    </div>
                </el-form>
            </div>
            <div class="d-flex a-c j-c pd-16" v-if="form.newsStatus !== '1'">
                <el-button @click="router.go(-1)">取消</el-button>
                <el-button @click="save" color="#0079fe" :disabled="disable">暂存</el-button>
                <el-button @click="submitGround" type="warning" :disabled="disable">立即上架</el-button>
            </div>
        </div>
    </div>
</template>

<script setup name="Brand-input" 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 { AddBtn } from '@/views/models';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { new_type } = toRefs<any>(proxy?.useDict('new_type'));
const router = useRouter();
const form = ref<any>({
    id: undefined,
    newsTitle: '',
    newsContent: '',
    newsImg: '',
    publishTime: '',
    newsOrigin: '',
    newsType: '10',
    extraInfo: {
        pinyin: '',
        dataList: []
    }
});
const rules = reactive({
    // newsImg: [
    //     { required: true, message: '请上传封面图片', trigger: 'blur' }
    // ],
    newsContent: [{ required: true, message: '请输入内容', trigger: 'blur' }]
});
const formRef = ref();
const varietyOptions = ref([]);

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 addDataList = () => {
    if (!form.value.extraInfo) {
        form.value.extraInfo = {
            dataList: [
                {
                    month: '',
                    subtitle: '',
                    content: '',
                    isMark: '0'
                }
            ]
        };
    } else {
        form.value.extraInfo.dataList.push({
            month: '',
            subtitle: '',
            content: '',
            isMark: '0'
        });
    }
};
const deleteList = (index) => {
    form.value.extraInfo.dataList.splice(index, 1);
};
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) {
        console.log(res.data.publishTime);

        form.value = res.data;
    }
};
const disable = ref(false);
const route = useRoute();
onMounted(() => {
    form.value.newsType = route.query?.newsType;
    if (route.query?.id) {
        // 编辑
        fetchDetail(route.query?.id);
    }
});
</script>
