<template>
    <el-card class="tjm_card_style_custom">
        <div class="tjm_card_title">查询表格</div>
        <div class="tjm_card_select">
            <el-form class="tjm_card_select_left" :model="params" inline label-width="80px" label-position="left">
                <el-form-item label="政策名称">
                    <el-input v-model="params.zcName" clearable placeholder="请输入政策名称"></el-input>
                </el-form-item>
                <el-form-item label="政策类别">
                    <el-select v-model="params.zcType" clearable placeholder="请选择政策类别">
                        <el-option v-for="item in typeDic" :key="item" :label="item" :value="item"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="填报日期">
                    <el-date-picker v-model="createTime" type="daterange" value-format="YYYY-MM-DD" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker>
                </el-form-item>

                <el-form-item>
                    <el-button type="primary" icon="search" @click="reloadTable">搜索</el-button>
                    <el-button icon="refresh-right" @click="reset">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
        <el-divider></el-divider>

        <div class="tjm_card_table">
            <el-table v-loading="loading" row-key="id" header-cell-class-name="tjm_card_table_header" height="400" :data="tableData" border>
                <el-table-column type="index" label="序号" width="55"></el-table-column>
                <el-table-column label="政策名称" prop="zcName" min-width="180" show-overflow-tooltip></el-table-column>
                <el-table-column label="政策概要" prop="zcAbstractContent" min-width="180" show-overflow-tooltip></el-table-column>
                <el-table-column label="政策类别" prop="zcType" min-width="100" show-overflow-tooltip></el-table-column>
                <el-table-column label="填报人" prop="zcCreateName" min-width="180" show-overflow-tooltip></el-table-column>
                <el-table-column label="填报单位" prop="zcCompanyName" min-width="180" show-overflow-tooltip></el-table-column>
                <el-table-column label="联系方式" prop="zcContactPhone" width="180" show-overflow-tooltip></el-table-column>
                <el-table-column label="填报日期" width="120" show-overflow-tooltip>
                    <template #default="scope">{{ scope.row.zcCreateTime.split(" ")[0] }}</template>
                </el-table-column>
                <el-table-column label="操作" fixed="right" width="160">
                    <template #default="scope">
                        <el-button type="primary" link icon="circle-check" @click.stop="table_approve(scope.row)">通过</el-button>
                        <el-button type="primary" link icon="circle-close" @click.stop="table_approve(scope.row, 'refuse')">退回</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>
        <div class="tjm_card_pagination">
            <yh-pagination v-model:pageNo="params.page" v-model:pageSize="params.size" :total="total" @paginationChange="reloadTable"></yh-pagination>
        </div>
    </el-card>

    <policy-share v-if="dialog.policy_share" ref="policy_share" @success="reloadTable" @closed="dialog.policy_share = false"></policy-share>
    <policy-strive v-if="dialog.policy_strive" ref="policy_strive" @success="reloadTable" @closed="dialog.policy_strive = false"></policy-strive>
    <policy-case v-if="dialog.policy_case" ref="policy_case" @success="reloadTable" @closed="dialog.policy_case = false"></policy-case>
</template>

<script>
import API from "@/api/policy/todo";
import { useUserStore } from "@/store/user";
import { typeDic } from "@/views/policyShare/main";

import yhPagination from "@/components/Pagination/index.vue";
import policyShare from "./share.vue";
import policyStrive from "./strive.vue";
import policyCase from "./case.vue";

export default {
    components: {
        yhPagination,
        policyShare,
        policyStrive,
        policyCase
    },

    data() {
        return {
            typeDic,

            loading: false,
            createTime: [],
            params: {
                page: 1,
                size: 10,
                todoUserId: useUserStore().userInfo.id
            },

            total: 0,
            tableData: [],

            dialog: {
                policy_share: false,
                policy_strive: false,
                policy_case: false
            }
        }
    },

    mounted() {
        this.reloadTable();
    },

    methods: {
        reloadTable() {
            this.params.beginCreateTime = this.createTime && this.createTime.length && this.createTime[0] + " 00:00:00" || null;
            this.params.endCreateTime = this.createTime && this.createTime.length && this.createTime[1] + " 23:59:59" || null;
            
            this.loading = true;
            API.get(this.params).then(res => {
                this.loading = false;
                if (res.code === 200) {
                    this.tableData = res.data.records;
                    this.total = res.data.total;
                } else ElMessage.error(res.msg);
            }).catch(() => this.loading = false);
        },
    
        reset() {
            this.createTime = [];
            this.params = {
                page: 1,
                size: 10,
                todoUserId: useUserStore().userInfo.id
            }
            this.reloadTable();
        },
    
        table_approve({ refId, refType }, mode = "agree") {
            this.dialog[refType] = true;
            nextTick(() => this.$refs[refType].open(mode).setData(refId));
        }
    }
}
</script>