<template>
  <el-dialog
    append-to-body
    :title="title"
    :visible.sync="dialogFormVisible"
    width="1000px"
    @close="close"
  >
    <vab-query-form>
      <vab-query-form-left-panel :span="4">
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="handleEdit(queryForm.activity_id, 'discount')"
        >
          {{ __('添加') }}
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel :span="20" />
    </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_name"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('价格')"
        prop="item_unit_price"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('折扣价')"
        prop="item_discount_price"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span
            v-if="
              scope.row.item_id === tabClickIndex &&
              scope.column.id === columnId
            "
          >
            <el-input
              ref="editInput"
              v-model="scope.row.item_discount_price"
              @blur.stop="inputBlur(scope.row)"
            />
          </span>
          <span v-else>{{ scope.row.item_discount_price }}</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 {
    cellEdit,
    getActivityBuyItems,
    removeActivityBuyItems,
  } from '@/api/store/activity/base'
  import TableEdit from '@/components/activity/SpecProductItemTable'
  import { isNumbers } from '@/utils/validate'

  export default {
    name: 'StoreActivityBuyItemTable',
    components: { TableEdit },
    data() {
      return {
        tabClickIndex: null, // 点击的行
        columnId: null, // 点击的列
        list: [],
        form: {},
        listLoading: true,
        dialogFormVisible: false,
        title: '',
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        queryForm: {},
      }
    },
    created() {},
    methods: {
      __,
      tabClick(row, column) {
        this.tabClickIndex = row.item_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,
          item_discount_price: row.item_discount_price,
          item_id: row.item_id,
        }
        if (!isNumbers(row.item_discount_price)) {
          this.$baseMessage('折扣价不能为负数且不能输入中文', 'warning')
          return
        }
        const { msg, status } = await cellEdit(obj)
        this.$baseMessage(msg, 'success', false, 'vab-hey-message-success')
        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 removeActivityBuyItems({
            action: 'discount',
            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()
        })
      },

      close() {
        this.dialogFormVisible = false
      },

      handleEdit(activity_id, action) {
        this.$refs['TableEdit'].showTable(activity_id, action)
      },
      async refresh() {
        this.$emit('fetch-data')
        this.fetchData()
      },

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