<template>
  <div class="store-transport-type-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="150px">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ __('编辑') }}
          </el-button>
          <el-button type="text" @click="handleDelete(row)">
            {{ __('删除') }}
          </el-button>
          <el-button
            :disabled="row.transport_type_free === 1"
            type="text"
            @click="showItems(row)"
          >
            {{ __('查看') }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        :label="__('模板名称')"
        prop="transport_type_name"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :formatter="formatRule"
        :label="__('计费规则')"
        prop="transport_type_pricing_method"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :formatter="formatId"
        :label="__('最后编辑时间')"
        prop="transport_type_time"
        show-overflow-tooltip
      />

      >
      <el-table-column
        align="center"
        :label="__('免运费额度')"
        prop="transport_type_freight_free"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :formatter="formatEnable"
        :label="__('全免运费')"
        prop="transport_type_free"
        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" />
    <item ref="item" @fetch-data="fetchData" />
  </div>
</template>

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

  import { doDelete, getList } from '@/api/store/transport/type'
  import Edit from './components/StoreTransportTypeEdit'
  import Item from './components/StoreTransportItem'
  import { renderTime } from '@/utils/date'

  export default {
    name: 'StoreTransportType',
    components: { Edit, Item },
    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,
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      formatId(row) {
        return renderTime(row.transport_type_time)
      },
      formatEnable(row) {
        return row.transport_type_free
          ? this.__('全免（不限制地区且免运费')
          : this.__('不全免')
      },
      formatRule(row) {
        return row.transport_type_pricing_method === 1
          ? this.__('按件数')
          : row.transport_type_pricing_method === 2
          ? this.__('按重量')
          : this.__('按体积')
      },

      handleEdit(row) {
        if (row.transport_type_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      showItems(row) {
        this.$refs['item'].showItems(row)
      },
      handleDelete(row) {
        this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
          const { msg, status } = await doDelete({
            transport_type_id: row.transport_type_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.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
