<template>
  <div class="main-contain">
    <div class="position">
      <bread-crumb :crumbs="crumbs"></bread-crumb>
    </div>
    <div class="app-container ">
      <div style="justify-content: space-between;margin: 0px 0 12px 0;display: flex;align-items: center;">
        <div>
          <el-select v-model="type_name" style="width:140px;margin-right:10px;" placeholder="请选择"
          @change="changeTypeName">
            <el-option
              v-for="item in types"
              :key="item.id"
              :label="item.type_name"
              :value="item.id">
            </el-option>
          </el-select>
          <el-date-picker
            size="small"
            v-model="start_time"
            prefix-icon="el-icon-date"
            :editable="false"
            style="width: 196px;"
            type="date"
            placeholder="选择日期时间"
            align="right"
            format="yyyy-MM-dd"
            value-format="yyyy-MM-dd"
            @change="startTimeChange"
           ></el-date-picker>-
            <el-date-picker
              size="small"
              v-model="end_time"
              prefix-icon="el-icon-date"
              :editable="false"
              style="width: 196px;"
              type="date"
              placeholder="选择日期时间"
              align="right"
              format="yyyy-MM-dd"
              value-format="yyyy-MM-dd"
              @change="endTimeChange"
          ></el-date-picker>
          <el-input
            size="small"
            style="width: 200px;margin-left:10px;"
            class="filter-item"
            v-model.trim="keywords"
            placeholder="耗材名称/商品编码/商品名称/规格名称"
          />
          <el-button
            size="small"
            class="filter-item"
            type="primary"
            icon="el-icon-search"
            @click="search"
            >搜索</el-button
          >
        </div>
        <div>
          <el-button size="small" type="primary" @click="exportList">导出</el-button>
          <el-button size="small" type="primary" @click="toPrint">打印</el-button>
        </div>
      </div>
      

      <el-table
        :row-style="{ color: '#303133' }"
        :header-cell-style="{
          backgroundColor: 'rgb(245, 247, 250)',
          color: '#606266'
        }"
        :data="WarehouseInfo.warehouseInfoDate"
        :class="signAndWeighBoxPatients"
        border
        v-loading="WarehouseInfo.loading"
      >
        <el-table-column label="商品编码" align="center">
          <template slot-scope="scope">
            {{ scope.row.good_code }}
          </template>
        </el-table-column>
        <el-table-column label="耗材名称" align="center">
          <template slot-scope="scope">
            {{ scope.row.good_name }}
          </template>
        </el-table-column>
        <el-table-column label="耗材类型" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.type.type_name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="规格型号" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.specification_name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="单位" align="center">
          <template slot-scope="scope">
            <span>{{getUnit(scope.row.good_unit)}}</span>
          </template>
        </el-table-column>
        <el-table-column label="入库数量" align="center">
          <template slot-scope="scope">
            <span
              @click="showStockInDetailDialog(scope.row.good_id)"
              >{{ stockInCount(scope.row) }}</span
            >
          </template>
        </el-table-column>
        <el-table-column label="入库退货" align="center">
          <template slot-scope="scope">
            <span
              @click="showSaleReturnDetailDialog(scope.row)"
              >{{ salesReturnCount(scope.row) }}</span
            >
          </template>
        </el-table-column>
        <el-table-column label="实际入库" align="center">
          <template slot-scope="scope">
            {{ stockInCount(scope.row) - salesReturnCount(scope.row) }}
          </template>
        </el-table-column>
        <el-table-column label="出库数量" align="center">
          <template slot-scope="scope">
            <span
              @click="showStockOutDetailDialog(scope.row.good_id)"
              >{{ stockOutCount(scope.row) }}</span
            >
          </template>
        </el-table-column>
        <el-table-column label="出库退库" align="center">
          <template slot-scope="scope">
            <span
              @click="showCancelStockDetailDialog(scope.row)"
              >{{ cancelStockCount(scope.row) }}</span
            >
          </template>
        </el-table-column>
        <el-table-column label="实际出库" align="center">
          <template slot-scope="scope">
            {{ stockOutCount(scope.row) - cancelStockCount(scope.row) }}
          </template>
        </el-table-column>

        <el-table-column label="剩余库存" align="center">
          <template slot-scope="scope">
            <span>{{
              stockInCount(scope.row) -
                salesReturnCount(scope.row) -
                stockOutCount(scope.row) +
                cancelStockCount(scope.row)
            }}</span>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :page-sizes="[10, 50, 100,200,500,1000]"
        :page-size="10"
        background
        align="right"
        style="margin-top:20px;"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { uParseTime } from "@/utils/tools";
