<template>
  <div class="chain-product-container">
    <vab-query-form>
      <vab-query-form-left-panel :span="4" />
      <vab-query-form-right-panel :span="20">
        <el-form
          ref="form"
          :inline="true"
          :model="queryForm"
          @submit.native.prevent
        >
          <el-form-item prop="product_id">
            <el-input
              v-model.trim="queryForm.product_id"
              clearable
              :placeholder="__('产品SPU')"
            />
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" @click="queryData">
              {{ __('查询') }}
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>
    <el-table
      v-loading="listLoading"
      border
      :data="list"
      :height="tableConfig.height"
      row-key="product_id"
      style="width: 100%"
      @cell-click="tabClick"
    >
      <el-table-column
        align="center"
        :label="__('产品SPU')"
        prop="product_id"
      />
      <el-table-column :label="__('商品图片')" prop="product_image">
        <template slot-scope="scope">
          <el-popover placement="top-start" title="" trigger="hover">
            <img
              alt=""
              :src="scope.row.product_image"
              style="width: 150px; height: 150px"
            />
            <img
              slot="reference"
              :src="scope.row.product_image"
              style="width: 30px; height: 30px"
            />
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('商品名称')"
        prop="product_name"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :label="__('商品SKU')"
        prop="item_id"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :label="__('商品属性')"
        prop="item_spec"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :label="__('店铺售价')"
        prop="item_unit_price"
        show-overflow-tooltip
        width="180"
      />
      <el-table-column
        align="center"
        :label="__('门店商品库存')"
        prop="chain_item_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.chain_item_quantity"
              @blur.stop="inputBlur(scope.row)"
            />
          </span>
          <span v-else>{{ scope.row.chain_item_quantity }}</span>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        :label="__('门店售价')"
        prop="chain_item_unit_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.chain_item_unit_price"
              @blur.stop="inputBlur(scope.row)"
            />
          </span>
          <span v-else>{{ scope.row.chain_item_unit_price }}</span>
        </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"
    />
  </div>
</template>

<script>
  import { getList, doEdit } from '@/api/chain/item/base'
  import { translateTitle as __ } from '@/utils/i18n'

  export default {
    name: 'ChainProduct',
    components: {},
    data() {
      return {
        tableConfig: {
          height: window.innerHeight - 220 - 90,
        },
        defaultCheckedKeys: [],
        data: [], // 产品规格数据
        defaultProps: {
          children: 'children',
          label: 'label',
        },
        nodeKey: 'id',
        loading: true,
        options: [], // 品牌数据
        tabClickIndex: null, // 点击的行
        columnId: null, // 点击的列
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        queryForm: {
          pageNum: 1,
          pageSize: 20,
          product_id: undefined,
        },
      }
    },
    created() {
      let query = this.$route.query
      if (query.product_verify_id) {
        this.queryForm.product_verify_id = Number(query.product_verify_id)
      }
      this.queryData()
    },
    methods: {
      __,
      showTable(row) {
        this.$refs['Table'].showTable(row)
      },
      outOfStock(row) {
        this.$refs['StockTable'].showTable(row)
      },
      reviewEdit(row) {
        this.$refs['ReviewEdit'].showTable(row)
      },
      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) {
        // 保存数据
        let obj = {
          product_id: row.product_id,
          item_id: row.item_id,
          chain_item_quantity: row.chain_item_quantity,
          chain_item_unit_price: row.chain_item_unit_price,
        }
        const { msg, status } = await doEdit(obj)
        this.$baseMessage(msg, 'success', false, 'vab-hey-message-success')
        this.fetchData()
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      handleSizeChange(val) {
        this.queryForm.pageSize = val
        this.fetchData()
      },
      handleCurrentChange(val) {
        this.queryForm.pageNum = val
        this.fetchData()
      },
      queryData() {
        this.queryForm.pageNum = 1
        this.fetchData()
      },
      clearData() {
        this.queryForm = {
          product_name: null,
          product_id: null,
          product_number: null,
          store_name: null,
          brand_id: null,
          product_state_id: null,
          product_verify_id: null,
        }
      },
      // 初始化表格数据
      async fetchData() {
        this.listLoading = true
        const { data } = await getList(this.queryForm)
        this.list = data.items
        this.total = data.records
        this.listLoading = false
      },
      change(id) {
        this.queryForm.category_id = id
      },
    },
  }
</script>
