<template>
  <div class="app-container app-container-scheduling">
    <div class="app-container_flex_auto">
      <div class="app-container_form" v-show="showSearch">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="68px">
          <el-form-item label="订单号" label-width="58px">
            <el-input
              v-model="queryParams.orderId"
              placeholder="请输入订单号"
              clearable
              style="width: 220px;"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="抬头名称" >
            <el-input
              v-model="queryParams.name"
              placeholder="请输入抬头名称"
              clearable
              style="width: 220px;"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
          <el-form-item label="类型" label-width="48px">
            <el-select
              v-model="queryParams.handlerType"
              placeholder="类型"
              clearable
              style="width: 120px"
            >
              <el-option
                v-for="dict in dict.type.lookup_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="状态" label-width="48px">
            <el-select
              v-model="queryParams.status"
              placeholder="状态"
              clearable
              style="width: 120px"
            >
              <el-option
                v-for="dict in dict.type.Invoicing_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="开票来源">
            <el-select
              v-model="queryParams.invoiceSource"
              placeholder="开票来源"
              clearable
              style="width: 150px"
            >
              <el-option
                v-for="dict in dict.type.Invoice_source"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </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>
      </div>
      <div class="app-container_table" :class="showSearch ? 'mt15' : ''">
        <!-- <el-row :gutter="10" class="mb8">
          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row> -->
        <div class="app-container_table_main">
          <el-table 
            ref="tables" 
            v-loading="loading" 
            :data="dataList"
            :summary-method="getSummaries"
            show-summary
            border 
            max-height="520px"
          >
            <el-table-column label="序号" align="center" type="index" width="60"></el-table-column>
            <el-table-column label="订单号" align="center" prop="orderId" width="170" />
            <el-table-column label="订单金额(元)" align="center" prop="invoiceAmount" width="110" />
            <el-table-column label="类型" align="center">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.lookup_type" :value="scope.row.handlerType"/>
              </template>
            </el-table-column>
            <el-table-column label="申请类型" align="center">
              <template slot-scope="scope">
                <span v-if="scope.row.invoiceLine == 'bs'">数电专票</span>
                <span v-else-if="scope.row.invoiceLine == 'pc'">数电普票</span>
              </template>
            </el-table-column>
            <el-table-column label="抬头名称" align="center" prop="name" width="120" />
            <el-table-column label="税号" align="center" prop="creditCode" width="140" />
            <el-table-column label="发票备注" align="center" prop="remark" width="160" show-overflow-tooltip />
            <el-table-column label="状态" align="center">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.Invoicing_type" :value="scope.row.status"/>
              </template>
            </el-table-column>
            <el-table-column label="开票来源" align="center">
              <template slot-scope="scope">
                <dict-tag :options="dict.type.Invoice_source" :value="scope.row.invoiceSource"/>
              </template>
            </el-table-column>
            <el-table-column label="申请时间" align="center" width="160">
              <template slot-scope="scope">
                <span>{{ parseTime(scope.row.createTime) }}</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
              <template slot-scope="scope">
                <el-button
                  size="mini"
                  type="text"
                  @click="openDetails(scope.row)"
                  v-hasPermi="['finance:InvoiceRecords:details']"
                >详情</el-button>
                
                <span v-hasPermi="['finance:InvoiceRecords:redblood']" style="display: inline-block;">
                  <el-button
                    size="mini"
                    type="text"
                    style="margin-left: 10px;"
                    v-if="scope.row.status == 1 || scope.row.status == 7"
                    @click="openRedBlood(scope.row)"
                  >冲红</el-button>
                </span>
                <!-- v-hasPermi="['finance:InvoiceRecords:relatedOrders']" -->
                <span style="display: inline-block;" v-hasPermi="['finance:InvoiceRecords:relatedOrders']">
                  <el-button
                    size="mini"
                    type="text"
                    style="margin-left: 10px;"
                    v-if="scope.row.invoiceSource == 2"
                    @click="openRelatedOrders(scope.row)"
                  >{{ scope.row.orderId && scope.row.orderId != '0'  ? '重新关联订单': '关联订单' }}</el-button>
                </span>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
      <div class="app-container_table_page">
        <pagination
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />
      </div>
    </div>
    <!-- 详情 -->
    <details-dia ref="detailsDia" @getList="getList"></details-dia>
    <!-- 关联订单 -->
    <relatedOrders ref="relatedOrders" @getList="getList"></relatedOrders>
  </div>
</template>

<script>

import { pageList,orderInvoiceRed } from '@/api/financeMr/InvoiceRecords'
import detailsDia from "./dialog/details.vue";
import relatedOrders from "./dialog/relatedOrders.vue";
import { myMixin } from "@/utils/mixin.js"
export default {
  name: "InvoiceRecords",
  //mixins: [myMixin],
  dicts: ['Invoicing_type','lookup_type','Invoice_source'],
  components: { detailsDia,relatedOrders },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户表格数据
      dataList: null,
      // 弹出层标题
      title: "",

      // 是否显示弹出层
      open: false,
      // 日期范围
      dateRange: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      pageList(this.addDateRange(this.queryParams, this.dateRange))
      .then(response => {
          this.dataList = response.data.rows;
          this.total = response.data.total;
          this.loading = false;
        }
      ).catch(()=>{
        this.dataList = [];
        this.total = 0;
        this.loading = false;
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计';
          return;
        }
        const values = data.map(item => Number(item[column.property]));
        if (!values.every(value => isNaN(value)) && column.property == 'invoiceAmount') {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] = Number(sums[index].toFixed(2));
        } else {
          sums[index] = '';
        }
      });
      return sums;
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.$set(this.queryParams, 'orderId', '');
      this.$set(this.queryParams, 'name', '');
      this.$set(this.queryParams, 'status', '');
      this.$set(this.queryParams, 'handlerType', '');
      this.$set(this.queryParams,'invoiceSource','')
      this.queryParams.pageNum = 1;
      this.handleQuery();
    },
    /** 详情按钮操作 */
    openDetails(row, type) {
      this.$refs["detailsDia"].openDialog("详情", row, type);
    },
    /**  打开  */
    openRelatedOrders(row, type){
      this.$refs["relatedOrders"].openDialog("关联订单",row,type)
    },
    openRedBlood(row){
      this.$confirm('您即将进行冲红操作,是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          try {
            let res = await orderInvoiceRed({
              orderId: row.orderId
            })
            if(res.code == 200){
              this.$message({
                type: 'success',
                message: '冲红成功!'
              });
            }else {
              this.$message.error(res.msg);
            }
          } catch (error) {
            this.$message.error('冲红失败!');
          }
          
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消冲红'
          });          
        });
    }
  }
};
</script>
<style scoped lang="scss">
.app-container-scheduling ::v-deep .el-select__tags {
    flex-wrap: inherit !important;
    overflow-x: auto !important;
}
.app-container-scheduling ::v-deep .el-form-item__content {
  position: relative;
}
.app-container-scheduling ::v-deep .el-form-item__content .query_clear{
  position: absolute;
  top: -15px;
  right: -10px;
  display: none;
  cursor: pointer;
  z-index: 99;
}
.app-container-scheduling ::v-deep .el-form-item__content:hover .query_clear {
  display: block;
}
</style>