<template>
    <div class="common-container">
        <TitleBar></TitleBar>
        <div class="main-container">
            <div class="search-container TableSelectMain">
                <el-form class="table-select-main" :model="searchForm" label-position="left">
                    <el-row :gutter="15">
                        <el-col :span="24" v-if="show">
                            <el-form-item>
                                <el-button
                                    type="primary"
                                    plain
                                    round
                                    icon="el-icon-plus"
                                    @click="create('add')"
                                >新建</el-button>
                                <el-button
                                    type="primary"
                                    plain
                                    round
                                    @click="deleteData(ids, 'batch')"
                                >批量删除</el-button>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
            </div>

            <div class="SearchTitle">
                已选择
                <span class="subject-color">{{ ids.length }}</span> 项
            </div>

            <div class="MaintenanceTable">
                <el-table
                    ref="multipleTable"
                    :data="tableData"
                    style="width: 100%"
                    tooltip-effect="dark"
                    @selection-change="selectionChange"
                    border
                    v-loading="listLoading"
                >
                    <el-table-column type="selection"></el-table-column>
                    <!-- <el-table-column prop="ProjectCode" label="项目编号"></el-table-column> -->
                    <el-table-column prop="taxName" label="税目" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column
                        prop="taxRate"
                        label="税率"
                        min-width="100px;"
                        :show-overflow-tooltip="true"
                    >
                        <template slot-scope="scope">{{ scope.row.taxRate }}%</template>
                    </el-table-column>
                    <el-table-column
                        prop="examineFlag"
                        label="审批状态"
                        min-width="100px;"
                        :show-overflow-tooltip="true"
                    >
                        <template slot-scope="scope">
                            <span class="warn" v-show="scope.row.examineFlag == 0">未审核</span>
                            <span class="success" v-show="scope.row.examineFlag == 1">审核通过</span>
                            <RejectReason
                                :content="scope.row.notExamineReason"
                                v-show="scope.row.examineFlag == 2"
                            ></RejectReason>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作">
                        <template slot-scope="scope">
                            <el-button
                                size="mini"
                                type="primary"
                                plain
                                round
                                v-if="show"
                                @click="deleteData(scope.row.id)"
                            >删除</el-button>
                            <el-button
                                size="mini"
                                type="primary"
                                plain
                                round
                                v-if="show"
                                @click="edit(scope.row)"
                            >编辑</el-button>
                            <el-button
                                size="mini"
                                type="primary"
                                plain
                                round
                                v-if="!show"
                                @click="pass(scope.row.id)"
                            >通过</el-button>
                            <el-button
                                size="mini"
                                type="primary"
                                plain
                                round
                                v-if="!show"
                                @click="rejectShow(scope.row.id)"
                            >驳回</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <div class="pagenation">
                    <PageComponent
                        :total="total"
                        @pagination="handlePagination"
                        :pageSize.sync="page.pageSize"
                        :pageNum.sync="page.pageNum"
                    ></PageComponent>
                </div>
            </div>
        </div>
        <addEditDialog @queryListData="queryListData" ref="addEditVisible"></addEditDialog>
        <!-- 驳回弹窗 -->
        <RejectDialog @isReject="isReject" ref="regectDialog"></RejectDialog>
    </div>
</template>
<script>
import TitleBar from '@/apps/mobile/components/common/TitleBar';
import addEditDialog from './components/addEditDialog';
import RejectReason from '@/apps/mobile/components/ReasonForRejection/index';
import RejectDialog from '@/apps/mobile/components/reject/index';
import { queryPage, removeData, editData, examine } from '@/apps/mobile/api/operation/taxRate/index';
export default {
    components: {
        TitleBar,
        addEditDialog,
        RejectReason,
        RejectDialog,
    },
    data() {
        return {
            searchForm: {}, // 搜索表单
            tableData: [], // 列表数据
            page: {
                // 页码
                pageNum: 1,
                pageSize: 5,
            },
            total: 5,
            listLoading: false, // table是否展示loading
            ids: [],
            show: 1,
        };
    },
    methods: {
        create() {
            this.$refs.addEditVisible.open('add');
        },
        edit(data) {
            this.$refs.addEditVisible.open('edit', data);
        },
        // 页码变化时
        handlePagination(val) {
            this.page = val;
            this.queryListData();
        },
        /**
         * table选择列被改变时
         * @param {Array} data 选中的列
         */
        selectionChange(data) {
            this.ids = data.map(item => {
                return item.id;
            });
        },

        queryListData() {
            // this.listLoading = true;
            let obj = {
                examineFlag: this.show === 0 ? 0 : null,
                ...this.page,
                ...this.searchForm,
                sort: {
                    order: 'asc',
                    orderBy: 'create_time',
                },
            };

            queryPage(obj)
                .then(res => {
                    if(res.statusCode == "10"){
                        this.$router.replace("/login");
                    }
                    this.tableData = res.data.records;
                    this.total = res.data.total;
                    this.listLoading = false;
                })
                .catch(() => {
                    this.listLoading = false;
                });
        },
        /**
         * 删除功能
         * @param {String} type 授权操作类型:'batch'批量授权  'single'单个授权
         * @param {Number} id 当前需要删除的id或集合
         */
        deleteData(id, type = 'single') {
            if (type === 'batch') {
                if (this.ids.length === 0) return this.$message.warning('请至少选中一项 !');
                // console.log(this.ids, "ids");

                this.delete({ids: this.ids});
            } else {
                this.delete({ids: [id]});
            }
        },
        delete(id) {
            this.$confirm('确定要删除该数据吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                closeOnClickModal: false,
            })
                .then(() => {
                    removeData(id)
                        .then(() => {
                            this.$message.success('删除成功');
                            this.queryListData();
                        })
                        .catch(() => {});
                })
                .catch(() => {});
        },
        showChange() {
            this.show = 0;
        },
        // 驳回弹框
        rejectShow(id) {
            this.$refs.regectDialog.open(id);
        },
        // 审核通过  (仅在审批页面使用)
        pass(id) {
            this.$confirm('是否通过审批?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    let obj = {
                        id,
                        examineFlag: 1,
                    };
                    this.approval(obj);
                })
                .catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消',
                    });
                });
        },
        // 审批
        approval(data) {
            examine(data).then(() => {
                this.$message.success('操作成功');
                this.queryListData();
            });
        },
        // 驳回 (审批页面调用)
        isReject(data) {
            // console.log(data, 'isRegect');
            let obj = {
                id: data.id,
                notExamineReason: data.checkComment,
                examineFlag: 2,
            };

            this.approval(obj);
        },
    },
    created() {
        this.$nextTick(() => {
            this.queryListData();
        });
    },
    // filters: {
    //     toPercent: num => {
    //         let str = Number(num * 100).toFixed(1);
    //         str += "%";
    //         return str;
    //     }
    // }
};
</script>
<style lang="stylus" scoped>
.TableSelectMain .el-date-editor.el-input.el-input--prefix.el-input--suffix.el-date-editor--date {
    width: 95%;
}
</style>
