<template>
  <div class="store-info-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          {{ __('添加') }}
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <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="__('店铺编号')"
        prop="store_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('店铺名称')"
        prop="store_name"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('用户昵称')"
        prop="user_nickname"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('用户账号')"
        prop="user_account"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('注册所在地')"
        prop="store_register_region"
        show-overflow-tooltip
        width="140"
      />
      <el-table-column
        align="center"
        :label="__('配送区域')"
        prop="store_deliver_region"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('结算周期')"
        prop="store_settlement_cycle"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('店铺banner')"
        prop="store_banner"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('店铺幻灯片及幻灯片链接')"
        prop="store_slide"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('详细地址')"
        prop="store_address"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('工作时间')"
        prop="store_workingtime"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('卖家电话')"
        prop="store_tel"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('qq')"
        prop="store_qq"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('旺旺')"
        prop="store_ww"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('是否开启装修')"
        prop="store_is_renovation"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('店铺印章')"
        prop="store_stamp"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('免运费额度')"
        prop="store_free_shipping"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('店铺绑定模板')"
        prop="store_template"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('打印订单页面下方说明文字')"
        prop="store_print_desc"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('店铺资料信息状态')"
        prop="store_state_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('付款状态')"
        prop="store_payment_state"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('本次开始时间')"
        prop="store_start_time"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('有效期至')"
        prop="store_end_time"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('关闭原因')"
        prop="store_close_reason"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('积分')"
        prop="store_points"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('购买须知')"
        prop="store_notice"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('营业时间')"
        prop="store_opening_hours"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('打烊时间')"
        prop="store_close_hours"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('消费送积分比率')"
        prop="store_points_consume_rate"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('所属商圈')"
        prop="store_circle"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('线下买单折扣')"
        prop="store_discount"
        show-overflow-tooltip
      />
      <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>
    <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/store/info'
  import Edit from './components/StoreInfoEdit'

  export default {
    name: 'StoreInfo',
    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.id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        if (row.id) {
          this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
            const { msg, status } = await doDelete({ ids: row.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.id).join()
            this.$baseConfirm(
              this.__('你确定要删除选中项吗'),
              null,
              async () => {
                const { msg, status } = await doDelete({ 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>