import BreadCrumb from "@/xt_pages/components/bread-crumb";
import {
  GetAllGoodInfo,
  GetAllGoodType,
  getAllStockQueryList
} from "@/api/stock";

export default {
  name: "stockIn",
  created() {
    this.GetAllStockQuery();
    this.GetAllGoodInfo();
    this.GetAllGoodType();
  },
  components: {
    BreadCrumb
  },
  data() {
    return {
      crumbs: [
        { path: false, name: "库存管理" },
        { path: "/stock/query", name: "库存查询" }
      ],
      keywords: "",
      total: 0,
      multipleSelection: [],
      signAndWeighBoxPatients: "sign-and-weigh-box-patients",
      start_time: "",
      end_time: "",
      page: 1,
      limit: 10,
      goodType: [],
      goodInfo: [],
      WarehouseInfo: {
        loading: false,
        warehouseInfoDate: []
      },
      options:[],
      value:"",
      type_name:"",
      types:[],
    };
  },
  methods: {
    GetAllStockQuery: function() {
      console.log(this.keywords);
      const Params = {
        page: this.page,
        limit: this.limit,
        keyword: this.keywords,
        start_time:this.start_time,
        end_time:this.end_time,
        type_name:this.type_name,
      };
     
      this.WarehouseInfo.loading = true;
      this.WarehouseInfo.warehouseInfoDate = [];
      getAllStockQueryList(Params).then(response => {
        if (response.data.state == 0) {
          this.WarehouseInfo.loading = false;
          this.$message.error(response.data.msg);
          return false;
        } else {
          this.WarehouseInfo.loading = false;
          this.total = response.data.data.total;
          for (let i = 0; i < response.data.data.list.length; i++) {
           this.WarehouseInfo.warehouseInfoDate.push( response.data.data.list[i]);
          }
        }
        console.log("数据源头",this.WarehouseInfo.warehouseInfoDate)
         
         for(let i=0;i<this.WarehouseInfo.warehouseInfoDate.length;i++){
            this.WarehouseInfo.warehouseInfoDate[i].type_name = this.WarehouseInfo.warehouseInfoDate[i].type.type_name

            this.WarehouseInfo.warehouseInfoDate[i].stockInCount = this.stockInCount(this.WarehouseInfo.warehouseInfoDate[i])

            this.WarehouseInfo.warehouseInfoDate[i].salesReturnCount = this.salesReturnCount(this.WarehouseInfo.warehouseInfoDate[i])

            this.WarehouseInfo.warehouseInfoDate[i].realCount = this.stockInCount(this.WarehouseInfo.warehouseInfoDate[i]) -  this.salesReturnCount(this.WarehouseInfo.warehouseInfoDate[i])

            this.WarehouseInfo.warehouseInfoDate[i].stockOutCount = this.stockOutCount(this.WarehouseInfo.warehouseInfoDate[i])

            this.WarehouseInfo.warehouseInfoDate[i].cancelStockCount = this.cancelStockCount(this.WarehouseInfo.warehouseInfoDate[i])

            this.WarehouseInfo.warehouseInfoDate[i].realOutCount =  this.stockOutCount(this.WarehouseInfo.warehouseInfoDate[i]) -  this.cancelStockCount(this.WarehouseInfo.warehouseInfoDate[i])

            this.WarehouseInfo.warehouseInfoDate[i].overplus = this.stockInCount(this.WarehouseInfo.warehouseInfoDate[i]) -this.salesReturnCount(this.WarehouseInfo.warehouseInfoDate[i]) - this.stockOutCount(this.WarehouseInfo.warehouseInfoDate[i]) + this.cancelStockCount(this.WarehouseInfo.warehouseInfoDate[i])
         }
        
        console.log("数据2222222",this.WarehouseInfo.warehouseInfoDate)
      });
    },

    GetAllGoodType: function() {
      GetAllGoodType().then(response => {
        if (response.data.state == 0) {
          this.$message.error(response.data.msg);
          return false;
        } else {
           var obj = {
            id:0,
            type_name:'全部'
          }
          this.types.push(obj)
          for (let i = 0; i < response.data.data.goodType.length; i++) {
            this.goodType.push(response.data.data.goodType[i]);
            this.types.push(response.data.data.goodType[i])
          }
          console.log("商品类型",this.types)
        }
      });
    },
    GetAllGoodInfo: function() {
      GetAllGoodInfo().then(response => {
        if (response.data.state == 0) {
          this.$message.error(response.data.msg);
          return false;
        } else {
          for (let i = 0; i < response.data.data.goodInfo.length; i++) {
            this.goodInfo.push(response.data.data.goodInfo[i]);
          }
        }
      });
    },
    handleBack: function() {
      this.$router.go(-1);
    },
    handleSizeChange(val) {
      this.limit = val;
      this.GetAllStockQuery();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.GetAllStockQuery();
    },
    calculate: function(val) {
      return Math.round(parseFloat(val) * 100) / 100;
    },
    startTimeChange: function() {
      this.GetAllStockQuery();
    },
    endTimeChange: function() {
      this.GetAllStockQuery();
    },
    stockInCount: function(row) {
      let total = 0;
      for (let i = 0; i < row.query_warehousing_info.length; i++) {
        total = total + row.query_warehousing_info[i].warehousing_count;
      }
      return total;
    },
    salesReturnCount: function(row) {
      let total = 0;
      for (let i = 0; i < row.query_sales_return_info.length; i++) {
        total = total + row.query_sales_return_info[i].count;
      }
      return total;
    },
    stockOutCount: function(row) {
      let total = 0;
      for (let i = 0; i < row.query_warehouseout_info.length; i++) {
        total = total + row.query_warehouseout_info[i].count;
      }
      return total;
    },
    cancelStockCount: function(row) {
      let total = 0;
      for (let i = 0; i < row.query_cancel_stock_info.length; i++) {
        total = total + row.query_cancel_stock_info[i].count;
      }
      return total;
    },
    showStockInDetailDialog: function(val) {},
    showSaleReturnDetailDialog: function() {},
    showStockOutDetailDialog: function() {},
    showCancelStockDetailDialog: function() {},
    search: function() {
      this.GetAllStockQuery();
    },

    exportList(){
         import('@/vendor/Export2Excel').then(excel => {
         const tHeader = ['商品编码', '耗材名称', '耗材类型','规格型号','入库数量','入库退货','实际入库','出库数量','出库退库','实际出库','剩余库存']
         const filterVal = ['good_code', 'good_name', 'type_name','specification_name','stockInCount','salesReturnCount','realCount','stockOutCount','cancelStockCount','realOutCount','overplus'] 
         const data = this.formatJson(filterVal, this.WarehouseInfo.warehouseInfoDate)
      
         excel.export_json_to_excel({
           header: tHeader,
           data,
           filename: '库存查询'
         })
          this.downloadLoading = false
        })
       },
       formatJson(filterVal, jsonData) {
        return jsonData.map(v => filterVal.map(j => v[j]));
      },
      changeTypeName(){
       this.GetAllStockQuery()
      },
      toPrint(){
        this.$router.push("/stock/stockprint?start_time="+this.start_time+"&end_time="+this.end_time+"&keyword="+this.keywords+"&type_name="+this.type_name)
        
      },

      getUnit(id){
        
        var name = ""
        for(let i=0;i<this.$store.getters.good_unit.length;i++){
           if(id == this.$store.getters.good_unit[i].id){
              name = this.$store.getters.good_unit[i].name
           } 
        }
        return name
      }
  }
};
</script>

<style rel="stylesheet/css" lang="scss" scoped>
.information {
  border: 1px #dcdfe6 solid;
  padding: 30px 20px 30px 20px;

  .border {
    border-bottom: 1px #dcdfe6 solid;
    margin: 0px 0 20px 0;
  }
}

.title {
  background: #409eff;
  height: 44px;
  line-height: 44px;
  padding: 0 0 0 10px;
  color: #fff;
  margin: 0 0 10px 0;
}

.edit_separater {
  border-top: 1px solid rgb(233, 233, 233);
  margin-top: 15px;
  margin-bottom: 15px;
}
</style>

<style>
.sign-and-weigh-box .sign-and-weigh-box-patients .cell {
  font-size: 12px;
}

.sign-and-weigh-box .sign-and-weigh-box-patients .current-row > td {
  background: #6fb5fa;
}

.count {
  color: #bd2c00;
}
.el-table td,
.el-table th.is-leaf,
.el-table--border,
.el-table--group {
  border-color: #d0d3da;
}
.el-table--border::after,
.el-table--group::after,
.el-table::before {
  background-color: #d0d3da;
}
</style>
