<template>
  <div class="active 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="6" style="width: 240px">
              <search-terrace
                :value="parmValue.platform_code"
                :disabled="false"
                :size="'mini'"
                :isDetail="false"
                :is_show="'0'"
                :placeholder="'所属平台'"
                @searchChange="platform_codesearchChange"
            /></el-col>
            <el-col :span="4" style="width: 205px; padding: 0 0 0 10px">
              <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.starttime"
                :end="parmValue.endtime"
                @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: 320px">
              <search-work-company
                :value="parmValue.company_id"
                :placeholder="'业务公司'"
                :disabled="false"
                :size="searchSize"
                :isDetail="false"
                @searchChange="company_idsearchChange"
              />
            </el-col>
            <el-col :span="4" style="width: 373px; padding: 0 0 0 10px">
              <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="routeGoto('activeDetail', { id: 'add', type: 'add' })"
              >
                添加
              </el-button>
            </el-col>
          </el-row>
        </div></template
      >
      <template #status="{ scope }">
        <el-tag
          :size="tablebtnSize"
          :type="
            scope.row.status == '4'
              ? ''
              : scope.row.status == '8'
              ? 'danger'
              : scope.row.status == '5' || scope.row.status == '7'
              ? 'info'
              : 'warning'
          "
          v-text="
            (statusOptions.find((item) => item.id == scope.row.status) || {})
              .label || '--'
          "
        ></el-tag>
      </template>
      <template #atime="{ scope }">
        {{ scope.row.start }}~{{ scope.row.end }}
      </template>
      <template #operation="{ scope }">
        <el-tooltip
          v-if="powers.some((i) => i == '007')"
          effect="dark"
          content="详情"
          placement="top"
        >
          <i
            class="el-icon-view tb-icon"
           
            @click="getRouter('activeDetail', scope.row.activity_code, )"
          ></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/goodStore/active";
import mixinPage from "@/mixins/elPaginationHandle";
import { mapGetters } from "vuex";
import resToken from "@/mixins/resToken";
import { listCol } from "./columns";
export default {
  name: "active",
  mixins: [mixinPage, resToken],
  computed: {
    //组件SIZE设置
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      const tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "active"
        ) || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  data() {
    return {
      select: "1",
      sinput: "",
      loading: false,
      parmValue: {
        activity_name: "",
        activity_code: "",
        status: "",
        platform_code: "",
        starttime: "",
        endtime: "",
        page: 1, // 页码
        size: 15, // 每页显示条数
        company_id: "",
        creater: "",
        company_name: "", //创建人部门
      },
      // 状态
      statusOptions: [
        { id: "0", label: "待产品部审核" },
        { id: "1", label: "待采购定价" },
        { id: "2", label: "待财务定售价" },
        { id: "3", label: "待活动价审核" },
        { id: "4", label: "审核完成" },
        { id: "5", label: "活动未开始" },
        { id: "6", label: "活动进行中" },
        { id: "7", label: "活动已结束" },
        { id: "8", label: "审核未通过" },
      ],
      // 表格 - 数据
      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){
      if (toRouter && queryId) {
        let model = {
          id: queryId,
          type: 'view',
        };

        //有多选框的条件
        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 = {
        activity_name: "",
        activity_code: "",
        status: "",
        platform_code: "",
        starttime: "",
        endtime: "",
        page: 1, // 页码
        size: 15, // 每页显示条数
        company_id: "",
        creater: "",
        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.starttime = e.startTime || "";
      this.parmValue.endtime = e.endTime || "";
      if (
        (this.parmValue.starttime !== "" && this.parmValue.endtime !== "") ||
        (this.parmValue.starttime === "" && this.parmValue.endtime === "")
      ) {
        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 res = await asyncRequest.status(model);
          if (res && res.code === 0) {
            this.loading = false;
            this.$notify.success({
              title: "状态修改成功！",
              message: "",
            });
            await this.searchList();
          } else if (res && res.code >= 100 && res.code <= 104) {
            await this.logout();
          } else {
            this.loading = false;
            this.$message.warning(res.message);
          }
        })
        .catch(() => {
          console.log("取消");
        });
    },
    async deleteItem(id) {
      await this.$confirm("确定要删除？", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const model = {
            id: id,
          };
          const res = await asyncRequest.delete(model);
          if (res && res.code === 0) {
            this.$notify.success({
              title: "删除成功",
              message: "",
            });
            this.searchList();
          } else if (res && res.code >= 100 && res.code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(res.message);
          }
        })
        .catch(() => {
          console.log("取消");
        });
    },
    // 刷新表格
    async searchList() {
      if (
        (this.parmValue.starttime !== "" && this.parmValue.endtime === "") ||
        (this.parmValue.starttime === "" && this.parmValue.endtime !== "")
      ) {
        this.$message.warning("时间区间不完整！");
        return;
      }
      this.loading = true;
      let model = JSON.parse(JSON.stringify(this.parmValue));
      model.activity_name = this.select === "1" ? this.sinput : "";
      model.activity_code = this.select === "2" ? this.sinput : "";
      model.creater = this.select === "3" ? this.sinput : "";
      model.company_name = this.select === "4" ? this.sinput : ""; // 部门
      const res = await asyncRequest.list(model);
      if (res && res.code === 0 && res.data) {
        this.tableData = res.data.list;
        this.pageInfo.total = Number(res.data.count);
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.tableData = [];
        this.pageInfo.total = 0;
      }
      this.loading = false;
    },
  },
};
</script>

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