<template>
  <div class="zxDiffOrder pagePadding">
    <div v-if=" powers.some((i) => i == '001')">
      <ex-table
        :table="table"
        v-loading="loading"
        :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="24">
                <el-col :span="6" style="width: 355px;">
                  <period-date-picker
                    :create_end="parmValue.create_end"
                    :create_start="parmValue.create_start"
                    :type="1"
                    :width="'165px'"
                    :size="searchSize"
                    @timeReturned="timeReturned($event)"
                  />
                </el-col>

                <el-col :span="4" style="margin-left: 10px">
                  <el-select
                    style="width:100%"
                    placeholder="状态"
                    v-model="parmValue.status"
                    size="mini"
                    clearable
                    @change="
                      pageInfo.curr = 1;
                      parmValue.page = 1;
                      searchList();
                    "
                  >
                    <el-option
                      v-for="s in statusOptions"
                      :label="s.label"
                      :value="s.id"
                      :key="s.id"
                    />
                  </el-select>
                </el-col>

                <el-col :span="3" style="width: 66px; float: right">
                  <el-button
                    type="primary"
                    :size="searchSize"
                    @click="searchList"
                    style="float: right; margin-left: 5px"
                  >刷新</el-button>
                </el-col>

                <el-col :span="4" style="width: 66px; float: right">
                  <el-button type="warning" class="fr" :size="searchSize" @click="restSearch">重置</el-button>
                </el-col>
              </el-col>
            </el-row>
            <el-row style="margin-top: 10px">
              <el-col :span="6" style="width: 400px">
                <el-input
                  v-model="value"
                  :size="searchSize"
                  placeholder="关键字"
                  clearable
                  :maxlength="40"
                  @change="
                      pageInfo.curr = 1;
                      parmValue.page = 1;
                      searchList();
                    "
                >
                  <template #prepend>
                    <el-select
                      v-model="parmValue.key"
                      style="width:100px"
                      @change="
                      pageInfo.curr = 1;
                      parmValue.page = 1;
                      searchList();
                    "
                    >
                      <el-option label="商品名称" value="spuCode" />
                      <el-option label="商品编码" value="good_name" />
                    </el-select>
                  </template>

                  <el-button
                    slot="append"
                    icon="el-icon-search"
                    @click="
                        pageInfo.curr = 1;
                        parmValue.page = 1;
                        searchList();
                      "
                  ></el-button>
                </el-input>
              </el-col>

              <el-button
                v-if="powers.includes('003') && !isSupertube"
                class="fr"
                type="success"
                size="mini"
                @click="getRouter('combinedRemoveDetail', 'add' )"
              >添加</el-button>
            </el-row>
          </div>
        </template>
        <template #status="{ scope }">
          <el-tag
            :size="tablebtnSize"
            :type="scope.row.status == '0' ? 'warning' : ''"
            v-text="
              (statusOptions.find((item) => item.id == scope.row.status) || {})
                .label || '--'
            "
          ></el-tag>
        </template>

        <template #company="{scope}">
            <el-tooltip :content="'业务公司编码 : ' + scope.row.companyNo" placement="top">
              <i class="el-icon-warning-outline" style="margin-right:2px;cursor:pointer" />
            </el-tooltip>
            {{scope.row.companyName}}
        </template>

        <template #order_source="{ scope }">
          <el-tag
            :size="tablebtnSize"
            v-text="
              ( xs_order_source_options.find((item) => item.id === scope.row.order_source) || {}).label || '--'
            "
          ></el-tag>
        </template>

        <template #catinfo="{ scope }">{{scope.row.catInfo.map(({name}) => name).join("-")}}</template>

        <template #specInfo="{ scope }">
          <span
            v-for="(si, sii) in scope.row.specInfo"
            :key="sii + si.spec_value_id"
          >{{ sii === 0 ? "" : "-" }}{{ si.spec_name }}[{{ si.spec_value }}]</span>
        </template>

        <template #order_type="{ scope }">
          <el-tag
            :size="tablebtnSize"
            v-text="
              (
              is_stock.find((item) => item.id == scope.row.is_stock) || {}
              ).name || '--'
            "
          ></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('combinedRemoveDetail', scope.row.id )"
            ></i>
          </el-tooltip>
        </template>
      </ex-table>
    </div>
    <div v-else>
      <no-auth></no-auth>
    </div>
  </div>
</template>
<script>
import mixinPage from "@/mixins/elPaginationHandle";
import resToken from "@/mixins/resToken";
import asyncRequest from "@/apis/service/sellOut/combinedAdd";
import companyHelper from "@/mixins/companyHelper";
import { mapGetters } from "vuex";
import { columns } from "./columns";

import {
  xs_order_source_options,
  xs_order_type_options
} from "@/assets/js/statusList";

