<template>
    <div v-loading="loading" element-loading-text="拼命加载中">
        <el-row :gutter="20">
            <el-col :span="8" :offset="8">
                <el-form
                    :model="scaninfoForm"
                    :rules="scaninfoFormRules"
                    ref="scaninfoFormRef"
                    label-position="top"
                >
                    <el-row :gutter="20">
                        <el-col :span="8">
                            <el-form-item
                                label="税控服务器类型："
                                prop="serverType"
                            >
                                <el-select
                                    v-model="scaninfoForm.serverType"
                                    placeholder="请选择"
                                    style="width: 100%"
                                >
                                    <el-option
                                        v-for="item in serverTypeOptions"
                                        :key="item.value"
                                        :label="item.label"
                                        :value="item.value"
                                    >
                                    </el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                        <el-col :span="16">
                            <el-form-item
                                label="税控服务器 IP ："
                                prop="hostIP"
                            >
                                <el-input
                                    v-model="scaninfoForm.hostIP"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-form-item label="发票上传地址：" prop="appUploadURL">
                        <el-input
                            v-model="scaninfoForm.appUploadURL"
                        ></el-input>
                    </el-form-item>
                    <el-row :gutter="20">
                        <el-col :span="16">
                            <el-form-item
                                label="受理 IP 地址："
                                prop="acceptanceIP"
                            >
                                <el-input
                                    v-model="scaninfoForm.acceptanceIP"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="8">
                            <el-form-item
                                label="受理端口："
                                prop="acceptancePort"
                            >
                                <el-input
                                    v-model="scaninfoForm.acceptancePort"
                                ></el-input>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-form-item style="text-align: center">
                        <el-button
                            type="primary"
                            @click="handleScan('scaninfoFormRef')"
                        >
                            扫 描
                        </el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
        <div>
            <el-row :gutter="10">
                <el-col :span="6">
                    <el-input
                        v-model="search.entName"
                        clearable
                        placeholder="请输入企业名称进行搜索"
                        prefix-icon="el-icon-search"
                        class="ent_input"
                    ></el-input>
                </el-col>
                <!-- <el-col :span="18">
                    <el-button
                        type="primary"
                        icon="el-icon-search"
                        @click="handleSearch"
                    >
                        搜索
                    </el-button>
                    <el-button @click="handleResetSearch">重置</el-button>
                </el-col> -->
            </el-row>
            <br />
            <el-table
                :data="filterSearchResultList"
                border
                stripe
                style="width: 100%"
            >
                <el-table-column
                    type="index"
                    :index="pageIndex"
                    align="center"
                    label="序号"
                    width="50"
                ></el-table-column>
                <el-table-column
                    prop="tax_payer_Name"
                    label="企业名称"
                    align="center"
                    show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                    prop="tax_no"
                    label="企业税号"
                    align="center"
                    show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                    prop="device_no"
                    label="税控设备编号"
                    align="center"
                    show-overflow-tooltip
                ></el-table-column>
                <!-- <el-table-column
                    prop="device_name"
                    label="服务器名称"
                    align="center"
                    show-overflow-tooltip
                ></el-table-column> -->
                <el-table-column
                    prop="server_ip"
                    label="税控服务器地址"
                    align="center"
                    show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                    prop="server_type_name"
                    label="服务器类型"
                    align="center"
                    show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                    prop="acceptance_ip"
                    label="统一受理IP地址"
                    align="center"
                    show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                    prop="acceptance_port"
                    label="统一受理端口号"
                    align="center"
                    show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                    prop="province"
                    label="省份"
                    align="center"
                    show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                    prop="oil_limit_num"
                    label="成品油限购数量(升)"
                    align="center"
                    show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                    prop="device_status"
                    label="主备状态"
                    align="center"
                    show-overflow-tooltip
                ></el-table-column>
                <!-- <el-table-column
                    prop="cert_pass"
                    label="证书口令"
                    align="center"
                    show-overflow-tooltip
                ></el-table-column> -->
                <el-table-column
                    prop="app_upload_url"
                    label="发票上传地址"
                    align="center"
                    show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                    prop="ent_type_name"
                    label="企业类型"
                    align="center"
                    show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                    prop="available_taxes"
                    label="企业税率"
                    align="center"
                    show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                    prop="tax_office_code"
                    label="税局代码"
                    align="center"
                    show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                    prop="tax_office_name"
                    label="税局名称"
                    align="center"
                    show-overflow-tooltip
                ></el-table-column>
                <el-table-column
                    label="操作"
                    width="100"
                    align="center"
                    fixed="right"
                >
                    <template slot-scope="scope">
                        <el-button
                            @click="handleEntInitDialogShow(scope.row)"
                            type="primary"
                            v-if="scope.row.init === false"
                        >
                            上 架
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!-- <br />
            <el-row type="flex" :gutter="20">
                <el-col :span="12"></el-col>
                <el-col :span="12">
                    <div style="text-align: right">
                        <el-pagination
                            layout="total, sizes, 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-col>
            </el-row> -->
        </div>

        <el-dialog
            title="上架信息确认"
            :visible.sync="entInitDialog.visible"
            width="40%"
            top="4vh"
            append-to-body
            @closed="handleEntInitDialogClosed"
        >
            <div
                v-loading="entInitDialog.loading"
                element-loading-text="拼命加载中"
            >
                <el-card>
                    <p>企业名称：{{ entInitDialog.rowData.tax_payer_Name }}</p>
                    <p>企业税号：{{ entInitDialog.rowData.tax_no }}</p>
                    <p>企业类型：{{ entInitDialog.rowData.ent_type_name }}</p>
                    <p>税控设备编号：{{ entInitDialog.rowData.device_no }}</p>
                </el-card>
                <br />
                <el-form
                    :model="entInitDialog.form"
                    :rules="entInitDialog.formRules"
                    ref="entInitDialogFormRef"
                    label-position="top"
                    size="mini"
                >
                    <el-row :gutter="20">
                        <el-col :span="8">
                            <el-form-item
                                label="企业简称："
                                prop="entShortName"
                            >
                                <el-input
                                    type="text"
                                    v-model="entInitDialog.form.entShortName"
                                    placeholder=""
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="16">
                            <el-form-item
                                label="企业所属省市："
                                prop="provinceCity"
                            >
                                <el-cascader
                                    v-model="entInitDialog.form.provinceCity"
                                    :options="entInitDialog.chinaProvinceCity"
                                    style="width: 100%"
                                ></el-cascader>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row :gutter="20">
                        <el-col :span="12">
                            <el-form-item
                                label="企业联系人："
                                prop="entContactPerson"
                            >
                                <el-input
                                    type="text"
                                    v-model="
                                        entInitDialog.form.entContactPerson
                                    "
                                    placeholder=""
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item
                                label="企业联系电话："
                                prop="entPhone"
                            >
                                <el-input
                                    type="text"
                                    v-model="entInitDialog.form.entPhone"
                                    placeholder=""
                                />
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row :gutter="20">
                        <el-col :span="12">
                            <el-form-item
                                label="企业管理者："
                                prop="entReserveMrName"
                            >
                                <el-input
                                    type="text"
                                    v-model="
                                        entInitDialog.form.entReserveMrName
                                    "
                                    placeholder=""
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item
                                label="企业管理者联系电话："
                                prop="entReserveMrMobile"
                            >
                                <el-input
                                    type="text"
                                    v-model="
                                        entInitDialog.form.entReserveMrMobile
                                    "
                                    placeholder=""
                                />
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row :gutter="20">
                        <el-col :span="12">
                            <el-form-item
                                label="企业开户银行"
                                prop="entBankName"
                            >
                                <el-input
                                    type="text"
                                    v-model="entInitDialog.form.entBankName"
                                    placeholder=""
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item
                                label="企业开户银行账号"
                                prop="entBankAccountNumber"
                            >
                                <el-input
                                    type="text"
                                    v-model="
                                        entInitDialog.form.entBankAccountNumber
                                    "
                                    placeholder=""
                                />
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-form-item
                        label="开票使用企业地址（不含企业联系电话）："
                        prop="entInvoiceAddress"
                    >
                        <el-input
                            type="text"
                            v-model="entInitDialog.form.entInvoiceAddress"
                            placeholder=""
                        />
                    </el-form-item>
                    <el-row :gutter="20">
                        <el-col :span="12">
                            <el-form-item label="收款人员" prop="entPayees">
                                <el-input
                                    type="text"
                                    v-model="entInitDialog.form.entPayees"
                                    placeholder=""
                                />
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="审核人员" prop="entReviewers">
                                <el-input
                                    type="text"
                                    v-model="entInitDialog.form.entReviewers"
                                    placeholder=""
                                />
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
            </div>

            <span slot="footer">
                <el-button @click="entInitDialog.visible = false"
                    >取 消</el-button
                >
                <el-button
                    type="primary"
                    @click="handleEntInit('entInitDialogFormRef')"
                    >确认并上架</el-button
                >
            </span>
        </el-dialog>
        <el-dialog
            title="上架企业选择商品"
            :visible.sync="selectedCommodityDialog.visible"
            width="50%"
            top="4vh"
            append-to-body
        >
            <div
                v-loading="selectedCommodityDialog.loading"
                element-loading-text="拼命加载中"
            >
                <el-card>
                    <p>
                        企业名称：{{
                            selectedCommodityDialog.entInfo.tax_payer_Name
                        }}
                    </p>
                    <p>
                        企业税号：{{ selectedCommodityDialog.entInfo.tax_no }}
                    </p>
                    <p>
                        企业类型：{{
                            selectedCommodityDialog.entInfo.ent_type_name
                        }}
                    </p>
                    <p>
                        税控设备编号：{{
                            selectedCommodityDialog.entInfo.device_no
                        }}
                    </p>
                </el-card>
                <br />
                <el-form :inline="true">
                    <el-form-item>
                        <el-select
                            v-model="selectedCommodityDialog.search.type"
                            placeholder="请选择商品类型"
                            disabled
                        >
                            <el-option
                                v-for="item in selectedCommodityDialog.commodityOptions"
                                :key="item.value"
                                :label="item.label"
                                :value="item.value"
                            >
                            </el-option>
                        </el-select>
                    </el-form-item>
                    <el-form-item>
                        <el-input
                            v-model="selectedCommodityDialog.search.name"
                            placeholder="请输入商品名称"
                        ></el-input>
                    </el-form-item>

                    <el-form-item>
                        <el-button
                            type="primary"
                            icon="el-icon-search"
                            @click="handleSearchCommodity"
                            >搜索</el-button
                        >
                    </el-form-item>
                </el-form>
                <el-table
                    :data="selectedCommodityDialog.search.resultList"
                    border
                    stripe
                    style="width: 100%"
                    height="300px"
                >
                    <el-table-column
                        type="index"
                        :index="pageIndex"
                        align="center"
                        label="序号"
                        width="50"
                    ></el-table-column>
                    <el-table-column
                        prop="productName"
                        label="商品名称"
                        show-overflow-tooltip
                    ></el-table-column>
                    <el-table-column
                        prop="type"
                        label="商品类型"
                        width="140"
                        align="center"
                        :formatter="fmtCommodityType"
                    ></el-table-column>
                    <el-table-column
                        prop="oriPrice"
                        label="产品原价（元）"
                        width="140"
                        align="center"
                        show-overflow-tooltip
                    ></el-table-column>
                    <el-table-column
                        prop="favouredPrice"
                        label="折扣后价格（元）"
                        width="140"
                        align="center"
                        show-overflow-tooltip
                    ></el-table-column>
                    <el-table-column
                        prop="createTime"
                        label="创建时间"
                        width="240"
                        align="center"
                        show-overflow-tooltip
                    ></el-table-column>
                    <el-table-column label="是否启用" width="80" align="center">
                        <template slot-scope="scope">
                            <el-switch disabled v-model="scope.row.enable">
                            </el-switch>
                        </template>
                    </el-table-column>
                    <el-table-column
                        label="操作"
                        width="160"
                        align="center"
                        fixed="right"
                    >
                        <template slot-scope="scope">
                            <el-button
                                type="primary"
                                @click="handleSubmitSelectCommodity(scope.row)"
                                >选择此商品</el-button
                            >
                        </template>
                    </el-table-column>
                </el-table>
                <br />
                <div style="text-align: center">
                    <el-pagination
                        layout="total, sizes, prev, pager, next"
                        :current-page="
                            selectedCommodityDialog.search.resultPagination
                                .currentPage
                        "
                        :page-sizes="
                            selectedCommodityDialog.search.resultPagination
                                .pageSizes
                        "
                        :page-size="
                            selectedCommodityDialog.search.resultPagination
                                .pageSize
                        "
                        :total="
                            selectedCommodityDialog.search.resultPagination
                                .total
                        "
                        @size-change="handleSearchCommodityResultSizeChange"
                        @current-change="
                            handleSearchCommodityResultCurrentChange
                        "
                    ></el-pagination>
                </div>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import CHINA_PROVINCE_CITY from "@/utils/china_province_city";
