<template>
    <el-card class="tjm_card_style_custom">
        <div class="tjm_card_title">条件检索</div>
        <div class="tjm_card_select">
            <el-scrollbar>
                <el-form class="tjm_card_select_left" :model="params" label-width="80px" label-position="left">
                    <el-row :gutter="15">
                        <el-col :lg="8" :md="12" :xs="24">
                            <el-form-item label="政策编号">
                                <el-input v-model="params.businessNo" clearable placeholder="请输入政策编号"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :lg="8" :md="12" :xs="24">
                            <el-form-item label="项目名称">
                                <el-input v-model="params.name" clearable placeholder="请输入项目名称"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :lg="8" :md="12" :xs="24">
                            <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-col>
                        <template v-if="paramsIsCollapse">
                            <el-col :lg="8" :md="12" :xs="24">
                                <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-col>
                            <el-col :lg="8" :md="12" :xs="24">
                                <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-col>
                            <el-col :lg="8" :md="12" :xs="24">
                                <el-form-item label="是否落地">
                                    <el-select v-model="params.isLand" clearable placeholder="请选择落地状态">
                                        <el-option v-for="(label, key) in whetherDic" :key="key" :label="label" :value="key"></el-option>
                                    </el-select>
                                </el-form-item>
                            </el-col>
                            <el-col :lg="8" :md="12" :xs="24">
                                <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-col>
                        </template>
                    </el-row>            
                </el-form>
                <div class="tjm_card_select_right">
                    <el-button type="primary" icon="search" @click="reloadTable">搜索</el-button>
                    <el-button icon="refresh-right" @click="reset">重置</el-button>
                    <el-button type="primary" link @click="paramsIsCollapse = !paramsIsCollapse">
                        <template v-if="paramsIsCollapse">收起<tjm-icon-ep-arrow-up-bold /></template>
                        <template v-else>更多<tjm-icon-ep-arrow-down-bold /></template>
                    </el-button>
                </div>
            </el-scrollbar>
        </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>
            </div>
            <div class="tjm_card_tools_right flex-row">
                <el-dropdown>
                    <el-button icon="upload-filled">导入</el-button>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <el-dropdown-item>
                                <table-import url="policyStrive" @importSuc="reloadTable">政策争取</table-import>
                            </el-dropdown-item>
                            <el-dropdown-item>
                                <table-import url="policyStriveMonth" @importSuc="reloadTable">月度完成</table-import>
                            </el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
                <el-button class="export-btn" icon="download" @click="table_export">导出</el-button>
                <el-dropdown @command="download_temp">
                    <el-button icon="download">导入模版下载</el-button>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <el-dropdown-item command="policy_strive">政策争取</el-dropdown-item>
                            <el-dropdown-item command="policy_strive_month">月度完成</el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
            </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 @row-click="(row, column) => !column.fixed && table_detail(row)">
                <el-table-column type="index" label="序号" width="55"></el-table-column>
                <template v-for="(item, index) in columns" :key="index">
                    <el-table-column :label="item.label" :prop="item.props" :min-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="280">
                    <template #default="scope">
                        <template v-if="scope.row.status == 'active' || scope.row.status == 'inactive'">
                            <template v-if="scope.row.status == 'active'">
                                <el-button type="primary" link icon="edit" @click.stop="table_edit(scope.row)">修改</el-button>
                                <el-button type="primary" link icon="upload" @click.stop="table_edit(scope.row)">上报</el-button>
                            </template>
                            <template v-if="scope.row.status == 'inactive'">
                                <el-button type="primary" link icon="tickets" @click.stop="table_edit(scope.row)">重新上报</el-button>
                                <el-button v-if="scope.row.processTaskId && scope.row.processInstanceId" type="primary" link @click.stop="table_process(scope.row)">
                                    <template #icon><tjm-icon-uis-process /></template>
                                    审批流程
                                </el-button>
                            </template>
                            <el-button type="primary" link icon="delete" @click.stop="table_del(scope.row)">删除</el-button>
                        </template>
                        <template v-if="scope.row.status == 'approve' || scope.row.status == 'done'">
                            <el-button type="primary" link icon="tickets" @click.stop="table_detail(scope.row)">详情</el-button>
                            <el-button v-if="scope.row.processTaskId && scope.row.processInstanceId" type="primary" link @click.stop="table_process(scope.row)">
                                <template #icon><tjm-icon-uis-process /></template>
                                审批流程
                            </el-button>
                        </template>
                        <el-button v-if="scope.row.status == 'approve' && scope.row.isWithdraw == 1" type="primary" link icon="refresh-right" @click.stop="table_withdraw(scope.row)">撤回</el-button>
                        <el-button v-if="scope.row.status == 'done'" class="no-m-l" type="primary" link icon="info-filled" @click.stop="table_detail(scope.row, 'explain')">月度争取情况说明</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.detail" ref="policyDetail" @success="reloadTable" @closed="closed('detail', $event)"></policy-detail>
    <policy-explain v-if="dialog.explain" ref="policyExplain" @success="reloadTable" @closed="dialog.explain = false"></policy-explain>
    <policy-process-dialog v-if="dialog.process" ref="processDialog" @success="reloadTable" @closed="closed('process', $event)"></policy-process-dialog>
    <yh-workflow v-if="dialog.workflow" ref="yhWorkflow" @closed="dialog.workflow = false"></yh-workflow>
