<template>
  <div class="luckyDraw pagePadding">
    <ex-table
      v-loading="loading"
      v-if="powers.some((i) => i == '001')"
      :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: 205px">
              <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 statusOptions"
                  :key="'status' + item.id"
                  :label="item.label"
                  :value="item.id"
                />
              </el-select>
            </el-col>
            <el-col :span="6" style="width: 303px; padding: 0 0 0 10px">
              <period-date-picker
                :type="1"
                :width="'135px'"
                :size="searchSize"
                :start="parmValue.start"
                :end="parmValue.end"
                @timeReturned="handleTime"
              />
            </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: 500px">
              <el-input
                :size="searchSize"
                v-model="sinput"
                :maxlength="40"
                clearable
                @blur="
                  pageInfo.curr = 1;
                  parmValue.page = 1;
                  searchList();
                "
                placeholder="关键字"
              >
                <el-select
                  v-model="select"
                  slot="prepend"
                  style="width: 130px"
                  @change="
                    pageInfo.curr = 1;
                    parmValue.page = 1;
                    searchList();
                  "
                  placeholder="关键字类型"
                >
                  <el-option label="活动名称" value="1" />
                  <el-option label="活动编码" value="2" />
                  <el-option label="活动公司" value="3" />
                  <el-option label="活动负责人" value="4"></el-option></el-select
              ></el-input>
            </el-col>
            <!-- <el-col
              :span="3"
              class="fr"
              style="width: 66px; padding: 0 0 0 10px"
              v-if="powers.some((i) => i == '003')"
            >
              <el-button
                :size="searchSize"
                type="success"
                style="float: right"
                @click="getRouter('othgoodsOfflineDetail', '0', 'add')"
              >
                添加
              </el-button>
            </el-col> -->
          </el-row>
        </div></template
      >
      <template #status="{ scope }">
        <el-tag
          :size="tablebtnSize"
          v-text="
            (statusOptions.find((item) => item.id == scope.row.status + '') || {})
              .label || '--'
          "
        ></el-tag>
      </template>
      <template #atime="{ scope }">
        {{ scope.row.startTime }}~{{ scope.row.endTime }}
      </template>
      <template #operation="{ scope }">
        <el-tooltip effect="dark" content="详情" placement="top">
          <i
            v-if="powers.some((i) => i == '007')"
            class="el-icon-view tb-icon"
            @click="getRouter('luckyDrawDetail', scope.row.actCode, 'view')"
          ></i>
        </el-tooltip>
        <!-- <el-tooltip effect="dark" content="修改" placement="top">
          <i
            class="el-icon-edit tb-icon"
            @click="getRouter('othgoodsOfflineDetail', scope.row.actCode, 'edit')"
          ></i>
        </el-tooltip>
        <el-tooltip effect="dark" content="删除" placement="top">
          <i class="el-icon-delete tb-icon" @click="deleteItem(scope.row.actCode)"></i>
        </el-tooltip> -->
      </template>
    </ex-table>
    <no-auth v-else></no-auth>
    <!-- 弹窗 新增/修改 -->
    <add-edit
      :id="modelId"
      :show-model="showModel"
      :sitem="sitem"
      @refresh="searchList"
      @cancel="showModel = false"
    />
  </div>
