<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 label="店铺区域" prop="area">
        <el-cascader
          v-model="queryParams.area"
          ref="myCascader"
          :placeholder="placeholder"
          :props="props"
          style="width: 100%"
          @change="cascaderChange"
        />
      </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>
    <span>已选择:{{ this.multipleSelection.length }}个</span>
    <el-button
      v-if="type == 2 && lookType != 'look'"
      size="mini"
      style="margin-left: 10px"
      @click="allremove"
    >
      批量移除
    </el-button>
    <el-table
      v-loading="loading"
      :data="TableList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        type="selection"
        width="55"
        :selectable="checkSelectable"
        v-if="lookType != 'look'"
      >
      </el-table-column>
      <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>

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

<script>
import {
  storeMultipleShopQueryPage,
  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: {
        createStartTime: "",
        createEndTime: "",
        pageNo: 1,
        pageSize: 10,
        cityProvCode: "",
        auditStatus: "",
        area: [],
        key: "",
      },
      props: {
        lazy: true,
        lazyLoad: this.lazyLoad,
        checkStrictly: true,
      },
      multipleSelection: [],
    };
  },
  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() {
    this.getList();
  },
  methods: {
    checkSelectable(row) {
      // 根据状态判断是否可选择，这里假设状态为 '禁用' 时禁止选择
      if (this.type == 1) {
        console.log(this.oldShopIds);
        if (this.oldShopIds) {
          let oldShop = JSON.parse(this.oldShopIds);
          if (!oldShop.includes(row.id)) {
            return row.status !== "禁用";
          }
        } else {
          return row.status !== "启用";
        }
      }
    },
    getStoreinfo() {
      queryPageByVoucherId({ voucherId: this.voucherid }).then((response) => {
        this.TableList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },

    allremove() {
      if (this.voucherid) {
        const selectedIds = this.multipleSelection.map((item) => item.id);
        queryPageByVoucherIdRemove({
          ids: selectedIds.toString(),
          // voucherId: this.voucherid,
        }).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 = [];
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
      this.$emit("tabSelect", this.multipleSelection);
    },
    pickerChange(val) {
      this.queryParams.createStartTime = val[0];
      this.queryParams.createEndTime = val[1];
    },
    cascaderChange(val) {
      this.queryParams.cityProvCode = val[val.length - 1];
    },

    /** 查询参数列表 */
    getList() {
      this.loading = true;
      storeMultipleShopQueryPage(this.queryParams).then((response) => {
        this.TableList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.createStartTime = "";
      this.queryParams.createEndTime = "";
      this.queryParams.area = [];
      this.queryParams.cityProvCode = "";
      this.dateRange = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },

    lazyLoad(node, resolve) {
      let level = node.level;
      if (!node.data) {
        getCity().then((res) => {
          //接口
          const nodes = Array.from(res.data).map((item, index) => ({
            value: item.cityId,
            label: `${item.name}`,
            leaf: level >= 2,
          }));
          // 通过调用resolve将子节点数据返回，通知组件数据加载完成
          resolve(nodes);
        });
      } else if (level == 1) {
        getCity({ parentCityId: node.data.value }).then((res) => {
          const nodes = Array.from(res.data).map((item) => ({
            value: item.cityId,
            label: `${item.name}`,
            leaf: true, // 二级节点设为叶子节点
            disabled: false, // 二级节点设为可选
          }));
          // 通过调用resolve将子节点数据返回，通知组件数据加载完成
          resolve(nodes);
        });
      } else {
        resolve({});
      }
    },
  },
};
</script>
