<template>
  <div v-loading="loading">
    <div class="white-box">
      <el-tabs v-model="filterStatus" @tab-click="handleFilterStatusClick">
        <el-tab-pane label="全部" name="-1" :lazy="true"></el-tab-pane>
        <el-tab-pane label="待审核" name="0" :lazy="true"></el-tab-pane>
        <el-tab-pane label="审核通过" name="1" :lazy="true"></el-tab-pane>
        <el-tab-pane label="审核拒绝" name="3" :lazy="true"></el-tab-pane>
      </el-tabs>
      <div class="filter-box">
        <filter-user :filter-types.sync="filterTypes" @select-value="handleFilterUser"></filter-user>
      </div>
      <el-table :data="tableData" stripe style="width: 100%;" @selection-change="handleSelectionChange" :height="tool.getTableHeight(true)">
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column label="发票代码" width="150">
          <template slot-scope="scope">
            {{scope.row.INVOICE_CODE}}
          </template>
        </el-table-column>
        <el-table-column label="发票号码" width="140">
          <template slot-scope="scope">
            <el-tag type="" size="small" class="no-border">
              {{scope.row.INVOICE_NUM}}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="提现流水号" width="220">
          <template slot-scope="scope">
            <el-tag type="warning" size="small" class="no-border">
              {{scope.row.WITHDRAW_SN}}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="开票日期" width="150">
          <template slot-scope="scope">
            {{scope.row.INVOICE_DATE}}
          </template>
        </el-table-column>
        <el-table-column label="交易金额" width="110">
          <template slot-scope="scope">
            {{scope.row.AMOUNT}}
          </template>
        </el-table-column>
        <el-table-column label="税率">
          <template slot-scope="scope">
            {{scope.row.TAX_RATE}}
          </template>
        </el-table-column>
        <el-table-column label="购买方名称" width="250">
          <template slot-scope="scope">
            {{scope.row.PURCHASER_NAME}}
          </template>
        </el-table-column>
        <el-table-column label="购买方纳税人识别号" width="160">
          <template slot-scope="scope">
            {{scope.row.PURCHASER_REGISTER_NUM}}
          </template>
        </el-table-column>
        <el-table-column label="购买方地址电话" width="300">
          <template slot-scope="scope">
            {{scope.row.PURCHASER_ADDRESS}}
          </template>
        </el-table-column>
        <el-table-column label="购买方开户行及账号" width="350">
          <template slot-scope="scope">
            {{scope.row.PURCHASER_BANK}}
          </template>
        </el-table-column>
        <el-table-column label="销售方名称" width="250">
          <template slot-scope="scope">
            {{scope.row.SELLER_NAME}}
          </template>
        </el-table-column>
        <el-table-column label="销售方纳税人识别号" width="160">
          <template slot-scope="scope">
            {{scope.row.SELLER_REGISTER_NUM}}
          </template>
        </el-table-column>
        <el-table-column label="销售方地址电话" width="350">
          <template slot-scope="scope">
            {{scope.row.SELLER_ADDRESS}}
          </template>
        </el-table-column>
        <el-table-column label="销售方开户行及账号" width="350">
          <template slot-scope="scope">
            {{scope.row.SELLER_BANK}}
          </template>
        </el-table-column>
        <el-table-column label="货物或应税劳务服务名称" width="180">
          <template slot-scope="scope">
            {{scope.row.ITEM_NAME}}
          </template>
        </el-table-column>
        <el-table-column label="发票备注" width="350">
          <template slot-scope="scope">
            {{scope.row.INVOICE_REMARK}}
          </template>
        </el-table-column>
        <el-table-column label="发票图片" width="120">
          <template slot-scope="scope">
            <el-button type="success" size="small" @click="handleInvoiceShow(scope.row)"
                       icon="el-icon-view" v-if="scope.row.INVOICE_URL">查看发票
            </el-button>
          </template>
        </el-table-column>
        <el-table-column label="审核状态" width="100">
          <template slot-scope="scope">
            <el-tag :type="tool.statusType(scope.row.AUDIT_STATUS)">{{scope.row.STATUS_NAME}}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="会员编号" width="150">
          <template slot-scope="scope">
            {{scope.row.BASE_INFO.USER_NAME}}
          </template>
        </el-table-column>
        <el-table-column label="会员姓名" width="110">
          <template slot-scope="scope">
            {{scope.row.BASE_INFO.REAL_NAME}}
          </template>
        </el-table-column>
        <el-table-column label="会员状态" width="110">
          <template slot-scope="scope">
            {{(scope.row.BASE_INFO.STATUS_NAME)}}
          </template>
        </el-table-column>
        <el-table-column label="身份证号" width="170">
          <template slot-scope="scope">
            {{scope.row.BASE_INFO.ID_CARD}}
          </template>
        </el-table-column>
        <el-table-column label="应注册类型" width="100">
          <template slot-scope="scope">
            {{scope.row.BASE_INFO.SHOULD_REG_TYPE?regTypes[scope.row.BASE_INFO.SHOULD_REG_TYPE].TYPE_NAME:''}}
          </template>
        </el-table-column>
        <el-table-column label="实时注册类型" width="110">
          <template slot-scope="scope">
            {{regTypes[scope.row.BASE_INFO.REG_TYPE].TYPE_NAME}}
          </template>
        </el-table-column>
        <el-table-column label="注册名称" width="160">
          <template slot-scope="scope">
            {{scope.row.BASE_INFO.REG_NAME}}
          </template>
        </el-table-column>
        <el-table-column label="创建时间" width="170">
          <template slot-scope="scope">
            {{tool.formatDate(scope.row.CREATED_AT)}}
          </template>
        </el-table-column>
        <el-table-column label="创建人" width="150">
          <template slot-scope="scope">
            {{scope.row.CREATE_ADMIN_NAME}}
          </template>
        </el-table-column>
        <el-table-column label="审核时间" width="170">
          <template slot-scope="scope">
            {{tool.formatDate(scope.row.AUDITED_AT)}}
          </template>
        </el-table-column>
        <el-table-column label="审核人" width="150">
          <template slot-scope="scope">
            {{scope.row.AUDIT_ADMIN_NAME}}
          </template>
        </el-table-column>
        <el-table-column label="备注" width="150">
          <template slot-scope="scope">
            {{scope.row.CREATE_REMARK}}
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="180">
          <template slot-scope="scope" v-if="scope.row.AUDIT_STATUS==='0' && (permission.hasPermission(`finance/invoice-audit-audit`)||permission.hasPermission(`finance/invoice-audit-delete`))">
            <el-dropdown size="small" trigger="click">
              <el-button type="primary" size="small" @click.stop="">
                Operate on this data<i class="el-icon-arrow-down el-icon--right"></i>
              </el-button>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="audit" @click.native="handleAuditShow(scope.row)"
                                  v-if="scope.row.AUDIT_STATUS==='0' && permission.hasPermission(`finance/invoice-audit-audit`)">审核数据
                </el-dropdown-item>
                <el-dropdown-item command="del" @click.native="handleDel(scope.row.ID)"
                                  v-if="scope.row.AUDIT_STATUS==='0' && permission.hasPermission(`finance/invoice-audit-delete`)">Delete Data
                </el-dropdown-item>
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
      <div class="white-box-footer">
        <pagination :total="totalCount" :page_size="pageSize" @size-change="handleSizeChange" @current-change="handleCurrentChange"></pagination>
      </div>
    </div>
    <el-dialog title="审核发票信息" :visible.sync="dialogAuditFormVisible">
      <el-form :model="invoiceForm" label-width="200px" style="width:600px;" v-loading="dialogAuditLoading">
        <el-form-item label="提现流水号">
          <el-input v-model="invoiceForm.withdrawSn" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="发票代码">
          <el-input v-model="invoiceForm.invoiceCode"></el-input>
        </el-form-item>
        <el-form-item label="发票号码">
          <el-input v-model="invoiceForm.invoiceNum"></el-input>
        </el-form-item>
        <el-form-item label="开票日期">
          <el-date-picker
              v-model="invoiceForm.invoiceDate"
              type="date"
              placeholder="Select date"
              value-format="yyyy-MM-dd"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="提现金额">
          <el-input v-model="invoiceForm.amount"></el-input>
        </el-form-item>
        <el-form-item label="税率">
          <el-input v-model="invoiceForm.taxRate"></el-input>
        </el-form-item>
        <el-form-item label="购买方名称">
          <el-input v-model="invoiceForm.purchaserName"></el-input>
        </el-form-item>
        <el-form-item label="购买方纳税人识别号">
          <el-input v-model="invoiceForm.purchaserRegisterNum"></el-input>
        </el-form-item>
        <el-form-item label="购买方地址电话">
          <el-input v-model="invoiceForm.purchaserAddress"></el-input>
        </el-form-item>
        <el-form-item label="购买方开户行及账号">
          <el-input v-model="invoiceForm.purchaserBank"></el-input>
        </el-form-item>
        <el-form-item label="销售方名称">
          <el-input v-model="invoiceForm.sellerName"></el-input>
        </el-form-item>
        <el-form-item label="销售方纳税人识别号">
          <el-input v-model="invoiceForm.sellerRegisterNum"></el-input>
        </el-form-item>
        <el-form-item label="销售方地址电话">
          <el-input v-model="invoiceForm.sellerAddress"></el-input>
        </el-form-item>
        <el-form-item label="销售方开户行及账号">
          <el-input v-model="invoiceForm.sellerBank"></el-input>
        </el-form-item>
        <el-form-item label="货物或应税劳务服务名称">
          <el-input v-model="invoiceForm.itemName"></el-input>
        </el-form-item>
        <el-form-item label="发票备注">
          <el-input v-model="invoiceForm.invoiceRemark" type="textarea"
                    :rows="2"></el-input>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="invoiceForm.createRemark"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogAuditFormVisible = false">Cancel<!-- 取 消 --></el-button>
        <el-button type="danger" @click.native="handleRejectAudit">Reject<!-- 拒 绝 --></el-button>
        <el-button type="primary" @click.native="handleAudit">Approve<!-- 通 过 --></el-button>
      </div>
    </el-dialog>
    <el-dialog title="查看发票图片" :visible.sync="dialogInvoiceVisible">
      <a :href="invoiceUrl" target="_blank"><img :src="invoiceUrl" alt="" style="max-width: 100%"></a>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogInvoiceVisible = false">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import Vue from 'vue'
  import network from '@/utils/network'
  import tool from '@/utils/tool'
  import store from '@/utils/vuexStore'
  import FilterUser from '@/components/FilterUser'
  import baseInfo from '@/utils/baseInfo'
  import permission from '@/utils/permission'
  import Pagination from '@/components/Pagination'
  import filterHelper from '../../utils/filterHelper'

  export default {
    name: 'finance_invoice-audit',
    components: {FilterUser,Pagination},
    mounted() {
      store.state.socket.onMessageCallback = this.onMessageCallback
      this.getData()
    },
    data() {
      return {
        allData: null,
        tableData: null,
        loading: true,
        dialogEditLoading: false,
        dialogAuditLoading: false,
        multipleSelection: [],
        currentPage: 1,
        totalPages: 1,
        totalCount: 1,
        pageSize: 20,
        tool: tool,
        permission: permission,
        filterTypes: {
          'createdAt': {isUserTable: false, name: '创建时间', other: 'date'},
          'INVOICE_CODE': {isUserTable: false, name: '发票代码'},
          'INVOICE_NUM': {isUserTable: false, name: '发票号码'},
          'INVOICE_DATE': {isUserTable: false, name: '开票日期'},
          'AMOUNT': {isUserTable: false, name: '交易金额'},
          'SN': {isUserTable: false, name: '提现流水号'},
        },
        filterModel: {},
        dialogEditFormVisible: false,
        dialogAuditFormVisible: false,
        dialogInvoiceVisible: false,
        invoiceUrl: '',
        regTypes: baseInfo.regTypes(),
        auditId: null,
        accountType: null,
        loadingInstance: null,
        invoiceForm: {
          id: null,
          withdrawId: null,
          withdrawSn: null,
          invoiceCode: null,
          invoiceNum: null,
          invoiceDate: null,
          amount: null,
          taxRate: null,
          purchaserName: null,
          purchaserRegisterNum: null,
          purchaserAddress: null,
          purchaserBank: null,
          sellerName: null,
          sellerRegisterNum: null,
          sellerAddress: null,
          sellerBank: null,
          itemName: null,
          invoiceRemark: null,
          createRemark: null,
        },
        filterStatus: '0',
      }
    },
    methods: {
      handleSelectionChange(val) {
        this.multipleSelection = val
      },
      handleCurrentChange(page) {
        this.getData(page, this.pageSize)
      },
      handleSizeChange(pageSize) {
        this.getData(this.currentPage, pageSize)
      },
      handleInvoiceShow(row) {
        this.dialogInvoiceVisible = true
        this.invoiceUrl = row.INVOICE_URL
      },
      handleAuditShow(row) {
        this.dialogAuditLoading = true
        this.auditId = row.ID
        this.dialogAuditFormVisible = true
        let vueObj = this
        network.getData('finance/invoice-audit-get', {id: this.auditId}).then(response => {
          vueObj.dialogAuditLoading = false
          vueObj.invoiceForm = response
        })
      },
      handleAudit() {
        this.$confirm('审核该条发票数据，对应的提现记录如果满足自动流转条件，则会标记为"提现已审核"。', 'Hint', {
          confirmButtonText: 'confirm', // 确定
          cancelButtonText: 'cancel', // 取消
          type: 'warning'
        }).then(() => {
          this.dialogAuditFormVisible = false
          this.$message({
            message: '正在通过审核',
            type: 'info'
          })
          this.invoiceForm.auditStatus = 'true'
          let path = 'finance/invoice-audit-audit'
          network.postData(path, this.invoiceForm).then(response => {
            this.$message({
              message: response,
              type: 'success'
            })
            this.getData(this.currentPage, this.pageSize)
          }).catch(response => {
          })
        }).then(response => {

        }).catch(response => {

        })
      },
      handleRejectAudit() {
        this.$confirm('拒绝审核该条发票数据，同时将对应的提现记录标记为未传发票？', 'Hint', {
          confirmButtonText: 'confirm', // 确定
          cancelButtonText: 'cancel', // 取消
          type: 'warning'
        }).then(() => {
          this.dialogAuditFormVisible = false
          this.$message({
            message: '正在拒绝审核',
            type: 'info'
          })
          this.invoiceForm.auditStatus = 'reject'
          let path = 'finance/invoice-audit-audit'
          network.postData(path, this.invoiceForm).then(response => {
            this.$message({
              message: response,
              type: 'success'
            })
            this.getData(this.currentPage, this.pageSize)
          }).catch(response => {
          })
        }).then(response => {

        }).catch(response => {

        })
      },
      handleDel(id = null) {
        let obj = this
        this.$confirm('删除该条发票数据，同时将对应的提现记录标记为未传发票？', 'Hint', {
          confirmButtonText: 'confirm', // 确定
          cancelButtonText: 'cancel', // 取消
          type: 'warning'
        }).then(() => {
          let selectedIds = []
          if (id === null) {
            for (let val of obj.multipleSelection) {
              selectedIds.push(val.ID)
            }
          } else {
            selectedIds.push(id)
          }
          return network.postData(`finance/invoice-audit-delete`, {
            selected: selectedIds
          })
        }).then(response => {
          this.$message({
            message: response,
            type: 'success'
          })
          obj.getData(obj.currentPage, obj.pageSize)
        }).catch(response => {

        })
      },
      handleFilterStatusClick(tab, event) {
        this.getData()
      },
      handleFilterUser(filterData) {
        filterHelper.handleFilterUser(this, filterData)
      },
      handleFilter() {
        this.getData()
      },
      getData(page, pageSize) {
        let filterData = this.filterModel
        filterData.filterStatus = this.filterStatus != '-1' ? `=,${this.filterStatus}` : ''
        let vueObj = this
        network.getPageData(this, 'finance/invoice-audit', page, pageSize, filterData, function (response) {
          vueObj.allData = response
        })
      },
      onMessageCallback() {
        this.getData(this.currentPage, this.pageSize)
      },
    }
  }

</script>

<style scoped>

</style>