</template>
<script>
import asyncRequest from "@/apis/service/youzan/luckDraw";
import mixinPage from "@/mixins/elPaginationHandle";
import { mapGetters } from "vuex";
import resToken from "@/mixins/resToken";
import { listCol, statusOptions } from "./columns";
export default {
  name: "luckyDraw",
  mixins: [mixinPage, resToken],
  computed: {
    //组件SIZE设置
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      const tran =
        this.$store.getters.btnList.find((item) => item.menu_route == "luckyDraw") || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  data() {
    return {
      select: "1",
      sinput: "",
      loading: false,
      parmValue: {
        act_name: "",
        actCode: "",
        status: "",
        start: "",
        end: "",
        page: 1, // 页码
        size: 15, // 每页显示条数
        company_id: "",
        contactor: "",
        company_name: "", //活动公司
      },
      // 状态
      statusOptions,
      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        _defaultHeader_: ["setcol"],
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
      // 表格 - 列参数
      columns: listCol,
    };
  },
  mounted() {
    const { back } = this.$route.query;
    if (back) {
      this.parmValue = JSON.parse(back);
      console.log(this.parmValue);
      const { page, size } = this.parmValue;
      // this.parmValue.start = start || last_start;
      // this.parmValue.end = end || last_end;
      // if(this.parmValue.brandid.length>0){
      //   this.brandid = [this.parmValue.brandid] ;
      // }

      this.pageInfo = {
        size: size,
        curr: page,
        total: 0,
      };
      //多选条件
      this.select = this.parmValue.select;
      // this.sselect = this.parmValue.sselect;
      this.sinput = this.parmValue.sinput;
    } else {
      this.select = "1";
      //  this.sselect = "创建时间"
    }
    this.searchList();
  },
  methods: {
    getRouter(toRouter, queryId, type) {
      if (toRouter && queryId) {
        let model = {
          id: queryId,
          type,
        };

        //有多选框的条件
        this.parmValue.select = this.select;
        // this.parmValue.sselect = this.sselect ;
        this.parmValue.sinput = this.sinput;
        //
        console.log(this.parmValue);
        let routerModel = {
          options: JSON.parse(JSON.stringify(this.parmValue)),
          router: this.$route.path,
        };
        model.preModel = JSON.stringify(routerModel);

        this.routeGoto(toRouter, model);
      } else {
        this.$message.warning("暂未找到相关流程！");
      }
    },
    restSearch() {
      // 表格 - 分页
      this.select = "1";
      this.sinput = "";
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0,
      };
      this.parmValue = {
        act_name: "",
        actCode: "",
        status: "",
        start: "",
        end: "",
        page: 1, // 页码
        size: 15, // 每页显示条数
        company_id: "",
        contactor: "",
        company_name: "", //活动公司
      };
      this.searchList();
    },
    async platform_codesearchChange(e) {
      const { id, code, label } = e;
      this.parmValue.platform_code = id || "";
      this.pageInfo.curr = 1;
      this.parmValue.page = 1;
      await this.searchList();
    },

    async company_idsearchChange(e) {
      const { id, code, label } = e;
      this.parmValue.company_id = code || "";
      this.pageInfo.curr = 1;
      this.parmValue.page = 1;
      await this.searchList();
    },
    // 新建/编辑/详情
    openModal(row, type) {
      this.sitem = row;
      this.modelId = type;
      this.showModel = true;
    },
    // 时间
    async handleTime(e) {
      this.parmValue.start = e.start || "";
      this.parmValue.end = e.end || "";
      if (
        (this.parmValue.start !== "" && this.parmValue.end !== "") ||
        (this.parmValue.start === "" && this.parmValue.end === "")
      ) {
        this.pageInfo.curr = 1;
        this.parmValue.page = 1;
        await this.searchList();
      }
    },
    /**
     * 启用/禁用
     * @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 { code, message } = await asyncRequest.status(model);
          if (code === 0) {
            this.loading = false;
            this.$notify.success({
              title: "状态修改成功！",
              message: "",
            });
            await this.searchList();
          } else if (code >= 100 && code <= 104) {
            await this.logout();
          } else {
            this.loading = false;
            this.$message.warning(message);
          }
        })
        .catch(() => {
          console.log("取消");
        });
    },
    async deleteItem(actCode) {
      await this.$confirm("确定要删除？", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const model = {
            actCode,
          };
          const { code, data, message } = await asyncRequest.delete(model);
          if (code === 0) {
            this.$notify.success({
              title: "删除成功",
              message: "",
            });
            this.searchList();
          } else if (code >= 100 && code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(message);
          }
        })
        .catch(() => {
          console.log("取消");
        });
    },
    // 刷新表格
    async searchList() {
      if (
        (this.parmValue.start !== "" && this.parmValue.end === "") ||
        (this.parmValue.start === "" && this.parmValue.end !== "")
      ) {
        this.$message.warning("时间区间不完整！");
        return;
      }
      this.loading = true;
      let model = JSON.parse(JSON.stringify(this.parmValue));
      model.act_name = this.select === "1" ? this.sinput : "";
      model.actCode = this.select === "2" ? this.sinput : "";
      model.contactor = this.select === "3" ? this.sinput : "";
      model.company_name = this.select === "4" ? this.sinput : ""; // 部门
      const { code, data } = await asyncRequest.list(model);
      if (code === 0) {
        const { count, list } = data;
        this.tableData = list;
        this.pageInfo.total = Number(count);
      } else if (code >= 100 && code <= 104) {
        await this.logout();
      } else {
        this.tableData = [];
        this.pageInfo.total = 0;
      }
      this.loading = false;
    },
  },
};
</script>

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