<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">{{ router.currentRoute.value.query.title || '新增广告' }}</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">
                        <el-row :gutter="90">
                            <el-col :span="8">
                                <el-form-item label="广告标题：" prop="title">
                                    <el-input v-model="form.title" clearable placeholder="请输入标题"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item label="页面中位置：" prop="adPosition">
                                    <el-select v-model="form.adPosition" clearable placeholder="请选择页面">
                                        <el-option v-for="item in dm_ad_position" :key="item.value" :label="item.label" :value="item.value" />
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="90">
                            <el-col :span="8">
                                <el-form-item label="广告样式：" prop="style">
                                    <el-select v-model="form.style" clearable placeholder="请选择广告样式">
                                        <el-option v-for="item in dm_ad_style" :key="item.value" :label="item.label" :value="item.value" />
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item label="链接类型：" prop="adType">
                                    <el-select v-model="form.adType" clearable placeholder="请选择链接类型">
                                        <el-option v-for="item in dm_ad_type" :key="item.value" :label="item.label" :value="item.value" />
                                    </el-select>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="90">
                            <el-col :span="8">
                                <el-form-item label="生效日期：" prop="validFrom">
                                    <el-date-picker style="width: 100%;" v-model="form.validFrom" type="date" placeholder="选择日期" format="YYYY-MM-DD" value-format="YYYY-MM-DD" :disabled="disable" />
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item label="失效日期：" prop="validUntil">
                                    <el-date-picker style="width: 100%;" v-model="form.validUntil" type="date" placeholder="选择日期" format="YYYY-MM-DD" value-format="YYYY-MM-DD" :disabled="disable" />
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="90">
                            <el-col :span="8">
                                <el-form-item label="放置页面：" prop="adPage">
                                    <el-select v-model="form.adPage" clearable placeholder="请选择页面">
                                        <el-option v-for="item in dm_ad_page" :key="item.value" :label="item.label" :value="item.value" />
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item label="备注：" prop="remark">
                                    <el-input v-model="form.remark" clearable placeholder="请输入备注"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="90">
                            <el-col :span="8">
                                <el-form-item label="跳转路径：" prop="adUrl">
                                    <el-input v-model="form.adUrl" placeholder="请输入跳转路径" :disabled="disable" :clearable="true" :show-word-limit="true" />
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item label="广告文字：" prop="adText">
                                    <el-input v-model="form.adText" placeholder="请输入广告文字" :disabled="disable" :clearable="true" :show-word-limit="true" />
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row :gutter="90">
                            <el-col :span="8">
                                <el-form-item label="广告图：" prop="adImg">
                                    <imageUpload v-model="form.adImg" :limit="1" isString :isShowTip="false"></imageUpload>
                                </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="warning" :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 { getAd , addAd, updateAd, publishAd as publishAdApi,unpublishAd as unpublishAdApi} from '@/api/dgtmedicine/ad';
import { useRouter } from 'vue-router';
import { title } from 'process';
import { any } from 'vue-types';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { news_status,dm_ad_type,dm_ad_position,dm_ad_page,dm_ad_style } = toRefs<any>(proxy?.useDict('news_status',
    'dm_ad_type','dm_ad_position','dm_ad_page','dm_ad_style'));
const router = useRouter();
const form = ref({
    id: undefined,
    title: '',
    adImg: '',
    validFrom: '',
    validUntil: '',
    adType: '',
    adText: '',
    adPage: '',
    adUrl: '',
    remark: '',
    adPosition:''
});
const rules = reactive({
    title: [
        { required: true, message: '请输入标题', trigger: 'blur' }
    ],
    content: [
        { 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 updateAd(form.value) : await addAd(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 updateAd(form.value) : await addAd(form.value);
    if (res && res.code === 200) {
        await publishAdApi(res.data.id);
        router.go(-1);
    }
}, 500);
const fetchDetail = async (id) => {
    const res = await getAd(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);
    }
});
</script>
