<template>
  <div class="main-contain">
    <div class="position">
      <bread-crumb
        v-if="this.$route.query.type == 1"
        :crumbs="crumbs"
      ></bread-crumb>
      <bread-crumb
        v-if="this.$route.query.type == 2"
        :crumbs="crumbs2"
      ></bread-crumb>

      <div style="float:right;">
        <el-button size="small" @click="back()" class="filter-item"
          >取 消</el-button
        >
        <el-button
          size="small"
          type="primary"
          @click="submit()"
          class="filter-item"
           v-loading="loading"
          >保 存</el-button
        >
        <el-button
          size="small"
          type="primary"
          @click="toCheck()"
          class="filter-item"
          v-show="showCheck"
          >审核</el-button
        >

      </div>
    </div>

    <div class="app-container">


      <stock-in-dialog
        ref="dialog" :propForm="propForm"
        :visibility="isVisibility"
        v-on:dialog-comfirm="comfirm"
        v-on:dialog-cancle="cancle"
      >
      </stock-in-dialog>


      <div class="cell clearfix">
         <label class="title"><span class="name"><span style="color:red">*</span>仓库</span> :</label>
         <el-select size="small" v-model="storehouse_id" filterable placeholder="请选择仓库" style="width:200px">
              <el-option
                v-for="(option, index) in list"
                :key="index"
                :label="option.storehouse_name"
                :value="option.id">
              </el-option>
         </el-select>
        <label class="title"><span class="name">出库时间</span> : </label>
        <el-date-picker
          size="small"
          v-model="warehouse_out_time"
          prefix-icon="el-icon-date"
          :editable="false"
          style="width: 200px;"
          type="date"
          placeholder="选择日期时间"
          align="right"
          format="yyyy-MM-dd"
          value-format="yyyy-MM-dd"
        ></el-date-picker>
      </div>


      <el-form :rules="tableRules" :model="recordInfo" ref="tableForm">
        <el-table
          :data="recordInfo.recordData"
          :class="signAndWeighBoxPatients"
          style="width: 100%"
          border
          max-height="450"
          :header-cell-style="{ backgroundColor: 'rgb(245, 247, 250)' }"
        >
         <el-table-column width="200" align="center" fixed="left">
            <template slot="header" slot-scope="scope">
              <span>耗材名称</span>
            </template>

            <template slot-scope="scope">
              <el-form-item style="padding-top: 20px">
                <el-autocomplete
                    class="checkSearch"
                    popper-class="my-autocomplete"
                    v-model="scope.row.good_name"
                    :fetch-suggestions="querySearchAsync"
                    :trigger-on-focus="true"
                    placeholder="请输入耗材名称"
                    @select="handleSelect"
                    @input="changeGoodName(scope.$index)"
                    :popper-append-to-body="true"
                  >
                    <i class="el-icon-search el-input__icon" slot="suffix"></i>
                  <template slot-scope="{ item }">
                    <div class="name">{{item.good_name +"  " +item.specification_name + "  "+  item.manufacturer}}</div>
                  </template>
                </el-autocomplete>
              </el-form-item>
            </template>
          </el-table-column>
          <el-table-column  width="140" align="center">
            <template slot="header" slot-scope="scope">
              <span>耗材类型</span>
            </template>
            <template slot-scope="scope">
              <el-form-item style="padding-top: 20px">
                <el-input
                  placeholder="请输入耗材类型"
                  v-model="scope.row.good_type_id"
                  :value="typeName(scope.row.good_type_id)"
                ></el-input>
              </el-form-item>
            </template>
          </el-table-column>

          <el-table-column width="140" align="center">
            <template slot="header" slot-scope="scope">
              <span>规格&单位<span style="color: red">*</span></span>
            </template>
            <template slot-scope="scope">
              <el-form-item style="padding-top: 20px">
                <el-input v-model="scope.row.name" placeholder="规格&单位"></el-input>
              </el-form-item>
            </template>
          </el-table-column>

          <el-table-column  width="150" align="center">
            <template slot="header" slot-scope="scope">
              <span>剩余库存</span>
            </template>
            <template slot-scope="scope">
              <el-form-item
                :rules="tableRules.count"
                style="padding-top: 17px"
              >
                <el-input
                  :disabled="true"
                  placeholder="剩余库存"
                  type="number"
                  v-model="scope.row.stock_count"
                ></el-input>
                
              </el-form-item>
            </template>
          </el-table-column>

          <!-- <el-table-column width="150" align="center">
            <template slot="header" slot-scope="scope">
              <span>剩余批次详情</span>
            </template>
            <template slot-scope="scope">
              {{ scope.row.warehouse_info_str }} 
            </template>
          </el-table-column> -->


         <el-table-column  width="150" align="center">
            <template slot="header" slot-scope="scope">
              <span>出库数量<span style="color: red">*</span></span>
            </template>
            <template slot-scope="scope">
              <el-form-item
                :prop="'recordData.' + scope.$index + '.count'"
                :rules="tableRules.count"
                style="padding-top: 17px"
              >
                <el-input
                 @input="getSupplyCount(scope.row.count,scope.row.stock_count,scope.$index)"
                  oninput="value=value.replace(/\D|^0/g,'')"
                  placeholder="请输入出库数量"
                  type="number"
                  v-model="scope.row.count"
                ></el-input>
              </el-form-item>
            </template>
          </el-table-column>

          <el-table-column label="出库对象"  width="150" align="center">
            <template slot-scope="scope">
               <el-select size="small" v-model="scope.row.admin_user_id" filterable placeholder="请选择出库对象">
                <el-option
                  v-for="(option, index) in doctorList"
                  :key="index"
                  :label="option.user_name"
                  :value="option.admin_user_id">
                </el-option>
               </el-select>
            </template>
          </el-table-column>


          <el-table-column label="使用班次"  width="100" align="center">
            <template slot-scope="scope">
               <el-select size="small" v-model="scope.row.class_type" filterable placeholder="请选择出库班次">
                <el-option
                  v-for="(option, index) in classType"
                  :key="index"
                  :label="option.name"
                  :value="option.name">
                </el-option>
               </el-select>
            </template>
          </el-table-column>


          <el-table-column label="使用患者"  width="150" align="center">
            <template slot-scope="scope">
               <el-select size="small" v-model="scope.row.patient_id" filterable placeholder="请选择出库对象">
                <el-option
                  v-for="(option, index) in patients"
                  :key="index"
                  :label="option.name"
                  :value="option.id">
                </el-option>
               </el-select>
            </template>
          </el-table-column>
