<template>
    <div class="page-container list-container">
        <!-- 功能按钮区 -->
        <div class="list-btns-container">
            <el-button type="primary" size="small" icon="Plus" @click="handleAdd"
                v-hasPermi="['business:invoice:add']">新增</el-button>

            <!-- <el-button
                    type="success"
                    size="small"
                    icon="Edit"
                    :disabled="single"
                    @click="handleUpdate"
                    v-hasPermi="['business:invoice:edit']"
            >修改</el-button>

            <el-button
                    type="danger"
                    size="small"
                    icon="Delete"
                    :disabled="multiple"
                    @click="handleDelete"
                    v-hasPermi="['business:invoice:remove']"
            >删除</el-button> -->

            <el-button type="warning" size="small" icon="Download" @click="handleExport"
                v-hasPermi="['business:invoice:export']">导出</el-button>
            <!--<right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>-->
        </div>
        <!-- 搜索区 -->
        <el-form class="list-search-container" :model="queryParams" ref="queryRef" :inline="true" label-width="68px">
            <el-form-item label="收票方:" prop="invoiceOther">
                <el-input size="small" v-model="queryParams.invoiceOther" style="width: 150px" placeholder="请输入收票方"
                    clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="合同编号: " prop="contractNo">
                <el-input size="small" v-model="queryParams.contractNo" style="width: 150px" placeholder="合同编号"
                    clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item label="发票类型: " prop="invoiceType">
                <el-select v-model="queryParams.invoiceType" placeholder="请选择发票类型" size="small" clearable
                    style="width: 150px">
                    <el-option label="增值税专用发票" value="1"></el-option>
                    <el-option label="增值税普通发票" value="2"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="状态:" prop="status">
                <el-select v-model="queryParams.status" placeholder="请选择状态" size="small" clearable style="width: 150px">
                    <el-option label="未提交" value="0"></el-option>
                    <el-option label="待开票" value="1"></el-option>
                    <el-option label="已拒绝" value="2"></el-option>
                    <el-option label="已开票" value="3"></el-option>
                    <el-option label="已退回" value="4"></el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="申请人: " prop="applierName">
                <el-input size="small" v-model="queryParams.applierName" style="width: 150px" placeholder="申请人"
                    clearable @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item prop="dateFilter" label="申请时间:">
                <el-date-picker v-model="queryParams.dateFilter" type="daterange" clearable size="small"
                    format="YYYY - MM - DD " value-format="YYYY-MM-DD" range-separator="至" start-placeholder="业务日期"
                    end-placeholder="业务日期" style="width: 100%;"></el-date-picker>
            </el-form-item>
            <el-form-item label="开票人:" prop="invoiceName">
                <el-input size="small" v-model="queryParams.invoiceName" placeholder="请输入开票人" clearable
                    style="width: 150px" @keyup.enter="handleQuery" />
            </el-form-item>
            <el-form-item prop="invoiceDateFilter" label="开票时间:">
                <el-date-picker v-model="queryParams.invoiceDateFilter" type="daterange" clearable size="small"
                    format="YYYY - MM - DD " value-format="YYYY-MM-DD" range-separator="至" start-placeholder="业务日期"
                    end-placeholder="业务日期" style="width: 100%;"></el-date-picker>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery" size="small">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery" size="small">重置</el-button>
            </el-form-item>

        </el-form>
        <!-- 列表区 -->
        <el-table v-loading="loading" :data="invoiceList" size="small" border height="100%">
            <el-table-column type="index" width="55" align="center" />
            <el-table-column label="收票方" align="center" prop="invoiceOther" width="200"></el-table-column>/>
            <el-table-column label="合同编号" align="center" prop="contractNo" />
            <el-table-column label="发票类型" align="center" prop="invoiceType">
                <template #default="scope">
                    <span v-if="scope.row.invoiceType == 1">增值税专用发票</span>
                    <span v-if="scope.row.invoiceType == 2">增值税普通发票</span>
                    <span v-if="scope.row.invoiceType == 3">增值税电子发票</span>
                </template>
            </el-table-column>
            <el-table-column label="开票金额" align="center" prop="allowInvoiceAmount" />
            <el-table-column label="发票备注" align="center" prop="remark" />
            <el-table-column label="状态" align="center" prop="status">
                <template #default="scope">
                    <el-link style="font-size: 12px;" type="warning" :underline="false"
                        v-if="scope.row.status == 0">未提交</el-link>
                    <el-link style="font-size: 12px;" type="primary" :underline="false"
                        v-if="scope.row.status == 1">待开票</el-link>
                    <el-link style="font-size: 12px;" type="danger" :underline="false"
                        v-if="scope.row.status == 2">已拒绝</el-link>
                    <el-link style="font-size: 12px;" type="success" :underline="false"
                        v-if="scope.row.status == 3">已开票</el-link>
                        <el-link style="font-size: 12px;" type="warning" :underline="false"
                        v-if="scope.row.status == 4">已退回</el-link>
                </template>
            </el-table-column>
            <el-table-column label="申请人" align="center" prop="applierName" />
            <el-table-column label="申请时间" align="center" prop="applierTime" width="180">
            </el-table-column>
            <el-table-column label="开票人" align="center" prop="invoiceName" />
            <el-table-column label="开票时间" align="center" prop="invoiceTime" />

            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template #default="scope">
                    <el-button link type="warning" size="small" icon="Edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['business:invoice:query']">查看</el-button>
                    <!-- <el-button link type="warning" size="small" icon="Edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['business:invoice:edit']">修改</el-button> -->
                    <!-- <el-button link type="danger" size="small" icon="Delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['business:invoice:remove']">删除</el-button> -->
                </template>
            </el-table-column>
        </el-table>

        <!-- 分页 -->
        <pagination v-show="total>0" :total="total" v-model:page="queryParams.pageNum"
            v-model:limit="queryParams.pageSize" @pagination="getList" />

        <!-- 表单 -->
        <invoice-form ref="invoiceRef" :get-list="getList"></invoice-form>
    </div>
