<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="4">
                            <el-form-item>
                                <el-input placeholder="请输入发票名称" v-model="searchForm.gmfMc"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="4">
                            <el-form-item>
                                <el-select
                                    placeholder="状态"
                                    v-model="searchForm.checkFlag"
                                    clearable
                                >
                                    <el-option label="已驳回" :value="0"></el-option>
                                    <el-option label="审批通过" :value="1"></el-option>
                                    <el-option label="待审核" :value="2"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="4">
                            <el-form-item>
                                <el-button type="primary" plain round @click="query">查询</el-button>
                                <el-button type="primary" plain round @click="reset">重置</el-button>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item>
                                <!-- <el-button type="primary" round plain icon="el-icon-plus">新建</el-button> -->
                                <el-button
                                    type="primary"
                                    round
                                    plain
                                    @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"
                    class="OutTable"
                    border
                    v-loading="listLoading"
                >
                    <el-table-column type="selection"></el-table-column>>
                    <el-table-column prop="gmfMc" label="名称" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column prop="email" label="邮箱" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column
                        prop="userInvoice.name"
                        label="发票抬头"
                        min-width="120px"
                        :show-overflow-tooltip="true"
                    ></el-table-column>
                    <el-table-column prop="invoiceType" label="发票种类" min-width="120px">
                        <template slot-scope="scope">
                            <span v-if="scope.row.invoiceType == 2">专用发票</span>
                            <span v-if="scope.row.invoiceType == 1">电子发票</span>
                            <span v-if="scope.row.invoiceType == 0"></span>
                        </template>
                    </el-table-column>
                    <el-table-column prop="type" label="发票类型" min-width="120px">
                        <template slot-scope="scope">
                            <span v-if="scope.row.type == 2">企业</span>
                            <span v-if="scope.row.type == 1">个人</span>
                        </template>
                    </el-table-column>
                    <el-table-column
                        prop="userInvoice.enterpriseNumber"
                        label="公司税号"
                        min-width="120px"
                        :show-overflow-tooltip="true"
                    ></el-table-column>
                    <el-table-column
                        prop="userInvoice.gmfDz"
                        label="公司地址"
                        :show-overflow-tooltip="true"
                    ></el-table-column>
                    <el-table-column
                        prop="userInvoice.gmfDh"
                        label="注册电话"
                        :show-overflow-tooltip="true"
                    ></el-table-column>
                    <el-table-column
                        prop="userInvoice.gmfYh"
                        label="开户银行"
                        :show-overflow-tooltip="true"
                    ></el-table-column>
                    <el-table-column
                        prop="userInvoice.gmfZh"
                        label="开户银行账号"
                        :show-overflow-tooltip="true"
                    ></el-table-column>
                    <el-table-column prop="hjje" label="合计金额" :show-overflow-tooltip="true"></el-table-column>
                    <!-- <el-table-column prop="url" label="发票链接" :show-overflow-tooltip="true">
                        <template slot-scope="scope">
                            <el-link
                                v-if="scope.row.invoiceImg && scope.row.invoiceImg.url"
                                :href="scope.row.invoiceImg.url"
                                type="primary"
                            >{{ scope.row.invoiceImg.url }}</el-link>
                            <span v-else>- 无 -</span>
                        </template>
                    </el-table-column>-->
                    <el-table-column prop="status" label="审核状态" :show-overflow-tooltip="true">
                        <template slot-scope="scope">
                            <span v-show="scope.row.checkFlag === 2" class="warn">未审核</span>
                            <span v-show="scope.row.checkFlag === 1" class="success">审核通过</span>
                            <RejectReason
                                :content="scope.row.reason"
                                v-show="scope.row.checkFlag == 0"
                            ></RejectReason>
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" min-width="210px">
                        <template slot-scope="scope">
                            <el-button
                                size="mini"
                                type="primary"
                                plain
                                round
                                @click="deleteData(scope.row.id)"
                            >删除</el-button>
                            <el-button
                                size="mini"
                                type="primary"
                                plain
                                round
                                @click="detailsFormShow(scope.row)"
                            >查看</el-button>
                            <el-button
                                size="mini"
                                type="primary"
                                plain
                                round
                                @click="pass(scope.row.id)"
                                :disabled="scope.row.checkFlag != 2"
                            >通过</el-button>
                            <el-button
                                size="mini"
                                type="primary"
                                plain
                                round
                                @click="rejectShow(scope.row.id)"
                                :disabled="scope.row.checkFlag != 2"
                            >驳回</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>
        <detailsDialog ref="detailsDialog"></detailsDialog>
        <!-- 驳回弹窗 -->
        <RejectDialog @isReject="isReject" ref="regectDialog"></RejectDialog>
        <TaxRateDialog ref="taxRateDialog" @queryListData="queryListData"></TaxRateDialog>
    </div>
