<template>
  <div class="sort pagePadding">
    <div v-if=" powers.some((i) => i == '001')">
      <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 0 80px">
              <el-col :span="4" style="width: 140px">
                <el-select
                  v-model="parmValue.msg_type"
                  filterable
                  clearable
                  :size="searchSize"
                  placeholder="消息类型"
                  style="width: 100%"
                  @change="
                    pageInfo.curr = 1;
                    parmValue.page = 1;
                    searchList();
                  "
                >
                  <el-option
                    v-for="item in msg_typeList"
                    :key="'msg_type' + 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_date"
                  :end="parmValue.end_date"
                  @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>
          </div></template
        >
        <template #key_is_display="{ scope }">
          <el-tag
            :size="tablebtnSize"
            :type="
              (
                is_display_list.find(
                  (item) => item.id == scope.row.key_is_display + ''
                ) || {}
              ).type || 'info'
            "
            v-text="
              (
                is_display_list.find(
                  (item) => item.id == scope.row.key_is_display + ''
                ) || {}
              ).label || '--'
            "
          ></el-tag>
        </template>

        <template #msg_type="{ scope }">
          <el-tag
            :size="tablebtnSize"
            :type="
              (msg_typeList.find((item) => item.id == scope.row.msg_type + '') || {})
                .type || '--'
            "
            v-text="
              (msg_typeList.find((item) => item.id == scope.row.msg_type + '') || {})
                .label || '--'
            "
          ></el-tag>
        </template>
      </ex-table>
    </div>
    <no-auth v-else></no-auth>
  </div>
</template>
<script>
import asyncRequest from "@/apis/service/youzan/othGoodLog";
import roleLevel from "@/assets/js/roleLevel";
import mixinPage from "@/mixins/elPaginationHandle";
import { mapGetters } from "vuex";
import resToken from "@/mixins/resToken";
export default {
  name: "othGoodLog",
  mixins: [mixinPage, resToken],

  computed: {
    //组件SIZE设置
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      const tran =
        this.$store.getters.btnList.find((i) => i.menu_route == "othGoodLog") || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  data() {
    return {
      msg_typeList: [
        { id: "1", label: "商品上架", type: "success" },
        { id: "2", label: "商品下架", type: "primary" },
        // { id: "3", label: "生成订单", type: "info" },
        { id: "4", label: "商品删除", type: "danger" },
        { id: "5", label: "商品编辑", type: "warning" },
      ],
      is_display_list: [
        { id: "0", label: "上架", type: "success" },
        { id: "1", label: "下架", type: "primary" },
      ],
      roleLevel: roleLevel,
      loading: false,
      sitem: {},
      showRateModel: false,
      modelRateId: "000",
      sitemRate: {},
      parmValue: {
        page: 1, // 页码
        size: 15, // 每页显示条数
        msg_type: "",
        start_date: "",
        end_date: "",
      },
      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        _defaultHeader_: ["setcol"],
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
      // 表格 - 列参数
      columns: [
        {
          prop: "id",
          label: "记录ID",
        },
        {
          prop: "msg_type",
          label: "数据类型",
          _slot_: "msg_type",
        },
        {
          prop: "key_item_id",
          label: "有赞商品ID",
        },
        // {
        //   prop: "key_alias",
        //   label: "有赞商品别名",
        // },
        // {
        //   prop: "key_kdt_id",
        //   label: "有赞店铺ID",
        // },
        // {
        //   prop: "key_is_display",
        //   label: "推送前状态",
        //   _slot_: "key_is_display",
        // },
        {
          prop: "key_title",
          label: "有赞商品名称",
        },
        {
          prop: "key_price",
          label: "有赞价格",
        },
        // {
        //   prop: "key_channel",
        //   label: "店铺类型", //0-网店，1：门店
        // },
        // {
        //   prop: "key_barcode",
        //   label: "有赞商品SKU码", //商品SKU维度的条形码，用于扫码快速搜索商品，使用场景如：扫码购、扫码搜索商品（只支持实物商品
        // },
        {
          prop: "skuCode",
          label: "采销商品编码",
        },
        {
          prop: "addtime",
          label: "创建时间",
        },
      ],
    };
  },
  mounted() {
    this.searchList();
  },
  methods: {
    restSearch() {
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0,
      };
      this.parmValue = {
        page: 1, // 页码
        size: 15, // 每页显示条数
        msg_type: "",
        start_date: "",
        end_date: "",
      };
      this.searchList();
    },
    async handleTime(e) {
      this.parmValue.start_date = e.startTime || "";
      this.parmValue.end_date = e.endTime || "";
      if (
        (this.parmValue.start_date !== "" && this.parmValue.end_date !== "") ||
        (this.parmValue.start_date === "" && this.parmValue.end_date === "")
      ) {
        this.pageInfo.curr = 1;
        this.parmValue.page = 1;
        await this.searchList();
      }
    },
    // 刷新表格
    async searchList() {
      if (this.loading) return;
      if (
        (this.parmValue.start !== "" && this.parmValue.end === "") ||
        (this.parmValue.start === "" && this.parmValue.end !== "")
      ) {
        this.$message.warning("时间区间不完整！");
        return;
      }
      this.loading = true;
      const { code, data } = await asyncRequest.list(this.parmValue);
      if (code === 0) {
        const { list, count } = data ?? {};

        let arr = list ?? [];
        arr.map((e) => {
          for (let key in e.data) {
            e[`key_${key}`] = e.data[key];
          }
          return e;
        });
        this.tableData = arr;
        this.pageInfo.total = count ?? 0;
      } else if (code >= 100 && code <= 104) {
        await this.logout();
      } else {
        this.tableData = [];
        this.pageInfo.total = 0;
      }
      this.loading = false;
    },
  },
};
</script>

<style lang="scss" scoped>
.sort {
  .my-breadcrumb {
    .my-breadcrumb-item {
      float: left;
      span {
        height: 32px;
        line-height: 32px;
      }
      .label {
        padding: 0 5px 0 0;
        color: 010101;
      }
      .separator {
        color: #787878;
        font-size: 13px;
        padding: 0 5px 0 0;
      }
      &:last-child {
        .label {
          color: #787878;
        }
      }
      &:hover {
        cursor: pointer;
      }
      &:last-child {
        cursor: default;
      }
      &:only-child {
        cursor: pointer;
      }
      // &.chover {
      //   cursor: pointer;
      // }
    }
  }
}
</style>
