<template>
  <div class="supplier-base-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="24">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          {{ __('添加') }}
        </el-button>
      </vab-query-form-left-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="handleEdit(row)">
            {{ __('编辑') }}
          </el-button>
          <el-button type="text" @click="handleDelete(row)">
            {{ __('删除') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('供应商名称')"
        prop="supplier_name"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('供应商名称')"
        prop="supplier_name"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('供应商类别')"
        prop="supplier_type_id"
        show-overflow-tooltip
        width="120"
      />

      <el-table-column
        align="center"
        :label="__('增值税税率')"
        prop="supplier_tax_rate"
        show-overflow-tooltip
        width="120"
      />

      <el-table-column
        align="center"
        :label="__('备注消息')"
        prop="supplier_remark"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :label="__('开户银行')"
        prop="supplier_bank"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('银行账号')"
        prop="supplier_card"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('开户名称')"
        prop="supplier_account_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('纳税人识别号')"
        prop="supplier_taxnum"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('发票抬头')"
        prop="supplier_invoice"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('联系人')"
        prop="supplier_contacter_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('联系人Im/MSN/QQ')"
        prop="supplier_contacter_im"
        show-overflow-tooltip
        width="160"
      />

      <el-table-column
        align="center"
        :label="__('联系人座机')"
        prop="supplier_contacter_telephone"
        show-overflow-tooltip
        width="120"
      />

      <el-table-column
        align="center"
        :label="__('联系人区域')"
        prop="supplier_contacter_district_ids"
        show-overflow-tooltip
        width="120"
      />

      <el-table-column
        align="center"
        :label="__('联系地址')"
        prop="supplier_contacter_address"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('邮编')"
        prop="supplier_contacter_code"
        show-overflow-tooltip
      />
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="edit" @fetch-data="fetchData" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { getList, doDelete } from '@/api/supplier/base'
  import Edit from './components/SupplierBaseEdit'

  export default {
    name: 'SupplierBase',
    components: { Edit },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          title: '',
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,

      handleEdit(row) {
        if (row.supplier_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        console.log(row)

        this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
          const { msg } = await doDelete({
            supplier_id: row.supplier_id,
            supplier_contacter_id: row.supplier_contacter_id,
          })
          if (200 == status) {
            this.$baseMessage(msg, 'success')
          } else {
            this.$baseMessage(msg, 'error')
          }
          await this.fetchData()
        })
      },

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