
<template>
  <div class="survey 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%" class="min">
            <el-row style="padding: 0 0 0 80px">
              <el-col :span="12" style="width: 280px">
                <search-work-company
                  :value="parmValue.companyNo"
                  :names="''"
                  :size="searchSize"
                  :is-detail="id !== 'add'"
                  :placeholder="'业务公司'"
                  @searchChange="supplierChange"
                />
              </el-col>
              <el-col :span="6" style="width: 273px; padding: 0 0 0 10px">
                <number-range
                  :lower="parmValue.stock_low"
                  :online="parmValue.stock_up"
                  :title="'库存量'"
                  :width="'120px'"
                  :size="searchSize"
                  @numReturned="numReturned($event, 0)"
                />
              </el-col>
              <el-col :span="6" style="width: 273px; padding: 0 0 0 10px">
                <number-range
                  :lower="parmValue.warn_low"
                  :online="parmValue.warn_up"
                  :width="'120px'"
                  :title="'预警量'"
                  :size="searchSize"
                  @numReturned="numReturned1($event, 0)"
                />
              </el-col>
              <el-col :span="3" style="width: 66px; float: right">
                <el-button
                  type="primary"
                  :size="searchSize"
                  style="float: right; margin-left: 5px"
                  @click="searchList"
                >
                  刷新
                </el-button>
              </el-col>
            </el-row>
            <el-row style="padding: 10px 0 0 0">
              <el-col :span="12" style="width: 360px">
                <search-supplier
                  :size="searchSize"
                  :disabled="parmValue.companyNo === ''"
                  :placeholder="'供应商公司'"
                  :value="parmValue.supplier_code"
                  @searchChange="selectSupplier"
                />
              </el-col>
              <el-col :span="12" style="width: 350px; padding-left: 10px">
                <search-stock
                  :isDetail="true"
                  :size="searchSize"
                  :value="parmValue.stock_code"
                  :disabled="supplier_code === ''"
                  :placeholder="'出库仓库'"
                  :isRelation="true"
                  :companyCode="supplier_code"
                  :names="''"
                  @searchChange="selectStock"
                />
              </el-col>
              <el-col :span="6" style="width: 150px; padding: 0 0 0 10px">
                <el-input
                  placeholder="采购人部门"
                  v-model="parmValue.company_name"
                  :size="searchSize"
                  @blur="
                    pageInfo.curr = 1;
                    parmValue.page = 1;
                    searchList();
                  "
                  class="input"
                >
                </el-input>
              </el-col>

              <el-col :span="4" style="width: 66px; float: right">
                <el-button
                  :size="searchSize"
                  class="fr"
                  type="warning"
                  @click="restSearch"
                  >重置</el-button
                >
              </el-col>
            </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 #operation="{ scope }">
          <el-tooltip
            v-if="powers.some((i) => i == '005')"
            effect="dark"
            content="修改"
            placement="top"
          >
            <i
              class="el-icon-edit tb-icon"
              @click="openModal(scope.row.id, false, scope.row)"
            ></i>
          </el-tooltip>
          <el-tooltip
            v-if="
              powers.some((i) => i == '004') && scope.row.status === '1'
            "
            effect="dark"
            content="禁用"
            placement="top"
          >
            <i
              class="el-icon-video-pause tb-icon"
              @click="statusConfirm(scope.row.id, scope.row.status)"
            ></i>
          </el-tooltip>
          <el-tooltip
            v-if="
              powers.some((i) => i == '004') && scope.row.status === '0'
            "
            effect="dark"
            content="启用"
            placement="top"
          >
            <i
              class="el-icon-video-play tb-icon"
              @click="statusConfirm(scope.row.id, scope.row.status)"
            ></i>
          </el-tooltip>
        </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>
      </ex-table>
      <add-edit
        :id="modelId"
        :sitem="sitem"
        :show-model="showModel"
        :is-detail="isDetail"
        @refresh="searchList"
        @cancel="showModel = false"
      />
    </div>
    <div v-else>
      <no-auth></no-auth>
    </div>
  </div>
</template>
   <script>