export default {
  name: "zxDiffOrder",
  mixins: [mixinPage, resToken, companyHelper],
  computed: {
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      const { btnList } = this.$store.getters;
      const tran =
        btnList.find(item => item.menu_route == "combinedRemove") || {};
      const { action } = tran ?? {};
      return action ?? [];
    }
  },
  data() {
    return {
      supplierNo: "",
      xs_order_source_options,
      xs_order_type_options,
      select: "1",
      input: "",
      addShowModel: false,
      addModelId: "",
      addIsDetail: false,
      customerCode: [],
      sitem: null,
      // 状态
      statusOptions: [
        { id: "1", label: "待审核" },
        { id: "2", label: "审核通过" },
        { id: "3", label: "审核驳回" }
      ],
      statusList: [
        "样品竞价单",
        "线上竞价",
        "线上贵金属",
        "线下竞价",
        "线下贵金属",
        "赠品竞价单",
        "项目竞聘"
      ],
      is_stock: [
        { id: "0", name: "非库存品" },
        { id: "1", name: "库存品" }
      ],
      key: "",
      value: "",
      loading: true,
      showModel: false,
      isDetail: false,
      modelId: 0,
      parmValue: {
        key: "spuCode",
        value: "",
        status: "",
        create_start: "",
        create_end: "",
        page: 1, // 页码
        size: 15 // 每页显示条数
      },
      tableData: [],
      passwordModel: false,
      passwordModelId: 0,
      isPasswordDetail: false,
      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        _defaultHeader_: ["setcol"]
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0
      },
      // 表格 - 列参数
      columns: columns
    };
  },
  mounted() {
    const { back } = this.$route.query;
    if (back) {
      this.parmValue = JSON.parse(back);
      const { page, size } = this.parmValue;

      this.pageInfo = {
        size: size,
        curr: page,
        total: 0
      };

      //多选条件
      this.select = this.parmValue.select;
      this.s_input = this.parmValue.s_input;
    } else {
      this.select = "1";
    }
    this.searchList();
  },

  methods: {
    async supplierChange(e) {
      const { code, label } = e;
      this.parmValue.supplierNo = code ? [code] : [];
      this.parmValue.supplierName = label;
      this.searchList();
    },
    getRouter(toRouter, queryId) {
      if (toRouter && queryId) {
        let model = {
          id: queryId
          // type: 'view',
        };

        //有多选框的条件
        this.parmValue.select = this.select;
        this.parmValue.s_input = this.s_input;
        //
        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("暂未找到相关流程！");
      }
    },
    async timeReturned(e) {
      if (e.create_startTime !== "") {
        this.parmValue.create_start = e.create_startTime;
      } else {
        this.parmValue.create_start = "";
      }

      if (e.create_endTime !== "") {
        this.parmValue.create_end = e.create_endTime;
      } else {
        this.parmValue.create_end = "";
      }
      if (
        this.parmValue.create_start !== "" &&
        this.parmValue.create_end !== ""
      ) {
        this.parmValue.page = 1;
        await this.searchList();
      }
    },
    restSearch() {
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0
      };
      this.select = "1";
      this.input = "";
      this.parmValue = {
        status: "",
        key: "spuCode",
        value: "",
        create_start: "",
        create_end: "",
        page: 1, // 页码
        size: 15 // 每页显示条数
      };
      this.searchList();
    },
    async customerChange(e) {
      if (e && e.id) {
        this.parmValue.customer_code = [e.code];
        this.parmValue.customerName = e.label;
      } else {
        this.parmValue.customer_code = [];
        this.parmValue.customerName = "";
      }
      this.parmValue.page = 1;
      await this.searchList();
    },
    openModal(id, isDetail) {
      this.showModel = true;
      this.modelId = id;
      this.isDetail = isDetail;
    },
    addModal(id, isDetail) {
      this.addModelId = id;
      this.addShowModel = true;
      this.addIsDetail = isDetail;
    },
    async deleteById(id, status) {
      await this.$confirm("确定要删除？", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(async () => {
          const model = {
            id: id
            // status: status === "1" ? "0" : "1",
          };
          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.create_start !== "" &&
          this.parmValue.create_end === "") ||
        (this.parmValue.create_start == "" && this.parmValue.create_end != "")
      ) {
        this.$message.warning("开始时间和结束时间不能为空");
        return;
      }
      this.loading = true;

      const { key, value, ...rest } = this.parmValue;

      console.log({
        needRela: true,
        [key]: value,

        ...rest
      });

      const res = await asyncRequest.list({
        needRela: true,
        [key]: value,
        flag: "0",
        ...rest
      });

      if (res && res.code === 0 && res.data) {
        this.tableData = res.data.list;
        this.tableData.map(v => {
          v.sale_price = this.setNum(v.sale_price);
          v.sale_fee = this.setNum(v.sale_fee);
          v.class_cat = "";
          if (v.can && v.can.length > 0) {
            v.can.forEach((x, i) => {
              v.class_cat += i === 0 ? x.name : "/" + x.name;
            });
          }
          return v;
        });
        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>
.zxDiffOrder {
}
</style>