</template>

<script>
import Folder from "@/api/folder";
import Temp from "@/api/system/template";
import API from "@/api/policy/strive";
import { useUserStore } from "@/store/user";
import { exportExcel } from "@/utils/exportExcel";
import { levelDic, typeDic, whetherDic } from "@/views/policyShare/main";
import { columns, statusDic } from "./main";

import tableImport from "@/components/Upload/tableImport.vue";
import yhPagination from "@/components/Pagination/index.vue";
import yhWorkflow from "@/components/Workflow/index.vue";
import policyDetail from "./dialog.vue";
import policyExplain from "./explain.vue";
import policyProcessDialog from "@/views/myAffairs/strive.vue";

export default {
    components: {
        tableImport,
        yhPagination,
        yhWorkflow,
        policyDetail,
        policyExplain,
        policyProcessDialog
    },

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

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

            total: 0,
            tableData: [],

            dialog: {
                detail: false,
                explain: false,
                process: false,
                workflow: false
            }
        }
    },

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

    methods: {
        columnFormat(row, props) {
            if (props == "createTime") return row[props].split(" ")[0];
            if (props == "status") return statusDic[row[props]] || "";
            if (props == "isLand") return whetherDic[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;
                this.tableData = res.data.records;
                this.total = res.data.total;
            }).catch(() => this.loading = false);
        },

        reset() {
            this.createTime = [];
            this.params = {
                page: 1,
                size: 10,
                createId: useUserStore().userInfo.id,
                userDeptId: useUserStore().userInfo.deptId
            }
            this.reloadTable();
        },

        download_temp(refType) {
            Temp.get({ refType }).then(res => {
                if (!res.data.records[0]) ElMessage.warning("暂无模版，请联系管理员");
                else {
                    const { templateName, templateUrl } = res.data.records[0];
                    Folder.download(templateUrl).then(res => {
                        const a = document.createElement("a");
                        const blob = new Blob([res.data], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });
                        a.download = templateName;
                        a.href = URL.createObjectURL(blob);
                        a.click();
                    });
                }
            });
        },

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

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

        table_add() {
            this.dialog.detail = true;
            nextTick(() => this.$refs.policyDetail.open());
        },

        table_edit(row) {
            if (row.status == "inactive") {
                this.dialog.process = true;
                nextTick(() => this.$refs.processDialog.open("resubmit").setData(row.id));
            }
            if (row.status == "active") {
                this.dialog.detail = true;
                nextTick(() => this.$refs.policyDetail.open("edit").setData(row.id));
            }
        },

        table_detail(row, mode = "detail") {
            this.dialog.explain = true;
            nextTick(() => this.$refs.policyExplain.open(mode).setData(row.id));
        },

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

        table_withdraw(row) {
            ElMessageBox.confirm("是否确认撤回?", "提示", {
                type: "warning",
                confirmButtonText: "确定",
                cancelButtonText: "取消"
            }).then(() => {
                API.withdraw(row).then(() => {
                    ElMessage.success("操作成功");
                    this.reloadTable();
                });
            });
        },

        closed(key, e) {
            e && this.reloadTable();
            this.dialog[key] = false;
        },

        table_process(row) {
            this.dialog.workflow = true;
            nextTick(() => this.$refs.yhWorkflow.getDetail(row));
        }
    }
}
</script>

<style lang="scss" scoped>
.export-btn {
  margin: 0 12px;
}

.no-m-l {
  margin-left: 0;
}
</style>