<template>
  <el-dialog
    append-to-body
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="1000px"
    @close="close"
  >
    <vab-query-form>
      <vab-query-form-left-panel :span="24">
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="handleEdit(queryForm.activity_id, '', queryForm.total_id)"
        >
          {{ __('新增') }}
        </el-button>
      </vab-query-form-left-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="__('操作')" width="125">
        <template #default="{ row }">
          <el-button type="text" @click="handleDelete(row)">
            {{ __('删除') }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('商品编号')"
        prop="item_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('商品名称')"
        prop="product_item_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('价格')"
        prop="price"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span
            v-if="
              scope.row.total_id === tabClickIndex &&
              scope.column.id === columnId
            "
          >
            <el-input
              ref="editInput"
              v-model="scope.row.price"
              @blur.stop="inputBlur(scope.row)"
            />
          </span>
          <span v-else>{{ scope.row.price }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('数量')"
        prop="quantity"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span
            v-if="
              scope.row.total_id === tabClickIndex &&
              scope.column.id === columnId
            "
          >
            <el-input
              ref="editInput"
              v-model="scope.row.quantity"
              @blur.stop="inputBlur(scope.row)"
            />
          </span>
          <span v-else>{{ scope.row.quantity }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('商品图片')"
        prop="product_image"
        show-overflow-tooltip
        width="150px"
      >
        <template slot-scope="scope">
          <el-image
            v-show="scope.row.product_image"
            :preview-src-list="[scope.row.product_image]"
            :src="scope.row.product_image"
            style="width: 35px; height: 35px"
          />
        </template>
      </el-table-column>
    </el-table>
    <TableEdit ref="TableEdit" @fetch-data="refresh" />
  </el-dialog>
</template>
<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import {
    listItem,
    removeItem,
    cellPriceQuantityEdit,
  } from '@/api/store/activity/base'
  import TableEdit from '@/components/activity/SpecProductItemTable'
  import { isNum, isNumber } from '@/utils/validate'

  export default {
    name: 'ActivityBuyItemTable',
    components: { TableEdit },
    data() {
      return {
        tabClickIndex: null, // 点击的行
        columnId: null, // 点击的列
        list: [],
        form: {},
        listLoading: true,
        dialogFormVisible: false,
        title: '',
        layout: 'total, sizes, prev, pager, next, jumper',
        queryForm: {
          total_id: 0,
          pageNum: 1,
          pageSize: 20,
        },
      }
    },
    created() {},
    methods: {
      tabClick(row, column) {
        this.tabClickIndex = row.total_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 = {
          activity_id: row.activity_id,
          total_id: row.total_id,
          item_id: row.item_id,
          price: row.price,
          quantity: row.quantity,
        }
        if (!isNum(row.price) || !isNumber(row.quantity)) {
          this.$baseMessage('价格或数量不能为负数且不能为中文', 'warning')
          return
        }
        const { msg, status } = await cellPriceQuantityEdit(obj)
        if (200 == status) {
          this.$baseMessage(msg, 'success')
        } else {
          this.$baseMessage(msg, 'error')
        }
        this.fetchData()
      },
      __,
      showTable(row) {
        this.queryForm.activity_id = row.activity_id
        this.dialogFormVisible = true
        this.fetchData()
      },
      handleDelete(row) {
        this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
          const { msg, status } = await removeItem({
            activity_id: this.queryForm.activity_id,
            item_id: row.item_id,
          })
          if (200 == status) {
            this.$baseMessage(msg, 'success')
          } else {
            this.$baseMessage(msg, 'error')
          }
          await this.refresh()
        })
      },
      async refresh() {
        this.fetchData()
        this.$emit('fetch-data')
      },
      close() {
        this.dialogFormVisible = false
      },

      handleEdit(activity_id, action, total_id) {
        this.$refs['TableEdit'].showTable(activity_id, '', total_id)
      },

      async fetchData() {
        this.listLoading = true
        const { data } = await listItem(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
    },
  }
</script>
