<template>
  <div class="stock-bill-container">
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.title"
              clearable
              :placeholder="__('请输入标题')"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              {{ __('查询') }}
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
    >
      <el-table-column align="center" :label="__('操作')" width="120">
        <template #default="{ row }">
          <el-button type="text" @click="handlExamine(row)">
            {{ __('编辑') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('购货')"
        prop="stock_bill_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('购货')"
        prop="stock_bill_number"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatChcked"
        :label="__('是否审核')"
        prop="stock_bill_checked"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('单据日期')"
        prop="stock_bill_date"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('更新时间')"
        prop="stock_bill_modify_time"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('创建时间')"
        prop="stock_bill_time"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('业务类别')"
        prop="bill_type_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('库存类型')"
        prop="stock_transport_type_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('所属店铺')"
        prop="store_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('所属仓库')"
        prop="warehouse_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('源单号码')"
        prop="order_id"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :label="__('经办人')"
        prop="employee_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('制单人')"
        prop="admin_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('其它金额')"
        prop="stock_bill_other_money"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('订单金额')"
        prop="stock_bill_amount"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('是否有效')"
        prop="stock_bill_enable"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('关联编号')"
        prop="stock_bill_src_id"
        show-overflow-tooltip
      />
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
    <Table ref="Table" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'

  import { BillList } from '@/api/stock/bill'
  import Edit from './components/StockBillEdit'
  import Table from './components/StockTable'

  export default {
    name: 'StockBill',
    components: { Edit, Table },
    data() {
      return {
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNo: 1,
          pageSize: 20,
          bill_type_id: 2750,
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,

      handlExamine(row) {
        this.$refs['Table'].handlExamine(row)
      },
      formatChcked(row) {
        return row.stock_bill_checked === 1 ? this.__('是') : this.__('否')
      },
      handleEdit(row) {
        if (row.id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },

      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNo = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNo = 1
        this.fetchData()
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await BillList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