</template>

<script setup name="Invoice">
    import { listInvoice, delInvoice, exportInfo } from "@/api/business/invoice";
    import invoiceForm from "./form"
    const { proxy } = getCurrentInstance()
    /** 字典数组区 */
    /** 查询 对象 */

    const invoiceList = ref([]);
    const loading = ref(true);
    const ids = ref([])
    const single = ref(true);
    const multiple = ref(true);
    const total = ref(0);
    /** 查询对象 */
    const queryParams = ref({
        pageNum: 1,
        pageSize: 20,
        invoiceType: null,
        allowInvoiceAmount: null,
        invoiceContent: null,
        invoiceMy: null,
        myCreditSocietyCode: null,
        myAddress: null,
        myAccount: null,
        invoiceOther: null,
        otherCreditSocietyCode: null,
        otherAddress: null,
        applierTime: null,
        status: null,
        invoiceTime: null,
        invoiceRemark: null,
    })

    /***********************  方法区  ****************************/
    onActivated(() => {
        // 你的逻辑
        getList();
    });
    /** 查询企业租户列表 */
    function getList() {
        loading.value = true;
        listInvoice(queryParams.value).then(response => {
            invoiceList.value = response.rows;
            total.value = response.total;
            loading.value = false;
        });
    }

    /** 搜索按钮操作 */
    function handleQuery() {
        queryParams.value.pageNum = 1;
        getList();
    }

    /** 重置按钮操作 */
    function resetQuery() {
        proxy.resetForm("queryRef");
        handleQuery();
    }

    // 多选框选中数据
    function handleSelectionChange(selection) {
        ids.value = selection.map(item => item.id);
        single.value = selection.length != 1;
        multiple.value = !selection.length;
    }

    /** 新增按钮操作 */
    function handleAdd() {
        proxy.$refs.invoiceRef.open()
    }

    /** 修改按钮操作 */
    function handleUpdate(row) {
        const id = row.id || ids.value
        proxy.$refs.invoiceRef.open(id)
    }


    /** 删除按钮操作 */
    function handleDelete(row) {
        const _ids = row.id || ids.value;
        proxy.$modal.confirm('是否确认删除选中的数据项？').then(function () {
            return delInvoice(_ids);
        }).then(() => {
            getList();
            proxy.$modal.msgSuccess("删除成功！");
        }).catch(() => { });
    }

    /** 导出按钮操作 */
    function handleExport() {
        exportInfo(queryParams.value)
    }
    getList();
</script>