<template>
  <div class="supplier pagePadding">
    <ex-table
      v-loading="loading"
      :table="table"
      :data="tableData"
      :columns="columns"
      :page="pageInfo"
      :size="size"
      @page-curr-change="handlePageChange"
      @page-size-change="handleSizeChange"
      @screen-reset="
        pageInfo.curr = 1;
        parmValue.page = 1;
        searchList();
      "
      @screen-submit="
        pageInfo.curr = 1;
        parmValue.page = 1;
        searchList();
      "
    >
      <template #table-header="{}">
        <div style="width: 100%">
          <el-row style="padding: 0 0 10px 80px">
            <el-col :span="4" style="width: 100px">
              <el-select
                v-model="parmValue.status"
                filterable
                clearable
                :size="searchSize"
                placeholder="状态"
                style="width: 100%"
                @change="
                  pageInfo.curr = 1;
                  parmValue.page = 1;
                  searchList();
                "
              >
                <el-option
                  v-for="item in statusList"
                  :key="'status' + item.code"
                  :label="item.name"
                  :value="item.code"
                />
              </el-select>
            </el-col>
            <el-col :span="4" style="width: 160px;padding:0 0 0 10px">
              <search-city
                :value="parmValue.store_city"
                :size="'mini'"
                :isDetail="false"
                :names="''"
                :disabled="false"
                :placeholder="'店铺所在城市'"
                @searchChange="searchChange($event,'store_city')"
              />
            </el-col>
            <el-col :span="4" style="width: 160px;padding:0 0 0 10px">
              <search-type
                :value="parmValue.type_id"
                :size="'mini'"
                :isDetail="false"
                :names="''"
                :disabled="false"
                :placeholder="'店铺类型'"
                @searchChange="searchChange($event,'type_id')"
              />
            </el-col>
            <el-col :span="4" style="width: 160px;padding:0 0 0 10px">
              <search-max-area
                :value="parmValue.area_id"
                :size="'mini'"
                :isDetail="false"
                :names="''"
                :disabled="false"
                :placeholder="'会场面积'"
                @searchChange="searchChange($event,'area_id')"
              />
            </el-col>
            <el-col :span="4" style="width: 160px;padding:0 0 0 10px">
              <search-max-num
                :value="parmValue.attendance_id"
                :size="'mini'"
                :isDetail="false"
                :names="''"
                :disabled="false"
                :placeholder="'容纳人数'"
                @searchChange="searchChange($event,'attendance_id')"
              />
            </el-col>
            
            <el-col :span="3" class="fr" style="width: 66px; padding: 0 0 0 10px">
              <el-button type="primary" :size="searchSize" @click="searchList">
                刷新
              </el-button>
            </el-col>
            <el-col :span="3" class="fr" style="width: 66px; padding: 0 0 0 10px">
              <el-button type="warning" :size="searchSize" @click="restSearch">
                重置
              </el-button>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="4" style="width: 160px;">
              <search-store-spec
                :value="parmValue.store_spec_id"
                :size="'mini'"
                :isDetail="false"
                :names="''"
                :disabled="false"
                :placeholder="'场地特色'"
                @searchChange="searchChange($event,'store_spec_id')"
              />
            </el-col>
            <el-col :span="4" style="width: 333px">
              <el-input
                :size="searchSize"
                v-model="parmValue.store_name"
                :maxlength="40"
                clearable
                @blur="
                  pageInfo.curr = 1;
                  parmValue.page = 1;
                  searchList();
                "
                @clear="
                  pageInfo.curr = 1;
                  parmValue.page = 1;
                  searchList();
                "
                placeholder="店铺名称"
              ></el-input>
            </el-col>

            <el-col
              :span="3"
              class="fr"
              style="width: 66px; padding: 0 0 0 10px"
              v-if="powers.some((i) => i == '002')"
            >
              <el-button
                :size="searchSize"
                type="success"
                style="float: right"
                @click="openModal('add', false, {})"
              >
                添加
              </el-button>
            </el-col>
          </el-row>
        </div></template
      >
      <template #store_image="{ scope }">
        <div
          v-if="scope.row.store_image"
          style="width: 20px; height: 20px"
          class="hover"
          v-viewer
        >
          <img
            :src="scope.row.store_image"
            style="display: inline-block; width: 100%; height: 100%"
            alt=""
          />
        </div>
      </template>
      <template #storeType="{ scope }">
        <el-tag
          :size="tablebtnSize"
          v-text="
            scope.row.storeType && scope.row.storeType.name
              ? scope.row.storeType.name
              : '--'
          "
        ></el-tag>
      </template>
      <template #supplier="{ scope }">
        <el-popover
          placement="top-start"
          title="供应商编码："
          v-if="scope.row.supplier_code && Number(scope.row.supplier_code) !== 0"
          width="200"
          trigger="hover"
          :content="scope.row.supplier_code"
        >
          <i class="el-icon-warning-outline" slot="reference" />
        </el-popover>
        <span>{{
          scope.row.supplier && scope.row.supplier ? scope.row.supplier.name : ""
        }}</span>
      </template>

      <template #status="{ scope }">
        <el-tag
          :size="tablebtnSize"
          :type="scope.row.status == '0' ? 'warning' : ''"
          v-text="
            (
              statusList.find((item) => Number(item.code) == Number(scope.row.status)) ||
              {}
            ).name || '--'
          "
        ></el-tag>
      </template>

      <template #operation="{ scope }">
        <el-tooltip effect="dark" content="详情" placement="top">
          <i
            class="el-icon-view tb-icon"
            @click="openModal(scope.row.id, true, scope.row)"
          ></i>
        </el-tooltip>

        <el-tooltip
          v-if="powers.some((i) => i == '005')"
          effect="dark"
          content="修改"
          placement="top"
        >
          <i
            class="el-icon-edit tb-icon"
            @click="openModal(scope.row.id, false, scope.row)"
          ></i>
        </el-tooltip>
        <el-tooltip
          v-if="powers.some((i) => i == '004') && scope.row.status + '' === '1'"
          effect="dark"
          content="禁用"
          placement="top"
        >
          <i
            class="el-icon-video-pause tb-icon"
            @click="changeStatus(scope.row.id, scope.row.status)"
          ></i>
        </el-tooltip>
        <el-tooltip
          v-if="powers.some((i) => i == '003') && scope.row.status + '' === '0'"
          effect="dark"
          content="启用"
          placement="top"
        >
          <i
            class="el-icon-video-play tb-icon"
            @click="changeStatus(scope.row.id, scope.row.status)"
          ></i>
        </el-tooltip>
        <el-tooltip
          v-if="powers.some((i) => i == '006')"
          effect="dark"
          content="删除"
          placement="top"
        >
          <i class="el-icon-delete tb-icon" @click="deleteItem(scope.row.id)"></i>
        </el-tooltip>
      </template>
    </ex-table>
    <add-edit
      :id="modelId"
      :sitem="sitem"
      :show-model="showModel"
      :is-detail="isDetail"
      @refresh="searchList"
      @cancel="showModel = false"
    />
  </div>
