<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(lm_news_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">
                        <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="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"
                                        :disabled="disable"></el-date-picker>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8" v-if='form.newsType === "6"'>
                                <el-form-item label="选择省份" prop="introduction">
                                    <el-select v-model="introduction" placeholder="选择省份" multiple style="width: 240px"
                                        @change="changeProvince" @remove-tag="removeProvince">
                                        <el-option v-for="item in options" :key="item.value" :label="item.label"
                                            :value="item.value"
                                            :disabled="item.disabled && !introduction.includes(item.value)" />
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <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">
                                    <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="请输入内容"></Editor>
                                    </div>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="管理办法附件" :prop="form.extraInfo ? 'extraInfo.accessory' : ''">
                                    <div class="flex1">
                                        <FileUpload v-model="form.extraInfo.accessory" format="array" :fileSize="100"
                                            :span="8" :limit="100"></FileUpload>
                                    </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>
                <template v-if="form.newsStatus !== '1'">
                    <el-button @click="save" color="#0079fe">暂存</el-button>
                    <el-button @click="submitGround" type="warning">立即上架</el-button>
                </template>
                <el-button v-if="form.newsStatus === '1'" @click="save" type="primary">提交</el-button>
            </div>
        </div>
    </div>
</template>

<script setup name="news-input" lang="ts">
import { ref, reactive, onMounted } from 'vue';
import { debounce } from 'lodash';
import { addNews, editNews, publishNews, getNewsInfo, fetchNewsList } from '@/api/dgtmedicine/news';
import { useRouter } from 'vue-router';
import { AddBtn } from '@/views/models';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { lm_news_type } = toRefs<any>(proxy?.useDict('lm_news_type'));
const router = useRouter();
const form = ref<any>({
    id: undefined,
    newsTitle: '',
    newsContent: '',
    newsImg: '',
    publishTime: '',
    newsOrigin: '',
    extraInfo: {
        accessory: [],
        introduction: ''
    }
});
const introduction = ref([]);
const rules = reactive({
    newsTitle: [{ required: true, message: '请输入标题', trigger: 'blur' }],
    newsContent: [{ required: true, message: '请输入内容', trigger: 'blur' }],
    introduction: [{
        required: false, trigger: 'change', message: '请选择省份',
    }]
});
const formRef = ref();
const varietyOptions = ref([]);

const options = ref([
    { value: '北京', label: '北京', disabled: false },
    { value: '天津', label: '天津', disabled: false },
    { value: '河北', label: '河北', disabled: false },
    { value: '山西', label: '山西', disabled: false },
    { value: '内蒙古', label: '内蒙古', disabled: false },
    { value: '辽宁', label: '辽宁', disabled: false },
    { value: '吉林', label: '吉林', disabled: false },
    { value: '黑龙江', label: '黑龙江', disabled: false },
    { value: '上海', label: '上海', disabled: false },
    { value: '江苏', label: '江苏', disabled: false },
    { value: '浙江', label: '浙江', disabled: false },
    { value: '安徽', label: '安徽', disabled: false },
    { value: '福建', label: '福建', disabled: false },
    { value: '江西', label: '江西', disabled: false },
    { value: '山东', label: '山东', disabled: false },
    { value: '河南', label: '河南', disabled: false },
    { value: '湖北', label: '湖北', disabled: false },
    { value: '湖南', label: '湖南', disabled: false },
    { value: '广东', label: '广东', disabled: false },
    { value: '广西', label: '广西', disabled: false },
    { value: '海南', label: '海南', disabled: false },
    { value: '重庆', label: '重庆', disabled: false },
    { value: '四川', label: '四川', disabled: false },
    { value: '贵州', label: '贵州', disabled: false },
    { value: '云南', label: '云南', disabled: false },
    { value: '西藏', label: '西藏', disabled: false },
    { value: '陕西', label: '陕西', disabled: false },
    { value: '甘肃', label: '甘肃', disabled: false },
    { value: '青海', label: '青海', disabled: false },
    { value: '宁夏', label: '宁夏', disabled: false },
    { value: '新疆', label: '新疆', disabled: false },
    { value: '台湾', label: '台湾', disabled: false },
    { value: '香港', label: '香港', disabled: false },
    { value: '澳门', label: '澳门', disabled: false }
])
const removeProvince = (e) => {
    options.value.forEach((item) => {
        if (item.value === e) {
            item.disabled = false
        }
    })
}
const changeProvince = () => {
    form.value.extraInfo.introduction = introduction.value.join(';;');
    if (!form.value.extraInfo.introduction) {
        rules.introduction[0].required = true
    } else {
        rules.introduction[0].required = false

    }
}
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('introduction');
    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;
        if (route.query?.newsType === '6') {
            const intro = form.value.extraInfo.introduction || '';
            introduction.value.push(...(intro.includes(';;') ? intro.split(';;') : [intro]));
        }
    }
};
const getList = async () => {
    const res = await fetchNewsList({
        pageNum: 1,
        pageSize: 50,
        newsType: '6'
    });
    res.rows.forEach(item => {
        options.value.forEach(element => {
            const introParts = item.extraInfo.introduction.split(';;');
            if (introParts.some(part => part === element.value)) {
                element.disabled = true;
            }
        });
    })
};
const disable = ref(false);
const route = useRoute();
onMounted(() => {
    form.value.newsType = route.query?.newsType;
    if (route.query?.id) {
        // 编辑
        fetchDetail(route.query?.id);
        getList()
    }
});
</script>
