<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="" prop="key">
        <el-input
          v-model="queryParams.key"
          placeholder="请输入店铺名称/ID搜索"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="TableList">
      <el-table-column label="店铺ID" align="center" prop="id" />
      <el-table-column
        label="店铺名称"
        align="center"
        prop="storeName"
        :show-overflow-tooltip="true"
      />
      <el-table-column
        label="店铺地址"
        align="center"
        prop="storeCity"
        :show-overflow-tooltip="true"
      />
      <el-table-column label="联系方式" align="center" prop="storePhone" />
      <el-table-column
        label="营业状态"
        align="center"
        prop="businessStatus"
        width="180"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.businessStatus == 0">未营业</span>
          <span v-if="scope.row.businessStatus == 1">营业中</span>
          <span v-if="scope.row.businessStatus == 2">禁用</span>
        </template>
      </el-table-column>

      <el-table-column
        v-if="type == 2"
        label="审核状态"
        align="center"
        prop="receiveStatus"
        width="180"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.receiveStatus == 0">待审核</span>
          <span v-if="scope.row.receiveStatus == 1">已同意</span>
          <span v-if="scope.row.receiveStatus == 2">已拒绝</span>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        width="200"
        v-if="lookType != 'look'"
      >
        <template slot-scope="scope">
          <el-button
            v-if="type == 1 && !oldshop.includes(scope.row.id)"
            size="mini"
            type="text"
            style="color: #00b529"
            @click="handlesave(scope.row)"
          >
            {{ scope.row.union ? "已添加" : "添加店铺参与联合券" }}
          </el-button>
          <el-button
            v-if="type == 2"
            size="mini"
            type="text"
            @click="handleRemove(scope.row)"
          >
            解除添加
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNo"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  getStoreInfoByKey,
  queryPageByVoucherId,
  queryPageByVoucherIdRemove,
} from "@/api/marketing/voucher";
import { getCity } from "@/api/common/index";
export default {
  props: ["type", "data", "voucherid", "lookType", "oldShopIds"],
  name: "chainStore",
  data() {
    return {
      loading: true,
      showSearch: true,
      total: 0,
      TableList: [],
      open: false,
      dateRange: [],
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        key: "",
      },
      multipleSelection: [],
      oldshop: [],
    };
  },
  watch: {
    type(newValue, oldValue) {
      if (newValue == 1) {
        this.getList();
      } else {
        if (this.voucherid) {
          this.getStoreinfo();
        } else {
          this.TableList = this.data;
          this.total = this.data.length;
        }
      }
    },
  },
  mounted() {
    if (this.oldShopIds) {
      this.oldshop = JSON.parse(this.oldShopIds);
    } else {
      this.oldshop = [];
    }
    this.getList();
  },
  methods: {
    handleRemove(record) {
      if (this.voucherid) {
        queryPageByVoucherIdRemove({
          ids: record.id,
        }).then((res) => {
          if (res.code == 200) {
            this.getStoreinfo();
          }
        });
      } else {
        this.TableList = this.TableList.filter((item) => {
          return !this.multipleSelection.some(
            (selectedItem) => selectedItem.id === item.id
          );
        });
      }
      // 清空选择
      this.multipleSelection = [];
    },

    handlesave(record) {
      record.union = record.id;
      this.multipleSelection.push(record);
      this.$emit("tabSelect", this.multipleSelection);
    },
    getStoreinfo() {
      queryPageByVoucherId({ voucherId: this.voucherid }).then((response) => {
        this.TableList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },

    /** 查询参数列表 */
    getList() {
      this.loading = true;
      getStoreInfoByKey(this.queryParams).then((response) => {
        this.TableList = response.data.records.map((item) => ({
          ...item,
          union: null,
        }));
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
  },
};
</script>
