<template>
  <div class="othOrder 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="{ selection }">
          <div style="width: 100%">
            <el-row style="padding: 0 0 0 80px">
              <el-col :span="6" style="width: 293px; padding: 0 0 0 0px">
                <period-date-picker
                  :type="1"
                  :width="'135px'"
                  :size="searchSize"
                  :start="parmValue.start"
                  :end="parmValue.end"
                  @timeReturned="handleTime"
                />
              </el-col>
              <el-col :span="4" style="width: 148px; padding: 0 0 0 8px">
                <el-select
                  :size="searchSize"
                  v-model="parmValue.status"
                  filterable
                  clearable
                  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="4" style="width: 240px; padding: 0 0 0 10px">
                <search-work-company
                  :type="'1'"
                  :value="parmValue.supplierNo"
                  :size="'mini'"
                  :placeholder="'销售方公司'"
                  :disabled="false"
                  :isDetail="false"
                  @searchChange="supplierChange"
                />
              </el-col>

              <el-col :span="3" style="width: 66px; float: right">
                <el-button
                  :size="searchSize"
                  type="primary"
                  style="float: right; margin-left: 5px"
                  @click="searchList"
                >
                  刷新
                </el-button>
              </el-col>
              <el-col :span="3" style="width: 66px; float: right">
                <el-button
                  :size="searchSize"
                  type="primary"
                  style="float: right; margin-left: 5px"
                  @click="set_user(selection)"
                >
                  设置订单用途
                </el-button>
              </el-col>
            </el-row>
            <el-row style="padding: 10px 0 0 0">
              <el-col :span="6" style="width: 371px">
                <search-customer
                  :value="customerCode"
                  :size="searchSize"
                  :names="parmValue.supplierName"
                  :is-detail="true"
                  :placeholder="'客户公司名称'"
                  @searchChange="customerChange"
                />
              </el-col>

              <el-col :span="4" style="width: 150px; padding: 0 0 0 9px">
                <el-select
                  :size="searchSize"
                  v-model="parmValue.order_type"
                  filterable
                  clearable
                  placeholder="商品来源"
                  style="width: 100%"
                  @change="
                    pageInfo.curr = 1;
                    parmValue.page = 1;
                    searchList();
                  "
                >
                  <el-option
                    v-for="item in xs_order_type_options"
                    :key="'orderstatus' + item.id"
                    :label="item.label"
                    :value="item.id"
                  />
                </el-select>
              </el-col>
              <el-col :span="6" style="width: 325px; padding: 0 0 0 10px">
                <el-input
                  clearable
                  placeholder="关键字"
                  v-model="input"
                  maxlength="40"
                  :size="searchSize"
                  @blur="searchList"
                  class="input-with-select"
                >
                  <el-select
                    v-model="select"
                    style="width: 100px"
                    slot="prepend"
                    placeholder="关键字类型"
                    @change="searchList"
                  >
                    <el-option label="订单编号" value="orderCode"></el-option>
                    <!-- <el-option
                      label="商品属性编号"
                      value="good_code"
                    ></el-option> -->
                    <!-- <el-option
                      label="供应商编号"
                      value="supplierNo"
                    ></el-option> -->
                    <!-- <el-option
                      label="客户编号"
                      value="customer_code"
                    ></el-option> -->
                    <el-option label="商品名称" value="good_name"></el-option>
                    <el-option label="创建人" value="apply_name"></el-option>
                    <el-option label="创建人部门" value="company_name"></el-option>
                  </el-select>
                </el-input>
              </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-row>
          </div>
        </template>
        <template #good_name="{ scope }">
          <span>{{ scope.row.good_name }}</span>
          <span v-for="(si, i) in scope.row.speclist" :key="si.spec_id + i">
            <span v-if="i !== 0">-</span>
            <span v-else>_</span>
            <span>{{ si.spec_name }}[{{ si.spec_value }}]</span>
          </span>
        </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 #order_type="{ scope }">
          <el-tag
            :size="tablebtnSize"
            v-text="
              (
                xs_order_type_options.find((item) => item.id == scope.row.order_type) ||
                {}
              ).label || '--'
            "
          ></el-tag>
        </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 #operation="{ scope }">
          <el-tooltip effect="dark" content="详情" placement="top">
            <i
              class="el-icon-view tb-icon"
              @click="getRouter('othOrderDetail', scope.row.id)"
            ></i>
          </el-tooltip>
        </template>
      </ex-table>

      <!--订单用途设置-->
      <user-add-edit
        :code="orderCodeList"
        :show-model="userShowModel"
        @refresh="searchList"
        @cancel="userShowModel = false"
      />
    </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/youzan/othOrder";
