<template>
  <div id="invoice_query" v-loading="loading" element-loading-text="拼命加载中......请稍等">
    <el-container>
      <el-form ref="form" style="width: 100%;">
        <el-row :gutter="30">
          <el-col :span="20">
            <el-row :gutter="10">
              <el-col :span="6">
                <el-form-item>
                  <el-input
                      v-model="search.entName"
                      clearable
                      placeholder="请输入..."
                  >
                    <template slot="prepend">客户名称</template>
                  </el-input>
                </el-form-item>
              </el-col>
              <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.invoiceState"
                      clearable
                      placeholder="请选择发票状态"
                      style="width: 100%;"
                  >
                    <el-option
                        v-for="item in invoiceStateOptions"
                        :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.hongchongFlag"
                      placeholder="请选择发票红冲状态"
                      style="width: 100%;"
                  >
                    <el-option
                        v-for="item in invoiceHongchongFlagOptions"
                        :key="item.value"
                        :label="item.label"
                        :value="item.value"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
            <el-row :gutter="10">
              <el-col :span="6">
                <el-form-item>
                  <el-input
                      v-model="search.invoiceCode"
                      clearable
                      placeholder="请输入..."
                  >
                    <template slot="prepend">发票代码</template>
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item>
                  <el-input
                      v-model="search.invoiceNumber"
                      clearable
                      placeholder="请输入..."
                  >
                    <template slot="prepend">发票号码</template>
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="6">
                <el-form-item>
                  <el-date-picker
                      v-model="search.dateRange"
                      type="daterange"
                      unlink-panels
                      range-separator="-"
                      start-placeholder="开始日期"
                      end-placeholder="结束日期"
                  >
                    <template slot="prepend">开票日期</template>
                  </el-date-picker>
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
          <el-col :span="4">
            <div>
              <el-button @click="handleSearch" type="primary" icon="el-icon-search">搜索</el-button>
              <el-button @click="handleResetSearch">重置</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </el-container>
    <br/>
    <el-container>
      <el-table :data="search.resultList" border stripe :height="tableHeight" style="width: 100%;">
        <el-table-column
            type="index"
            :index="pageIndex"
            align="center"
            label="序号"
            width="50"
        ></el-table-column>
        <el-table-column
            prop="invoiceCategory"
            label="发票种类"
            width="160"
            align="center"
            :formatter="fmtInvoiceCategory"
        ></el-table-column>
        <el-table-column prop="invoiceCode" label="发票代码" width="120" show-overflow-tooltip></el-table-column>
        <el-table-column
            prop="invoiceNumber"
            label="发票号码"
            width="120"
            show-overflow-tooltip
        ></el-table-column>
        <el-table-column
            prop="status"
            label="发票状态"
            width="120"
            :formatter="fmtInvoiceStatus"
        ></el-table-column>
        <el-table-column prop="invoiceDate" label="开票日期" width="120" show-overflow-tooltip></el-table-column>
        <el-table-column prop="customerName" label="名称" width="240" show-overflow-tooltip></el-table-column>
        <el-table-column
            prop="customerEntTaxId"
            label="纳税人识别号"
            width="180"
            show-overflow-tooltip
        ></el-table-column>
        <el-table-column prop="totalAmount" label="合计金额" width="100" show-overflow-tooltip></el-table-column>
        <el-table-column prop="totalTaxes" label="合计税额" width="100" show-overflow-tooltip></el-table-column>
        <el-table-column
            prop="totalAmountTaxes"
            label="价税合计"
            width="100"
            show-overflow-tooltip
        ></el-table-column>
        <el-table-column prop="invoicePerson" label="开票人" width="100" show-overflow-tooltip></el-table-column>
        <el-table-column
            prop="interactTypeDetail"
            label="电子邮件"
            width="180"
            show-overflow-tooltip
        ></el-table-column>
        <el-table-column label="操作" width="180" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button @click="handleShowInvoiceDetails(scope.row)" type="primary">查看票样</el-button>
            <el-button
                @click="handleInvoiceRefresh(scope.row)"
                :disabled="handleInvoiceRefreshDisabled(scope.row)"
                type="primary"
            >刷新
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-container>

    <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>

    <el-dialog
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        center
        width="300mm"
        top="10vh"
        :visible.sync="invoiceDetails.dialogVisible"
        @opened="handleInvoiceDetailsDialogOpened"
        @closed="handleInvoiceDetailsDialogClosed"
    >
      <div v-loading="invoiceDetails.loading" element-loading-text="拼命加载中......请稍等">
        <el-row>
          <el-col :span="6">
            <el-radio-group v-model="invoiceDetails.tabPosition">
              <el-radio-button
                  v-for="item in invoiceDetails.tabPositonOptions"
                  :key="item.value"
                  :label="item.value"
                  :disabled="item.disabled"
              >{{ item.label }}
              </el-radio-button>
            </el-radio-group>
          </el-col>
          <el-col :span="18">
            <div style="text-align: right;">
              <el-button
                  :disabled="invoiceInvalidDisabled"
                  @click="handleInvoiceInvalid"
              >发票作废
              </el-button>
              <el-button
                  :disabled="downloadInvoiceDisabled"
                  @click="handleDownloadInvoicePDF"
              >下载PDF格式发票
              </el-button>
              <!-- <el-button :disabled="downloadInvoiceDisabled" @click="handleDownloadInvoiceOFD">
                  下载OFD格式发票
              </el-button>-->
              <el-button
                  :disabled="printInvoiceDisabled"
                  @click="handlePrintInvoice"
              >打印发票
              </el-button>
              <el-button @click="handlePrintMargins">打印机页边距设置</el-button>
            </div>
          </el-col>
        </el-row>
        <el-divider></el-divider>
        <div>
          <el-card v-show="invoiceDetails.tabPosition == 1" style="text-align: center;">
            <invoice-dzfp ref="invoice-dzfp-sample"></invoice-dzfp>
          </el-card>
          <div v-show="invoiceDetails.tabPosition == 2">
            <el-table
                :data="invoiceDetails.goodsList"
                height="550px"
                border
                stripe
                style="width: 100%;"
            >
              <el-table-column
                  type="index"
                  :index="pageIndex"
                  align="center"
                  label="序号"
                  width="50"
              ></el-table-column>
              <el-table-column
                  prop="goodsCode"
                  label="商品编码"
                  width="180"
                  show-overflow-tooltip
              ></el-table-column>
              <el-table-column
                  prop="goodsName"
                  label="项目名称"
                  width="180"
                  show-overflow-tooltip
              ></el-table-column>
              <el-table-column prop="goodsSpecification" label="规格型号"></el-table-column>
              <el-table-column prop="goodsUnit" label="单位"></el-table-column>
              <el-table-column prop="goodsQuantity" label="数量"></el-table-column>
              <el-table-column prop="goodsPrice" label="单价"></el-table-column>
              <el-table-column prop="goodsTotalPrice" label="金额"></el-table-column>
              <el-table-column prop="goodsTaxRate" label="税率"></el-table-column>
              <el-table-column prop="goodsTotalTax" label="税额"></el-table-column>
            </el-table>
          </div>
        </div>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  invoiceQuery,
  invoiceRefresh,
  invoiceRetry,
  getInvoicePrintData,
} from "@/api/invoice_management";

