<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">
          {{ __('添加') }}
        </el-button>
      </vab-query-form-left-panel>
    </vab-query-form>
    <el-table
      v-loading="listLoading"
      border
      :data="list"
      height="400"
      @cell-click="tabClick"
    >
      <el-table-column align="center" :label="__('操作')" width="120">
        <template #default="{ row }">
          <el-button type="text" @click="handleDelete(row)">
            {{ __('删除') }}
          </el-button>
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        :label="__('单笔消费满足金额')"
        prop="total"
        show-overflow-tooltip
        width="300"
      >
        <template slot-scope="scope">
          <span
            v-if="
              scope.row.total_id === tabClickIndex &&
              scope.column.id === columnId
            "
          >
            <el-input
              ref="editInput"
              v-model="scope.row.total"
              @blur.stop="inputBlur(scope.row)"
            />
          </span>
          <span v-else>{{ scope.row.total }}</span>
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        :label="__('满即送商品')"
        show-overflow-tooltip
        width="300px"
      >
        <template slot-scope="scope">
          <div v-if="scope.row.item == undefined || scope.row.item.length <= 0">
            <el-button type="text" @click="handleTable(scope.row)">
              {{ __('请选择商品') }}
            </el-button>
          </div>
          <div v-if="scope.row.item">
            <el-row v-for="item in scope.row.item" :key="item.item_id">
              <el-button type="text" @click="handleTable(scope.row)">
                {{ item.product_item_name }}
              </el-button>
            </el-row>
          </div>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('最大数量')"
        prop="max_num"
        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.max_num"
              @blur.stop="inputBlur(scope.row)"
            />
          </span>
          <span v-else>{{ scope.row.max_num }}</span>
        </template>
      </el-table-column>
    </el-table>
    <itemTable ref="itemTable" @fetch-data="fetchAll" />
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { isNumber, isNumbers } from 'vue-plugin-utils'
  import {
    getActivityBuyItems,
    addRule,
    removeRule,
    cellEditRule,
  } from '@/api/store/activity/base'
  import itemTable from './storeActivityBuyItemTable'

  export default {
    name: 'ReviseMarkupRulesTable',
    components: { itemTable },
    data() {
      return {
        tabClickIndex: null, // 点击的行
        columnId: null, // 点击的列
        list: [],
        from: {},
        listLoading: true,
        queryForm: {},
        title: '',
        dialogFormVisible: false,
      }
    },
    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

        if (
          !isNumbers(row.total) ||
          !isNumbers(row.max_num)
        ) {
          this.$baseMessage(
            '单笔消费满足金额且最大数量不能为负数且不能输入中文',
            'warning'
          )
          return
        }

        // 保存数据
        let obj = {
          activity_id: row.activity_id,
          total_id: row.total_id,
          max_num: row.max_num,
          total: row.total,
        }

        const { msg, status } = await cellEditRule(obj)
        this.$baseMessage(msg, 'success', false, 'vab-hey-message-success')
        this.fetchData()
      },
      __,

      handleTable(row) {
        this.$refs['itemTable'].showTable(row)
      },
      handleDelete(row) {
        this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
          const { msg, status } = await removeRule({
            total_id: row.total_id,
            activity_id: row.activity_id,
          })
          if (200 == status) {
            this.$baseMessage(msg, 'success')
          } else {
            this.$baseMessage(msg, 'error')
          }
          await this.fetchData()
        })
      },
      showTable(row) {
        ;(this.queryForm.activity_id = row.activity_id),
          (this.from.activity_id = row.activity_id)
        this.dialogFormVisible = true
        this.fetchData()
      },
      close() {
        this.dialogFormVisible = false
      },
      async handleEdit() {
        const { msg, status } = await addRule(this.from)
        if (200 == status) {
          this.$baseMessage(msg, 'success')
        } else {
          this.$baseMessage(msg, 'error')
        }
        this.fetchData()
      },
      fetchAll() {
        this.fetchData()
        this.$emit('fetch-data')
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await getActivityBuyItems(this.queryForm)
        this.list = data.items
        this.listLoading = false
      },
    },
  }
</script>
