<template>
  <div class="store-express-logistics-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 type="text" @click="handleDelete(row)">
            {{ __('删除') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('物流编号')"
        prop="logistics_id"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :label="__('物流名称')"
        prop="logistics_name"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :label="__('物流公司编号')"
        prop="express_id"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('快递公司')"
        prop="express_name"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :label="__('联系电话')"
        prop="logistics_mobile"
        show-overflow-tooltip
      />

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

      <el-table-column
        align="center"
        :label="__('联系地址')"
        prop="logistics_address"
        show-overflow-tooltip
      />

      <el-table-column :label="__('是否默认')" prop="logistics_is_default">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.logistics_is_default"

            :active-value="1"

            :inactive-value="0"
            @change="sChange(scope.row)"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('是否启用')"
        prop="logistics_is_enable"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.logistics_is_enable"

            :active-value="1"

            :inactive-value="0"
            @change="sChangeLogistics(scope.row)"
          />
        </template>
      </el-table-column>
    </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, enable, getList } from '@/api/store/express/logistics'
  import Edit from './components/StoreExpressLogisticsEdit'

  export default {
    name: 'StoreExpressLogistics',
    components: { Edit },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        queryForm: {
          pageNum: 1,
          pageSize: 20,
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      handleEdit(row) {
        if (row.logistics_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },

      handleDelete(row) {
        if (row.logistics_id) {
          this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
            const { msg, status } = await doDelete({
              logistics_ids: row.logistics_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
      },
      async sChange(row) {
        const param = {
          logistics_id: row.logistics_id,
          logistics_is_default: row.logistics_is_default,
          enable_name: 'logistics_is_enable',
        }
        const { msg, status } = await enable(param)
        this.$baseMessage(msg, 'success', false, 'vab-hey-message-success')
        this.fetchData()
      },

      async sChangeLogistics(row) {
        const param = {
          logistics_id: row.logistics_id,
          logistics_is_enable: row.logistics_is_enable,
          enable_name: 'logistics_is_enable',
        }
        const { msg, status } = await enable(param)
        this.$baseMessage(msg, 'success', false, 'vab-hey-message-success')
        this.fetchData()
      },
    },
  }
</script>
