<template>
    <div class="p-3">
        <div class="bg-fff flex1 ov-hd d-flex flex-cln" v-show="showSearch">
            <div class="pd3-16-16-0 border-bottom">
                <div class="f-s-20 c-333 f-w-7 mb-10">团体标准</div>
                <div class="d-flex">
                    <el-form class="flex1" ref="formRef" :model="queryParams" inline label-width="auto">
                        <el-form-item label="标准编号:" prop="code">
                            <el-input v-model="queryParams.code" style="width: 180px;" clearable placeholder="请输入标准编号"></el-input>
                        </el-form-item>
                        <!-- 标准名称 -->
                        <el-form-item label="标准名称:" prop="chTitle">
                            <el-input v-model="queryParams.chTitle" style="width: 180px;" clearable placeholder="请输入标准名称"></el-input>
                        </el-form-item>
                        <!-- 起草牵头单位 -->
                        <el-form-item label="起草牵头单位:" prop="takeUnit">
                            <el-input v-model="queryParams.takeUnit" style="width: 180px;" clearable placeholder="请输入起草牵头单位"></el-input>
                        </el-form-item>
                        <!-- 是否上架 -->
                        <el-form-item label="是否上架:" prop="putawayFlag">
                            <el-select v-model="queryParams.putawayFlag" clearable placeholder="请选择是否上架" style="width: 160px;">
                                <el-option v-for="item in news_status" :key="item.value" :label="item.label" :value="item.value" />
                            </el-select>
                        </el-form-item>
                        <el-form-item>
                            <el-button type="primary" @click="handleQuery">查询</el-button>
                            <el-button @click="resetQuery">重置</el-button>
                        </el-form-item>
                    </el-form>
                    <el-button type="primary" @click="router.push({ path: '/skill/group/post-input', query: { newsType: queryParams.newsType } })">新增团体标准</el-button>
                </div>
            </div>
            <div class="d-flex flex1 ov-hd flex-cln pd-12">
                <div class="flex1 ov-hd">
                    <vxe-table :loading="loading" border :data="dataList" min-height="0" max-height="100%">
                        <vxe-column title="序号" align="center" type="seq" width="60" />
                        <vxe-column title="标准编号">
                            <template #default="{ row }">
                                <el-tag class="mr-10" v-if="+row?.validStatus" type="primary" effect="dark" round>有效</el-tag>
                                <el-tag class="mr-10" v-else type="danger" effect="dark" round>无效</el-tag>
                                {{ row?.code }}
                            </template>
                        </vxe-column>
                        <vxe-column title="标准名称" field="chTitle" :formatter="colNoData"></vxe-column>
                        <vxe-column title="立项日期" align="center" width="110">
                            <template #default="{ row }">
                                {{ parseTime(row?.projectDate, '{y}-{m}-{d}') || '-' }}
                            </template>
                        </vxe-column>
                        <vxe-column title="发布日期" align="center" width="110">
                            <template #default="{ row }">
                                {{ parseTime(row?.issueDate, '{y}-{m}-{d}') || '-' }}
                            </template>
                        </vxe-column>
                        <vxe-column title="实施日期" align="center" width="110">
                            <template #default="{ row }">
                                {{ parseTime(row?.putDate, '{y}-{m}-{d}') || '-' }}
                            </template>
                        </vxe-column>
                        <vxe-column title="标准类型">
                            <template #default="{ row }">
                                {{ selectDictLabel(standard_group_type, row?.type) || '-' }}
                            </template>
                        </vxe-column>
                        <vxe-column title="起草牵头单位" field="takeUnit" :formatter="colNoData"></vxe-column>
                        <vxe-column title="标准公告" field="newsName" :formatter="colNoData"></vxe-column>
                        <vxe-column title="标准文本">
                            <template #default="{ row }">
                                <div v-if="row?.standardText">
                                    <el-link :href="row?.standardText" type="primary" target="_blank">{{ row?.standardText }}</el-link>
                                </div>
                                <div v-else>-</div>
                            </template>
                        </vxe-column>
                        <vxe-column title="上架状态" fixed="right" align="center" width="90" field="putawayFlag">
                            <template #default="{ row }">
                                <dict-tag :options="news_status" :value="row.putawayFlag" />
                            </template>
                        </vxe-column>
                        <vxe-column title="操作" width="180" align="center" fixed="right">
                            <template #default="{ row }">
                                <el-button type="warning" size="small" @click="row.putawayFlag === '1' ? unpublishPost(row) : publishPost(row)" text>
                                    {{ row.putawayFlag === '1' ? '下架' : '上架' }}
                                </el-button>
                                <span></span>
                                <el-button type="primary" size="small" @click="editPost(row)" text>编辑</el-button>
                                <!-- 删除 -->
                                <el-button type="danger" size="small" @click="deletePost(row)" text>删除</el-button>
                            </template>
                        </vxe-column>
                    </vxe-table>
                </div>
                <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />
            </div>
        </div>
    </div>
</template>

<script setup name="news-list-cum" lang="ts">
import { colNoData } from '@/utils/noData';
import { DateRange } from '@/views/models/index';
import { httpRequests } from '@/utils/httpRequests';

const router = useRouter();
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { standard_group_type, news_status } = toRefs<any>(proxy?.useDict('standard_group_type', 'news_status'));
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const formRef = ref<ElFormInstance>();
const dataList = ref<any[]>([]);
const initFormData = {};
const data = reactive<any>({
    form: { ...initFormData },
    queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: ''
    },
    rules: {}
});

const { queryParams, form } = toRefs(data);
/** 查询会员信息列表 */
const getList = async () => {
    loading.value = true;
    const res: any = await httpRequests.get('/dgtmedicine/groupStandard/list', queryParams.value);
    dataList.value = res.rows;
    total.value = res.total;
    loading.value = false;
};
const publishPost = async (row: any) => {
    const res: any = await httpRequests.get(`/dgtmedicine/groupStandard/changeStatus/${row.id}`);
    getList();
    proxy.$modal.msgSuccess('操作成功');
};
const unpublishPost = async (row: any) => {
    const res: any = await httpRequests.get(`/dgtmedicine/groupStandard/changeStatus/${row.id}`);
    getList();
    proxy.$modal.msgSuccess('操作成功');
};
const editPost = (row: any) => {
   router.push({ path: '/skill/group/post-input', query: { id: row.id } })
};
const deletePost = async (row: any) => {
    const res = await proxy.$modal.confirm('是否确认删除该团体标准？')
    console.log(res);
    if (res == 'confirm') {
        await httpRequests.get(`/dgtmedicine/groupStandard/remove/${row.id}`);
        proxy.$modal.msgSuccess('删除成功');
        getList();
    }
};
/** 搜索按钮操作 */
const handleQuery = () => {
    queryParams.value.pageNum = 1;
    getList();
};

/** 重置按钮操作 */
const resetQuery = () => {
    formRef.value?.resetFields();
    handleQuery();
};
/** 发布新闻 */
const route = useRoute();
onMounted(() => {
    queryParams.value.newsType = route.query?.newsType;
    getList();
});
</script>
