<template>
  <div class="stockWarning pagePadding">
    <div v-if=" powers.some((i) => i == '001')">
      <div style="width: 100%">
        <el-row style="padding: 15px 0 0 0px">
          <el-col :span="12" style="width: 290px;margin-right:10px">
            <search-work-company
              :value="parmValue.companyNo"
              :names="''"
              :size="searchSize"
              :is-detail="id !== 'add'"
              :disabled="isDetail"
              :placeholder="'业务公司'"
              @searchChange="supplierChange"
            />
          </el-col>
          <el-col :span="12" style="width: 300px; padding-left: 10px">
            <search-supplier
              :size="searchSize"
              :placeholder="'供应商公司'"
              :disabled="parmValue.companyNo === ''"
              :value="parmValue.supplier_code"
              @searchChange="selectSupplier"
            />
          </el-col>
          <el-col :span="12" style="width: 240px; padding-left: 10px">
            <search-stock
              :isDetail="true"
              :size="searchSize"
              :value="parmValue.stock_code"
              :disabled="supplier_code === '' || parmValue.companyNo === ''"
              :placeholder="'出库仓库'"
              :isRelation="true"
              :companyNo="parmValue.companyNo"
              :companyCode="supplier_code"
              :names="''"
              @searchChange="selectStock"
            />
          </el-col>
          <el-col :span="12" style="width: 290px;margin:0 10px">
            <el-input
              v-model="parmValue.good_name"
              size="mini"
              clearable
              placeholder="商品名称"
              @blur="
              pageInfo.curr = 1;
              parmValue.page = 1;
              searchList();
              "
              @clear="
              pageInfo.curr = 1;
              parmValue.page = 1;
              searchList();
              "
            />
          </el-col>
          <el-col :span="12" style="width: 290px;margin-right:10px">
            <el-input
              v-model="parmValue.spuCode"
              clearable
              size="mini"
              placeholder="商品成本编码"
              @blur="
              pageInfo.curr = 1;
            parmValue.page = 1;
            searchList();
            "
            @clear="
              pageInfo.curr = 1;
              parmValue.page = 1;
              searchList();
              "
            />
          </el-col>
          <el-col :span="12" style="width: 290px;margin-right:10px">
            <el-select
              placeholder="是否库存品"
              clearable
              size="mini"
              v-model="parmValue.is_stock"
              @change="
              pageInfo.curr = 1;
              parmValue.page = 1;
              searchList();
            "
            >
              <el-option value="1" label="是" />
              <el-option value="0" label="否" />
            </el-select>
          </el-col>
        </el-row>
      </div>

      <div class="stock-survey clear">
        <el-row v-for="item in stock_list" :key="item.id" style="float: left">
          <el-col :span="24" class="card">
            <el-card shadow="always">
              <i :class="item.icon" class="icon"></i>
              <countTo style="display: block" :startVal="0" :endVal="item.endVal" :duration="3000"></countTo>
              <p style="margin-top: 5px">{{ item.label }}</p>
            </el-card>
          </el-col>
        </el-row>
      </div>
      <ex-table
        v-loading="loading"
        :table="table"
        :data="tableData"
        :columns="columns"
        :page="pageInfo"
        :size="size"
        @row-click="handClick"
        @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%; height: 30px; line-height: 30px">
            <i class="el-icon-s-unfold" style="height: 10px"></i>库存操作记录
          </div>
        </template>
        <template #type_status="{ scope }">
          <el-tag
            :size="'mini'"
            :type="scope.row.type == '0' ? 'warning' : ''"
            v-text="
            (statusOptions.find((item) => item.id == scope.row.type) || {})
              .label || '--'
          "
          ></el-tag>
        </template>
        <template #is_stock="{scope}">
          <span>{{scope.row.is_stock === '1' ? "是" : "否"}}</span>
        </template>
        <template #reviewer="{ scope }">
          <!-- {{scope.row.item}} -->
          <el-tag v-if="scope.row.item && scope.row.item.length > 0" :size="tablebtnSize">
            <span v-for="(si, sii) in scope.row.item" :key="si + sii">{{ si }}</span>
          </el-tag>
        </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/stock/survey";
import countTo from "vue-count-to";
import { mapGetters } from "vuex";

