<template>
  <el-dialog
    append-to-body
    :title="title"
    :visible.sync="dialogFormVisible"
    width="800px"
    @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="__('折上折金额')"
        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>
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import {
    listReductionRule,
    addRule,
    removeRule,
    cellEditRule,
  } from '@/api/store/activity/base'
  import { isNumbers, isNumber } from '@/utils/validate'

  export default {
    name: 'ReviseMarkupRulesTable',
    components: {},
    data() {
      return {
        tabClickIndex: null, // 点击的行
        columnId: null, // 点击的列
        list: [],
        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
        // 保存数据
        let obj = {
          activity_id: row.activity_id,
          total_id: row.total_id,
          max_num: row.max_num,
          total: row.total,
        }
        if (!isNumbers(row.total) || !isNumber(row.max_num)) {
          row.total = 0
          row.max_num = 0
          this.$baseMessage(
            '单笔订单需满足金额或折上折金额不能为负数且不能输入中文',
            'warning'
          )
          return
        }
        const { msg, status } = await cellEditRule(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()
      },
      close() {
        this.dialogFormVisible = false
      },
      async handleEdit() {
        const { msg, status } = await addRule({
          activity_id: this.queryForm.activity_id,
        })
        if (200 == status) {
          this.$baseMessage(msg, 'success')
        } else {
          this.$baseMessage(msg, 'error')
        }
        this.fetchData()
      },
      handleDelete(row) {
        this.$baseConfirm(this.__('你确定要删除当前项吗'), null, async () => {
          const { msg, status } = await removeRule({
            activity_id: row.activity_id,
            total_id: row.total_id,
          })
          if (200 == status) {
            this.$baseMessage(msg, 'success')
          } else {
            this.$baseMessage(msg, 'error')
          }
          this.fetchData()
        })
      },
      async fetchData() {
        this.listLoading = true
        const { data } = await listReductionRule(this.queryForm)
        this.list = data.items
        this.listLoading = false
      },
    },
  }
</script>