<!--
          <el-table-column  width="150" align="center">
            <template slot="header" slot-scope="scope">
              <span>进货价</span>
            </template>
            <template slot-scope="scope">
              <el-form-item
                :prop="'recordData.' + scope.$index + '.buy_price'"
                :rules="tableRules.buy_price"
                style="padding-top: 20px"
              >
                <el-input
                  :disabled="true"
                  placeholder="进货价"
                  type="number"
                  v-model="scope.row.buy_price"
                ></el-input>
              </el-form-item>
            </template>
          </el-table-column> -->

          <el-table-column  width="150" align="center">
            <template slot="header" slot-scope="scope">
              <span>出货单价<span style="color: red">*</span></span>
            </template>
            <template slot-scope="scope">
              <el-form-item
                :prop="'recordData.' + scope.$index + '.price'"
                :rules="tableRules.price"
                style="padding-top: 20px"
              >
                <el-input
                  placeholder="请输入单价"
                  type="number"
                  v-model="scope.row.price"
                ></el-input>
              </el-form-item>
            </template>
          </el-table-column>

          <el-table-column label="总价"  width="80" align="center">
            <template slot-scope="scope">
              {{ calculate(scope.row.price * scope.row.count) }}
            </template>
          </el-table-column>

          <el-table-column label="生产厂商"  width="150" align="center">
            <template slot-scope="scope">
               <el-select size="small" v-model="scope.row.manufacturer" filterable placeholder="请选择厂商">
                <el-option
                  v-for="(option, index) in manufacturerList"
                  :key="index"
                  :label="option.manufacturer_name"
                  :value="option.id">
                </el-option>
               </el-select>
            </template>
          </el-table-column>

         <el-table-column label="生产日期"  width="150" align="center">
           <template slot-scope="scope">
              <el-date-picker
                prefix-icon="el-icon-date"
                style="width: 145px"
                v-model="scope.row.product_date"
                type="date"
                placeholder="选择日期时间"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
            </template>
         </el-table-column>

          <el-table-column label="有效期"  width="150" align="center">
            <template slot-scope="scope">
              <el-date-picker
                prefix-icon="el-icon-date"
                style="width: 145px"
                v-model="scope.row.expiry_date"
                type="date"
                placeholder="选择日期时间"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
              ></el-date-picker>
            </template>
          </el-table-column>

          <el-table-column  width="150" align="center">
            <template slot="header" slot-scope="scope">
            <span>批准文号<span style="color: red" ></span></span>
          </template>
          <template slot-scope="scope">
              <el-input placeholder="请输入批准文号" v-model="scope.row.license_number" ></el-input>
          </template>
        </el-table-column>

        <el-table-column align="center" width="150">
            <template slot="header" slot-scope="scope">
              <span>注册编码</span>
            </template>
            <template slot-scope="scope">
              <el-form-item style="padding-top: 20px">
                <el-input placeholder="请输入注册编码" v-model="scope.row.register_number" ></el-input>
              </el-form-item>
            </template>
        </el-table-column>

          <el-table-column label="经销商"  width="150" align="center">
            <template slot-scope="scope">
               <el-select size="small" v-model="scope.row.dealer" filterable placeholder="请选择厂商">
                <el-option
                  v-for="(option, index) in dealerList"
                  :key="index"
                  :label="option.dealer_name"
                  :value="option.id">
                </el-option>
               </el-select>
            </template>
          </el-table-column>

          <el-table-column label="备注"  width="150" align="center">
            <template slot-scope="scope">
              <el-input v-model="scope.row.remark"></el-input>
            </template>
          </el-table-column>

          <el-table-column label="操作" align="center" min-width="150" fixed="right">
            <template slot-scope="scope">
              <el-tooltip
                class="item"
                effect="dark"
                content="新增"
                placement="top"
              >
                <el-button
                  size="mini"
                  type="primary"
                  icon="el-icon-circle-plus-outline"
                  @click="handleEdit(scope.$index, scope.row)"
                >
                </el-button>
              </el-tooltip>
              <el-tooltip
                class="item"
                effect="dark"
                content="删除"
                placement="top"
              >
                <el-button
                  size="mini"
                  type="danger"
                  icon="el-icon-delete"
                  @click="handleDelete(scope.$index, scope.row)"
                >
                </el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
    </div>
  </div>
