<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单ID" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单ID"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input
          v-model="queryParams.remark"
          placeholder="请输入备注"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" clearable>
          <el-option v-for="item in statusOption" :value="item.value" :label="item.name" />
        </el-select>
      </el-form-item>
      <el-form-item label="是否冲红" prop="status">
        <el-select v-model="queryParams.redStatus" clearable>
          <el-option value="01" label="是" />
        </el-select>
      </el-form-item>
      <el-form-item label="冲红时间" prop="redTime">
        <el-date-picker v-model="redTime" size="small" style="width: 220px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" @change="changeTime"></el-date-picker>
      </el-form-item>
      <el-form-item label="开票时间" prop="billTime">
        <el-date-picker v-model="billTime" size="small" style="width: 220px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" @change="changeTime"></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['bill:billLog:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="billLogList">
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="药品订单编号" align="center" prop="orderCode" />
      <el-table-column label="开票类型" align="center" prop="billType">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.billType == 0">企业</el-tag>
          <el-tag v-if="scope.row.billType == 1">个人</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="开票订单号" align="center" prop="orderNo" />
      <el-table-column label="发票号码" align="center" prop="invoiceNo" />
      <el-table-column label="红字确认单编号" align="center" prop="billNo" />
      <el-table-column label="红字确认单uuid" align="center" prop="billUuid" />
      <el-table-column label="发票种类" align="center" prop="invoiceKind" />
      <el-table-column label="开票金额" align="center" prop="orderAmount" />
      <el-table-column label="购方名称" align="center" prop="payerName" />
      <el-table-column label="购方税号" align="center" prop="payerTaxNo" />
<!--      <el-table-column label="冲红原因 1:销货退回;2:开票有误;3:服务中止;4:发生销售折让" align="center" prop="redReason" />-->
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="冲红原因" align="center" prop="redReason">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.redReason == 1">销货退回</el-tag>
          <el-tag v-if="scope.row.redReason == 2">开票有误</el-tag>
          <el-tag v-if="scope.row.redReason == 3">服务中止</el-tag>
          <el-tag v-if="scope.row.redReason == 4">发生销售折让</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="冲红时间" align="center" prop="redTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.redTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="开票时间" align="center" prop="invoiceTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.invoiceTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="发票图片" align="center" prop="pictureUrl">
        <template slot-scope="scope">
          <a class="a" v-if="scope.row.imgUrls" :href="scope.row.imgUrls" target="_blank">打开发票图片</a>
        </template>
      </el-table-column>
      <el-table-column label="发票PDF" align="center" prop="pdfUrl">
        <template slot-scope="scope">
          <a class="a" v-if="scope.row.pdfUrl" :href="scope.row.pdfUrl" target="_blank">打开发票PDF</a>
        </template>
      </el-table-column>
      <el-table-column label="失败原因" align="center" prop="failCause">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" :content="scope.row.failCause" placement="top-start">
            <span class="white-space">{{scope.row.failCause}}</span>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="开票状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-tag v-for="item in statusOption" v-if="scope.row.status==item.value">{{ item.name }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="冲红" align="center" prop="redStatus">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.redStatus == '01'" type="danger">冲红</el-tag>
        </template>
      </el-table-column>

    </el-table>

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

  </div>
</template>

<script>
import { listBillLog, getBillLog, delBillLog, addBillLog, updateBillLog, exportBillLog } from "@/api/bill/billLog";

export default {
  name: "BillLog",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      statusOption: [
        {value: 2, name: '开票成功'},
        {value: 20, name: '开票中'},
        {value: 21, name: '开票成功签章中'},
        {value: 22, name: '开票失败'},
        {value: 24, name: '开票成功签章失败'},
        {value: 3, name: '发票已作废'},
        {value: 31, name: '发票作废中'},
      ],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单开票记录表格数据
      billLogList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      billTime: null,
      redTime: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        orderId: null,
        orderNo: null,
        serialNo: null,
        invoiceCode: null,
        invoiceno: null,
        billNo: null,
        billUuid: null,
        redOrderNo: null,
        allElectronicInvoiceNumbe: null,
        invoiceKind: null,
        orderAmount: null,
        payerName: null,
        payerTaxNo: null,
        address: null,
        telephone: null,
        redReason: null,
        paperPdfUrl: null,
        redTime: null,
        invoiceTime: null,
        pictureUrl: null,
        pdfUrl: null,
        failCause: null,
        statusMsg: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询订单开票记录列表 */
    getList() {
      this.loading = true;
      listBillLog(this.queryParams).then(response => {
        this.billLogList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderId: null,
        orderNo: null,
        serialNo: null,
        invoiceCode: null,
        invoiceno: null,
        billNo: null,
        billUuid: null,
        redOrderNo: null,
        allElectronicInvoiceNumbe: null,
        invoiceKind: null,
        orderAmount: null,
        payerName: null,
        payerTaxNo: null,
        address: null,
        telephone: null,
        redReason: null,
        remark: null,
        paperPdfUrl: null,
        redTime: null,
        invoiceTime: null,
        pictureUrl: null,
        pdfUrl: null,
        failCause: null,
        statusMsg: null,
        status: "0",
        createTime: null,
        createBy: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单开票记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBillLog(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单开票记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBillLog(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBillLog(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除订单开票记录编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delBillLog(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    changeTime() {
      if (this.billTime != null) {
        this.queryParams.sBillTime = this.billTime[0];
        this.queryParams.eBillTime = this.billTime[1];
      } else {
        this.queryParams.sBillTime = null;
        this.queryParams.eBillTime = null;
      }
      if (this.redTime != null) {
        this.queryParams.sRedTime = this.redTime[0];
        this.queryParams.eRedTime = this.redTime[1];
      } else {
        this.queryParams.sRedTime = null;
        this.queryParams.eRedTime = null;
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有订单开票记录数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportBillLog(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>

<style scoped>
.white-space{
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.a{
  text-decoration: underline;
}
</style>