import SKServer from "@/api/skserver";
import CommodityService from "@/api/commodity";

// 商品类型
const commodityOptions = [
    {
        value: "base",
        label: "基础主体套餐",
    },
    {
        value: "ext",
        label: "附加增值套餐",
    },
];

// 税控服务器类型选项列表
const serverTypeOptions = [
    {
        value: "1",
        label: "税控盘组",
    },
    {
        value: "3",
        label: "税务UKey",
    },
];

// 企业类型列表
const entTypes = [
    {
        value: "1",
        label: "一般纳税人",
    },
    {
        value: "2",
        label: "小规模纳税人",
    },
    {
        value: "3",
        label: "起征点以下纳税人",
    },
];

export default {
    name: "ScanDeviceServer",
    data() {
        return {
            loading: false,
            serverTypeOptions: serverTypeOptions,
            scaninfoForm: {
                serverType: "",
                hostIP: "",
                appUploadURL: "",
                acceptanceIP: "",
                acceptancePort: "",

                // serverType: "1",
                // hostIP: "192.168.10.151",
                // appUploadURL: "http://192.168.10.11:8093/SKServer/Fpsc",
                // acceptanceIP: "yunnan.chinatax.gov.cn",
                // acceptancePort: "7001",
            },
            scaninfoFormRules: {
                serverType: [
                    {
                        required: true,
                        message: "请选择税控服务器类型",
                        trigger: "change",
                    },
                ],
                hostIP: [
                    {
                        required: true,
                        message: "请输入税控服务器 IP ",
                        trigger: "change",
                    },
                ],
                appUploadURL: [
                    {
                        required: true,
                        message: "请输入应用服务器地址",
                        trigger: "change",
                    },
                ],
                acceptanceIP: [
                    {
                        required: true,
                        message: "请输入受理 IP 地址",
                        trigger: "change",
                    },
                ],
                acceptancePort: [
                    {
                        required: true,
                        message: "请输入受理端口",
                        trigger: "change",
                    },
                ],
            },
            search: {
                entName: "",
                resultList: [],
                resultPagination: {
                    currentPage: 1,
                    pageSizes: [10, 30, 50, 100],
                    pageSize: 10,
                    total: 0,
                },
            },
            entInitDialog: {
                visible: false,
                loading: false,
                entTypes: entTypes,
                chinaProvinceCity: CHINA_PROVINCE_CITY,
                rowData: {}, // 表格种选中的上架企业
                form: {
                    // entType: "", // 企业类型
                    entShortName: "", // 企业简称
                    provinceCity: ["北京市", "北京市"], // 企业所属省市
                    entContactPerson: "", // 企业联系人
                    entPhone: "", // 企业电话
                    entReserveMrName: "", // 企业管理者
                    entReserveMrMobile: "", // 企业管理者手机号码
                    entBankName: "", // 企业开户银行
                    entBankAccountNumber: "", // 企业开户银行账号
                    entInvoiceAddress: "", // 开票使用企业地址
                    // entAvailableTaxes: "", // 企业税率
                    entPayees: "", // 收款人员
                    entReviewers: "", // 审核人员
                },
                formRules: {
                    // entType: [
                    //     {
                    //         required: true,
                    //         message: "请选择企业类型类型",
                    //         trigger: "change",
                    //     },
                    // ],
                    entShortName: [
                        {
                            required: true,
                            message: "请输入企业简称",
                            trigger: "change",
                        },
                    ],
                    provinceCity: [
                        {
                            required: true,
                            message: "请选择企业所属省市",
                            trigger: "change",
                        },
                    ],
                    entPhone: [
                        {
                            required: true,
                            message: "请输入企业联系电话",
                            trigger: "change",
                        },
                    ],
                    entReserveMrName: [
                        {
                            required: true,
                            message: "请输入企业管理者",
                            trigger: "change",
                        },
                    ],
                    entReserveMrMobile: [
                        {
                            required: true,
                            message: "请输入企业管理者联系电话",
                            trigger: "change",
                        },
                    ],
                    entBankName: [
                        {
                            required: true,
                            message: "请输入企业开户银行",
                            trigger: "change",
                        },
                    ],
                    entBankAccountNumber: [
                        {
                            required: true,
                            message: "请输入企业开户银行账号",
                            trigger: "change",
                        },
                    ],
                    entInvoiceAddress: [
                        {
                            required: true,
                            message: "请输入开票使用企业地址",
                            trigger: "change",
                        },
                    ],
                    // entAvailableTaxes: [
                    //     {
                    //         required: true,
                    //         message: "请输入企业税率",
                    //         trigger: "change",
                    //     },
                    // ],
                },
            },
            selectedCommodityDialog: {
                visible: false,
                loading: false,
                entInfo: {}, // 上架企业信息
                commodityOptions: commodityOptions,
                search: {
                    type: "base", // 商品类型
                    name: "", // 商品名称
                    resultList: [],
                    resultPagination: {
                        currentPage: 1,
                        pageSizes: [10, 30, 50, 100],
                        pageSize: 10,
                        total: 0,
                    },
                },
            },
        };
    },
    computed: {
        filterSearchResultList() {
            let _this = this;
            return this.search.resultList.filter(
                (data) =>
                    !_this.search.entName ||
                    data.tax_payer_Name
                        .toLowerCase()
                        .includes(_this.search.entName.toLowerCase())
            );
        },
    },
    methods: {
        pageIndex(index) {
            let currentPage = this.search.resultPagination.currentPage; //当前页码，具体看组件取值
            let pageSize = this.search.resultPagination.pageSize; //每页条数，具体是组件取值
            return index + 1 + (currentPage - 1) * pageSize;
        },
        // 扫描税控服务器
        handleScan(formName) {
            // 清空搜索信息
            this.search.entName = "";

            let _this = this;
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    _this.loading = true;
                    SKServer.editDeviceServer({
                        acceptance_ip: _this.scaninfoForm.acceptanceIP,
                        acceptance_port: _this.scaninfoForm.acceptancePort,
                        app_upload_url: _this.scaninfoForm.appUploadURL,
                        server_type: _this.scaninfoForm.serverType,
                        host_ip: _this.scaninfoForm.hostIP,
                    })
                        .then((val) => {
                            // _this.$message.success(val.message);
                            _this.search.resultList = val.data;
                        })
                        .catch((err) => {
                            _this.$message.error(err);
                        })
                        .finally(() => {
                            _this.loading = false;
                        });
                } else {
                    _this.$message.error(
                        "输入信息不合法，请检查您的输入信息！"
                    );
                    return false;
                }
            });
        },
        // handleSearch() {
        //     // this.callInvoiceQuery(1, this.search.resultPagination.pageSize);
        // },
        // handleResetSearch() {
        //     this.search.entName = "";
        // },
        // 显示企业上架信息弹窗
        handleEntInitDialogShow(row) {
            this.entInitDialog.rowData = row;
            this.entInitDialog.initSucceed = false;

            this.loading = true;
            let _this = this;
            SKServer.getEnterpriseEnter({
                host_ip: row.server_ip,
                tax_no: row.tax_no,
                device_no: row.device_no,
            })
                .then((val) => {
                    if (val.data) {
                        // 数据反写
                        // _this.entInitDialog.form.entType = val.data.ent_type; // 企业类型
                        _this.entInitDialog.form.entShortName =
                            val.data.short_name; // 企业简称
                        _this.entInitDialog.form.provinceCity = [
                            val.data.province,
                            val.data.city,
                        ]; // 省份-城市
                        _this.entInitDialog.form.entContactPerson =
                            val.data.contacts; // 企业联系人
                        _this.entInitDialog.form.entInvoiceAddress =
                            val.data.ent_address; // 开票使用企业地址
                        _this.entInitDialog.form.entPhone = val.data.ent_phone; // 企业电话
                        _this.entInitDialog.form.entBankAccountNumber =
                            val.data.bank_account; // 企业开户银行账号
                        _this.entInitDialog.form.entBankName =
                            val.data.bank_name; // 企业开户银行
                        // _this.entInitDialog.form.entAvailableTaxes =
                        //     val.data.available_taxes; // 企业税率
                        _this.entInitDialog.form.entReserveMrMobile =
                            val.data.reserve_mobile; // 企业管理者手机号码
                        _this.entInitDialog.form.entReserveMrName =
                            val.data.reserve_name; // 企业管理者
                        _this.entInitDialog.form.entPayees = val.data.payees; // 收款人员
                        _this.entInitDialog.form.entReviewers =
                            val.data.reviewers; // 审核人员
                    } else {
                        _this.entInitDialog.form = {};
                    }

                    // 显示弹窗
                    _this.entInitDialog.visible = true;
                })
                .catch((err) => {
                    _this.$message.error(err);
                })
                .finally(() => {
                    _this.loading = false;
                });
        },
        handleEntInit(formName) {
            let _this = this;
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    _this.entInitDialog.loading = true;
                    // 格式化省市
                    let tempProvinceCity =
                        _this.entInitDialog.form.provinceCity;
                    if (tempProvinceCity) {
                        tempProvinceCity = tempProvinceCity.join("-");
                    } else {
                        tempProvinceCity = "";
                    }
                    let args = {
                        host_ip: _this.entInitDialog.rowData.server_ip,
                        tax_no: _this.entInitDialog.rowData.tax_no,
                        device_no: _this.entInitDialog.rowData.device_no,
                        ent_type: _this.entInitDialog.rowData.ent_type,
                        ent_name: _this.entInitDialog.rowData.tax_payer_Name,
                        short_name: _this.entInitDialog.form.entShortName,
                        contacts: _this.entInitDialog.form.entContactPerson,
                        ent_address: _this.entInitDialog.form.entInvoiceAddress,
                        ent_phone: _this.entInitDialog.form.entPhone,
                        tax_office_code:
                            _this.entInitDialog.rowData.tax_office_code,
                        tax_office_name:
                            _this.entInitDialog.rowData.tax_office_name,
                        bank_account:
                            _this.entInitDialog.form.entBankAccountNumber,
                        bank_name: _this.entInitDialog.form.entBankName,
                        device_pos: "",
                        device_type: "", // 后台skserver处理
                        available_taxes:
                            _this.entInitDialog.rowData.available_taxes,
                        drawers: "",
                        payees: _this.entInitDialog.form.entPayees,
                        province_city: tempProvinceCity,
                        reserve_mobile:
                            _this.entInitDialog.form.entReserveMrMobile,
                        reserve_name: _this.entInitDialog.form.entReserveMrName,
                        reviewers: _this.entInitDialog.form.entReviewers,
                    };
                    SKServer.createDeviceAccount(args)
                        .then((val) => {
                            // 发票修复
                            SKServer.deviceInvoiceRecovery({
                                host_ip: _this.entInitDialog.rowData.server_ip,
                                tax_no: _this.entInitDialog.rowData.tax_no,
                                device_no:
                                    _this.entInitDialog.rowData.device_no,
                            })
                                .then((val2) => {
                                    _this.$message.success("企业上架成功！");
                                    _this.$message.success(
                                        "发票修复请求提交成功！" + val2.message
                                    );
                                })
                                .catch((err2) => {
                                    _this.$message.success("企业上架成功");
                                    _this.$message.error(
                                        "发票修复请求提交失败！" + err2
                                    );
                                })
                                .finally(() => {
                                    _this.entInitDialog.loading = false;
                                    // _this.entInitDialog.visible = false;
                                    // 选择商品弹窗显示
                                    _this.selectedCommodityDialog.entInfo =
                                        _this.entInitDialog.rowData;
                                    _this.selectedCommodityDialog.visible = true;
                                });
                        })
                        .catch((err) => {
                            _this.$message.error("上架失败！" + err);
                        })
                        .finally(() => {
                            _this.entInitDialog.loading = false;
                        });
                } else {
                    _this.$message.error(
                        "输入信息不合法，请检查您的输入信息！"
                    );
                    return false;
                }
            });
        },
        handleEntInitDialogClosed() {
            const _this = this;
            _this.loading = true;

            const tempList = _this.search.resultList;
            let tempArr = [];
            for (const item of tempList) {
                tempArr.push(item.device_no);
            }

            SKServer.deviceLaunchState({
                host_ip: _this.entInitDialog.rowData.server_ip,
                deviceNos: tempArr,
            })
                .then((val) => {
                    if (val.data) {
                        for (const item of val.data) {
                            var index = tempList.findIndex(
                                (device) => device.device_no === item.device_no
                            );
                            if (index !== -1) {
                                tempList[index].init = item.init;
                            }
                        }
                        _this.search.resultList = tempList;
                    }
                })
                .catch((err) => {
                    _this.$message.error("设备上架状态刷新失败！" + err);
                })
                .finally(() => {
                    _this.loading = false;
                });
        },
        pageIndex(index) {
            let currentPage = this.selectedCommodityDialog.search
                .resultPagination.currentPage; //当前页码，具体看组件取值
            let pageSize = this.selectedCommodityDialog.search.resultPagination
                .pageSize; //每页条数，具体是组件取值
            return index + 1 + (currentPage - 1) * pageSize;
        },
        fmtCommodityType(row, column, cellValue) {
            let result = cellValue;
            for (const item of commodityOptions) {
                if (item.value === cellValue) {
                    result = item.label;
                    break;
                }
            }
            return result;
        },
        // 查询产品
        handleSearchCommodity() {
            this.callFind(
                1,
                this.selectedCommodityDialog.search.resultPagination.pageSize
            );
        },
        handleSearchCommodityResultSizeChange(val) {
            this.callFind(1, val);
        },
        handleSearchCommodityResultCurrentChange(val) {
            this.callFind(
                val,
                this.selectedCommodityDialog.search.resultPagination.pageSize
            );
        },
        callFind(pageNum, pageSize) {
            this.selectedCommodityDialog.loading = true;
            let _this = this;
            CommodityService.find({
                productName: this.selectedCommodityDialog.search.name,
                type: this.selectedCommodityDialog.search.type,
                pageNum: pageNum,
                pageSize: pageSize,
            })
                .then((val) => {
                    _this.selectedCommodityDialog.search.resultPagination.total =
                        val.data.total;
                    _this.selectedCommodityDialog.search.resultPagination.pageSize =
                        val.data.size;
                    _this.selectedCommodityDialog.search.resultPagination.currentPage =
                        val.data.current;
                    _this.selectedCommodityDialog.search.resultList =
                        val.data.records;
                })
                .catch((err) => {
                    _this.$message.error(err);
                })
                .finally(() => {
                    _this.selectedCommodityDialog.loading = false;
                });
        },
        // 提交选择的商品
        handleSubmitSelectCommodity(row) {
            let _this = this;
            this.$confirm("此操作将为该企业设定商品，请确认?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    // 确定
                    _this.selectedCommodityDialog.loading = true;
                    // let args = {
                    //     entTaxId: "92530121MA6MXYB94W",
                    //     deviceType: 2,
                    //     taxDiscId: "889912003051",
                    //     productId: row.productId,
                    // };
                    let args = {
                        entTaxId: _this.selectedCommodityDialog.entInfo.tax_no,
                        deviceType:
                            _this.selectedCommodityDialog.entInfo.device_type,
                        taxDiscId:
                            _this.selectedCommodityDialog.entInfo.device_no,
                        productId: row.productId,
                    };
                    CommodityService.saveEntProduct(args)
                        .then((val) => {
                            _this.$message.success(val.msg);

                            _this.selectedCommodityDialog.visible = false;
                            _this.entInitDialog.visible = false;
                        })
                        .catch((err) => {
                            _this.$message.error(err);
                        })
                        .finally(() => {
                            _this.selectedCommodityDialog.loading = false;
                        });
                })
                .catch(() => {
                    // 无取消操作
                });
        },
    },
};
</script>

<style scoped>
</style>