</template>

<script>
import { uParseTime } from "@/utils/tools";
import {  postWarehouseOut,GetAllConfig,GetAllGoodInfoByID,postSearchGoodList,getStockBatchNumber,checkWarehouseOut } from "@/api/stock";

import BreadCrumb from "../components/bread-crumb";
import StockInDialog from './Dialog/stockInDialog'
import { getDataConfig } from '@/utils/data'
export default {
  components: { StockInDialog,  BreadCrumb },
  name: "salesReturnOrderAdd",

  data() {
    return {
      crumbs: [
        { path: false, name: "库存管理" },
        { path: false, name: "耗材出库单" },
        { path: false, name: "新增出库单" }
      ],
      crumbs2: [
        { path: false, name: "库存管理" },
        { path: false, name: "其他出库单" },
        { path: false, name: "新增出库单" }
      ],
      type: this.$route.query.type,
      signAndWeighBoxPatients: "sign-and-weigh-box-patients",
      warehouse_out_time: "",
      currentIndex: 0,
      recordInfo: {
        recordData: []
      },
      tableRules: {
        price: [{ required: true, message: "单价不能为空", trigger: "blur" }],
        count: [{ required: true, message: "数量不能为空", trigger: "blur" }],
      },
      ruleForm: {
        manufacturer: [
          { required: true, message: "请选择厂商", trigger: "change" }
        ]
      },
      // prop
      isVisibility: false,
      propForm: {
        goods: [],
        goodType: [],
        goodInfo: [],
        goodUnit: [],
        manufacturer: 0,
        dealer: 0
      },
      form: {
        manufacturer: 0,
        dealer: 0
      },
      warehouseInfoList: [],
      manufacturer: [],
      dealerList: [],
      goodType: [],
      goodInfo:[],
      goodList:[],
      manufacturerList:[],
      numberList:[],
      storehouse_id:"",
      list:[],
      doctorList:[],
      showCheck:false,
      showReturnCheck:false,
      warehouseOut:{},
      warehouseOut_id:0,
      is_check:0,
      loading:false,
      patients:[],
      classType:[{id:1,name:"上午"},{id:2,name:"下午"},{id:3,name:"晚上"}],
      tableWarehouseInfoList:[],
      org_id:0,
    };
  },
  methods: {
    comfirm: function(val) {
      this.propForm.goodType = []
      this.propForm.goods = []

      this.$refs.dialog.hide()

      if (val.selectedGoodInfo.length > 0) {
        for (let i = val.selectedGoodInfo.length - 1; i >= 0; i--) {
          if (i == 0) {
            this.recordInfo.recordData[this.currentIndex].good_type_id = val.selectedGoodInfo[i].good_type_id
            this.recordInfo.recordData[this.currentIndex].good_id = val.selectedGoodInfo[i].id
            this.recordInfo.recordData[this.currentIndex].price = val.selectedGoodInfo[i].buy_price.toString()
            this.recordInfo.recordData[this.currentIndex].good_unit = val.selectedGoodInfo[i].good_unit
          } else {
            const tempForm = {}
            tempForm['good_type_id'] =val.selectedGoodInfo[i].good_type_id
            tempForm['good_id'] = val.selectedGoodInfo[i].id
            tempForm['count'] = ''
            tempForm['price'] = val.selectedGoodInfo[i].buy_price.toString()
            tempForm['remark'] = ''
            tempForm['good_unit'] = val.selectedGoodInfo[i].good_unit
            this.recordInfo.recordData.splice(this.currentIndex + 1, 0, tempForm)
          }
        }
      }

      this.currentIndex = -1


    },
    cancle: function() {
      this.$refs.dialog.hide();
      this.propForm.goodType = [];
      this.propForm.goods = []

    },
    GetConfigInfo: function() {

      GetAllConfig().then(response => {
        const loading = this.$loading({
          lock: true,
          text: "Loading",
          spinner: "el-icon-loading",
          background: "rgba(0, 0, 0, 0.7)"
        });

        if (response.data.state == 0) {
          this.$message.error(response.data.msg);
          return false;
        } else {
          loading.close();
          this.manufacturer = response.data.data.manufacturer
          this.dealer = response.data.data.dealer
          this.goodType = response.data.data.goodType
          this.goodInfo = response.data.data.goodInfo
          this.manufacturer.splice(0, 0, { id: 0, manufacturer_name: '全部' })
          this.dealer.splice(0, 0, { id: 0, dealer_name: '全部' })
          this.manufacturer.splice(0, 0, { id: -1, manufacturer_name: '其他' })
          this.dealer.splice(0, 0, { id: -1, dealer_name: '其他' })
          this.list = response.data.data.list
          this.storehouse_id = response.data.data.configlist.storehouse_out_info
          this.doctorList =  response.data.data.doctorList
          var obj ={id:0,name:"血透室"}
          // var objOne ={id:-1,name:"血透室"}
          this.patients =[]
          this.patients.push(obj)
          // this.patients.push(objOne)
          if(response.data.data.patients!=null){
            for(let i=0;i<response.data.data.patients.length;i++){
              this.patients.push(response.data.data.patients[i])
            }
          }
          
        }
      });
    },
    typeName: function(good_type_id) {
      let name = "";
      for (let i = 0; i < this.goodType.length; i++) {
        if (this.goodType[i].id == good_type_id) {
          name = this.goodType[i].type_name;
        }
      }
      return name;
    },
    typeNameOne:function(good_info_id){
       let name = ''
      for (let i = 0; i < this.goodInfo.length; i++) {
        if (this.goodInfo[i].id == good_info_id) {
          name = this.goodInfo[i].good_name
        }
      }
      return name
    },
    specificationName: function(good_info_id) {
      let name = ''
      for (let i = 0; i < this.goodInfo.length; i++) {
        if (this.goodInfo[i].id == good_info_id) {
          name = this.goodInfo[i].specification_name
        }
      }
      return name
    },
    handleEdit: function(index, row) {
      const tempObj = {};
      tempObj["good_type_id"] = 0;
      tempObj["good_id"] = 0;
      tempObj["count"] = "";
      tempObj["price"] = "";
      tempObj["remark"] = "";
      tempObj["good_unit"] = "";
      tempObj["number"] = ""
      tempObj["specification_name"] = ""
      tempObj["expiry_date"] = ""
      tempObj["product_date"] = ""
      tempObj["license_number"] = ""
      tempObj["warehouse_info_id"] = 0
      tempObj["admin_user_id"] = this.$store.getters.xt_user.user.user_name
      tempObj["buy_price"]= ""
      tempObj["stock_count"] = ""
      tempObj["id"] = 0
      tempObj['register_number'] = ''
      tempObj['patient_id'] =0
      tempObj["class_type"] = ""
      tempObj["dealer"] = ""
      tempObj["manufacturer"] = ""
      this.recordInfo.recordData.push(tempObj);
    },
    handleDelete: function(index, row) {
      if (this.recordInfo.recordData.length <= 1) {
        this.$message.error("只有一条记录的时候无法删除");
        return;
      } else {
        this.recordInfo.recordData.splice(index, 1);
      }
    },
    calculate: function(val) {
      if (isNaN(val)) {
        return "";
      }
      if (val == 0) {
        return "";
      }
      return Math.round(parseFloat(val) * 100) / 100;
    },
    getTime(val, temp) {
      if (val != 0) {
        return uParseTime(val, temp);
      } else {
        return "";
      }
    },
   getTimeOne(val) {
      if(val < 0){
        return ""
      }
      if(val == ""){
      return ""
      }else {
      return uParseTime(val, '{y}-{m}-{d}')
      }
    },
    showDialog(index, row) {
      const loading = this.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
      })
      this.currentIndex = index
      const params = {
        manufacturer_id: this.form.manufacturer,
        dealer_id: this.form.dealer
      }
      this.propForm.goods = []
      GetAllGoodInfoByID(params).then(response => {
          if (response.data.state == 0) {
            this.$message.error(response.data.msg)
            return false
            loading.close()

          } else {
            loading.close()

            if (response.data.data.goodInfo.length <= 0) {
              this.$message.error('该厂商或经销商没有物品信息')
              return
            }
            this.$refs.dialog.show()
            for (let i = 0; i < response.data.data.goodInfo.length; i++) {
              this.propForm.goodType.push(response.data.data.goodInfo[i].type)
            }
            const obj = {}
            this.propForm.goodType = this.propForm.goodType.reduce((cur, next) => {
              obj[next.id] ? '' : obj[next.id] = true && cur.push(next)
              return cur
            }, []) // 设置cur默认类型为数组，并且初始值为空的数组
          }

          for (let i = 0; i < this.propForm.goodType.length; i++) {
            let goodInfo = []
            let goodObj = {}

            for (let a = 0; a < response.data.data.goodInfo.length; a++) {
              var respObj = response.data.data.goodInfo[a]
              respObj['isSelected'] = false
              if (respObj.type.id == this.propForm.goodType[i].id) {
                goodInfo.push(respObj)
              }
            }
            const obj = {}
            goodInfo = goodInfo.reduce((cur, next) => {
              obj[next.id] ? '' : obj[next.id] = true && cur.push(next)
              return cur
            }, []) // 设置cur默认类型为数组，并且初始值为空的数组

            this.$set(goodObj, this.propForm.goodType[i].id, goodInfo)
            this.propForm.goods.push(goodObj)

          }

        }
      )

      this.$refs.dialog.show();
    },
    back() {
      this.$router.go(-1);
    },
    submit() {
      this.loading = true
      this.$refs["tableForm"].validate(valid => {
        if (valid) {
          const array = this.recordInfo.recordData;
          let total = 0;
          for (let i = 0; i < array.length; i++) {
            if (array[i].good_type_id == 0) {
              this.$message.error("商品类型不能为空");
              this.loading = false
              return;
            }
            if (array[i].good_id == 0) {
              this.$message.error("规格名称不能为空");
              this.loading = false
              return;
            }
            total = total + array[i].price * array[i].return_count;
          }
          if (this.recordInfo.recordData.length <= 0) {
            this.$message.success("请添加出库商品");
            this.loading = false
            return;
          }

          for(let i=0;i<this.recordInfo.recordData.length;i++){
          
            if( this.recordInfo.recordData[i].count == 0){
              this.$message.error("出库数量不能为空!")
              this.loading = false
              return
            } 

             if(this.recordInfo.recordData[i].dealer == ""){
                this.recordInfo.recordData[i].dealer = 0
             }
            if(this.recordInfo.recordData[i].expiry_date == "" || this.recordInfo.recordData[i].expiry_date == undefined){
              this.recordInfo.recordData[i].expiry_date = ""
            }
            if(this.recordInfo.recordData[i].product_date == "" || this.recordInfo.recordData[i].product_date == undefined){
              this.recordInfo.recordData[i].product_date = ""
            }

             this.recordInfo.recordData[i].price = this.recordInfo.recordData[i].price.toString()

             this.recordInfo.recordData[i].stock_count = this.recordInfo.recordData[i].stock_count.toString()

             this.recordInfo.recordData[i].buy_price = this.recordInfo.recordData[i].buy_price.toString()

              this.recordInfo.recordData[i].count = this.recordInfo.recordData[i].count.toString()

             

          }

          for(let i=0;i<this.recordInfo.recordData.length;i++){
             
            if(parseInt(this.recordInfo.recordData[i].count) > parseInt(this.recordInfo.recordData[i].stock_count) ){
               this.$message.error(this.recordInfo.recordData[i].good_name+"/"+this.recordInfo.recordData[i].specification_name+"库存不足!")
               this.loading = false
               return
            }

            for(let j=0;j<this.manufacturerList.length;j++){
              if(this.recordInfo.recordData[i].manufacturer == this.manufacturerList[j].manufacturer_name){
                 this.recordInfo.recordData[i].manufacturer = this.manufacturerList[j].id
              }
            }
            for(let z=0;z<this.dealerList.length;z++){
              if(this.recordInfo.recordData[i].dealer == this.dealerList[z].dealer_name){
                  this.recordInfo.recordData[i].dealer = this.dealerList[z].id
              }
            }

            for(let y=0;y<this.numberList.length;y++){
               if(this.recordInfo.recordData[i].number == this.numberList[y].id){
                   this.recordInfo.recordData[i].number = this.numberList[y].number
               }
            }

            for(let s=0;s<this.doctorList.length;s++){

              if(this.recordInfo.recordData[i].admin_user_id == this.doctorList[s].user_name){
                this.recordInfo.recordData[i].admin_user_id = this.doctorList[s].admin_user_id
              }

            }
            
          }
          const params = {
            stockOut: this.recordInfo.recordData
          };
          console.log("pramsswoowowowoow",params)
          console.log(" this.warehouseOut_id", this.warehouseOut_id)
          const loading = this.$loading({
            lock: true,
            text: 'Loading',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          })
          postWarehouseOut(
            params,
            this.warehouse_out_time,
            this.form.dealer,
            this.form.manufacturer,
            this.type,
            this.storehouse_id,
            this.warehouseOut_id
          ).then(response => {
            if (response.data.state == 0) {
              this.$message.error("库存不足")
              loading.close()
              this.loading = false
              return false;
            } else {
               var msg = response.data.data.msg
               this.loading = false
               loading.close()
               var good_name = response.data.data.good_name
               var specification_name = response.data.data.specification_name
               var storehose_name = response.data.data.storehose_name
               var str = storehose_name + " " + good_name +"*" + specification_name +"无库存,请入库"
               var warehouseOut =  response.data.data.warehouseOut
               this.warehouseOut = warehouseOut
               this.warehouseOut_id = warehouseOut.id

               if(msg == 1){
                  this.$message.error(str)
               }

               if(msg == 2){
                 this.$message.success("保存成功");
                 this.recordInfo.recordData = [];
                 var warehouseOutInfoList =  response.data.data.warehouseOutInfoList
                 var manufacturerList = response.data.data.manufacturerList
                 var dealerList = response.data.data.dealerList

                 var doctorList = response.data.data.doctorList
                 for(let i=0;i<warehouseOutInfoList.length;i++){
                    for(let j=0;j<manufacturerList.length;j++){
                       if(warehouseOutInfoList[i].manufacturer == manufacturerList[j].id){
                          warehouseOutInfoList[i].manufacturer = manufacturerList[j].manufacturer_name
                       }
                    }
                    for(let z=0;z<dealerList.length;z++){
                      if(warehouseOutInfoList[i].dealer == dealerList[z].id){
                         warehouseOutInfoList[i].dealer_id = dealerList[z].dealer_name
                      }
                    }
                    for(let s=0;s<doctorList.length;s++){
                       if(warehouseOutInfoList[i].admin_user_id == doctorList[s].admin_user_id){
                          warehouseOutInfoList[i].admin_user_id = doctorList[s].user_name
                       }
                    }
                    warehouseOutInfoList[i].good_name = warehouseOutInfoList[i].GoodInfo.good_name

                    warehouseOutInfoList[i].name =  warehouseOutInfoList[i].GoodInfo.specification_name +"/"+ warehouseOutInfoList[i].GoodInfo.packing_unit
                    warehouseOutInfoList[i].expiry_date = this.getTimeOne(warehouseOutInfoList[i].expiry_date)
                    warehouseOutInfoList[i].product_date = this.getTimeOne(warehouseOutInfoList[i].product_date)
                    warehouseOutInfoList[i].patient_id = warehouseOutInfoList[i].patient_id
                    

                 }
                 this.recordInfo.recordData = []
                 this.recordInfo.recordData = warehouseOutInfoList

                 this.showCheck = true
               }
               loading.close()
               this.loading = false
               loading.close()

            }
          });
        } else {
          return false;
        }
      });
    },
    changeManufacturer(val) {
      this.propForm.manufacturer = val;
    },
    changeDealer(val) {
      this.propForm.dealer = val;
    },
    querySearchAsync(keyword, cb) {
      let key = '';
      if (keyword != undefined) {
        key = keyword
      }

      postSearchGoodList(key,this.storehouse_id).then(response => {
        if (response.data.state == 1) {
            var list = response.data.data.list

            this.goodList = list
            var manufacturerList = response.data.data.manufacturerList
            this.manufacturerList = manufacturerList
            var dealerList =  response.data.data.dealerList
            this.dealerList = dealerList
            for(let i=0;i<this.goodList.length;i++){
              for(let j=0;j<this.manufacturerList.length;j++){
                if(this.goodList[i].manufacturer == this.manufacturerList[j].id){
                    this.goodList[i].manufacturer = this.manufacturerList[j].manufacturer_name
                }
              }

              for(let z=0;z<this.dealerList.length;z++){
                if(this.goodList[i].dealer == this.dealerList[z].id){
                   this.goodList[i].dealer = this.dealerList[z].dealer_name
                }
              }
            }


            cb(this.goodList)
        } else {
          cb([])
        }
      })
    },
    changeGoodName(val){
      this.currentIndex = val
    },
    handleSelect(val){
        console.log("val000000000",val)
        var arr = []
        for(let i=0;i<this.recordInfo.recordData.length;i++){
           arr.push(this.recordInfo.recordData[i].good_id)
        }
        var str = arr.join(",")

        if(str.indexOf(val.id)!=-1){
          this.$message.error("该耗材已存在列表中")
          return
        }
        //获取当前耗材的批号
        this.getStockBatchNumber(val.id)
        var total_count = 0
        for(let i=0;i<val.xt_warehouse_info.length;i++){
           total_count += val.xt_warehouse_info[i].stock_count
        }

        for(let i=0;i<this.recordInfo.recordData.length;i++){
          if(this.currentIndex == i){
            this.recordInfo.recordData[i].id = 0
            this.recordInfo.recordData[i].good_name = val.good_name
            this.recordInfo.recordData[i].good_type_id = val.good_type_id
            this.recordInfo.recordData[i].name = val.specification_name +"/"+val.packing_unit
            this.recordInfo.recordData[i].manufacturer = val.first_xt_warehouse_info.manufacturer
            this.recordInfo.recordData[i].remark = val.remark
            this.recordInfo.recordData[i].buy_price = val.buy_price
            this.recordInfo.recordData[i].stock_count = total_count
            this.recordInfo.recordData[i].license_number = val.number
            this.recordInfo.recordData[i].register_number = val.register_number
            this.recordInfo.recordData[i].patient_id = 0
            this.recordInfo.recordData[i].xt_warehouse_info = val.xt_warehouse_info
            this.recordInfo.recordData[i].packing_unit = val.packing_unit
            if( val.first_xt_warehouse_info!=null&&val.first_xt_warehouse_info.expiry_date >0){
              this.recordInfo.recordData[i].expiry_date = this.getTimeOne(val.first_xt_warehouse_info.expiry_date)
            }else{
              this.recordInfo.recordData[i].expiry_date = ""
            }

            if(val.first_xt_warehouse_info!=null&&val.first_xt_warehouse_info.product_date >0){
              this.recordInfo.recordData[i].product_date = this.getTimeOne(val.first_xt_warehouse_info.product_date)
            }else{
              this.recordInfo.recordData[i].product_date = ""
            }
           
          
            if(val.packing_price == 0){
              this.recordInfo.recordData[i].price ="0"
            }else{
              this.recordInfo.recordData[i].price = val.packing_price
            }

            if(this.org_id == 0 || this.org_id == 10633){
               this.recordInfo.recordData[i].price = val.first_xt_warehouse_info.packing_price
            }

            this.recordInfo.recordData[i].specification_name = val.specification_name
            this.recordInfo.recordData[i].good_id = val.id
            if(val.dealer == 0){
             this.recordInfo.recordData[i].dealer = ""
            }else{
             this.recordInfo.recordData[i].dealer = val.first_xt_warehouse_info.dealer
            }

            console.log("日期我我我哦我",  this.recordInfo.recordData)
         }


      }
    },

    getStockBatchNumber(id){
       var params = {
         id:id,
         storehouse_id:this.storehouse_id,
       }
       console.log("parmas----------------------",params)
      getStockBatchNumber(params).then(response=>{
        if(response.data.state == 1){
          var list = response.data.data.list
          this.numberList = list
          // var lastWarehouseInfoTwo = response.data.data.lastWarehouseInfoTwo
          // this.tableWarehouseInfoList = lastWarehouseInfoTwo
        }
     })
    },
    getDataConfig(module, filed_name) {
      return getDataConfig(module, filed_name)
    },
    changeNumberList(val,index){
       for(let i=0;i<this.recordInfo.recordData.length;i++){
         if(index == i){
          this.recordInfo.recordData[i].warehouse_info_id = val.number
         }
       }

    },
    getSupplyCount(count,sum_count,index){
       
      if(sum_count < count){
        this.$message.error("出库数量不能大于剩余库存")
        for(let i=0;i<this.recordInfo.recordData.length;i++){
          if(index == i){
            this.recordInfo.recordData[i].count = ""
          }
          return
        }
      }else{
       for(let i=0;i<this.recordInfo.recordData.length;i++){
          if(index == i){
            this.recordInfo.recordData[i].count = count
          }
          return
        }
      }

    },
    toCheck(){
       const loading = this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
       var params = {
         id:this.warehouseOut.id
       }
      checkWarehouseOut(params).then(response=>{
        if(response.data.state == 1){
          var msg = response.data.data.msg
          var good_name = response.data.data.good_name
          var specification_name = response.data.data.specification_name
          var storehose_name = response.data.data.storehose_name
          var str = storehose_name + " " + good_name +"*" + specification_name +"库存不足,请入库"
          if(msg == 1){
            this.$message.error(str)
            loading.close()
           }
           if(msg == 2){
            this.$message.success("审核成功!")
            this.recordInfo.recordData = [];
            this.$router.back(-1);
            this.showCheck = false
            this.showReturnCheck = true
            loading.close()
           }
        }else{
          this.$message.error("审核失败!")
          loading.close()
        }
      })
    },
    toReturnCheck(){
       const loading = this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.7)'
        })
       var params = {
         id:this.warehouseOut_id,
       }
      returnCheckWarehouseOut(params).then(response=>{
         if(response.data.state == 1){
            var list = response.dat.data.list
            this.$message.success("反审核成功")
             this.showCheck = true
            this.showReturnCheck = false
            loading.close()
         }else{
          this.$message.error("反审核失败!")
          loading.close()
         }
      })
    }

  },
  created() {
    this.org_id = this.$store.getters.xt_user.org.id
    var nowDate = new Date();
    var nowYear = nowDate.getFullYear();
    var nowMonth = nowDate.getMonth() + 1;
    var nowDay = nowDate.getDate();
    this.warehouse_out_time =
      nowYear +
      "-" +
      (nowMonth < 10 ? "0" + nowMonth : nowMonth) +
      "-" +
      (nowDay < 10 ? "0" + nowDay : nowDay);
    const tempObj = {};
    tempObj["good_type_id"] = 0;
    tempObj["good_id"] = 0;
    tempObj["return_count"] = 0;
    tempObj["price"] = "";
    tempObj["remark"] = "";
    tempObj["good_unit"] = ""
    tempObj["number"] = ""
    tempObj["specification_name"] = ""
    tempObj["dealer"] = ""
    tempObj["license_number"] = ""
    tempObj["warehouse_info_id"] = 0
    tempObj["admin_user_id"] = this.$store.getters.xt_user.user.user_name
    tempObj["buy_price"]= ""
    tempObj["stock_count"] = ""
    tempObj["id"] = 0
    tempObj['register_number'] = ''
    tempObj['patient_id'] = 0
    tempObj["class_type"] = ""
    tempObj["product_date"] = ""
    tempObj["expiry_date"] = ""
    tempObj["count"] = ""
    tempObj["dealer"] = ""
    tempObj["manufacturer"] = ""
   
    this.recordInfo.recordData.push(tempObj);
    this.GetConfigInfo();
    this.propForm.goodUnit = this.$store.getters.good_unit;
    this.unitList =  getDataConfig('hemodialysis','units')

  }
};
</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;
  }
}

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

<style lang="scss">
.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;
}
.el-autocomplete-suggestion{
  // width: 400px !important;
  width: auto !important;
}
::-webkit-scrollbar{
  height: 20px;
}
</style>