import {printInvoice, printMargins} from "@/api/printer";

import {InvoiceInvalid, InvoiceResultData} from "@/api/invoice_ext";

import {GetInvoiceDownloadUrl} from "@/api/invoice_info";

import InvoiceDZFP from "@/components/InvoiceSample/InvoiceDZFP";

import DateHelper from "@/utils/date";

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

// 发票状态列表
const invoiceStateOptions = [
  {
    value: 2,
    label: "开票中",
  },
  {
    value: 3,
    label: "开票成功",
  },
  {
    value: 4,
    label: "开票失败",
  },
];

// 发票红冲状态列表
const invoiceHongchongFlagOptions = [
  {
    value: 1,
    label: "发票待成功开具",
  },
  {
    value: 2,
    label: "可红冲开票",
  },
  {
    value: 3,
    label: "红冲开票中",
  },
  {
    value: 4,
    label: "红冲失败",
  },
  {
    value: 5,
    label: "红冲成功",
  },
  {
    value: 6,
    label: "非蓝字发票",
  },
];

// 发票明细页面标签列表
const invoiceDetailsTabPositonOptions = [
  {
    value: 1,
    label: "发票",
  },
  {
    value: 2,
    label: "商品清单",
  },
];

// 发票明细页面无发票清单标签列表
const invoiceDetailsTabPositonDisabledGoodsListOptions = [
  {
    value: 1,
    label: "发票",
  },
  {
    value: 2,
    label: "商品清单",
    disabled: true,
  },
];

