<template>
  <el-dialog
    append-to-body
    :title="title"
    :visible.sync="dialogFormVisible"
    width="1100px"
    @close="close"
  >
    <vab-query-form>
      <vab-query-form-left-panel :span="12">
        <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.pai_enable"
              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>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              {{ __('查询') }}
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="12">
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="handleEdit(queryForm.pa_id)"
        >
          {{ __('添加') }}
        </el-button>
      </vab-query-form-right-panel>
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      border
      :data="list"
      height="500"
      width="60%"
      @cell-click="tabClick"
    >
      <el-table-column
        align="center"
        :label="__('店铺编号')"
        prop="store_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('SPU')"
        prop="product_id"
        show-overflow-tooltip
      />

      <el-table-column
        align="center"
        :label="__('商品编号')"
        prop="item_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('产品名称')"
        prop="product_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('活动价格')"
        prop="pai_unit_price"
      >
        <template slot-scope="scope">
          <span
            v-if="
              scope.row.pai_id === tabClickIndex && scope.column.id === columnId
            "
          >
            <el-input
              ref="editInput"
              v-model="scope.row.pai_unit_price"
              @blur.stop="inputBlur(scope.row)"
            />
          </span>
          <span v-else>{{ scope.row.pai_unit_price }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('商品分类')"
        prop="category_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatStarttime"
        :label="__('开始时间')"
        prop="pa_starttime"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        :formatter="formatEndtime"
        :label="__('结束时间')"
        prop="pa_endtime"
        show-overflow-tooltip
        width="160"
      />
      <el-table-column
        align="center"
        :formatter="formatEnable"
        :label="__('审核状态')"
        prop="pai_enable"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :formatter="formatPaiTime"
        :label="__('提报时间')"
        prop="pai_time"
        show-overflow-tooltip
        width="160"
      />
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <Table ref="Table" @fetch-data="refresh" />
  </el-dialog>
</template>
<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { renderTime } from '@/utils/date'
  import { doEdit, getList } from '@/api/plantform/activity/item'
  import Table from './productItemTable'

  export default {
    name: 'ActivityTable',
    components: { Table },
    data() {
      return {
        tabClickIndex: null, // 点击的行
        columnId: null, // 点击的列
        options: [
          {
            value: 1,
            label: this.__('待审核'),
          },
          {
            value: 3,
            label: this.__('审核失败'),
          },
          {
            value: 2,
            label: this.__('已审核'),
          },
        ],
        list: [],
        form: {},
        selectRows: [],
        formItem: {},
        listLoading: true,
        dialogFormVisible: false,
        title: '',
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        queryForm: {
          pageNum: 1,
          pageSize: 20,
        },
      }
    },
    created() {},
    methods: {
      __,
      formatPaiTime(row) {
        return renderTime(row.pai_time)
      },
      formatEnable(row) {
        return row.pai_enable === 0
          ? this.__('待审核')
          : row.pai_enable === 1
          ? this.__('已通过')
          : this.__('审核失败')
      },
      formatEndtime(row) {
        return renderTime(row.pa_endtime)
      },
      formatStarttime(row) {
        return renderTime(row.pa_starttime)
      },
      showTable(pa_id) {
        this.queryForm.pa_id = pa_id
        this.dialogFormVisible = true
        this.fetchData()
      },
      tabClick(row, column) {
        this.tabClickIndex = row.pai_id
        this.columnId = column.id
        // inout 获取焦点
        this.$nextTick(() => {
          if (!this.$refs['editInput']) return
          this.$refs['editInput'].focus()
        })
      },
      // 失去焦点初始化
      async inputBlur(row) {
        this.tabClickIndex = null
        this.columnId = null
        // 保存数据
        let obj = {
          pai_id: row.pai_id,
          pai_unit_price: row.pai_unit_price,
        }
        const { msg, status } = await doEdit(obj)
        this.$baseMessage(msg, 'success', false, 'vab-hey-message-success')
      },

      close() {
        this.queryForm = this.$options.data().queryForm
        this.dialogFormVisible = false
      },

      handleEdit(pa_id) {
        this.$refs['Table'].showTable(pa_id)
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      async refresh() {
        this.$emit('fetch-data')
        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>
