<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="邀请时间">
        <el-date-picker
          v-model="dateRange"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @change="pickerChange"
        ></el-date-picker>
      </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 label="审核状态" prop="auditStatus">
        <el-select
          v-model="queryParams.auditStatus"
          placeholder="请选择审核状态"
        >
          <el-option label="全部" value=""> </el-option>
          <el-option label="待审核" value="0"> </el-option>
          <el-option label="已同意" value="1"> </el-option>
          <el-option label="已拒绝" value="2"> </el-option>
        </el-select>
      </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-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          >添加</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="TableList"
      @selection-change="handleSelectionChange"
    >
      <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">
        <template slot-scope="scope">
          {{
            scope.row.createTime
              ? $moment(parseInt(scope.row.createTime)).format(
                  "yyyy-MM-DD HH:mm:ss"
                )
              : ""
          }}
        </template>
      </el-table-column>

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

      <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
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleLook(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"
    />
    <shop-dialog ref="shopRef" @submit="shopSubmit"></shop-dialog>
  </div>
</template>

<script>
import shopDialog from "./shopDialog.vue";
import { getCity } from "@/api/common/index";
import { queryPage } from "@/api/chain/index";
export default {
  components: {
    "shop-dialog": shopDialog,
  },
  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,
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    shopSubmit() {
      this.getList();
    },
    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;
      queryPage(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();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.shopRef.openDialog();
      console.log(this.$refs.shopRef);
    },
    handleLook(record) {
      this.$router.push({
        path: "/shop/manage/shopInfoCommon/detail",
        query: { id: record.id, shopName: record.storeName },
      });
    },
    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>
