<template>
  <div class="plantform-subsite-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-form :inline="true" :model="queryForm" @submit.native.prevent>
          <el-form-item>
            <el-input
              v-model.trim="queryForm.subsite_name"
              clearable
              :placeholder="__('请输入分站名称')"
              style="width: 160px"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              {{ __('查询') }}
            </el-button>
            <el-button icon="el-icon-refresh" 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="__('操作')" 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="subsite_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('分站名称')"
        prop="subsite_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('区域城市名称')"
        prop="subsite_item_city_name"
        show-overflow-tooltip
      >
        <template #default="{ row }">
          <el-link type="primary" @click="handleTable(row)">
            {{ formatSubsiteItemCityName(row) }}
          </el-link>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('logo')"
        prop="subsite_logo"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-image
            v-show="scope.row.subsite_logo"
            :preview-src-list="[scope.row.subsite_logo]"
            :src="scope.row.subsite_logo"
            style="width: 35px; height: 35px"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :formatter="formatEnable"
        :label="__('是否开启')"
        prop="subsite_enable"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('SEO标题')"
        prop="subsite_title"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('SEO关键字')"
        prop="subsite_keywords"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('SEO描述')"
        prop="subsite_description"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('站点描述')"
        prop="subsite_des"
        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 { getList, doDelete } from '@/api/plantform/subsite'
  import Edit from './components/PlantformSubsiteEdit'
  import Item from './components/PlantformTransportEdit'

  export default {
    name: 'PlantformSubsite',
    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,
          subsite_name: '',
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      async handleTable(row) {
        let obj = {
          subsite_id: row.subsite_id,
          subsite_city_ids: row.subsite_city_ids,
          subsite_item_city_ids: row.subsite_item_city_ids,
        }
        this.$refs['item'].showEdit(obj)
      },
      formatSubsiteItemCityName(subsite_item_city_name) {
        let subsiteItemCityName = subsite_item_city_name.subsite_item_city_name
        if (!subsiteItemCityName) {
          return '请选择地区'
        }
        return this.handlerCityName(subsiteItemCityName)
      },
      handlerCityName(subsiteItemCityName) {
        let str = ''
        if (!subsiteItemCityName) {
          return str
        }
        let cityObjs = JSON.parse(subsiteItemCityName)
        cityObjs.forEach((cityObj) => {
          str += cityObj.provinces + '(' + cityObj.citys.toString() + ')' + ';'
        })
        return str
      },
      __,
      handleEdit(row) {
        if (row.subsite_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      formatEnable(row) {
        return row.subsite_enable === 1 ? this.__('启用') : this.__('禁用')
      },
      handleDelete(row) {
        if (row.subsite_id) {
          this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
            const { msg, status } = await doDelete({
              subsite_id: row.subsite_id,
            })
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            await this.fetchData()
          })
        } else {
          this.$baseMessage(this.__('未选中任何行'), 'error')
        }
      },
      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>