import mixinPage from "@/mixins/elPaginationHandle";
import resToken from "@/mixins/resToken";
import { statusList } from "@/assets/js/statusList";
import asyncRequest from "@/apis/service/stock/stockWarning";
import colums from "./colums";
import addEdit from "./addEdit";
import { mapGetters } from "vuex";
export default {
  name: "survey",
  mixins: [mixinPage, resToken],
  components: {
    addEdit,
  },
  computed: {
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      const tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "stockWarning"
        ) || {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  data() {
    return {
      stock_code: "",
      supplier_code: "",
      isDisabled: true,
      statusList: statusList,
      loading: true,
      showModel: false,
      isDetail: false,
      modelId: 0,
      parmValue: {
        companyNo: "",
        goods_sn: "",
        supplier_code: [],
        stock_code: [],
        warn_low: "",
        warn_up: "",
        stock_low: "",
        stock_up: "",
        company_name:"", //部门
        page: 1,
        size: 15,
      },
      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        _defaultHeader_: ["setcol"],
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
      // 表格 - 列参数
      columns: colums,
    };
  },
  mounted() {
    this.searchList();
  },

  methods: {
    //业务公司选择
    async supplierChange(e) {
      console.log(e);
      const { code } = e;
      this.parmValue.companyNo = code || ""; //业务公司编码
      this.parmValue.supplier_code = [];
      this.supplier_code = "";
      this.parmValue.stock_code = [];
      this.stock_code = "";
      this.parmValue.page = 1;
      this.pageInfo.curr = 1;
      await this.searchList();
    },
    // 选择供应商仓库
    async selectStock(e) {
      const { code } = e;
      this.parmValue.stock_code = code ? [code] : [];
      this.stock_code = code || "";
      this.parmValue.stock_code = [];
      this.stock_code = "";
      this.parmValue.page = 1;
      this.pageInfo.curr = 1;
      await this.searchList();
    },
    // 选择供应商
    async selectSupplier(e) {
      const { code } = e;
      this.parmValue.supplier_code = code ? [code] : [];
      this.supplier_code = code || "";
      this.parmValue.stock_code = [];
      this.stock_code = "";
      this.parmValue.page = 1;
      this.pageInfo.curr = 1;
      await this.searchList();
    },
    // 预警量范围框
    async numReturned1(e) {
      if (e && e.lower != "" && e.online != "") {
        this.parmValue.warn_low = e.lower;
        this.parmValue.warn_up = e.online;
      } else {
        this.parmValue.warn_low = "";
        this.parmValue.warn_up = "";
      }
      if (this.parmValue.warn_low != "" && this.parmValue.warn_up != "") {
        await this.searchList();
      }
    },
    // 库存量范围框
    async numReturned(e) {
      if (e && e.lower != "" && e.online != "") {
        this.parmValue.stock_low = e.lower;
        this.parmValue.stock_up = e.online;
      } else {
        this.parmValue.stock_low = "";
        this.parmValue.stock_up = "";
      }
      if (this.parmValue.stock_low != "" && this.parmValue.stock_up != "") {
        await this.searchList();
      }
    },
    // 重置搜索栏
    async restSearch() {
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0,
      };
      this.parmValue = {
        companyNo: "",
        goods_sn: "",
        supplier_code: [],
        stock_code: [],
        warn_low: "",
        warn_up: "",
        stock_low: "",
        stock_up: "",
        company_name:"", //部门
        page: 1,
        size: 15,
      };
      await this.searchList();
    },

    openModal(id, isDetail, sitem) {
      this.showModel = true;
      this.row = id;
      this.isDetail = isDetail;
      this.sitem = sitem;
    },

    async searchList() {
      this.loading = true;
      let model = JSON.parse(JSON.stringify(this.parmValue));
      model.supplierNo = model.supplier_code.toString() || "";
      model.wsm_code = model.stock_code.toString() || "";
      delete model["supplier_code"];
      delete model["stock_code"];
      const res = await asyncRequest.list(model);
      if (res && res.code === 0 && res.data) {
        res.data.list.forEach((element) => {
          if (element.sort_s != "" && element.sort_t != "") {
            element.classArr = `${element.sort_f}/${element.sort_s}/${element.sort_t}`;
          } else {
            element.classArr = element.sort_f;
          }
        });
        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>
