<template>
  <div class="plantform-activity-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.pa_name"
              clearable
              :placeholder="__('请输入标题')"
            />
          </el-form-item>
          <el-form-item>
            <el-select
              v-model="queryForm.pa_state"
              clearable
              :placeholder="__('请选择')"
            >
              <el-option
                v-for="item in options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item>
            <div class="block">
              <el-date-picker
                v-model="queryForm.pa_starttime"
                :placeholder="__('开始日期')"
                type="date"
                value-format="yyyy-MM-dd HH:mm:ss"
              />
            </div>
          </el-form-item>
          <el-form-item>
            <div class="block">
              <el-date-picker
                v-model="queryForm.pa_endtime"
                :placeholder="__('结束日期')"
                type="date"
                value-format="yyyy-MM-dd HH:mm:ss"
              />
            </div>
          </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="__('操作')" width="125">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">
            {{ __('编辑') }}
          </el-button>
          <el-button type="text" @click="showTable(row)">
            {{ __('查看') }}
          </el-button>
          <el-button type="text" @click="handleDelete(row)">
            {{ __('删除') }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        :label="__('活动编号')"
        prop="pa_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('活动名称')"
        prop="pa_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('活动标题')"
        prop="pa_title"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatStarttime"
        :label="__('开始时间')"
        prop="pa_starttime"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatEndtime"
        :label="__('结束时间')"
        prop="pa_endtime"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatState"
        :label="__('活动状态')"
        prop="pa_state"
        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" />
    <Table ref="Table" />
  </div>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'

  import { doDelete, getList } from '@/api/plantform/activity'
  import { renderTime } from '@/utils/date'
  import Edit from './components/PlantformActivityEdit'
  import Table from './components/ActivityItemTable'

  export default {
    name: 'PlantformActivity',
    components: { Edit, Table },
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        options: [
          {
            value: 0,
            label: this.__('待审核'),
          },
          {
            value: 1,
            label: this.__('审核成功'),
          },
          {
            value: 2,
            label: this.__('未通过审核'),
          },
        ],
        value: '',
        list: [],
        items: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          pa_starttime: '',
        },
      }
    },
    created() {
      let query = this.$route.query
      if (query.pa_state || query.pa_state === 0) {
        this.queryForm.pa_state = Number(query.pa_state)
      }
      this.queryData()
    },
    methods: {
      __,
      formatStarttime(row) {
        return renderTime(row.pa_starttime)
      },
      formatEndtime(row) {
        return renderTime(row.pa_endtime)
      },
      showTable(row) {
        this.$refs['Table'].showTable(row.pa_id)
      },
      handleEdit(row) {
        if (row.pa_id) {
          this.$refs['edit'].showEdit(row)
        } else {
          this.$refs['edit'].showEdit()
        }
      },
      handleDelete(row) {
        this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
          const { msg, status } = await doDelete({ pa_id: row.pa_id })
          if (200 == status) {
            this.$baseMessage(msg, 'success')
          } else {
            this.$baseMessage(msg, 'error')
          }
          await this.fetchData()
        })
      },

      formatState(row) {
        return row.pa_state === 0
          ? this.__('未开启')
          : row.pa_state === 1
          ? this.__('正常')
          : row.pa_state === 2
          ? this.__('已结束')
          : this.__('管理员关闭')
      },

      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>