export default {
  name: "survey",
  mixins: [mixinPage, resToken],
  components: {
    countTo
  },
  computed: {
    ...mapGetters(["tablebtnSize", "searchSize", "size"]),
    powers() {
      const tran =
        this.$store.getters.btnList.find(item => item.menu_route == "survey") ||
        {};
      const { action } = tran ?? {};
      return action ?? [];
    }
  },
  data() {
    return {
      statusOptions: [
        {
          id: "1",
          label: "增加"
        },
        {
          id: "2",
          label: "减少"
        }
      ],
      stock_code: "",
      supplier_code: "",
      parmValue: {
        companyNo: "",
        good_name: "", //商品名称
        spuCode: "", //商品成本编码
        is_stock: "", //是否库存商品
        supplier_code: [],
        stock_code: [],
        page: 1,
        size: 15
      },
      stock_list: [
        {
          id: "1",
          icon: "el-icon-box",
          label: "当前库存",
          endVal: 0
        },
        {
          id: "2",
          icon: "el-icon-download",
          label: "待入库",
          endVal: 0
        },
        {
          id: "3",
          icon: "el-icon-upload2",
          label: "待出库",
          endVal: 0
        },
        {
          id: "4",
          icon: "el-icon-shopping-cart-full",
          label: "可用库存",
          endVal: 0
        },
        {
          id: "5",
          icon: "el-icon-truck",
          label: "在途库存",
          endVal: 0
        }
      ],

      loading: true,
      showModel: false,
      isDetail: false,
      modelId: 0,
      tableData: [], // 表格 - 数据
      table: {
        // 表格 - 参数
        stripe: true,
        border: true
        // _defaultHeader_: ["setcol"],
      },
      // 表格 - 分页
      pageInfo: {
        size: 15,
        curr: 1,
        total: 0
      },
      columns: [
        {
          prop: "good_log_code",
          label: "业务编号",
          width: "160px"
        },
        {
          prop: "good_name",
          label: "商品名称",
          width: "160px"
        },
        {
          prop:"spuCode",
          label:"商品成本编码",
          width:"160px"
        },
        {
          prop:"is_stock",
          label:"是否库存品",
           _slot_: "is_stock",
          width:'70px'
        },
        {
          prop: "action_type_cn",
          label: "库存来源",
          width: "90px"
        },
        {
          prop: "type",
          label: "变化类别",
          _slot_: "type_status",
          width: "70px"
        },
        {
          prop: "stock",
          label: "变动数量",
          width: "90px"
        },
        {
          prop: "companyNo",
          label: "业务公司编号",
          width: "150px"
        },
        {
          prop: "company",
          label: "业务公司名称",
          "min-width": "150px"
        },
        {
          prop: "code",
          label: "仓库所在公司编号",
          width: "150px"
        },
        {
          prop: "name",
          label: "仓库所在公司名称",
          "min-width": "150px"
        },
        {
          prop: "wsm_code",
          label: "仓库编号",
          width: "165px"
        },
        {
          prop: "wsm_name",
          label: "仓库名称",
          "min-width": "100px"
        },

        {
          prop: "action_name",
          label: "操作人",
          width: "70px"
        },
        {
          prop: "reviewer",
          label: "操作人部门",
          _slot_: "reviewer",
          width: "130px"
        },
        {
          prop: "addtime",
          label: "操作时间",
          width: "145px",
          sortable: true
        }
      ]
    };
  },
  mounted() {
    this.stock_code = "";
    this.supplier_code = "";
    this.stockList();
    this.searchList();
  },

  methods: {
    //业务公司选择
    supplierChange(e) {
      const { code } = e;
      this.parmValue.companyNo = code || ""; //供应商编码
      //清空子集
      this.parmValue.supplier_code = [];
      this.supplier_code = "";
      this.parmValue.stock_code = [];

      this.parmValue.page = 1;
      this.pageInfo.curr = 1;
      this.stockList();
      this.searchList();
    },
    // 选择供应商
    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;
      this.stockList();
      this.searchList();
    },

    // 选择供应商仓库
    selectStock(e) {
      const { code } = e;
      this.parmValue.stock_code = code ? [code] : [];
      this.stock_code = code || "";

      this.parmValue.page = 1;
      this.pageInfo.curr = 1;
      this.stockList();
      this.searchList();
    },

    //获取表格上方的卡片数据
    async stockList() {
      let model = {
        companyNo: this.parmValue.companyNo,
        supplier_code: this.supplier_code,
        stock_code: this.stock_code
      };
      const res = await asyncRequest.getstockwsm(model);

      // console.log(res);
      if (res && res.code === 0 && res.data) {
        const {
          intra_stock,
          total_stock,
          usable_stock,
          wait_in_stock,
          wait_out_stock,
          warn_stock
        } = res.data;
        this.stock_list[0].endVal = Number(total_stock);
        this.stock_list[1].endVal = Number(wait_in_stock);
        this.stock_list[2].endVal = Number(wait_out_stock);
        this.stock_list[3].endVal = Number(usable_stock);
        this.stock_list[4].endVal = Number(intra_stock);
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
      }
    },
    //获取表格数据
    async searchList() {
      this.tableData = [];
      this.loading = true;
      let model = JSON.parse(JSON.stringify(this.parmValue));
      model.supplier_code = this.supplier_code;
      model.stock_code = this.stock_code;
      const res = await asyncRequest.list(model);
      if (res && res.code === 0 && res.data) {
        this.tableData = res.data.list;
        this.tableData.forEach(v => {
          if (v.empty) {
            v.code = v.empty.code;
            v.wsm_name = v.empty.wsm_name;
            v.name = v.empty.name;
          }
        });

        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>
 /deep/.el-input {
   margin-bottom: 10px;
 }

.card {
  width: 200px;
  height: 115px;
  flex: 1;
}

@media screen and (max-width: 1500px) {
  .card {
    width: 160px;
  }
}

@media screen and (max-width: 1350px) {
  .card {
    width: 130px;
  }
}

.stock-survey {
  width: 100%;
  display: flex;
  // position: relative;
  justify-content: space-around;
  overflow: hidden;
  padding: 20px 0 20px 10px;
  text-align: center;

  .icon {
    font-size: 40px;
    padding-bottom: 10px;
  }

  p {
    cursor: default;
  }
}
</style>
