<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.status" clearable placeholder="请选择状态">
                        <el-option v-for="(label, key) in statusDic" :key="key" :label="label" :value="key"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="是否入库">
                    <el-select v-model="params.isInWh" clearable placeholder="请选择入库状态">
                        <el-option v-for="(label, key) in inWHDic" :key="key" :label="label" :value="key"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="入库类别">
                    <el-select v-model="params.inWhType" clearable placeholder="请选择入库类别">
                        <el-option v-for="item in storageTypeDic" :key="item" :label="item" :value="item"></el-option>
                    </el-select>
                </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_left">
                <el-button type="primary" icon="plus" @click="table_add">新增</el-button>
                <el-button type="primary" icon="upload">导入</el-button>
            </div>
            <div class="tjm_card_tools_right">
                <el-button icon="upload-filled">导出</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="450" :data="tableData" border>
                <el-table-column type="index" width="50"></el-table-column>
                <el-table-column label="政策编号" prop="businessNo" width="180"></el-table-column>
                <el-table-column label="状态" width="180">
                    <template #default="scope">{{ formatStatus(scope.row) }}</template>
                </el-table-column>
                <el-table-column label="政策名称" prop="name" width="180"></el-table-column>
                <el-table-column label="政策等级" prop="zcLevel" width="180"></el-table-column>
                <el-table-column label="政策类别" prop="zcType" width="180"></el-table-column>
                <el-table-column label="政策文号" prop="docNo" width="180"></el-table-column>
                <el-table-column label="填报人" prop="createName" width="180"></el-table-column>
                <el-table-column label="填报单位" prop="companyName" width="180"></el-table-column>
                <el-table-column label="填报时间" prop="createTime" width="180"></el-table-column>
                <el-table-column label="联系方式" prop="contactPhone" width="180"></el-table-column>
                <el-table-column label="考核评分" prop="score" width="180"></el-table-column>
                <el-table-column label="是否入库" width="180">
                    <template #default="scope">{{ formatInWh(scope.row.isInWh) }}</template>
                </el-table-column>
                <el-table-column label="入库类别" prop="inWhType" width="180"></el-table-column>
                <el-table-column label="操作" fixed="right" width="260">
                    <template #default="scope">
                        <template v-if="scope.row.status == 'active' || scope.row.status == 'inactive'">
                            <el-button type="primary" icon="edit" link @click="table_edit(scope.row)">修改</el-button>
                            <el-button type="primary" icon="edit" link @click="table_update(scope.row)">提交</el-button>
                            <el-button type="primary" icon="delete" link @click="table_del(scope.row)">删除</el-button>
                        </template>
                        <el-button v-if="scope.row.status == 'approve' || scope.row.status == 'done'" type="primary" link @click="table_edit(scope.row, 'detail')">详情</el-button>
                        <el-button v-if="scope.row.status == 'approve'" type="primary" link @click="table_update(scope.row, 'withdraw')">撤回</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-detail v-if="dialog" ref="policyDetail" @success="reloadTable" @closed="dialog = false"></policy-detail>
</template>

<script>
import API from "@/api/policy/share"
import { levelDic, typeDic, statusDic, inWHDic, storageTypeDic } from "./main";
import yhPagination from "@/components/Pagination/index.vue";
import policyDetail from "./dialog.vue";

export default {
    components: {
        yhPagination,
        policyDetail
    },

    data() {
        return {
            levelDic, typeDic, statusDic, inWHDic, storageTypeDic,

            loading: false,
            params: {
                page: 1,
                size: 10
            },

            total: 0,
            tableData: [],

            dialog: false
        }
    },

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

    methods: {
        formatStatus({ status, isInWh }) {
            if (status == "done" && isInWh == 1) return "审核入库"
            return statusDic[status] || "";
        },

        formatInWh (value)  {
            return inWHDic[value] || "";
        },

        reloadTable(mode = "add") {
            if (mode == "add") this.params.page = 1;

            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() {
            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_add() {
            this.dialog = true;
            this.$nextTick(() => this.$refs.policyDetail.init());
        },

        table_edit(row, mode = "edit") {
            this.dialog = true;
            this.$nextTick(() => this.$refs.policyDetail.init(mode).setData(row.id));
        },

        table_del(row) {
            ElMessageBox.confirm("是否确认删除?", "删除警告", {
                type: "warning",
                confirmButtonText: "确定",
                cancelButtonText: "取消"
            }).then(() => {
                API.del({ ids: row.id }).then(res => {
                    if (res.code == 200) {
                        ElMessage.success("操作成功");
                        this.reloadTable();
                    } else ElMessage.warning(res.msg);
                });
            });
        },

        table_update(row, mode = "saveApprove") {
            const msg = mode == "withdraw" && "撤回" || "提交";
            ElMessageBox.confirm(`是否确认${msg}?`, `${msg}警告`, {
                type: "warning",
                confirmButtonText: "确定",
                cancelButtonText: "取消"
            }).then(() => {
                API[mode](row).then(res => {
                    if (res.code === 200) {
                        ElMessage.success("操作成功");
                        this.reloadTable();
                    } else ElMessage.error(res.msg);
                });
            });
        }
    }
}
</script>

<style lang="scss" scoped>
</style>