<template>
  <div class="othTransferOrderLog 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 10px 80px">
              <el-col :span="4" style="width: 140px">
                <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 status_list"
                    :key="'status' + item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </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.addtime_start"
                  :end="parmValue.addtime_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 style="width: 100%">
              <el-col :span="4" style="width: 480px">
                <el-input
                  :size="searchSize"
                  v-model="sinput"
                  :maxlength="40"
                  @blur="
                    pageInfo.curr = 1;
                    parmValue.page = 1;
                    searchList();
                  "
                  placeholder="关键字"
                >
                  <el-select
                    v-model="parmValue.elSelect"
                    :size="searchSize"
                    style="width: 140px"
                    placeholder="关键字类型"
                    slot="prepend"
                    @change="
                      pageInfo.curr = 1;
                      parmValue.page = 1;
                      searchList();
                    "
                  >
                    <el-option label="平台订单号" value="1"></el-option>
                    <el-option label="平台商品编号" value="2"></el-option>
                    <el-option label="销售订单号" value="3"></el-option>
                    <el-option label="线上商品编号" value="4"></el-option>
                    <!-- <el-option label="所属平台" value="5"></el-option> -->
                  </el-select>
                </el-input>
              </el-col>
            </el-row>
          </div>
        </template>
        <template #expand="{ scope }">
          <el-form label-position="left" inline :size="'mini'">
            <el-row>
              <el-col :span="6">
                <el-form-item label="收件人">
                  <span>{{ scope.row.receiver_name }}</span>
                </el-form-item></el-col
              >
              <el-col :span="6">
                <el-form-item label="联系电话">
                  <span>{{ scope.row.receiver_tel }}</span>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="联系地址">
                  <span>{{ scope.row.address }}</span>
                </el-form-item></el-col
              >
              <el-col :span="24" v-if="scope.row.buyer_messages">
                <el-form-item label="订单备注">
                  <span>{{ scope.row.buyer_messages }}</span>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </template>

        <template #pay_type="{ scope }">
          <el-tag
            :size="tablebtnSize"
            :type="''"
            v-text="
              (pay_type_list.find((item) => item.value == scope.row.pay_type + '') || {})
                .label || '--'
            "
          ></el-tag>
        </template>
        <template #status="{ scope }">
          <el-tag
            :size="tablebtnSize"
            :type="''"
            v-text="
              (status_list.find((item) => item.value == scope.row.status + '') || {})
                .label || '--'
            "
          ></el-tag>
        </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('othTransferOrderLogDetail', scope.row.id)"
            ></i>
          </el-tooltip>
          <el-tooltip
            v-if="powers.some((i) => i == '092') && scope.row.status + '' === '8'"
            effect="dark"
            content="重新转单"
            placement="top"
          >
            <i class="el-icon-refresh-left tb-icon" @click="resetOrder(scope.row.id)"></i>
          </el-tooltip>
        </template>
      </ex-table>
    </div>
    <no-auth v-else></no-auth>
  </div>
</template>
<script>
import asyncRequest from "@/apis/service/youzan/othTransferOrderLog";
import mixinPage from "@/mixins/elPaginationHandle";
import { mapGetters } from "vuex";
import resToken from "@/mixins/resToken";
import { columns, pay_type_list, status_list } from "./columns";
export default {
  name: "othTransferOrderLog",
  mixins: [mixinPage, resToken],
  computed: {
    //组件SIZE设置
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      const tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "othTransferOrderLog"
        ) || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  data() {
    return {
      sinput: "",
      loading: false,
      parmValue: {
        page: 1, // 页码
        size: 15, // 每页显示条数
        status: "",
        elSelect: "1",
        addtime_start: "",
        addtime_end: "",
        tid: "",
        plat_code: "",
        orderCode: "",
        skuCode: "",
        platform_name: "",
      },
      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        _defaultHeader_: ["setcol"],
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
      // 表格 - 列参数
      columns,
      pay_type_list,
      status_list,
    };
  },
  mounted() {
    if (this.$route.query.back) {
      const { sinput = '', size, page, ...rest } = JSON.parse(this.$route.query.back)
      this.sinput = sinput;
      this.parmValue = rest;
      this.pageInfo.curr = page;
      this.pageInfo.size = size;
    }
    this.searchList();
  },
  methods: {
    restSearch() {
      this.sinput = "";
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0,
      };
      this.parmValue = {
        page: 1, // 页码
        size: 15, // 每页显示条数
        status: "",
        addtime_start: "",
        addtime_end: "",
        elSelect: "1",
        tid: "",
        plat_code: "",
        orderCode: "",
        skuCode: "",
        platform_name: "",
      };
      this.searchList();
    },
    async handleTime(e) {
      this.parmValue.addtime_start = e.startTime || "";
      this.parmValue.addtime_end = e.endTime || "";
      if (
        (this.parmValue.addtime_start !== "" && this.parmValue.addtime_end !== "") ||
        (this.parmValue.addtime_start === "" && this.parmValue.addtime_end === "")
      ) {
        this.pageInfo.curr = 1;
        this.parmValue.page = 1;
        await this.searchList();
      }
    },
    //点击详情
    getRouter(toRouter, queryId) {
      if (toRouter && queryId) {
        let model = {
          id: queryId,
          type: "view",
        };

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

        this.routeGoto(toRouter, model);
      } else {
        this.$message.warning("暂未找到相关流程！");
      }
    },
    async resetOrder(id) {
      if (this.loading) return;
      this.loading = true;
      const { code, message } = await asyncRequest.retry({ id: id });
      if (code === 0) {
        this.loading = false;
        await this.searchList();
      } else if (code >= 100 && code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(message);
      }
      this.loading = false;
    },
    // 刷新表格
    async searchList() {
      if (this.loading) return;
      if (
        (this.parmValue.addtime_start !== "" && this.parmValue.addtime_end === "") ||
        (this.parmValue.addtime_start === "" && this.parmValue.addtime_end !== "")
      ) {
        this.$message.warning("时间区间不完整！");
        return;
      }
      this.loading = true;
      let model = JSON.parse(JSON.stringify(this.parmValue));
      model.tid = model.elSelect === "1" ? this.sinput : "";
      model.plat_code = model.elSelect === "2" ? this.sinput : "";
      model.orderCode = model.elSelect === "3" ? this.sinput : "";
      model.skuCode = model.elSelect === "4" ? this.sinput : "";
      // model.platform_name = model.elSelect = "1" ? this.sinput : "";
      const { code, data } = await asyncRequest.list(model);
      if (code === 0) {
        const { list, count } = data ?? {};
        this.tableData = list ?? [];
        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>
.othTransferOrderLog {
  .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>
