<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();
      "
      @addEmit="openModal('add', false, {})"
    >
      <template #image="{ scope }">
        <div
          v-if="scope.row.image"
          style="width: 20px; height: 20px"
          class="hover"
          v-viewer
        >
          <img
            :src="scope.row.image"
            style="display: inline-block; width: 100%; height: 100%"
            alt=""
          />
        </div>
      </template>

      <template #operation="{ scope }">
        <el-tooltip 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 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"
      :store_id="store_id"
      :show-model="showModel"
      :is-detail="isDetail"
      @refresh="searchList"
      @cancel="showModel = false"
    />
  </div>
</template>
<script>
import asyncRequest from "@/apis/service/operate/shopTrim/conference";
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 //当前状态
  supplierName  //供应商名称
  creator //创建人 
*/

export default {
  name: "shop",
  props: ["store_id"],
  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 ?? [];
    },
  },
  watch: {
    store_id: function (val) {
      this.restSearch();
    },
  },
  data() {
    return {
      statusList,
      roleLevel: roleLevel,
      loading: false,
      showModel: false,
      modelId: "",
      modelSitem: null,
      stype: {},

      parmValue: {
        page: 1, // 页码
        size: 15, // 每页显示条数
      },

      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
      },
      // 表格 - 分页
      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, // 每页显示条数
      };
      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;
    },

    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() {
      this.loading = true;
      const model = {
        ...this.parmValue,
        limit: this.parmValue.size,
        type: "会议服务设施",
        store_id: this.store_id,
      };
      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>
