<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 ? '编辑' : '新增' }}团体标准</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="standardRule">
                                    <el-input v-model="form.standardRule" clearable placeholder="请输入标准状态"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item label="标准编号：" prop="code">
                                    <el-input v-model="form.code" clearable placeholder="请输入标准编号"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item label="标准类型：" prop="type">
                                    <el-select v-model="form.type" placeholder="请选择标准类型" clearable>
                                        <el-option v-for="item in standard_group_type" :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="chTitle">
                                    <el-input v-model="form.chTitle" clearable placeholder="请输入中文标题"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item label="英文标题：" prop="enTitle">
                                    <el-input v-model="form.enTitle" clearable placeholder="请输入英文标题"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item label="国际标准分类号：" prop="inTaxonomy">
                                    <el-input v-model="form.inTaxonomy" clearable placeholder="请输入国际标准分类号"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item label="中国标准分类号：" prop="chTaxonomy">
                                    <el-input v-model="form.chTaxonomy" clearable placeholder="请输入中国标准分类号"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item label="国民经济分类：" prop="nationalEconomy">
                                    <el-input v-model="form.nationalEconomy" clearable placeholder="请输入国民经济分类"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item label="立项日期：" prop="projectDate">
                                    <el-date-picker class="flex1" v-model="form.projectDate" type="date" placeholder="选择日期时间" format="YYYY-MM-DD" value-format="YYYY-MM-DD"></el-date-picker>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item label="发布日期：" prop="issueDate">
                                    <el-date-picker class="flex1" v-model="form.issueDate" type="date" placeholder="选择日期时间" format="YYYY-MM-DD" value-format="YYYY-MM-DD"></el-date-picker>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item label="实施日期：" prop="putDate">
                                    <el-date-picker class="flex1" v-model="form.putDate" type="date" placeholder="选择日期时间" format="YYYY-MM-DD" value-format="YYYY-MM-DD"></el-date-picker>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item label="起草牵头单位：" prop="takeUnit">
                                    <el-input v-model="form.takeUnit" clearable placeholder="请输入起草牵头单位"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item label="范围：" prop="scope">
                                    <el-input v-model="form.scope" clearable placeholder="请输入范围"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item label="主要技术内容：" prop="technologyContent">
                                    <el-input type="textarea" v-model="form.technologyContent" :rows="4" clearable placeholder="请输入主要技术内容"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item label="标准文本：" prop="standard">
                                    <div class="flex1">
                                        <FileUpload v-model="form.standard" format="object" :fileSize="100" :span="24" :limit="100"></FileUpload>
                                    </div>
                                </el-form-item>
                            </el-col>
                            <el-col :span="16">
                                <el-form-item label="标准公告：" prop="newsId">
                                    <el-select v-model="form.newsId" filterable placeholder="请选择标准公告" clearable>
                                        <el-option v-for="item in noticeList" :key="item.id" :label="item.newsTitle" :value="item.id" />
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :span="8">
                                <el-form-item label="是否有效：" prop="validStatus">
                                    <el-radio-group v-model="form.validStatus">
                                        <el-radio value="1" size="large">有效</el-radio>
                                        <el-radio value="0" size="large">无效</el-radio>
                                    </el-radio-group>
                                </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.putawayFlag !== '1'">
                    <el-button @click="save('0')" color="#0079fe">暂存</el-button>
                    <el-button @click="save('1')" type="warning">立即上架</el-button>
                </template>
                <el-button v-if="form.putawayFlag === '1'" @click="save('1')" 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 { useRouter } from 'vue-router';
import { httpRequests } from '@/utils/httpRequests';
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { standard_group_type, news_status } = toRefs<any>(proxy?.useDict('standard_group_type', 'news_status'));
const router = useRouter();
const form = ref<any>({
    id: undefined,
    groupType: '1',
    putawayFlag: '0',
    standard: null,
    standardRule: '现行',
    code: '',
    type: '',
    chTitle: '',
    enTitle: '',
    inTaxonomy: '',
    chTaxonomy: '',
    nationalEconomy: '',
    takeUnit: '',
    newsId: '',
    validStatus: '1'
});
const rules = reactive({
    standardRule: [{ required: true, message: '请选择标准状态', trigger: 'blur' }],
    code: [{ required: true, message: '请输入标准编号', trigger: 'blur' }],
    type: [{ required: true, message: '请选择标准类型', trigger: 'change' }],
    chTitle: [{ required: true, message: '请输入中文标题', trigger: 'blur' }],
    enTitle: [{ required: true, message: '请输入英文标题', trigger: 'blur' }],
    inTaxonomy: [{ required: true, message: '请输入国际标准分类号', trigger: 'blur' }],
    chTaxonomy: [{ required: true, message: '请输入中国标准分类号', trigger: 'blur' }],
    nationalEconomy: [{ required: true, message: '请输入国民经济分类', trigger: 'blur' }],
    takeUnit: [{ required: true, message: '请输入起草牵头单位', trigger: 'blur' }],
    standard: [{ required: true, message: '请上传标准文本', trigger: 'change' }],
    newsId: [{ required: true, message: '请选择标准公告', trigger: 'change' }],
    validStatus: [{ required: true, message: '请选择是否有效', trigger: 'change' }]
});
const formRef = ref();

const save = debounce(async (putawayFlag: string) => {
    await formRef.value.validate();
    const params = {
        ...form.value,
        standardText: form.value.standard?.url || '',
        putawayFlag: putawayFlag || form.value.putawayFlag
    }
    const res = form.value.id ? await httpRequests.post('/dgtmedicine/groupStandard/edit', params) : await httpRequests.post('/dgtmedicine/groupStandard/add', params);
    if (res && res.code === 200) {
        proxy.$modal.msgSuccess('操作成功');
        router.go(-1);
    }
}, 500);

const disable = ref(false);
const route = useRoute();
const noticeList = ref<any[]>([]);
// 获取公示列表
const fetchNoticeList = async () => {
    const res: any = await httpRequests.get('/dgtmedicine/news/list', {
        pageNum: 1,
        pageSize: 9999,
        newsType: route.query?.newsType || '31',
        newsStatus: '1',
        validStatus: '1'
    });
    if (res && res.code === 200) {
        noticeList.value = res.rows;
    }
};
// 获取详情
const fetchDetail = async (id: string) => {
    disable.value = true;
    const res: any = await httpRequests.get(`/dgtmedicine/groupStandard/getInfo/${id}`);
    if (res && res.code === 200) {
        const data = res.data;
        form.value = {
            ...data,
            standard: data.standardText ? { fileName: data.standardText, url: data.standardText } : null
        };
    }
    disable.value = false;
};
onMounted(() => {
    fetchNoticeList();
    if (route.query?.id) {
        // 编辑
        fetchDetail(route.query?.id as string);
    }
});
</script>