/**
 * 获取发票种类文本；
 * @param {Number} intKey 发票类别的数字标识
 */
function GetInvoiceCategoryLabel(intKey) {
  let result = intKey;
  for (const item of invoiceCategoryOptions) {
    if (item.value === intKey) {
      result = item.label;
      break;
    }
  }
  return result;
}

export default {
  name: "invoice_query",
  components: {
    "invoice-dzfp": InvoiceDZFP,
  },
  data() {
    return {
      loading: false,
      invoiceCategoryOptions: invoiceCategoryOptions,
      invoiceStateOptions: invoiceStateOptions,
      invoiceHongchongFlagOptions: invoiceHongchongFlagOptions,
      search: {
        entName: "",
        invoiceState: "",
        hongchongFlag: "",
        dateRange: [
          DateHelper.LocalCurrentMonthStart(),
          DateHelper.LocalCurrentMonthEnd(),
        ],
        invoiceCode: "",
        invoiceNumber: "",
        invoiceCategory: "",
        resultList: [],
        resultPagination: {
          currentPage: 1,
          pageSizes: [10, 30, 50, 100],
          pageSize: 10,
          total: 0,
        },
      },
      invoiceDetails: {
        loading: false,
        dialogVisible: false,
        tabPositonOptions: invoiceDetailsTabPositonOptions,
        tabPosition: 1,
        selectRow: {}, // 用户选中的发票数据；
        goodsList: [], // 发票清单；
      },
      tableHeight: ''
    };
  },
  created() {
    this.tableHeight = window.innerHeight - 250
    this.callInvoiceQuery(
        this.search.resultPagination.currentPage,
        this.search.resultPagination.pageSize
    );
  },
  mounted() {
    this.tableHeight = window.innerHeight - 250
    window.onresize = () => {
      return (() => {
        this.tableHeight = window.innerHeight - 250
      })()
    }
  },
  computed: {
    // 发票作废功能是否禁用；
    invoiceInvalidDisabled: function () {
      let row = this.invoiceDetails.selectRow;

      // 没有操作数据
      if (!row) return true;

      // 发票未开具成功
      if (row.status != 3) return true;
      // 发票已作废
      if (row.invalidFlag) return true;
      // 发票不是纸质发票
      if (row.invoiceCategory != 3 && row.invoiceCategory != 4)
        return true;

      // 发票开具时间不属于当前月份
      var nowMonth = new Date().getMonth();
      let invoiceMonth = new Date(row.invoiceDate).getMonth();
      if (nowMonth != invoiceMonth) return true;

      return false;
    },
    // 下载发票功能是否禁用；
    downloadInvoiceDisabled: function () {
      let result = true;
      let row = this.invoiceDetails.selectRow;
      if (row) {
        result = !(
            row.status == 3 &&
            (row.invoiceCategory == 1 || row.invoiceCategory == 2)
        );
      }
      return result;
    },
    // 打印发票功能是否禁用；
    printInvoiceDisabled: function () {
      let result = true;
      let row = this.invoiceDetails.selectRow;
      if (row) {
        result = !(
            row.status == 3 &&
            (row.invoiceCategory == 3 || row.invoiceCategory == 4)
        );
      }
      return result;
    },
  },
  methods: {
    pageIndex(index) {
      let currentPage = this.search.resultPagination.currentPage; //当前页码，具体看组件取值
      let pageSize = this.search.resultPagination.pageSize; //每页条数，具体是组件取值
      return index + 1 + (currentPage - 1) * pageSize;
    },
    fmtInvoiceStatus(row, column, cellValue) {
      if (cellValue == 2) {
        return "开票中";
      } else if (cellValue == 3) {
        if (row.hongchongFlag == 5) {
          return "正数已红冲成功";
        } else if (row.invalidFlag === true) {
          return "正数作废成功";
        } else {
          return "正数开票成功";
        }
      } else if (cellValue == 4) {
        return "开票失败";
      }
      return cellValue;
    },
    fmtInvoiceCategory(row, column, cellValue) {
      return GetInvoiceCategoryLabel(cellValue);
    },
    handlePrintMargins() {
      printMargins();
    },
    // 发票刷新按钮是否禁用；
    handleInvoiceRefreshDisabled(row) {
      return !(row.status == 2);
    },
    // 发票刷新；
    handleInvoiceRefresh(row) {
      this.loading = true;
      let _this = this;
      invoiceRefresh(row.invoiceReqFlowNo)
          .then((val) => {
            _this.$message.success(val.msg);
            _this.loading = false;
            return _this.callInvoiceQuery(
                _this.search.resultPagination.currentPage,
                _this.search.resultPagination.pageSize
            );
          })
          .catch((err) => {
            _this.$message.error(err);
            _this.loading = false;
          });
    },
    // 显示发票详细信息弹窗；
    handleShowInvoiceDetails(row) {
      console.log("显示发票详细信息：");
      console.log(row);

      // 开票中
      if (row.status == 2) {
        this.$message.error("发票开票中，无法查看票样！");
        return false;
      }

      // 开票失败
      if (row.status == 4) {
        this.$message.error("发票开票失败，无法查看票样！");
        return false;
      }

      // 如果此时发票状态不是开票成功
      if (row.status != 3) {
        this.$message.error("发票状态未知，请您稍后重试！");
        return false;
      } else {
        // 设置用户选中的发票数据；
        this.invoiceDetails.selectRow = row;
        this.invoiceDetails.dialogVisible = true;
      }
    },
    // 发票详细页面打开结束时的回调；
    handleInvoiceDetailsDialogOpened() {
      // 重置
      this.invoiceDetails.tabPosition = 1;
      this.invoiceDetails.goodsList = []; // 发票清单；

      let row = this.invoiceDetails.selectRow;
      if (row) {
        this.invoiceDetails.loading = true;
        let _this = this;
        InvoiceResultData(row.orderId, row.invoiceType)
            .then((val) => {
              // 设置发票票样显示信息；
              let invoiceSample = _this.$refs["invoice-dzfp-sample"];
              let title = GetInvoiceCategoryLabel(
                  row.invoiceCategory
              );
              invoiceSample.setTitle(title);
              invoiceSample.setInvoiceBaseInfo({
                invoiceCode: val.data.invoiceData.invoiceCode,
                invoiceNo: val.data.invoiceData.invoiceNo,
                invoiceDate: val.data.invoiceData.invoiceDate,
                invoiceCheckCode:
                val.data.invoiceData.invoiceCheckCode,
                machineNo: val.data.invoiceData.machineNo,
                invoiceTotalPrice:
                val.data.invoiceData.invoiceTotalPrice,
                invoiceTotalTax:
                val.data.invoiceData.invoiceTotalTax,
                invoiceTotalPriceTax:
                val.data.invoiceData.invoiceTotalPriceTax,
                payee: val.data.invoiceData.payee,
                checker: val.data.invoiceData.checker,
                drawer: val.data.invoiceData.drawer,
                taxControlCode: val.data.invoiceData.taxControlCode,
                remarks: val.data.invoiceData.remarks,
              });
              invoiceSample.setInvoiceGF({
                name: val.data.invoiceData.buyerName,
                entTaxId: val.data.invoiceData.buyerEntTaxId,
                bankAccount: val.data.invoiceData.buyerBankAccount,
                addressPhone:
                val.data.invoiceData.buyerAddressPhone,
              });
              invoiceSample.setInvoiceXF({
                name: val.data.invoiceData.sellerEntName,
                entTaxId: val.data.invoiceData.sellerEntTaxId,
                bankAccount: val.data.invoiceData.sellerBankAccount,
                addressPhone:
                val.data.invoiceData.sellerAddressPhone,
              });
              if (val.data.invoiceData.goodsListMark == 1) {
                // 有清单；
                invoiceSample.setInvoiceCommodity([]);
                _this.invoiceDetails.goodsList =
                    val.data.invoiceData.goodsDetailVOs;
                _this.invoiceDetails.tabPositonOptions = invoiceDetailsTabPositonOptions;
              } else {
                invoiceSample.setInvoiceCommodity(
                    val.data.invoiceData.goodsDetailVOs
                );
                _this.invoiceDetails.tabPositonOptions = invoiceDetailsTabPositonDisabledGoodsListOptions;
              }
              _this.invoiceDetails.loading = false;
            })
            .catch((err) => {
              _this.$message.error(err);
              _this.invoiceDetails.loading = false;

              _this.invoiceDetails.selectRow = {};
              _this.invoiceDetails.dialogVisible = false;
            });
      } else {
        console.log("没有选中数据！");
      }
    },
    // 发票详细页面关闭结束时的回调
    handleInvoiceDetailsDialogClosed() {
      let row = this.invoiceDetails.selectRow;
      if (row) {
        this.callInvoiceQuery(
            this.search.resultPagination.currentPage,
            this.search.resultPagination.pageSize
        );
      } else {
        console.log("没有选中数据！");
      }
    },
    // 发票作废；
    handleInvoiceInvalid() {
      let _this = this;
      let row = this.invoiceDetails.selectRow;
      if (row) {
        this.$confirm(
            "发票代码：" +
            row.invoiceCode +
            " 发票号码：" +
            row.invoiceNumber +
            " 是否作废当前发票！",
            "提示",
            {
              confirmButtonText: "确定",
              cancelButtonText: "取消",
              type: "warning",
            }
        )
            .then(() => {
              _this.invoiceDetails.loading = true;
              InvoiceInvalid({
                invoiceInvalidType: row.invoiceType,
                invoiceCategory: row.invoiceCategory,
                invoiceCode: row.invoiceCode,
                invoiceNumber: row.invoiceNumber,
              })
                  .then((val) => {
                    _this.$message.success(val.msg);
                    _this.invoiceDetails.loading = false;
                  })
                  .catch((err) => {
                    _this.$message.error(err);
                    _this.invoiceDetails.loading = false;
                  });
            })
            .catch(() => {
              // 取消；
            });
      } else {
        console.log("没有选中数据！");
      }
    },
    // 下载PDF格式发票；
    handleDownloadInvoicePDF() {
      let row = this.invoiceDetails.selectRow;
      let _this = this;
      let download = function (url) {
        let fullUrl = url + ".pdf";
        console.log(fullUrl);
        _this
            .isRunUrl(fullUrl)
            .then((val) => {
              let a = document.createElement("a");
              a.href = fullUrl;
              a.target = "_blank";
              a.click();
            })
            .catch((err) => {
              _this.$message.error("PDF格式发票文件下载链接无效！");
            });
      };
      if (row) {
        let url = row.fileUrl;
        if (url === null || url === "" || url === undefined) {
          GetInvoiceDownloadUrl({
            invoiceCode: row.invoiceCode,
            invoiceNumber: row.invoiceNumber,
          })
              .then((val) => {
                if (val.data) {
                  download(val.data.fileUrl);
                } else {
                  _this.$message.error(
                      "PDF格式发票文件下载链接无效！"
                  );
                }
              })
              .catch((err) => {
                _this.$message.error(err);
              });
        } else {
          download(url);
        }
      } else {
        console.log("没有选中数据！");
      }
    },
    // 下载OFD格式发票；
    // handleDownloadInvoiceOFD() {
    //     let row = this.invoiceDetails.selectRow;
    //     let url = row.fileUrl;
    //     if (row) {
    //         if (url === null || url === '' || url === undefined) {
    //             this.$message.error('OFD格式发票文件下载链接无效！');
    //         } else {
    //             let fullUrl = url + '.ofd';
    //             console.log(fullUrl);
    //             this.isRunUrl(fullUrl).then(val => {
    //                 let a = document.createElement('a')
    //                 a.href = fullUrl;
    //                 a.target = "_blank";
    //                 a.click();
    //             }).catch(err => {
    //                 this.$message.error('OFD格式发票文件下载链接无效！');
    //             })
    //         }
    //     } else {
    //         console.log('没有选中数据！')
    //     }
    // },
    // 打印发票；
    handlePrintInvoice() {
      let row = this.invoiceDetails.selectRow;
      if (row) {
        printInvoice(row.invoiceReqFlowNo, row.invoiceType)
            .then()
            .catch((err) => {
              this.$message.error(err);
            });
      } else {
        console.log("没有选中数据！");
      }
    },
    handleSearch() {
      this.callInvoiceQuery(1, this.search.resultPagination.pageSize);
    },
    handleResetSearch() {
      this.search.entName = "";
      this.search.invoiceState = "";
      this.search.hongchongFlag = "";
      this.search.dateRange = "";
      this.search.invoiceCode = "";
      this.search.invoiceNumber = "";
      this.search.invoiceCategory = "";
    },
    handleSearchResultSizeChange(val) {
      this.callInvoiceQuery(1, val);
    },
    handleSearchResultCurrentChange(val) {
      this.callInvoiceQuery(val, this.search.resultPagination.pageSize);
    },
    /**
     * 查询发票
     * @param {Number} pageNum : 当前页码
     * @param {Number} pageSize : 每页数量
     */
    callInvoiceQuery(pageNum, pageSize) {
      let beginTime = "";
      let endTime = "";
      if (this.search.dateRange instanceof Array) {
        beginTime = this.search.dateRange[0];
        endTime = this.search.dateRange[1];
        beginTime = this.dateFormat(
            new Date(beginTime),
            "yyyy-MM-dd HH:mm:ss"
        );
        endTime = this.dateFormat(
            new Date(endTime),
            "yyyy-MM-dd HH:mm:ss"
        );
      }
      this.loading = true;
      let _this = this;
      invoiceQuery({
        invoiceCode: this.search.invoiceCode,
        invoiceNumber: this.search.invoiceNumber,
        invoiceCategory: this.search.invoiceCategory,
        customerName: this.search.entName,
        invoiceState: this.search.invoiceState,
        hongchongFlag: this.search.hongchongFlag,
        invoiceDateBegin: beginTime,
        invoiceDateEnd: endTime,
        pageNum: pageNum,
        pageSize: pageSize,
      })
          .then((val) => {
            _this.search.resultPagination.total = val.data.total;
            _this.search.resultPagination.pageSize = val.data.size;
            _this.search.resultPagination.currentPage =
                val.data.current;
            _this.search.resultList = val.data.records;
            _this.loading = false;
          })
          .catch((err) => {
            _this.$message.error(err);
            _this.loading = false;
          });
    },
    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;
    },
    /**
     * 是否可以运行链接/下载连接是否有效；
     */
    isRunUrl(url) {
      return new Promise(function (resolve, reject) {
        // 测试链接连通性, 主要检测404错误
        // 由于AJAX通常无法区分404和跨域问题
        // 所以只能用script 或者 link标签
        // link比script更容易捕获错误
        var dom = document.createElement("link");
        dom.href = url;
        dom.rel = "stylesheet";
        document.head.appendChild(dom);
        dom.onload = function () {
          document.head.removeChild(dom);
          resolve();
        };
        dom.onerror = reject;
      });
    },
  },
};
</script>

<style scoped>
</style>
