<template>
  <div class="chain-base-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.chain_id"
              clearable
              :placeholder="__('请输入编号')"
            />
          </el-form-item>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.chain_name"
              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"
        fixed="left"
        :label="__('操作')"
        width="100"
      >
        <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"
        fixed="left"
        :label="__('门店编号')"
        prop="chain_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('管理员')"
        prop="user_nickname"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :label="__('管理员账号')"
        prop="user_account"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('门店名称')"
        prop="chain_name"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :label="__('手机号码')"
        prop="chain_mobile"
        show-overflow-tooltip
        width="120"
      />
      <el-table-column
        align="center"
        :label="__('联系人')"
        prop="chain_contacter"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :label="__('经度')"
        prop="chain_lng"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :label="__('纬度')"
        prop="chain_lat"
        show-overflow-tooltip
        width="100"
      />
      <el-table-column
        align="center"
        :label="__('地区')"
        prop="chain_district_info"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        :label="__('地区编号')"
        prop="chain_district_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('实体店铺详细地址')"
        prop="chain_address"
        show-overflow-tooltip
        width="140"
      />
      <el-table-column
        align="center"
        :label="__('营业时间')"
        prop="chain_opening_hours"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('关闭时间')"
        prop="chain_close_hours"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('门店图片')"
        prop="chain_img"
        show-overflow-tooltip
        width="100px"
      >
        <template slot-scope="scope">
          <el-image
            v-show="scope.row.chain_img"
            :preview-src-list="[scope.row.chain_img]"
            :src="scope.row.chain_img"
            style="width: 35px; height: 35px"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :formatter="formatTime"
        :label="__('添加时间')"
        prop="chain_time"
        show-overflow-tooltip
        width="160px"
      />
    </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/chain/base'
  import Edit from './components/ChainBaseEdit'
  import { renderTime } from '@/utils/date'

  export default {
    name: 'ChainBase',
    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,
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,

      handleEdit(row) {
        if (row.chain_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      formatTime(row) {
        const chain_time = row.chain_time
        return chain_time ? renderTime(chain_time) : ''
      },
      handleDelete(row) {
        this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
          const { msg, status } = await doDelete({ chain_id: row.chain_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>
