<template>
  <div class="base-product-unit-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="4">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          {{ __('添加') }}
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="20">
        <el-button icon="el-icon-refresh" type="primary" @click="queryData">
          {{ __('刷新') }}
        </el-button>
      </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="handleEdit(row)">
            {{ __('编辑') }}
          </el-button>
          <el-button
            :disabled="row.unit_is_buildin"
            type="text"
            @click="handleDelete(row)"
          >
            {{ __('删除') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('商品计量单位编号')"
        prop="unit_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('商品计量单位父编号')"
        prop="parent_unit_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('商品计量单位名称')"
        prop="unit_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('商品计量单位注释')"
        prop="unit_remark"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('换算关系')"
        prop="unit_rate"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('gu编号')"
        prop="unit_guid"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('基本单位')"
        prop="unit_is_base"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('商品计量单位分类')"
        prop="unit_type_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatBuildin"
        :label="__('系统内置')"
        prop="unit_is_buildin"
        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 { doDelete, getList } from '@/api/base/product/unit'
  import Edit from './components/BaseProductUnitEdit'
  import VabQueryFormRightPanel from '@/vab/components/VabQueryForm/components/VabQueryFormRightPanel'

  export default {
    name: 'BaseProductUnit',
    components: { VabQueryFormRightPanel, 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: {
      formatBuildin(row) {
        return row.unit_is_buildin ? this.__('是') : this.__('否')
      },
      __,

      handleEdit(row) {
        if (row.unit_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        if (row.unit_id) {
          this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
            const { msg, status } = await doDelete({ unit_ids: row.unit_id })
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            await this.fetchData()
          })
        } else {
          if (this.selectRows.length > 0) {
            const ids = this.selectRows.map((item) => item.unit_id).join()
            this.$baseConfirm(
              this.__('你确定要删除选中项吗'),
              null,
              async () => {
                const { msg, status } = await doDelete({ unit_ids: ids })
                if (200 == status) {
                  this.$baseMessage(msg, 'success')
                } else {
                  this.$baseMessage(msg, 'error')
                }
                await this.fetchData()
              }
            )
          } else {
            this.$baseMessage(this.__('未选中任何行'), 'error')
            return false
          }
        }
      },
      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.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
