<template>
    <div
        id="invoice_get"
        v-loading="loading"
        element-loading-text="拼命加载中......请稍等"
    >
        <el-container>
            <el-form label-position="top" ref="form" style="width: 100%">
                <el-row :gutter="10">
                    <el-col :span="6">
                        <el-form-item label="起始日期：">
                            <el-date-picker
                                v-model="search.beginDate"
                                type="date"
                                placeholder="选择日期"
                                style="width: 100%"
                            >
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item label="结束日期：">
                            <el-date-picker
                                v-model="search.endDate"
                                type="date"
                                placeholder="选择日期"
                                style="width: 100%"
                            >
                            </el-date-picker>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item>
                            <span slot="label" style="color: #909399">
                                <span>&nbsp;&nbsp;</span>
                            </span>
                            <el-button
                                @click="handleSearch"
                                type="primary"
                                icon="el-icon-search"
                                >搜索</el-button
                            >
                            <el-button @click="handleResetSearch"
                                >重置</el-button
                            >
                        </el-form-item>
                    </el-col>
                </el-row>
                <el-row :gutter="10">
                    <el-col :span="6">
                        <el-form-item>
                            <el-select
                                v-model="search.invoiceCategory"
                                placeholder="请选择发票种类"
                                style="width: 100%"
                            >
                                <el-option
                                    v-for="item in invoiceCategoryOptions"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                    <el-col :span="6">
                        <el-form-item>
                            <el-select
                                v-model="search.invoiceGetStatus"
                                placeholder="请选择发票申领状态"
                                style="width: 100%"
                            >
                                <el-option
                                    v-for="item in invoiceGetStatusOptions"
                                    :key="item.value"
                                    :label="item.label"
                                    :value="item.value"
                                ></el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
        </el-container>
        <div>
            <el-button
                type="primary"
                icon="el-icon-plus"
                @click="handleShowInvoiceGetCommitDialog"
                >发票申领</el-button
            >
        </div>
        <br />
        <el-container>
            <el-table
                ref="searchResultMultipleTable"
                :data="search.resultList"
                border
                stripe
                :height="tableHeight"
                style="width: 100%"
                :default-sort="{ prop: 'applyDate', order: 'descending' }"
            >
                <!-- <el-table-column type="selection" width="55"></el-table-column> -->
                <el-table-column
                    type="index"
                    :index="pageIndex"
                    align="center"
                    fixed="left"
                    label="序号"
                    width="50"
                ></el-table-column>
                <el-table-column
                    prop="invoiceCateName"
                    label="发票类别"
                    align="center"
                ></el-table-column>
                <!-- <el-table-column prop="invoiceKindName" label="发票种类"></el-table-column> -->
                <el-table-column
                    prop="applyNum"
                    label="申领份数"
                    align="center"
                ></el-table-column>
                <el-table-column
                    prop="applyDate"
                    label="申领时间"
                    align="center"
                    sortable
                ></el-table-column>
                <el-table-column
                    prop="handleDate"
                    label="处理时间"
                    align="center"
                ></el-table-column>
                <el-table-column
                    prop="handleStatus"
                    label="处理状态"
                    fixed="right"
                    align="center"
                    :formatter="fmtInvoiceGetStatus"
                ></el-table-column>
                <el-table-column
                    prop="handleInformation"
                    label="处理信息"
                    fixed="right"
                    align="center"
                    show-overflow-tooltip
                    :formatter="fmtHandleInformation"
                ></el-table-column>
                <el-table-column
                    label="操作"
                    align="center"
                    fixed="right"
                    width="320"
                >
                    <template slot-scope="scope">
                        <el-button
                            @click="handleConfirmInvoicePurchase(scope.row)"
                            :disabled="
                                disabledConfirmInvoicePurchase(scope.row)
                            "
                            type="text"
                            >申领确认</el-button
                        >
                        <el-button
                            @click="handleRevokeInvoicePurchase(scope.row)"
                            :disabled="disabledRevokeInvoicePurchase(scope.row)"
                            type="text"
                            >申领撤销</el-button
                        >
                        <el-button
                            @click="handleShowInvoicePurchaseDetails(scope.row)"
                            :disabled="
                                disabledShowInvoicePurchaseDetails(scope.row)
                            "
                            type="text"
                            >发票信息</el-button
                        >
                        <el-button
                            @click="
                                handleShowInvoicePurchaseOperator(scope.row)
                            "
                            type="text"
                            >经办人信息</el-button
                        >
                    </template>
                </el-table-column>
            </el-table>
        </el-container>
        <br />
        <div style="text-align: right">
            <el-pagination
                layout="prev, pager, next"
                :current-page="search.resultPagination.currentPage"
                :page-sizes="search.resultPagination.pageSizes"
                :page-size="search.resultPagination.pageSize"
                :total="search.resultPagination.total"
                @size-change="handleSearchResultSizeChange"
                @current-change="handleSearchResultCurrentChange"
            ></el-pagination>
        </div>

        <el-dialog
            title="网上申领"
            :visible.sync="invoiceGetCommit.dialogVisible"
            :close-on-click-modal="false"
            :close-on-press-escape="false"
            :show-close="false"
            @opened="handleInvoiceGetCommitOpenedDialog"
            @close="handleInvoiceGetCommitCloseDialog"
            width="55%"
            center
        >
            <div
                v-loading="invoiceGetCommit.loading"
                element-loading-text="拼命加载中......请稍等"
            >
                <el-form
                    ref="invoiceGetCommitForm"
                    :model="invoiceGetCommit.form"
                    :rules="invoiceGetCommit.rules"
                    label-width="170px"
                >
                    <el-form-item
                        label="发票类别"
                        prop="invoiceCategory"
                        style="text-align: left"
                    >
                        <el-select
                            v-model="invoiceGetCommit.form.invoiceCategory"
                            value-key="invoiceCateCode"
                            placeholder="请选择"
                            @change="handleInvoiceCategoryChange"
                        >
                            <el-option
                                v-for="item in invoiceGetCommit.pzhdInfos"
                                :key="item.invoiceCateCode"
                                :label="item.invoiceCateName"
                                :value="item"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item
                        label="发票种类"
                        prop="invoiceKind"
                        style="text-align: left"
                    >
                        <el-select
                            v-model="invoiceGetCommit.form.invoiceKind"
                            value-key="invoiceKindCode"
                            placeholder="请选择"
                        >
                            <el-option
                                v-for="item in invoiceGetCommit.invoiceKindCodes"
                                :key="item.invoiceKindCode"
                                :label="item.invoiceKindName"
                                :value="item"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item
                        label="申领份数"
                        prop="number"
                        style="text-align: left"
                    >
                        <el-input-number
                            v-model="invoiceGetCommit.form.number"
                            controls-position="right"
                            :precision="0"
                            :min="1"
                        ></el-input-number>
                    </el-form-item>
                    <el-form-item
                        label="经办人名称"
                        prop="claimant"
                        style="text-align: left"
                    >
                        <el-select
                            v-model="invoiceGetCommit.form.claimant"
                            value-key="index"
                            placeholder="请选择"
                            @change="handleInvoiceOperatorChange"
                        >
                            <el-option
                                v-for="(
                                    item, index
                                ) in invoiceGetCommit.operatorInfos"
                                :key="index"
                                :label="item.operatorName"
                                :value="item"
                            ></el-option>
                        </el-select>
                    </el-form-item>
                    <el-row :gutter="10">
                        <el-col :span="8">
                            <el-form-item
                                label="经办人证件类型"
                                prop="certType"
                                style="text-align: left"
                            >
                                <el-select
                                    v-model="invoiceGetCommit.form.certType"
                                    value-key="paperType"
                                    placeholder="请选择"
                                >
                                    <el-option
                                        v-for="(
                                            item, index
                                        ) in invoiceGetCommit.papers"
                                        :key="index"
                                        :label="item.paperTypeChn"
                                        :value="item"
                                    ></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="16">
                            <el-form-item
                                label="经办人证件号码"
                                prop="certNo"
                                style="width: 100%"
                            >
                                <el-input
                                    v-model="invoiceGetCommit.form.certNo"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-form-item
                        label="领票方式"
                        prop="getWay"
                        style="text-align: left"
                    >
                        <el-radio-group v-model="invoiceGetCommit.form.getWay">
                            <el-radio
                                v-for="item in invoiceGetCommit.invoiceGetWayOptions"
                                :key="item.value"
                                :label="item.value"
                                :value="item.value"
                                >{{ item.label }}</el-radio
                            >
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item
                        label="收件人"
                        prop="receiver"
                        v-if="isGetway2"
                        style="text-align: left"
                    >
                        <el-input
                            v-model="invoiceGetCommit.form.receiver"
                        ></el-input>
                    </el-form-item>
                    <el-form-item
                        label="快递地址"
                        prop="receiverAddress"
                        v-if="isGetway2"
                        style="width: 100%"
                    >
                        <el-input
                            type="textarea"
                            v-model="invoiceGetCommit.form.receiverAddress"
                        ></el-input>
                    </el-form-item>
                    <el-form-item
                        label="移动电话"
                        prop="mobilePhoneNumber"
                        v-if="isGetway2"
                        style="width: 100%"
                    >
                        <el-input
                            v-model="invoiceGetCommit.form.mobilePhoneNumber"
                        ></el-input>
                    </el-form-item>
                    <el-form-item
                        label="固定电话"
                        prop="fixedPhoneNumber"
                        v-if="isGetway2"
                        style="width: 100%"
                    >
                        <el-input
                            v-model="invoiceGetCommit.form.fixedPhoneNumber"
                        ></el-input>
                    </el-form-item>
                    <el-form-item
                        label="邮政编码"
                        prop="zipCode"
                        v-if="isGetway2"
                        style="width: 100%"
                    >
                        <el-input
                            v-model="invoiceGetCommit.form.zipCode"
                        ></el-input>
                    </el-form-item>
                    <el-form-item
                        label="申领说明"
                        prop="remark"
                        style="width: 100%"
                    >
                        <el-input
                            type="textarea"
                            v-model="invoiceGetCommit.form.remark"
                        ></el-input>
                    </el-form-item>
                </el-form>
                <div style="text-align: center">
                    <el-button @click="invoiceGetCommit.dialogVisible = false"
                        >取 消</el-button
                    >
                    <el-button type="primary" @click="handleInvoiceGetCommitOk"
                        >发票申领</el-button
                    >
                </div>
            </div>
        </el-dialog>

        <el-dialog
            title="发票信息"
            :visible.sync="invoicePurchaseDetails.dialogVisible"
            width="45%"
            center
        >
            <el-table
                :data="invoicePurchaseDetails.data"
                border
                stripe
                :height="300"
                style="width: 100%"
            >
                <el-table-column
                    type="index"
                    :index="pageIndex"
                    align="center"
                    label="序号"
                    width="50"
                ></el-table-column>
                <el-table-column
                    prop="invoiceCode"
                    label="发票代码"
                    align="center"
                ></el-table-column>
                <el-table-column
                    prop="invoiceBeginNo"
                    label="发票起始号码"
                    align="center"
                ></el-table-column>
                <el-table-column
                    prop="invoiceEndNo"
                    label="发票终止号码"
                    align="center"
                ></el-table-column>
                <el-table-column
                    prop="invoiceNums"
                    label="发票份数"
                    align="center"
                ></el-table-column>
            </el-table>
        </el-dialog>

        <el-dialog
            title="经办人信息"
            :visible.sync="invoicePurchaseOperator.dialogVisible"
            width="40%"
            center
        >
            <el-form label-width="170px">
                <el-form-item label="发票申领人：" style="width: 100%">
                    <el-input
                        v-model="invoicePurchaseOperator.operatorName"
                        disabled
                    ></el-input>
                </el-form-item>
                <el-form-item label="发票申领人证件类型：" style="width: 100%">
                    <el-input
                        v-model="invoicePurchaseOperator.paperTypeChn"
                        disabled
                    ></el-input>
                </el-form-item>
                <el-form-item label="发票申领人证件号码：" style="width: 100%">
                    <el-input
                        v-model="invoicePurchaseOperator.paperNumber"
                        disabled
                    ></el-input>
                </el-form-item>
                <el-form-item
                    label="领票方式"
                    prop="getWay"
                    style="text-align: left"
                >
                    <el-radio-group
                        v-model="invoicePurchaseOperator.getWay"
                        disabled
                    >
                        <el-radio
                            v-for="item in invoiceGetCommit.invoiceGetWayOptions"
                            :key="item.value"
                            :label="item.value"
                            :value="item.value"
                            >{{ item.label }}</el-radio
                        >
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="收件人：" style="width: 100%">
                    <el-input
                        v-model="invoicePurchaseOperator.receiverName"
                        disabled
                    ></el-input>
                </el-form-item>
                <el-form-item label="移动电话：" style="width: 100%">
                    <el-input
                        v-model="invoicePurchaseOperator.mobilePhoneNumber"
                        disabled
                    ></el-input>
                </el-form-item>
                <el-form-item label="固定电话：" style="width: 100%">
                    <el-input
                        v-model="invoicePurchaseOperator.fixedPhoneNumber"
                        disabled
                    ></el-input>
                </el-form-item>
                <el-form-item label="快递地址：" style="width: 100%">
                    <el-input
                        v-model="invoicePurchaseOperator.receiverAddress"
                        disabled
                    ></el-input>
                </el-form-item>
                <el-form-item label="邮政编码：" style="width: 100%">
                    <el-input
                        v-model="invoicePurchaseOperator.zipCode"
                        disabled
                    ></el-input>
                </el-form-item>
                <el-form-item label="申领说明" style="width: 100%">
                    <el-input
                        type="textarea"
                        v-model="invoicePurchaseOperator.remark"
                        disabled
                    ></el-input>
                </el-form-item>
            </el-form>
        </el-dialog>
    </div>
