<template>
  <div class="supplier-invoice-list">
    <template v-if="!detailVisible">
      <el-form :inline="true" :model="dataForm" @keyup.enter.native="search()">
        <el-form-item label="发票号">
          <el-input v-model="dataForm.invoiceNumber" placeholder="发票号" clearable/>
        </el-form-item>
        <el-form-item label="开票日期">
          <el-date-picker
            v-model="dataForm.createTime"
            type="date"
            value-format="yyyy-MM-dd">
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button @click="search()">查询</el-button>
        </el-form-item>
      </el-form>
      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        style="width: 100%;">
        <el-table-column
          label="序号"
          type="index"
          width="50"
          align="center">
        </el-table-column>
        <el-table-column
          prop="code"
          header-align="center"
          align="center"
          min-width="120"
          :show-tooltip-when-overflow="true"
          label="发票编码">
        </el-table-column>
        <el-table-column
          prop="type"
          :formatter="typeFormat"
          header-align="center"
          align="center"
          label="发票类型">
        </el-table-column>
        <el-table-column
          prop="createTime"
          header-align="center"
          align="center"
          min-width="120"
          label="开票日期">
        </el-table-column>
        <el-table-column
          prop="typeCode"
          header-align="center"
          align="center"
          min-width="120"
          label="发票类别代码">
        </el-table-column>
        <el-table-column
          prop="invoiceNumber"
          header-align="center"
          align="center"
          min-width="120"
          label="发票号">
        </el-table-column>
        <el-table-column
          prop="creatorName"
          header-align="center"
          align="center"
          min-width="120"
          label="开票人">
        </el-table-column>
        <el-table-column
          prop="invoiceAmount"
          header-align="center"
          align="center"
          min-width="120"
          label="发票金额">
        </el-table-column>
        <el-table-column
          prop="notes"
          header-align="center"
          align="center"
          min-width="180"
          :show-tooltip-when-overflow="true"
          label="备注">
        </el-table-column>
        <el-table-column
          fixed="right"
          header-align="center"
          align="center"
          width="150"
          label="操作">
          <template slot-scope="scope">
            <el-button v-if="true" type="text" size="small" @click="detailHandle(scope.row.purchaseInvoiceId)">详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
    </template>
    <!-- 弹窗, 新增 / 修改 -->
    <!-- <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @onChose="onClose"/> -->
    <detail v-if="detailVisible" ref="detail" @onChose="onClose"/>
  </div>
</template>

<script>
  import { getSupplierInvoiceInfo } from '@/api/finance'
  import Detail from './purchase-invoice-detail'
  export default {
    name: 'supplier-invoice-list',
    components: {
      Detail
    },
    props: {
      supplierId: {
        type: [Number, String],
        default: 0
      }
    },
    data () {
      return {
        detailVisible: false,
        addOrUpdateVisible: false,
        dataForm: {
          contractNumber: ''
        },
        options: [],
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        typeState: [ // 发票类型映射关系
          {
            code: '1',
            value: '增值税专用发票'
          },
          {
            code: '2',
            value: '增值税普通发票'
          },
          {
            code: '3',
            value: '形式发票'
          }
        ]
      }
    },
    created () {
      this.getDataList()
    },
    methods: {
      onClose () {
        console.log(3333)
        this.addOrUpdateVisible = false
        this.detailVisible = false
        this.$emit('changeDetailShow', false)
      },
      // 查询
      search () {
        this.pageIndex = 1
        this.getDataList()
      },
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        let params = {
          'current': this.pageIndex,
          'size': this.pageSize,
          'supplierId': this.supplierId,
          'invoiceNumber': this.dataForm.invoiceNumber,
          'createTime': this.dataForm.createTime
        }
        getSupplierInvoiceInfo(params).then(({data}) => {
          if (data && data.code === '200') {
            this.dataList = data.data.records
            this.totalPage = Number(data.data.total)
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 转换属性“级别”
      typeFormat (row) {
        for (let i = 0; i < this.typeState.length; i++) {
          if (this.typeState[i].code === String(row.type)) {
            return this.typeState[i].value
          }
        }
      },
      detailHandle (id) {
        this.detailVisible = true
        this.$nextTick(() => {
          this.$refs.detail.init(id)
        })
      },
      // 新增/修改
      addOrUpdateHandle (id, customerAccount, disable) {
        console.log(customerAccount)
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id, customerAccount, this.customerId, disable)
        })
        this.$emit('changeDetailShow', true)
      }
    }
  }
</script>

<style scoped>

</style>
