<template>
  <div class="page-base-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="4">
        <el-button icon="el-icon-plus" type="primary" @click="handleEdit">
          {{ __('添加') }}
        </el-button>
        <el-button
          v-if="false"
          icon="el-icon-delete"
          type="danger"
          @click="handleDelete"
        >
          {{ __('批量删除') }}
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="20">
        <el-form
          v-if="false"
          :inline="true"
          :model="queryForm"
          @submit.native.prevent
        >
          <el-form-item>
            <el-input
              v-model.trim="queryForm.page_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>
        <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"
      @selection-change="setSelectRows"
    >
      <el-table-column
        v-if="false"
        align="center"
        show-overflow-tooltip
        type="selection"
      />
      <el-table-column
        align="center"
        fixed="left"
        :label="__('操作')"
        width="150"
      >
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ __('编辑') }}
          </el-button>
          <el-button type="text" @click="handleDelete(row)">
            {{ __('删除') }}
          </el-button>
          <el-button type="text" @click="handleDiy(row)">
            {{ __('装修') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('页面编号')"
        prop="page_id"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :label="__('页面名称')"
        prop="page_name"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('所属店铺')"
        prop="store_id"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('所属用户')"
        prop="user_id"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('所属分站')"
        prop="subsite_id"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('类型')"
        prop="page_type"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('页面布局模板')"
        prop="page_tpl"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('所属APP')"
        prop="app_id"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('页面代码')"
        prop="page_code"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('')"
        prop="page_nav"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('')"
        prop="page_config"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('')"
        prop="page_share_title"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('')"
        prop="page_share_image"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('')"
        prop="page_qrcode"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('是否首页')"
        prop="page_index"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.page_index"

            :active-value="1"

            :inactive-value="0"
            @change="sChange(scope.row, 'page_index')"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('拼团首页')"
        prop="page_gb"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.page_gb"

            :active-value="1"

            :inactive-value="0"
            @change="sChange(scope.row, 'page_gb')"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('活动首页')"
        prop="page_activity"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.page_activity"

            :active-value="1"

            :inactive-value="0"
            @change="sChange(scope.row, 'page_activity')"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('积分首页')"
        prop="page_point"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.page_point"

            :active-value="1"

            :inactive-value="0"
            @change="sChange(scope.row, 'page_point')"
          />
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('团购首页')"
        prop="page_gbs"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.page_gbs"

            :active-value="1"

            :inactive-value="0"
            @change="sChange(scope.row, 'page_gbs')"
          />
        </template>
      </el-table-column>
      <el-table-column
        v-if="false"
        align="center"
        :label="__('组合套餐')"
        prop="page_package"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('批发团购首页')"
        prop="page_pfgb"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('社区')"
        prop="page_sns"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('资讯')"
        prop="page_article"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('零元购区')"
        prop="page_zerobuy"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('高额返区')"
        prop="page_higharea"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('今日爆款')"
        prop="page_taday"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('每日好店')"
        prop="page_everyday"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('整点秒杀')"
        prop="page_secondkill"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('天天秒淘')"
        prop="page_secondday"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('设置土特产')"
        prop="page_rura"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('用户页banner')"
        prop="page_likeyou"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('兑换专区')"
        prop="page_exchange"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('新品首发')"
        prop="page_new"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('新人优惠')"
        prop="page_newperson"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('升级VIP')"
        prop="page_upgrade"
        show-overflow-tooltip
      />
      <el-table-column
        v-if="false"
        align="center"
        :label="__('信息发布')"
        prop="page_message"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatBuildin"
        :label="__('是否内置')"
        prop="page_buildin"
        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" />
    <diy ref="diy" />
  </div>
</template>

<script>
  import { mapActions, mapGetters } from 'vuex'
  import { translateTitle as __ } from '@/utils/i18n'

  import { getList, doDelete } from '@/api/page/base'
  import Edit from './components/PageBaseEdit'
  import Diy from './components/PageBaseDiy'
  import { enable } from '@/api/page/base'

  export default {
    name: 'PageBase',
    components: { Edit, Diy },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          page_name: '',
          pageNum: 1,
          pageSize: 50,
          page_type: 2,
        },
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      __,
      formatBuildin(row) {
        return row.crontab_buildin === 0 ? this.__('否') : this.__('是')
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      handleEdit(row) {
        if (row.page_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        if (row.page_id) {
          this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
            const { msg, status } = await doDelete({ page_id: row.page_id })

            if (status == 200) {
              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.page_id).join()
            this.$baseConfirm(
              this.__('你确定要删除选中项吗'),
              null,
              async () => {
                const { msg, status } = await doDelete({ page_id: ids })

                if (status == 200) {
                  this.$baseMessage(msg, 'success')
                } else {
                  this.$baseMessage(msg, 'error')
                }
                await this.fetchData()
              }
            )
          } else {
            this.$baseMessage(this.__('未选中任何行'), 'error')
            return false
          }
        }
      },
      handleDiy(row) {
        if (row.page_id) {
          this.$refs['diy'].showDiy(row)
        } else {
          this.$refs['diy'].showDiy()
        }
      },
      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, enable_name) {
        let param = {
          page_id: row.page_id,
          enable_name: enable_name,
          enable_id: 'page_id',
        }

        param[enable_name] = row[enable_name]

        const { msg, status } = await enable(param)

        if (status == 200) {
          this.$baseMessage(msg, 'success')
        } else {
          this.$baseMessage(msg, 'error')
        }
      },
    },
  }
</script>
