
<template>
  <div class="survey pagePadding">
    <div
      v-if="powers && powers.length > 0 && powers.some((item) => item == '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="24">
                <el-col :span="12" style="width: 300px; padding: 0">
                  <search-supplier
                    :size="searchSize"
                    :placeholder="'供应商公司'"
                    :value="parmValue.supplier_code"
                    @searchChange="selectSupplier"
                  />
                </el-col>
                <el-col :span="12" style="width: 200px; padding-left: 10px">
                  <search-stock
                    :isDetail="true"
                    :size="searchSize"
                    :value="parmValue.stock_code"
                    :disabled="isDisabled"
                    :placeholder="'出库仓库'"
                    :isRelation="true"
                    :companyCode="supplier_code"
                    :names="''"
                    @searchChange="selectStock"
                  />
                </el-col>
                <el-col style="width: 200px; padding-left: 10px">
                  <el-input
                    placeholder="商品编码"
                    :size="searchSize"
                    v-model="parmValue.goods_sn"
                    @blur="searchList"
                    clearable
                  />
                </el-col>
                <el-col :span="4" style="width: 54px; margin-left: 10px">
                  <el-button
                    :size="searchSize"
                    class="fr"
                    type="warning"
                    @click="restSearch"
                    >重置</el-button
                  >
                </el-col>
                <el-col :span="3" style="width: 56px; float: right">
                  <el-button
                    type="primary"
                    :size="searchSize"
                    style="float: right; margin-left: 5px"
                    @click="searchList"
                  >
                    刷新
                  </el-button>
                </el-col>
              </el-col>
            </el-row>
            <el-row>
              <el-col :span="6" style="width: 273px; padding: 10px 0 0 0px">
                <num-ber-range
                  :lower="parmValue.stock_num_low"
                  :online="parmValue.stock_num_up"
                  :title="'库存量'"
                  :width="'120px'"
                  :size="searchSize"
                  @numReturned="numReturned($event, 0)"
                />
              </el-col>
              <el-col :span="6" style="width: 303px; padding-top: 10px">
                <num-ber-range
                  :lower="parmValue.warn_num_low"
                  :online="parmValue.warn_num_up"
                  :width="'135px'"
                  :title="'预警量'"
                  :size="searchSize"
                  @numReturned="numReturned1($event, 0)"
                />
              </el-col>
              <el-col :span="4" style="width: 44px; padding-top: 10px">
                <el-button
                  style="width: 100%"
                  type="primary"
                  icon="el-icon-search"
                  :size="searchSize"
                  @click="searchList"
                >
                </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((item) => item == '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((item) => item == '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((item) => item == '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>
      </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() {
      let tran =
        this.$store.getters.btnList.find(
          (item) => item.menu_route == "stockWarning"
        ) || {};
      if (tran && tran.action && tran.action.length > 0) {
        return tran.action;
      } else {
        return [];
      }
    },
  },
  data() {
    return {
      stock_code: "",
      supplier_code: "",
      isDisabled: true,
      statusList: statusList,
      loading: true,
      showModel: false,
      isDetail: false,
      modelId: 0,
      parmValue: {
        goods_sn: "",
        supplier_code: [],
        stock_code: [],
        warn_num_low: "",
        warn_num_up: "",
        stock_num_low: "",
        stock_num_up: "",
      },
      // 表格 - 数据
      tableData: [],
      // 表格 - 参数
      table: {
        stripe: true,
        border: true,
        _defaultHeader_: ["setcol"],
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0,
      },
      // 表格 - 列参数
      columns: colums,
    };
  },
  mounted() {
    this.searchList();
  },

  methods: {
    // 选择供应商仓库
    selectStock(e) {
      if (e && e.id) {
        this.parmValue.stock_code = [e.code];
        this.stock_code = e.code;
      } else {
        this.parmValue.stock_code = [];
        this.stock_code = "";
      }
      if (this.parmValue.stock_code) {
        this.searchList();
      }
    },
    // 选择供应商
    selectSupplier(e) {
      if (e && e.id) {
        this.parmValue.supplier_code = [e.code];
        this.supplier_code = e.code;
        this.isDisabled = false;
      } else {
        this.parmValue.stock_code = [];
        this.parmValue.supplier_code = [];
        this.stock_code = "";
        this.supplier_code = "";
        this.isDisabled = true;
      }
      if (this.parmValue.supplier_code) {
        this.searchList();
      }
    },
    // 预警量范围框
    numReturned1(e) {
      if (e && e.lower != "" && e.online != "") {
        this.parmValue.warn_num_low = e.lower;
        this.parmValue.warn_num_up = e.online;
      } else {
        this.parmValue.warn_num_low = "";
        this.parmValue.warn_num_up = "";
      }
      if (
        this.parmValue.warn_num_low != "" &&
        this.parmValue.warn_num_up != ""
      ) {
        this.searchList();
      }
    },
    // 库存量范围框
    numReturned(e) {
      if (e && e.lower != "" && e.online != "") {
        this.parmValue.stock_num_low = e.lower;
        this.parmValue.stock_num_up = e.online;
      } else {
        this.parmValue.stock_num_low = "";
        this.parmValue.stock_num_up = "";
      }
      if (
        this.parmValue.stock_num_low != "" &&
        this.parmValue.stock_num_up != ""
      ) {
        this.searchList();
      }
    },
    // 重置搜索栏
    restSearch() {
      // 表格 - 分页
      this.pageInfo = {
        size: 15,
        curr: 1,
        total: 0,
      };
      this.parmValue = {
        goods_sn: "",
        supplier_code: [],
        stock_code: [],
        warn_num_low: "",
        warn_num_up: "",
        stock_num_low: "",
        stock_num_up: "",
      };
      this.searchList();
    },

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

    async searchList() {
      this.loading = true;
      const {
        supplier_code,
        stock_code,
        goods_sn,
        stock_num_low,
        stock_num_up,
        warn_num_low,
        warn_num_up,
      } = JSON.parse(JSON.stringify(this.parmValue));
      let model = {
        supplierNo: supplier_code.toString() || "",
        wsm_code: stock_code.toString() || "",
        page: 1,
        size: 15,
        stock_low: stock_num_low,
        stock_up: stock_num_up,
        warn_low: warn_num_low,
        warn_up: warn_num_up,
        good_code: goods_sn,
        good_name: "",
        type_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>