</template>
<script>
import TitleBar from '@/apps/mobile/components/common/TitleBar';
import detailsDialog from './components/detaisDialog';
import TaxRateDialog from './components/taxRateDialog';
import RejectDialog from '@/apps/mobile/components/reject/index';
import RejectReason from '@/apps/mobile/components/ReasonForRejection/index';
import { queryPage, editData, examineData, removeData } from '@/apps/mobile/api/payment/auditInvoice/index';
export default {
    components: {
        TitleBar,
        detailsDialog,
        RejectDialog,
        RejectReason,
        TaxRateDialog,
    },
    data() {
        return {
            searchForm: {},
            tableData: [],
            page: {
                pageNum: 1,
                pageSize: 5,
            },
            total: 5,
            listLoading: false,
            time: [],
            ids: [],
        };
    },
    methods: {
        selectionChange(data) {
            this.ids = data.map(item => {
                return item.id;
            });
        },
        // 页码变化时
        handlePagination(val) {
            this.page = val;
            this.queryListData();
        },
        closeDialog(dialogVisibleName) {
            this[dialogVisibleName] = false;
            this.detailsForm = {};
        },
        detailsFormShow(data) {
            this.$refs.detailsDialog.open({ id: data.id, ruleData: data });
        },
        queryListData() {
            // this.listLoading = true;
            let obj = {
                ...this.page,
                ...this.searchForm,
                sort: {
                    order: 'desc',
                    orderBy: 'create_time',
                },
            };
            queryPage(obj)
                .then(res => {
                    console.log(res, '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') {
            let obj = {};
            if (type === 'batch') {
                if (this.ids.length === 0) return this.$message.warning('请至少选中一项 !');
                // console.log(this.ids, "ids");
                obj = {
                    ids: this.ids
                }
                this.delete(obj);
            } else {
                obj = {
                    ids: [id]
                }
                this.delete(obj);
            }
        },
        delete(obj) {
            this.$confirm('确定要删除该数据吗?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                closeOnClickModal: false,
            })
                .then(() => {
                    removeData(obj)
                        .then(() => {
                            this.$message.success('删除成功');
                            this.queryListData();
                        })
                        .catch(() => {});
                })
                .catch(() => {});
        },
        // 查询
        query() {
            for (let k in this.searchForm) {
                if (this.searchForm[k] === '') {
                    delete this.searchForm[k];
                }
            }
            this.queryListData();
        },
        // 重置
        reset() {
            this.searchForm = {};
            this.queryListData();
        },
        // 通过
        pass(id) {
            // this.$refs.taxRateDialog.open(id);
            this.$confirm('是否通过审批?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
            })
                .then(() => {
                    let obj = {
                        id,
                        checkFlag: 1,
                    };
                    this.approval(obj);
                })
                .catch(() => {});
        },

        // 驳回弹框
        rejectShow(id) {
            this.$refs.regectDialog.open(id);
        },
        // 确定驳回
        isReject(data) {
            let obj = {
                id: data.id,
                reason: data.checkComment,
                checkFlag: 0,
            };
            this.approval(obj);
        },
        // 审批
        approval(data) {
            examineData(data).then(res => {
                if (res.statusCode === '1') {
                    this.$message.success('操作成功');
                } else {
                    this.$message.success(res.message);
                }
                this.queryListData();
            });
        },
    },
    created() {
        this.queryListData();
    },
};
</script>
<style scoped>
.TableSelectMain .el-date-editor.el-input.el-input--prefix.el-input--suffix.el-date-editor--date {
    width: 95%;
}
.el-date-editor {
    border-radius: 20px;
    overflow: hidden;
    height: 30px;
    line-height: 30px;
}
.el-range-editor.el-input__inner {
    margin-top: 5px;
}
</style>
