<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.businessNo" clearable placeholder="请输入政策编号"></el-input>
                </el-form-item>
                <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.isInWh" clearable placeholder="请选择入库状态">
                        <el-option v-for="(label, key) in whetherDic" :key="key" :label="label" :value="parseInt(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 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_left">
                <el-button type="primary" icon="plus" @click="table_add">新增</el-button>
            </div>
            <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 @row-click="(row, column) => !column.fixed && table_edit(row, 'detail')">
                <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" :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="210">
                    <template #default="scope">
                        <el-button type="primary" link icon="edit" @click.stop="table_edit(scope.row)">修改</el-button>
                        <el-button type="primary" link icon="delete" @click.stop="table_del(scope.row)">删除</el-button>
                        <el-button v-if="scope.row.isInWh == 1 && scope.row.inWhType == '文件类'" class="no-m-l" type="primary" link @click.stop="table_edit(scope.row, 'decode')">
                            <template #icon><tjm-icon-fluent-emoji-high-contrast-open-book /></template>
                            添加解读
                        </el-button>
                        <el-button v-if="scope.row.processTaskId && scope.row.processInstanceId" :class="scope.row.isInWh == 0 || scope.row.inWhType == '解读类' && 'no-m-l'" type="primary" link @click.stop="table_process(scope.row)">
                            <template #icon><tjm-icon-uis-process /></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-detail v-if="dialog.detail" ref="policyDetail" @success="reloadTable" @closed="closed"></policy-detail>
    <yh-workflow v-if="dialog.workflow" ref="yhWorkflow" @closed="dialog.workflow = false"></yh-workflow>
</template>

<script>
import API from "@/api/policy/share";
import { exportExcel } from "@/utils/exportExcel";
import { levelDic, typeDic, storageTypeDic, whetherDic } from "@/views/policyShare/main";
import { columns } from "./main";

import yhPagination from "@/components/Pagination/index.vue";
import yhWorkflow from "@/components/Workflow/index.vue";
import policyDetail from "./dialog.vue";

export default {
    components: {
        yhPagination,
        yhWorkflow,
        policyDetail
    },

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

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

            total: 0,
            tableData: [],

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

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

    methods: {
        columnFormat(row, props) {
            if (props == "createTime") return row[props].split(" ")[0];
            if (props == "isInWh") return whetherDic[row[props]] || "";
            if (props == "inWhType") return row.isInWh == 1 && row[props] || "";
            return row[props];
        },

        reloadTable(mode = "saveDone") {
            if (mode == "saveDone") 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 = [];
            this.params = {
                page: 1,
                size: 10,
                status: "done"
            }
            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_add() {
            this.dialog.detail = true;
            nextTick(() => this.$refs.policyDetail.open());
        },

        table_edit(row, mode = "edit") {
            this.dialog.detail = true;
            nextTick(() => this.$refs.policyDetail.open(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.error(res.msg);
                });
            }).catch(() => ElMessage.success("已取消"));
        },

        closed(e) {
            e && this.reloadTable();
            this.dialog.detail = false;
        },

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

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