</template>
<script>
import asyncRequest from "@/apis/service/operate/shop";
import { statusList } from "@/assets/js/statusList";
import roleLevel from "@/assets/js/roleLevel";
import mixinPage from "@/mixins/elPaginationHandle";
import { mapGetters } from "vuex";
import resToken from "@/mixins/resToken";
import { columns } from "./columns";
import addEdit from "./addEdit";
/**
 * @props
  createStart //创建开始时间
  createEnd   //创建结束时间
  status //当前状态
  store_name  //供应商名称
  creator //创建人 
*/

export default {
  name: "shop",
  mixins: [mixinPage, resToken],
  components: {
    addEdit,
  },
  computed: {
    //组件SIZE设置
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      const tran =
        this.$store.getters.btnList.find((item) => item.menu_route == "shop") || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  data() {
    return {
      statusList,
      roleLevel: roleLevel,
      loading: false,
      showModel: false,
      modelId: "",
      modelSitem: null,
      stype: {},

      parmValue: {
        page: 1, // 页码
        size: 15, // 每页显示条数
        store_city: [], //市级id
        store_name: "", //店铺名称
        status: "", //当前状态
        type_id: [],
        area_id: [],
        attendance_id: [],
        store_spec_id: [],
      },

      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        _defaultHeader_: ["setcol"],
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
      // 表格 - 列参数
      columns,
    };
  },
  mounted() {
    this.searchList();
  },
  methods: {
    restSearch() {
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0,
      };
      this.parmValue = {
        page: 1, // 页码
        size: 15, // 每页显示条数
        store_city: [], //市级id
        store_name: "", //店铺名称
        status: "", //当前状态
        type_id: [],
        area_id: [],
        attendance_id: [],
        store_spec_id: [],
      };
      this.searchList();
    },
    openModal(id, isDetail, sitem) {
      const { status } = sitem;
      if (!isDetail && id !== "add" && Number(status) === 1) {
        this.$message.warning("禁用后，才可以修改！");
        return;
      }
      this.showModel = true;
      this.modelId = id;
      this.isDetail = isDetail;
      this.sitem = sitem;
    },
    
    async searchChange(e,key) {
      const { code } = e;
      this.parmValue[key] = code ? [code] : [];
      this.pageInfo.curr = 1;
      this.parmValue.page = 1;
      await this.searchList();
    },
  
    
    gotoEdit(row, type) {
      const { status } = row;
      if (type === "005" && status + "" === "1") {
        this.$message.warning("禁用后，才可以修改！");
        return;
      }
      this.routeGoto("shopDetail", {
        id: row.code,
        type: "edit",
      });
    },
    /**
     * 启用/禁用
     * @param {String} id id
     * @param {String} status 0-禁用 1-启用
     */
    async changeStatus(id, status) {
      await this.$confirm(`确定要改为${status + "" === "1" ? "禁用" : "启用"}？`, {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          this.loading = true;
          const model = {
            id: id,
            status: status + "" === "1" ? "0" : "1",
          };
          const res = await asyncRequest.status(model);
          this.loading = false;
          if (res && res.code === 1) {
            this.$notify.success({
              title: "状态修改成功！",
              message: "",
            });
            await this.searchList();
          }
        })
        .catch(() => {
          console.log("取消");
        });
    },
    async deleteItem(id) {
      await this.$confirm("确定要删除？", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const model = {
            ids: id,
          };
          const res = await asyncRequest.delete(model);
          if (res && res.code === 1) {
            this.$notify.success({
              title: "删除成功",
              message: "",
            });
            this.searchList();
          }
        })
        .catch(() => {
          console.log("取消");
        });
    },
    // 刷新表格
    async searchList() {
      if (this.loading) {
        return;
      }
      this.loading = true;
      const model = {
        ...this.parmValue,
        limit: this.parmValue.size,
        store_city:this.parmValue.store_city.toString(),
        type_id:this.parmValue.type_id.toString(),
        attendance_id:this.parmValue.attendance_id.toString(),
        store_spec_id:this.parmValue.store_spec_id.toString(),
        area_id: this.parmValue.area_id.toString()
      };
      const { code, data, msg } = await asyncRequest.list(model);
      let scode = parseInt(code + "");
      if (scode === 1) {
        const { list, total } = data;
        this.tableData = list;
        this.pageInfo.total = Number(total + "");
      } else {
        this.tableData = [];
        this.pageInfo.total = 0;
      }
      this.loading = false;
    },
  },
};
</script>

<style lang="scss" scoped></style>
