<template>
  <div class="app-container">
    <el-card v-show="showSearch">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" label-width="100px" class="card">
        <!-- <el-row :span="24"> -->
        <el-form-item :label="$t('开票编号')">
          <el-input style="max-width: 188px" v-model="queryParams.invoiceNo" :placeholder="$t('请输入开票编号')" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item :label="$t('收款编号')">
          <el-input style="max-width: 188px" v-model="queryParams.receiptNo" :placeholder="$t('请输入收款编号')" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item :label="$t('提单号')">
          <el-input style="max-width: 188px" v-model="queryParams.tidanNo" :placeholder="$t('请输入提单号')" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>
        <el-form-item :label="$t('订单号')">
          <el-input style="max-width: 188px" v-model="queryParams.orderNo" :placeholder="$t('请输入订单号')" clearable @keyup.enter.native="handleQuery" />
        </el-form-item>

        <el-form-item :label="$t('开票状态')">
          <dict-selector multiple :type="DICT_TYPE.INVOICING_STATUS" v-model="queryParams.invoicingStatus" clearable />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" icon="el-icon-search" @click="handleQuery">{{ $t("搜索") }}</el-button>
          <el-button icon="el-icon-refresh" @click="resetQuery">{{ $t("重置") }}</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <div class="tableTop">
      <el-table v-loading="loading" :data="invoiceList" border>
        <el-table-column :label="$t('开票编号')" fixed="left" width="160" align="center" prop="invoiceNo">
          <template slot-scope="{ row }">
            <router-link
              :to="{
                path: '/financial/invoiceOperate',
                query: {
                  id: row.id,
                  key: 'view'
                }
              }"
              class="link-type"
            >
              {{ row.invoiceNo }}
            </router-link>
          </template>
        </el-table-column>
        <el-table-column :label="$t('收款单号')" fixed="left" width="160" align="center" prop="receiptNo">
          <template slot-scope="{ row }">
            <router-link
              :to="{
                path: '/financial/receiptDetail',
                query: {
                  id: row.receiptId
                }
              }"
              class="link-type"
            >
              {{ row.receiptNo }}
            </router-link>
          </template>
        </el-table-column>
        <el-table-column :label="$t('申请日期')" width="160" align="center" prop="createTime" />
        <el-table-column :label="$t('申请人/部门')" width="200" align="center">
          <template slot-scope="{ row }">
            <div>{{ row.applyUserName }}/{{ row.departmentName }}</div>
          </template>
        </el-table-column>
        <el-table-column :label="$t('开票抬头')" width="160" align="center" prop="invoiceHeader" />
        <el-table-column :label="$t('开票金额')" align="center" prop="invoiceMoney" />
        <el-table-column :label="$t('开票品名')" width="160" align="center" prop="invoiceItem" />
        <el-table-column :label="$t('税率')" align="center" prop="taxRate" />

        <el-table-column :label="$t('开票状态')" align="center" prop="invoicingStatus">
          <template slot-scope="scope">
            <dict-tag :type="DICT_TYPE.INVOICING_STATUS" :value="scope.row.invoicingStatus" />
          </template>
        </el-table-column>
        <el-table-column :label="$t('开票资料状态')" width="160" align="center">
          <template slot-scope="scope">
            <dict-tag :type="DICT_TYPE.INVOICE_DATA_STATUS" :value="scope.row.invoiceDataStatus" />
          </template>
        </el-table-column>
        <el-table-column :label="$t('订单/提单')" width="260" align="center" prop="orderNo">
          <template slot-scope="{ row }">
            <div v-for="item in row.orderInfoList" :key="item.orderId">
              <router-link :to="`/order/detail?orderId=${item.orderId}`" class="link-type"> {{ item.orderNo }}/{{ item.tidanNo }} </router-link>
            </div>
          </template>
        </el-table-column>
        <el-table-column :label="$t('收款状态/核销比例')" width="160" align="center">
          <template slot-scope="scope">
            <dict-tag :type="DICT_TYPE.ECW_RECEIPT_STATE" :value="scope.row.receiptState" />
            / {{ scope.row.writeOffProportion }}
          </template>
        </el-table-column>
        <el-table-column :label="$t('操作')" width="200" fixed="right" align="center">
          <template slot-scope="{ row }">
            <!-- 待开票0已提交0可以驳回 -->
            <el-button type="text" v-if="row.invoicingStatus === 0 && row.invoiceDataStatus === 0" @click="handleInvoiceOperate({ id: row.id, key: 'reject' })">{{ $t("驳回资料") }}</el-button>
            <!-- 开票状态=待开票 -->
            <el-button v-if="row.invoicingStatus === 0" @click="handleInvoiceOperate({ id: row.id, key: 'cancel' })" type="text">{{ $t("取消开票") }}</el-button>
            <!-- 开票状态=待开票且开票资料状态=已提交    提交开票提交的是开票信息 -->
            <el-button v-if="row.invoicingStatus === 0 && row.invoiceDataStatus === 0" @click="handleInvoiceOperate({ id: row.id, key: 'submitInfo' })" type="text">{{ $t("提交开票") }}</el-button>
            <!-- 开票状态=待开票0 -->
            <el-button v-if="row.invoicingStatus === 0" @click="handleInvoiceOperate({ id: row.id, key: 'edit' })" type="text">{{ $t("修改开票申请") }}</el-button>
            <!--开票状态=已开票1 -->
            <el-button v-if="row.invoicingStatus === 1" type="text" @click="handleInvoiceOperate({ id: row.id, key: 'editInfo' })">{{ $t("修改开票信息") }}</el-button>
            <!-- 开票状态=已开票 -->
            <el-button v-if="row.invoicingStatus === 1" @click="handleInvoiceOperate({ id: row.id, key: 'delInfo' })" type="text">{{ $t("删除开票信息") }}</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.rows" @pagination="getInvoiceList" />
    </div>
  </div>
</template>

<script>
import { loadInvApplicationList } from "@/api/ecw/financial"
export default {
  data() {
    return {
      showSearch: true,
      loading: false,
      invoiceList: [],
      total: 0,
      queryParams: {
        page: 1,
        rows: 10,
        invoiceNo: "", //开票编号
        invoicingStatus: [], //开票状态
        orderNo: "", //订单号
        receiptNo: "", //收款单号
        tidanNo: "" //	提单号
      }
    }
  },
  created() {
    this.getInvoiceList()
  },
  activated() {
    this.getInvoiceList()
  },
  methods: {
    handleInvoiceOperate({ id, key }) {
      this.$router.push("/financial/invoiceOperate?id=" + id + "&key=" + key)
    },
    async getInvoiceList() {
      this.loading = true
      let params = {
        ...this.queryParams
      }
      const { data } = await loadInvApplicationList(params)
      this.loading = false
      this.total = data.total
      this.invoiceList = data.list
    },
    resetQuery() {
      this.queryParams = {
        page: 1,
        rows: 10,
        invoiceNo: "", //开票编号
        invoicingStatus: [], //开票状态
        orderNo: "", //订单号
        receiptNo: "", //收款单号
        tidanNo: "" //	提单号
      }
      this.getInvoiceList()
    },
    handleQuery() {
      this.queryParams.page = 1
      this.getInvoiceList()
    }
  }
}
</script>

<style lang="scss" scoped>
.card {
  padding: 20px;
}
.tableTop {
  margin-top: 20px;
}
</style>