import { columns } from "./columns"; //表格列参数
import userAddEdit from "./userAddEdit";
import { mapGetters } from "vuex";
import { xs_order_type_options, xs_order_source_options } from "@/assets/js/statusList";
export default {
  name: "othOrder",
  mixins: [mixinPage, resToken],
  components: {
    userAddEdit,
  },
  computed: {
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      const tran =
        this.$store.getters.btnList.find((i) => i.menu_route == "othOrder") || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  data() {
    return {
      sitem: null,
      select: "orderCode",

      input: "",
      customerCode: [],
      // 状态
      statusOptions: [
        { id: "0", label: "待发货" },
        { id: "1", label: "待发货完成" },
        { id: "2", label: "发货已完成" },
        { id: "3", label: "订单已取消" },
      ],
      xs_order_type_options,
      xs_order_source_options,
      loading: false,
      showModel: false,
      isDetail: false,
      modelId: 0,
      parmValue: {
        order_type: "", //订单类型
        orderCode: "", //销售订单code
        apply_name: "", // 申请人
        good_name: "", // 产品名称
        good_code: "", // 产品属性编号
        status: "", //状态
        page: 1, // 页码
        size: 15, // 每页显示条数
        supplierNo: "", //供应商编号
        customer_code: "", //客户编号
        start: "", //新建起始时间
        end: "", // 新建结束时间
        company_name: "", //创建人部门
      },
      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        _defaultHeader_: ["setcol"],
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
      // 表格 - 列参数
      columns: columns,
      orderCodeList: [],
      userShowModel: false,
    };
  },
  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.customer_code.length > 0) {
        this.customerCode = [this.parmValue.customer_code];
      }

      this.pageInfo = {
        size: size,
        curr: page,
        total: 0,
      };
      //多选条件
      this.select = this.parmValue.select;
      // this.sselect = this.parmValue.sselect;
      this.input = this.parmValue.input;
    } else {
      this.select = "orderCode";
      //  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.input = this.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("暂未找到相关流程！");
      }
    },
    restSearch() {
      this.select = "orderCode";
      this.input = "";
      this.customerCode = [];
      this.parmValue = {
        order_type: "",
        orderCode: "", //销售订单code
        apply_name: "", // 申请人
        good_name: "", // 产品名称
        good_code: "", // 产品属性编号
        status: "", //状态
        page: 1, // 页码
        size: 15, // 每页显示条数
        supplierNo: "", //供应商编号
        customer_code: "", //客户编号
        start: "", //新建起始时间
        end: "", // 新建结束时间
        company_name: "", //部门
      };
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0,
      };
      this.searchList();
    },
    openModal(id, isDetail, sitem) {
      this.modelId = id;
      this.isDetail = isDetail;
      this.sitem = sitem;
      this.showModel = true;
    },
    async supplierChange(e) {
      const { id, code, label } = e;
      this.parmValue.supplierNo = code || "";
      this.parmValue.page = 1;
      this.pageInfo.curr = 1;
      await this.searchList();
    },
    set_user(selection) {
      if (selection.length === 0) {
        this.$message.warning("至少选择一个订单！");
        return;
      }
      this.orderCodeList = [];
      let isok = true;
      selection.forEach((e) => {
        this.orderCodeList.push(e.orderCode);
        if (e.use_order) {
          isok = false;
        }
      });

      if (!isok) {
        this.$message.warning("只能选择未绑定的订单！");
        return;
      }
      this.userShowModel = true;
    },
    // 客户选择
    async customerChange(e) {
      if (e && e.id) {
        this.customerCode = [e.code];
        this.parmValue.supplierName = e.label;
        this.parmValue.customer_code = e.code;
      } else {
        this.customerCode = [];
        this.parmValue.supplierName = "";
        this.parmValue.customer_code = "";
      }
      this.parmValue.page = 1;
      this.pageInfo.curr = 1;
      await this.searchList();
    },
    async stockChange(e) {
      if (e && e.id) {
        this.parmValue.stockNo = [e.code];
      } else {
        this.parmValue.stockNo = [];
      }

      this.parmValue.page = 1;
      this.pageInfo.curr = 1;
      await this.searchList();
    },
    async searchList() {
      if (
        (this.parmValue.start !== "" && this.parmValue.end === "") ||
        (this.parmValue.start === "" && this.parmValue.end !== "")
      ) {
        this.$message.warning("时间区间不完整！");
        return;
      }
      this.loading = true;
      this.parmValue.orderCode = this.select === "orderCode" ? this.input : "";
      this.parmValue.good_name = this.select === "good_name" ? this.input : "";
      this.parmValue.apply_name = this.select === "apply_name" ? this.input : "";

      this.parmValue.company_name = this.select === "company_name" ? this.input : "";

      const { code, data } = await asyncRequest.list(this.parmValue);
      if (code === 0) {
        const { list, count } = data ?? {};
        this.tableData = list ?? [];
        this.tableData.forEach((v) => {
          v.good_class = "";
          if (v.can && v.can.length > 0) {
            v.can.forEach((x, i) => {
              v.good_class += i === 0 ? x.name : "_" + x.name;
            });
          }
        });
        this.pageInfo.total = Number(count ?? "0");
      } else if (code >= 100 && code <= 104) {
        await this.logout();
      } else {
        this.tableData = [];
        this.pageInfo.total = 0;
      }
      this.loading = false;
    },
    // 时间
    async handleTime(e) {
      if (e.startTime !== "") {
        this.parmValue.start = e.startTime;
      } else {
        this.parmValue.start = "";
      }
      if (e.endTime !== "") {
        this.parmValue.end = e.endTime;
      } else {
        this.parmValue.end = "";
      }
    },
  },
};
</script>
<style lang="scss" scoped>
.othOrder {
  // text-align: right;
}
</style>