</template>

<script>
import {
    GetInvoicePurchaseList,
    GetPrepareInfo,
    CommitInvoicePurchase,
    RevokeInvoicePurchase,
    ConfirmInvoicePurchase,
    GetInvoicePurchaseRecord,
} from "@/api/invoice_purchase";

import DateHelper from "@/utils/date";
import SEND from "@/api/send";

// 发票类型列表
const invoiceCategoryOptions = [
    {
        value: 1,
        label: "增值税普通电子发票",
    },
    {
        value: 2,
        label: '增值税电子专用发票',
    },
    {
        value: 3,
        label: "增值税普通纸质发票",
    },
    {
        value: 4,
        label: "增值税专用纸质发票",
    },
];

// 发票申领状态选项列表
const invoiceGetStatusOptions = [
    {
        value: 0,
        label: "申领中",
    },
    {
        value: 1,
        label: "申领成功",
    },
    {
        value: 2,
        label: "申领失败",
    },
];

// 发票申领领购方式
const invoiceGetWayOptions = [
    {
        value: 1,
        label: "自行领取",
    },
    {
        value: 2,
        label: "快递配送",
    },
];

export default {
    name: "invoice_get",
    data() {
        return {
            loading: false,
            invoiceCategoryOptions: invoiceCategoryOptions,
            invoiceGetStatusOptions: invoiceGetStatusOptions,
            search: {
                invoiceCategory: null,
                invoiceGetStatus: 0,
                beginDate: DateHelper.LocalCurrentMonthStart(),
                endDate: DateHelper.LocalCurrentMonthEnd(),
                resultList: [],
                resultPagination: {
                    currentPage: 1,
                    pageSizes: [10, 30, 50, 100],
                    pageSize: 10,
                    total: 0,
                },
            },
            invoiceGetCommit: {
                dialogVisible: false,
                loading: false,
                invoiceGetWayOptions: invoiceGetWayOptions,
                // 票种信息列表
                // item 结构定义：
                // invoiceCateCode: "026"
                // invoiceCateName: "增值税电子发票"
                // kindCodes:[]
                // kindCodes item 如下：
                // invoiceKindCode: "11"
                // invoiceKindName: "增值税电子普通发票"
                pzhdInfos: [],
                invoiceKindCodes: [], // 根据票种信息动态生成列表
                operatorInfos: [], // 经办人列表
                papers: [], // 证件类型列表
                // receiverInfos: [], // 收件人信息
                form: {
                    invoiceCategory: "",
                    invoiceKind: "",
                    number: 1,
                    claimant: "",
                    certType: "",
                    certNo: "",
                    getWay: "",
                    remark: "",
                    receiver: "",
                    receiverAddress: "",
                    mobilePhoneNumber: "",
                    fixedPhoneNumber: "",
                    zipCode: "",
                },
                rules: {
                    invoiceCategory: [
                        {
                            required: true,
                            message: "请选择发票类型",
                            trigger: "change",
                        },
                    ],
                    invoiceKind: [
                        {
                            required: true,
                            message: "请选择发票种类",
                            trigger: "change",
                        },
                    ],
                    number: [
                        {
                            required: true,
                            message: "请输入申领份数",
                            trigger: "change",
                        },
                    ],
                    claimant: [
                        {
                            required: true,
                            message: "请选择经办人名称",
                            trigger: "change",
                        },
                    ],
                    certType: [
                        {
                            required: true,
                            message: "请选择经办人证件类型",
                            trigger: "change",
                        },
                    ],
                    certNo: [
                        {
                            required: true,
                            message: "请输入经办人证件号码",
                            trigger: "change",
                        },
                    ],
                    getWay: [
                        {
                            required: true,
                            message: "请选择领票方式",
                            trigger: "change",
                        },
                    ],
                    remark: [
                        {
                            required: true,
                            message: "请输入申领说明",
                            trigger: "change",
                        },
                    ],
                    receiver: [
                        {
                            required: true,
                            message: "请选择收件人",
                            trigger: "change",
                        },
                    ],
                    receiverAddress: [
                        {
                            required: true,
                            message: "请输入快递地址",
                            trigger: "change",
                        },
                    ],
                    mobilePhoneNumber: [
                        {
                            required: true,
                            message: "请输入移动电话",
                            trigger: "change",
                        },
                    ],
                    fixedPhoneNumber: [
                        {
                            required: true,
                            message: "请输入固定电话",
                            trigger: "change",
                        },
                        {
                            min: 1,
                            max: 15,
                            message: "固定电话长度最多为15个字符",
                            trigger: "change",
                        },
                    ],
                    zipCode: [
                        {
                            required: true,
                            message: "请输入邮编",
                            trigger: "change",
                        },
                    ],
                },
            },
            invoicePurchaseDetails: {
                dialogVisible: false,
                data: [],
            },
            invoicePurchaseOperator: {
                dialogVisible: false,
                operatorName: "",
                paperTypeChn: "",
                paperNumber: "",
                getWay: "",
                receiverName: "",
                receiverAddress: "",
                mobilePhoneNumber: "",
                fixedPhoneNumber: "",
                zipCode: "",
                remark: "",
            },
            tableHeight: "",
        };
    },
    created() {
        this.tableHeight = window.innerHeight - 250;
        this.callGetInvoicePurchaseList(
            this.search.resultPagination.currentPage,
            this.search.resultPagination.pageSize
        );
    },
    mounted() {
        this.tableHeight = window.innerHeight - 250;
        window.onresize = () => {
            return (() => {
                this.tableHeight = window.innerHeight - 250;
            })();
        };
    },
    computed: {
        // 是否是快递配送
        isGetway2() {
            return this.invoiceGetCommit.form.getWay == 2;
        },
    },
    methods: {
        pageIndex(index) {
            let currentPage = this.search.resultPagination.currentPage; //当前页码，具体看组件取值
            let pageSize = this.search.resultPagination.pageSize; //每页条数，具体是组件取值
            return index + 1 + (currentPage - 1) * pageSize;
        },
        fmtInvoiceGetStatus(row, column, cellValue) {
            let result = cellValue;
            for (const item of invoiceGetStatusOptions) {
                if (item.value == cellValue) {
                    result = item.label;
                    break;
                }
            }
            return result;
        },
        fmtHandleInformation(row, column, cellValue) {
            return "税局返回信息：" + cellValue;
        },
        fmtGetWay(row, column, cellValue) {
            let result = cellValue;
            for (const item of invoiceGetWayOptions) {
                if (item.value === cellValue) {
                    result = item.label;
                    break;
                }
            }
            return result;
        },
        handleSearchResultSizeChange(val) {
            this.callGetInvoicePurchaseList(1, val);
        },
        handleSearchResultCurrentChange(val) {
            this.callGetInvoicePurchaseList(
                val,
                this.search.resultPagination.pageSize
            );
        },
        handleSearch() {
            this.callGetInvoicePurchaseList(
                1,
                this.search.resultPagination.pageSize
            );
        },
        handleResetSearch() {
            this.search.invoiceCategory = null;
            this.search.invoiceGetStatus = 0;
            this.search.beginDate = null;
            this.search.endDate = null;
        },
        /**
         * 显示发票申领弹窗
         */
        handleShowInvoiceGetCommitDialog() {
            this.invoiceGetCommit.dialogVisible = true;
        },
        handleInvoiceGetCommitOpenedDialog() {
            // 重置
            this.invoiceGetCommit.pzhdInfos = [];
            this.invoiceGetCommit.operatorInfos = [];
            this.invoiceGetCommit.papers = [];
            // this.invoiceGetCommit.receiverInfos = [];
            this.invoiceGetCommit.form.invoiceCategory = "";
            this.invoiceGetCommit.form.invoiceKind = "";
            this.invoiceGetCommit.form.number = 1;
            this.invoiceGetCommit.form.claimant = "";
            this.invoiceGetCommit.form.certType = "";
            this.invoiceGetCommit.form.certNo = "";
            this.invoiceGetCommit.form.getWay = "";
            this.invoiceGetCommit.form.remark = "";
            this.invoiceGetCommit.form.receiver = "";
            this.invoiceGetCommit.form.receiverAddress = "";
            this.invoiceGetCommit.form.mobilePhoneNumber = "";
            this.invoiceGetCommit.form.fixedPhoneNumber = "";
            this.invoiceGetCommit.form.zipCode = "";

            // 加载开票需要的准备信息；
            this.invoiceGetCommit.loading = true;
            let _this = this;
            GetPrepareInfo()
                .then((val) => {
                    _this.invoiceGetCommit.pzhdInfos = _this.convertPZHDInfos(
                        val.data.pzhdInfos
                    );
                    _this.invoiceGetCommit.operatorInfos =
                        val.data.operatorInfos;
                    _this.invoiceGetCommit.papers = val.data.papers;
                    // _this.invoiceGetCommit.receiverInfos = val.data.receiverInfos;
                    _this.invoiceGetCommit.loading = false;

                    // 获取发票邮寄地址信息
                    _this.invoiceGetCommit.loading = true;
                    SEND.getSendInfo()
                        .then((val) => {
                            _this.invoiceGetCommit.loading = false;
                            // _this.sendId = val.data.sendId;
                            _this.invoiceGetCommit.form.getWay = val.data.getWay;
                            _this.invoiceGetCommit.form.receiver = val.data.receiverName;
                            _this.invoiceGetCommit.form.receiverAddress =
                                val.data.receiverAddress;
                            _this.invoiceGetCommit.form.mobilePhoneNumber =
                                val.data.mobilePhoneNumber;
                            _this.invoiceGetCommit.form.zipCode = val.data.zipCode;
                            _this.invoiceGetCommit.form.fixedPhoneNumber =
                                val.data.fixedPhoneNumber;
                        })
                        .catch((error) => {
                            _this.invoiceGetCommit.loading = false;
                        });
                })
                .catch((error) => {
                    _this.invoiceGetCommit.loading = false;
                    this.$message.error(error);
                });
        },
        handleInvoiceGetCommitCloseDialog() {
            // 取消验证提示
            this.$refs["invoiceGetCommitForm"].resetFields();
        },
        // 发票类型修改；
        handleInvoiceCategoryChange() {
            this.invoiceGetCommit.form.invoiceKind = null;
            if (this.invoiceGetCommit.form.invoiceCategory) {
                this.invoiceGetCommit.invoiceKindCodes = [
                    ...this.invoiceGetCommit.form.invoiceCategory.kindCodes,
                ];
                this.invoiceGetCommit.form.invoiceKind =
                    this.invoiceGetCommit.invoiceKindCodes.length > 0
                        ? this.invoiceGetCommit.invoiceKindCodes[0]
                        : null;
            }
        },
        // 经办人修改；
        handleInvoiceOperatorChange() {
            this.invoiceGetCommit.form.certNo = "";
            this.invoiceGetCommit.form.certType = "";
            if (this.invoiceGetCommit.form.claimant) {
                this.invoiceGetCommit.form.certNo = this.invoiceGetCommit.form.claimant.paperNumber;
                this.invoiceGetCommit.form.certType = this.getSelectCertType(
                    this.invoiceGetCommit.form.claimant.paperType
                );
            }
        },
        // 收件人修改；
        // handleReceiverNameChange() {
        //     this.invoiceGetCommit.form.receiverAddress = '';
        //     this.invoiceGetCommit.form.mobilePhoneNumber = '';
        //     this.invoiceGetCommit.form.fixedPhoneNumber = '';
        //     this.invoiceGetCommit.form.zipCode = '';
        //     if (this.invoiceGetCommit.form.receiver) {
        //         this.invoiceGetCommit.form.receiverAddress = this.invoiceGetCommit.form.receiver.defaultAddress;
        //         if (this.invoiceGetCommit.form.receiver.receiverAddress) {
        //             this.invoiceGetCommit.form.receiverAddress = this.invoiceGetCommit.form.receiver.receiverAddress;
        //         }
        //         this.invoiceGetCommit.form.mobilePhoneNumber = this.invoiceGetCommit.form.receiver.mobilePhoneNumber;
        //         this.invoiceGetCommit.form.fixedPhoneNumber = this.invoiceGetCommit.form.receiver.fixedPhoneNumber;
        //         this.invoiceGetCommit.form.zipCode = this.invoiceGetCommit.form.receiver.zipCode;
        //     }
        // },
        /**
         * 发票申领
         */
        handleInvoiceGetCommitOk() {
            let _this = this;
            this.$refs["invoiceGetCommitForm"].validate((valid) => {
                if (valid) {
                    let args = {
                        invoiceKindCode:
                            _this.invoiceGetCommit.form.invoiceKind
                                .invoiceKindCode,
                        invoiceKindName:
                            _this.invoiceGetCommit.form.invoiceKind
                                .invoiceKindName,
                        invoiceCateCode:
                            _this.invoiceGetCommit.form.invoiceCategory
                                .invoiceCateCode,
                        invoiceCateName:
                            _this.invoiceGetCommit.form.invoiceCategory
                                .invoiceCateName,
                        applyNum: _this.invoiceGetCommit.form.number,
                        operatorName:
                            _this.invoiceGetCommit.form.claimant.operatorName,
                        paperType:
                            _this.invoiceGetCommit.form.certType.paperType,
                        paperTypeChn:
                            _this.invoiceGetCommit.form.certType.paperTypeChn,
                        paperNumber: _this.invoiceGetCommit.form.certNo,
                        remark: _this.invoiceGetCommit.form.remark,
                        getWay: _this.invoiceGetCommit.form.getWay,
                    };
                    if (_this.invoiceGetCommit.form.getWay == 2) {
                        args.receiverName =
                            _this.invoiceGetCommit.form.receiver;
                        args.receiverAddress =
                            _this.invoiceGetCommit.form.receiverAddress;
                        args.mobilePhoneNumber =
                            _this.invoiceGetCommit.form.mobilePhoneNumber;
                        args.fixedPhoneNumber =
                            _this.invoiceGetCommit.form.fixedPhoneNumber;
                        args.zipCode = _this.invoiceGetCommit.form.zipCode;
                    }
                    console.log("测试发票申领");
                    console.log(args);
                    CommitInvoicePurchase(args)
                        .then((val) => {
                            _this.callGetInvoicePurchaseList(
                                _this.search.resultPagination.currentPage,
                                _this.search.resultPagination.pageSize
                            );
                            _this.$alert(val.data.statusMemo, "结果信息", {
                                confirmButtonText: "确定",
                                callback: (action) => {
                                    _this.invoiceGetCommit.dialogVisible = false;
                                },
                            });
                            // this.$message.success(val.data.statusMemo);
                        })
                        .catch((err) => {
                            _this.$message.error(err);
                        });
                } else {
                    _this.$message.error(
                        "输入信息不合法，请检查您的输入信息！"
                    );
                    return false;
                }
            });
        },
        disabledConfirmInvoicePurchase(row) {
            // 申领状态为成功并且申领未确认，确认按钮可用；
            return !(row.handleStatus == 1 && row.resultEnterFlag == "N");
        },
        disabledRevokeInvoicePurchase(row) {
            // 申领状态为申领中，撤销按钮可用；
            return !(row.handleStatus == 0);
        },
        disabledShowInvoicePurchaseDetails(row) {
            // 申领状态为成功，发票信息按钮可用；
            return !(row.handleStatus == 1);
        },
        handleShowInvoicePurchaseDetails(row) {
            this.invoicePurchaseDetails.data = row.details;
            this.invoicePurchaseDetails.dialogVisible = true;
        },
        handleShowInvoicePurchaseOperator(row) {
            // 重置
            this.invoicePurchaseOperator.operatorName = "";
            this.invoicePurchaseOperator.paperTypeChn = "";
            this.invoicePurchaseOperator.paperNumber = "";
            this.invoicePurchaseOperator.getWay = "";
            this.invoicePurchaseOperator.receiverName = "";
            this.invoicePurchaseOperator.receiverAddress = "";
            this.invoicePurchaseOperator.mobilePhoneNumber = "";
            this.invoicePurchaseOperator.fixedPhoneNumber = "";
            this.invoicePurchaseOperator.zipCode = "";
            this.invoicePurchaseOperator.remark = "";

            this.loading = true;
            let _this = this;
            GetInvoicePurchaseRecord(row.applySerialNumber)
                .then((val) => {
                    if (val.data) {
                        _this.invoicePurchaseOperator.operatorName =
                            val.data.operatorName;
                        _this.invoicePurchaseOperator.paperTypeChn =
                            val.data.paperTypeChn;
                        _this.invoicePurchaseOperator.paperNumber =
                            val.data.paperNumber;
                        _this.invoicePurchaseOperator.getWay = val.data.getWay;
                        _this.invoicePurchaseOperator.receiverName =
                            val.data.receiverName;
                        _this.invoicePurchaseOperator.receiverAddress =
                            val.data.receiverAddress;
                        _this.invoicePurchaseOperator.mobilePhoneNumber =
                            val.data.mobilePhoneNumber;
                        _this.invoicePurchaseOperator.fixedPhoneNumber =
                            val.data.fixedPhoneNumber;
                        _this.invoicePurchaseOperator.zipCode =
                            val.data.zipCode;
                        _this.invoicePurchaseOperator.remark = val.data.remark;

                        _this.invoicePurchaseOperator.dialogVisible = true;
                    } else {
                        _this.$message.error("无经办人等信息！");
                    }
                    _this.loading = false;
                })
                .catch((err) => {
                    _this.invoicePurchaseOperator.dialogVisible = false;
                    _this.loading = false;
                    _this.$message.error(err);
                });
        },
        handleConfirmInvoicePurchase(row) {
            this.$confirm(
                row.invoiceCateName + " 网上申领确认，是否继续！",
                "提示",
                {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                }
            )
                .then(() => {
                    ConfirmInvoicePurchase({
                        invoiceCateCode: row.invoiceCateCode,
                        invoiceCateName: row.invoiceCateName,
                        applySerialNumber: row.applySerialNumber,
                        details: row.details,
                    })
                        .then((val) => {
                            this.$message.success(val.msg);
                            this.callGetInvoicePurchaseList(
                                this.search.resultPagination.currentPage,
                                this.search.resultPagination.pageSize
                            );
                        })
                        .catch((err) => {
                            this.$message.error(err);
                        });
                })
                .catch(() => {});
        },
        handleRevokeInvoicePurchase(row) {
            this.$confirm(
                row.invoiceCateName + " 网上申领撤销，是否继续！",
                "提示",
                {
                    confirmButtonText: "确定",
                    cancelButtonText: "取消",
                    type: "warning",
                }
            )
                .then(() => {
                    RevokeInvoicePurchase({
                        invoiceCateCode: row.invoiceCateCode,
                        invoiceCateName: row.invoiceCateName,
                        applySerialNumber: row.applySerialNumber,
                    })
                        .then((val) => {
                            this.$message.success(val.msg);
                            this.callGetInvoicePurchaseList(
                                this.search.resultPagination.currentPage,
                                this.search.resultPagination.pageSize
                            );
                        })
                        .catch((err) => {
                            this.$message.error(err);
                        });
                })
                .catch(() => {});
        },
        /**
         * 发票申领记录分页查询
         * @param {Number} pageNum : 当前页码
         * @param {Number} pageSize : 每页数量
         */
        callGetInvoicePurchaseList(pageNum, pageSize) {
            let beginTime = this.search.beginDate;
            let endTime = this.search.endDate;
            if (beginTime) {
                beginTime = this.dateFormat(new Date(beginTime), "yyyy-MM-dd");
            }
            if (endTime) {
                endTime = this.dateFormat(new Date(endTime), "yyyy-MM-dd");
            }
            if (beginTime && endTime) {
                if (DateHelper.ValidateDateRange(beginTime, endTime) == false) {
                    this.$message.error(
                        "起始日期与结束日期不合法，请确定后重试！"
                    );
                    return false;
                }
            } else {
                this.$message.error(
                    "起始日期与结束日期为必输项，请确定后重试！"
                );
                return false;
            }

            let _this = this;
            // 有发票种类使用的查询方法
            let funcHaveInvoiceCategory = function () {
                _this.loading = true;
                GetInvoicePurchaseList({
                    handleStatus: _this.search.invoiceGetStatus,
                    invoiceCategory: _this.search.invoiceCategory,
                    applyDateBegin: beginTime,
                    applyDateEnd: endTime,
                    pageNum: pageNum,
                    pageSize: pageSize,
                })
                    .then((val) => {
                        if (
                            val.data.hasNext &&
                            pageNum >= _this.search.resultPagination.currentPage
                        ) {
                            _this.search.resultPagination.total =
                                (pageNum + 1) * pageSize;
                        }
                        _this.search.resultPagination.pageSize = pageSize;
                        _this.search.resultPagination.currentPage = pageNum;
                        _this.search.resultList = val.data.statusInfoVOs;

                        let newMsg = val.data.statusMemo;
                        if (newMsg) {
                            _this.$alert(newMsg, "结果信息", {
                                confirmButtonText: "确定",
                            });
                        }
                        _this.loading = false;
                    })
                    .catch((err) => {
                        _this.loading = false;
                        _this.$message.error(err);
                    });
            };

            // 没有有发票种类使用的查询方法
            let funcNoInvoiceCategory = function () {
                _this.loading = true;
                let resultListTemp = new Array(); // 数据
                let newMsgTextTemp = ""; // 提示文本，用换行符区分每条消息；

                // 消息累计；
                let funcAddMsg = function (msg) {
                    if (msg) {
                        if (newMsgTextTemp) {
                            newMsgTextTemp += msg;
                        } else {
                            newMsgTextTemp = msg;
                        }
                    }
                };

                // 1 增值税普通电子发票
                GetInvoicePurchaseList({
                    handleStatus: _this.search.invoiceGetStatus,
                    invoiceCategory: 1,
                    applyDateBegin: beginTime,
                    applyDateEnd: endTime,
                    pageNum: pageNum,
                    pageSize: pageSize,
                })
                    .then((val) => {
                        // 记录消息；
                        funcAddMsg(val.data.statusMemo);
                        // 3 增值税普通纸质发票
                        return GetInvoicePurchaseList({
                            handleStatus: _this.search.invoiceGetStatus,
                            invoiceCategory: 3,
                            applyDateBegin: beginTime,
                            applyDateEnd: endTime,
                            pageNum: pageNum,
                            pageSize: pageSize,
                        });
                    })
                    .then((val) => {
                        // 记录消息；
                        funcAddMsg(val.data.statusMemo);
                        // 4 增值税专用纸质发票
                        return GetInvoicePurchaseList({
                            handleStatus: _this.search.invoiceGetStatus,
                            invoiceCategory: 4,
                            applyDateBegin: beginTime,
                            applyDateEnd: endTime,
                            pageNum: pageNum,
                            pageSize: pageSize,
                        });
                    })
                    .then((val) => {
                        // 记录消息；
                        funcAddMsg(val.data.statusMemo);
                        // 全部执行成功
                        _this.search.resultPagination.pageSize = pageSize;
                        _this.search.resultPagination.currentPage = pageNum;
                        _this.search.resultList = resultListTemp;

                        // 显示提示信息
                        if (newMsgTextTemp) {
                            _this.$alert(newMsgTextTemp, "结果信息", {
                                confirmButtonText: "确定",
                            });
                        }

                        _this.loading = false;
                    })
                    .catch((err) => {
                        _this.loading = false;
                        _this.$message.error(err);
                    });
            };

            if (
                this.search.invoiceCategory == null ||
                this.search.invoiceCategory == ""
            ) {
                funcNoInvoiceCategory();
            } else {
                funcHaveInvoiceCategory();
            }
        },
        // 获取选中的身份证类型信息
        getSelectCertType(type) {
            let result = null;
            let papers = this.invoiceGetCommit.papers;
            if (papers) {
                for (const item of papers) {
                    if (item.paperType == type) {
                        result = item;
                        break;
                    }
                }
            }
            return result;
        },
        convertPZHDInfos(list) {
            // 票种信息列表 item
            // invoiceCateCode: "026"
            // invoiceCateName: "增值税电子发票"
            // invoiceKindCode: "11"
            // invoiceKindName: "增值税电子普通发票"
            // 转换为：
            // invoiceCateCode: "026"
            // invoiceCateName: "增值税电子发票"
            // kindCodes:[
            //   {
            //      invoiceKindCode: "11"
            //      invoiceKindName: "增值税电子普通发票"
            //   }
            // ]
            let result = [];
            if (list) {
                let tempMap = new Map();
                for (const item of list) {
                    if (tempMap.has(item.invoiceCateCode)) {
                        let mapItem = tempMap.get(item.invoiceCateCode);
                        mapItem.kindCodes.push({
                            invoiceKindCode: item.invoiceKindCode,
                            invoiceKindName: item.invoiceKindName,
                        });
                        tempMap.set(item.invoiceCateCode, mapItem);
                    } else {
                        let mapItem = {
                            invoiceCateCode: item.invoiceCateCode,
                            invoiceCateName: item.invoiceCateName,
                        };
                        mapItem.kindCodes = [
                            {
                                invoiceKindCode: item.invoiceKindCode,
                                invoiceKindName: item.invoiceKindName,
                            },
                        ];
                        tempMap.set(item.invoiceCateCode, mapItem);
                    }
                }
                tempMap.forEach(function (key) {
                    result.push(key);
                });
            }
            return result;
        },
        dateFormat(date, fmt) {
            // 小时只支持24小时制
            var o = {
                "M+": date.getMonth() + 1, // 月份
                "d+": date.getDate(), // 日
                "H+": date.getHours(), // 24小时,
                "m+": date.getMinutes(), // 分
                "s+": date.getSeconds(), // 秒
                "q+": Math.floor((date.getMonth() + 3) / 3), // 季度
                S: date.getMilliseconds(), // 毫秒
            };
            if (/(y+)/.test(fmt))
                fmt = fmt.replace(
                    RegExp.$1,
                    (date.getFullYear() + "").substr(4 - RegExp.$1.length)
                );
            for (var k in o)
                if (new RegExp("(" + k + ")").test(fmt))
                    fmt = fmt.replace(
                        RegExp.$1,
                        RegExp.$1.length == 1
                            ? o[k]
                            : ("00" + o[k]).substr(("" + o[k]).length)
                    );
            return fmt;
        },
    },
};
</script>

<style lang="stylus" scoped>
.express-address-panel {
    background: #f7f9fa;
    // padding-left: 20px;
    // padding-top: 20px;
    padding: 20px;
}
</style>
