<template>
  <div>
    <!-- 穿梭框 -->
    <el-transfer
      filterable
      filter-placeholder="请输入自提点名称"
      v-model="checked.shopCodeList"
      :filter-method="(query, item) => (typeof item.name == 'string' && item.name.indexOf(query) >= 0)"
      :props="{ key: 'storeCode', label: 'name' }"
      :data="pickupList">
    </el-transfer>
    <!-- 导入 -->
    <dm-upload-file
      :action="`${origin}${api.giftShopListUpload}`"
      accept=".xls,.xlsx"
      with-credentials
      :on-success="onUploadSucc">
    </dm-upload-file>
  </div>
</template>

<script>
/**
 * 所有自提点
 * 部分自提点
 *  获取所有自提点
 *  回显时根据礼品id获取当前礼品的所有自提点
 * 批量导入
 *  上传礼品自提点列表
 *  部分自提点时的回显，只需要回显导入的数量
 *  新增时需要回显导入的数量，和导入的文件名称
 */

// 组件的返回结果
/**
 * 点击穿梭框下面的确定按钮或者文件上传完成后返回如下数据
 * {
 *    type: 1,
 *    shopCodeList: [],
 *    uuid: 123
 * }
 */
import { origin } from '@/config/index.js';
import api from '@/api/gift.js';
const { getPickupList, getGiftShopList } = api;
export default {
  name: 'SelectPickupStore',
  props: {
    giftId: String, // 礼品id
    type: String, // 门店自提类型
    sum: Number, // 批量导入的数量
  },
  data() {
    return {
      api: {
        // method: 'post',
        // useFormData: true
        giftShopListUpload: '/gift-shop-list-upload', // 上传礼品自提点列表EXCEL
        giftShopListDownload: '/gift-shop-list-download', // 下载礼品自提点列表EXCEL
        downloadGiftTemplate: '/gift/template-download', // 下载模板
      },
      pickupList: [], // 所有自提点
      giftPickupList: [], // 礼品的自提点
      // 选择自提点相关的数据
      checked: {
        shopCodeList: [],
        uuid: ''
      },
    };
  },
  created() {
    this.init();
  },
  methods: {
    init() {
      // 获取所有自提点
      // TODO 考虑分页加载
      this.pickupList = [];
      getPickupList({ currentPage: 1, pageSize: 20 }).then(res => {
        const { result } = res.result || {};
        this.pickupList = this.pickupList.concat(result);
      });
      // 编辑而且是部分自提点时，获取礼品自提点
      getGiftShopList().then(res => {
        this.giftPickupList = res.result || [];
      });
    },
    // 自提点上传
    onUploadSucc(resp) {
      const { uuid, errorList } = resp.result;
      console.log(uuid, errorList);
    },
    // 自提点下载
    downloadPickup() {
      window.location.href = `${origin}${this.api.giftShopListDownload}?id=${this.giftId}`;
    },
    // 向外传递数据
    onChange() {
      this.$emit('change', this.checked);
    }
  }
};
</script>
