<template>
  <el-dialog
    append-to-body
    :title="title"
    :visible.sync="dialogFormVisible"
    width="60%"
    @close="close"
  >
    <vab-query-form>
      <vab-query-form-right-panel :span="24">
        <el-button
          icon="el-icon-plus"
          type="primary"
          @click="synchronization(queryForm.item_id)"
        >
          {{ __('同步到相同SPU下所有商品') }}
        </el-button>
      </vab-query-form-right-panel>
    </vab-query-form>
    <el-table border :data="list" height="610" @cell-click="tabClick">
      <el-table-column
        align="center"
        :label="__('策略编号')"
        prop="policy_wholesale_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('商品SKU')"
        prop="item_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('所属店铺')"
        prop="store_id"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('最大数量')"
        prop="item_quantity"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span
            v-if="
              scope.row.policy_wholesale_id === tabClickIndex &&
              scope.column.id === columnId
            "
          >
            <el-input
              ref="editInput"
              v-model="scope.row.item_quantity"
              @blur.stop="changeQuantity(scope.row)"
            />
          </span>
          <span v-else>{{ scope.row.item_quantity }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('商品原价')"
        prop="item_unit_price"
        show-overflow-tooltip
      />
      <el-table-column
        align="center"
        :label="__('阶梯价格')"
        prop="policy_wholesale_price"
        show-overflow-tooltip
      >
        <template slot-scope="scope">
          <span
            v-if="
              scope.row.policy_wholesale_id === tabClickIndex &&
              scope.column.id === columnId
            "
          >
            <el-input
              ref="editInput"
              v-model="scope.row.policy_wholesale_price"
              @blur.stop="inputBlur(scope.row)"
            />
          </span>
          <span v-else>{{ scope.row.policy_wholesale_price }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('是否启用')"
        prop="policy_wholesale_enable"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.policy_wholesale_enable"

            :active-value="1"

            :inactive-value="0"
            @change="sChange(scope.row)"
          />
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNum"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
  </el-dialog>
</template>
<script>
  import { translateTitle as __ } from '@/utils/i18n'

  import {
    doEdit,
    editDiscountRate,
    getList,
    revisesTrategyPrice,
  } from '@/api/product/pricing/policy/wholesale'

  export default {
    name: 'PricingPolicyWholesaleTable',
    components: {},
    data() {
      return {
        tabClickIndex: null, // 点击的行
        columnId: null, // 点击的列
        list: [],
        form: {},
        total: 0,
        listLoading: true,
        dialogFormVisible: false,
        title: '',
        layout: 'total, sizes, prev, pager, next, jumper',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
        },
      }
    },
    created() {},
    methods: {
      async synchronization(item_id) {
        this.$baseConfirm(
          this.__('请确认同步到SPU下所有商品吗？'),
          null,
          async () => {
            const { msg, status } = await editDiscountRate({ item_id: item_id })
            this.$baseMessage(msg, 'success', false, 'vab-hey-message-success')
            this.fetchData()
          }
        )
      },
      tabClick(row, column) {
        this.tabClickIndex = row.policy_wholesale_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 = {
          policy_wholesale_price: row.policy_wholesale_price,
          policy_wholesale_id: row.policy_wholesale_id,
        }
        const { msg, status } = await revisesTrategyPrice(obj)
        this.$baseMessage(msg, 'success', false, 'vab-hey-message-success')
        this.fetchData()
      },
      async changeQuantity(row) {
        this.tabClickIndex = null
        this.columnId = null
        // 保存数据
        let obj = {
          item_quantity: row.item_quantity,
          policy_wholesale_id: row.policy_wholesale_id,
        }
        const { msg, status } = await revisesTrategyPrice(obj)
        this.$baseMessage(msg, 'success', false, 'vab-hey-message-success')
        this.fetchData()
      },
      __,
      showTable(row) {
        this.queryForm.item_id = row.item_id
        this.dialogFormVisible = true
        this.fetchData()
      },

      close() {
        this.dialogFormVisible = false
      },
      async sChange(row) {
        const param = {
          policy_wholesale_id: row.policy_wholesale_id,
          policy_wholesale_enable: row.policy_wholesale_enable,
        }
        const { msg, status } = await doEdit(param)
        this.$baseMessage(msg, 'success', false, 'vab-hey-message-success')
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        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>
