<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.name" clearable placeholder="请输入项目名称"></el-input>
                </el-form-item>
                <el-form-item label="项目等级">
                    <el-select v-model="params.zcLevel" clearable placeholder="请选择项目等级">
                        <el-option v-for="item in levelDic" :key="item" :label="item" :value="item"></el-option>
                    </el-select>
                </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-select v-model="params.isLand" clearable placeholder="请选择状态">
                        <el-option v-for="(label, key) in statusDic" :key="key" :label="label" :value="parseInt(key)"></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_tools">
            <div class="tjm_card_tools_right">
                <el-button icon="download" @click="table_export">导出</el-button>
            </div>
        </div>
        <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" width="50"></el-table-column>
                <template v-for="(item, index) in columns" :key="index">
                    <el-table-column :label="item.label" :prop="item.props" :width="item.width || 180" show-overflow-tooltip>
                        <template #default="scope">{{ columnFormat(scope.row, item.props) }}</template>
                    </el-table-column>
                </template>
                <el-table-column label="操作" fixed="right" width="240">
                    <template #default="scope">
                        <el-button type="primary" link icon="tickets" @click="table_detail(scope.row)">详情</el-button>
                        <el-button v-if="scope.row.isReward == 0 && scope.row.isLand == 1 && scope.row.rewardRadix" type="primary" link icon="star" @click="table_score(scope.row)">打分</el-button>
                        <el-button v-if="scope.row.rewardScore" type="primary" link @click="table_reward(scope.row)">
                            <template #icon><tjm-icon-tabler-arrows-split-2 /></template>
                            奖励分配
                        </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-strive-detail v-if="dialog.detail" ref="policyStriveDetail" @success="reloadTable" @closed="closed"></policy-strive-detail>
    <policy-strive-score v-if="dialog.score" ref="policyStriveScore" @success="reloadTable" @closed="closed"></policy-strive-score>
    <policy-strive-reward v-if="dialog.reward" ref="policyStriveReward" @success="reloadTable" @closed="closed"></policy-strive-reward>
</template>

<script>
import API from "@/api/policy/strive"
import { exportExcel } from "@/utils/exportExcel";
import { levelDic, typeDic } from "@/views/policyShare/main";
import { columns, statusDic } from "./main";
import yhPagination from "@/components/Pagination/index.vue";
import policyStriveDetail from "@/views/policyStrive/dialog.vue";
import policyStriveScore from "./scoreDialog.vue";
import policyStriveReward from "./rewardDialog.vue";

export default {
    components: {
        yhPagination,
        policyStriveDetail,
        policyStriveScore,
        policyStriveReward
    },

    data() {
        return {
            columns, levelDic, typeDic, statusDic,

            loading: false,
            createTime: [],
            params: {
                page: 1,
                size: 10,
                status: "done"
            },

            total: 0,
            tableData: [],

            dialog: {
                detail: false,
                score: false,
                reward: false
            }
        }
    },

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

    methods: {
        columnFormat(row, props) {
            if (props == "status") return statusDic[row.isLand] || "";
            if (props == "landAmount" || props == "rewardRadix") return row.isLand == 1 && row[props] || "";
            return row[props];
        },

        reloadTable(mode = "add") {
            if (mode == "add") this.params.page = 1;
            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 = [];
            for (const key in this.params) {
                if (key == "page") this.params[key] = 1;
                else if (key == "size") this.params[key] = 10;
                else this.params[key] = null;
            }
            this.reloadTable();
        },

        table_export() {
            const header = this.columns.map(c => c.label);
            const data = this.tableData.map(v => this.columns.map(c => c.props).map(j => this.columnFormat(v, j)));

            exportExcel(header, data, [], `${this.$route.name}.xlsx`);
        },

        table_detail(row) {
            this.dialog.detail = true;
            this.$nextTick(() => this.$refs.policyStriveDetail.init("detail").setData(row.id));
        },

        table_score(row) {
            this.dialog.score = true;
            this.$nextTick(() => this.$refs.policyStriveScore.init().setData(row));
        },

        table_reward(row) {
            this.dialog.reward = true;
            this.$nextTick(() => this.$refs.policyStriveReward.init().setData(row));
        }
    }
}
</script>