<template>
  <div>
    <div v-show="showSearch">
     <el-autocomplete
        class="checkSearch"
        popper-class="my-autocomplete"
        v-model="form.drug_name"
        :fetch-suggestions="querySearchAsync"
        :trigger-on-focus="true"
        placeholder="请输入药品名称"
        @select="handleSelect"
        style="width:300px;"
      >
        <i class="el-icon-search el-input__icon" slot="suffix"></i>
      <template slot-scope="{ item }">
        <div class="name">{{ item.drug_name + item.dose + item.dose_unit + "*" +item.min_number + item.min_unit +"/" + item.max_unit + "  "+item.manufacturer}}</div>
      </template>
    </el-autocomplete>  
  </div>
   <div v-show="showTable">
    <div>日期：{{nowTime}}   盘点人：{{user_name}}</div>
    <div style="float:right;margin-bottom:10px"><el-button type="primary" @click="saveInentoryList">保存</el-button></div>
    <el-collapse v-model="activeNames">
      <el-collapse-item :name="index" v-for="(item,index) in tableData" :key="index">
        <template slot="title">
          <div style="font-size:16px">{{item.drug_name}}</div> &nbsp;&nbsp;{{item.specification_name}} <el-button type="warning" icon="el-icon-remove" style="margin-left:20px;" @click="toMove(item,index)">移除</el-button>
        </template>
        <el-table :data="item.child" border :header-cell-style="{ backgroundColor: 'rgb(245, 247, 250)' }" style="width:100%">
              <el-table-column prop="date" label="药品名称" width="150" align="center">
                <template slot-scope="scope">
                  {{scope.row.drug_name}}
                </template>
              </el-table-column>
              <el-table-column  prop="name" label="规格" width="100"  align="center">
                <template slot-scope="scope">
                  {{scope.row.specification_name}}
                </template>
              </el-table-column>
             <el-table-column  prop="name" label="入库单号" width="100"  align="center">
                <template slot-scope="scope">
                  {{scope.row.warehousing_order}}
                </template>
              </el-table-column>
              <el-table-column  prop="name" label="有效期" width="100"  align="center">
                <template slot-scope="scope">
                  {{getTime(scope.row.expiry_date)}}
                </template>
              </el-table-column>
              <el-table-column prop="name" label="批号" width="100"  align="center">
                  <template slot-scope="scope">
                    {{scope.row.batch_number}}
                </template>
              </el-table-column>
              <el-table-column prop="name" label="进货价" width="100"  align="center">
                  <template slot-scope="scope">
                    {{scope.row.price}}
                  </template> 
              </el-table-column>
            
              <el-table-column prop="name" label="生产厂商" width="100"  align="center">
                  <template slot-scope="scope">
                    {{scope.row.manufacturer_name}}
                  </template> 
              </el-table-column>
              <el-table-column prop="name" label="盘点前数量" width="180"  align="center">
                <template slot-scope="scope">
                    <span><el-input style="width:50px" v-model="scope.row.stock_max_number" :disabled="true"></el-input>{{scope.row.max_unit}}</span> 
                    <span v-if="scope.row.XtBaseDrug.max_unit!=scope.row.XtBaseDrug.min_unit"><el-input style="width:50px" v-model="scope.row.stock_min_number" :disabled="true"></el-input>{{scope.row.min_unit}}</span> 
                  </template> 
              </el-table-column>

             <el-table-column prop="name" label="盘点后数量" width="180"  align="center">
                <template slot-scope="scope">
                  <el-input style="width:50px" v-model="scope.row.last_stock_max_number"></el-input>{{scope.row.max_unit}}
                 <span v-if="scope.row.XtBaseDrug.max_unit!=scope.row.XtBaseDrug.min_unit"><el-input style="width:50px" v-model="scope.row.last_stock_min_number"></el-input>{{scope.row.min_unit}}</span> 
                 </template> 
              </el-table-column>

              <el-table-column prop="name" label="盘点原因" width="180"  align="center">
                <template slot-scope="scope">
                   <el-select v-model="scope.row.type" placeholder="请选择">
                      <el-option
                        v-for="item in reasonList"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id">
                      </el-option>
                    </el-select>
                 </template> 
              </el-table-column>
             
             <el-table-column prop="name"  width="200"  align="center">
                <template slot-scope="scope">
                    <span><el-button type="danger" @click="toDelete(scope.$index,scope.row.drug_id)">不盘点此批次</el-button></span>
                  </template> 
              </el-table-column>
          </el-table>
      </el-collapse-item>
      <el-autocomplete
          class="checkSearch"
          popper-class="my-autocomplete"
          v-model="form.drug_name"
          :fetch-suggestions="querySearchAsync"
          :trigger-on-focus="true"
          placeholder="请输入药品名称"
          @select="handleSelect"
          style="width:300px;"
          v-show="showTableOne"
        >
          <i class="el-icon-search el-input__icon" slot="suffix"></i>
        <template slot-scope="{ item }">
          <div class="name">{{ item.drug_name + item.dose + item.dose_unit + "*" +item.min_number + item.min_unit +"/" + item.max_unit + "  "+item.manufacturer}}</div>
        </template>
       </el-autocomplete>  
    </el-collapse>
   </div>
  </div>
</template>

<script>
import { uParseTime } from '@/utils/tools'
import { postSearchDrugWarehouseList,getDrugWarehouseInfoList,saveDrugInventory,getDrugInventoryList,SaveDrugCheckedInventory,getDrugInventoryDetail,modifyInventory,SaveDrugProofInventory,getDrugWarehouseInfoTotal,deleteDrugInventory,saveInentoryList} from "@/api/drug/drug"
import { getDrugBatchNumber } from  "@/api/drug/drug_stock"
import { getDataConfig } from '@/utils/data'
const moment = require("moment");
export default {
    name: "drugInventory",
    data() {
        return{
            nowTime: moment(new Date()).format("YYYY-MM-DD HH:MM:SS"),
            user_name:this.$store.getters.xt_user.user.user_name,
            searchKey:'',
            value1:'',
            value2:'',
            tableData: [],
            dialogVisible:false,
            form: {
                id:"",
                drug_name: '',
                retail_price :'',
                new_price:"",
                count:"",
                remark:"",
                warehousing_order:"",
                manufacturer:"",
                dealer:"",
                number:"",
                warehousing_unit:"",
                specification_name:"",
                buy_price:"",
                drug_id:"",
                packing_unit:"",
                drug_origin_place:"",
                report_count:"",
                total:"",
                dose:"",
                dose_unit:"",
                min_number:"",
                min_unit:"",
                max_unit:"",
                last_price:"",
                batch_number:"",
                product_date:"",
                expiry_date:"",
                warehouse_info_id:"",
                proof_count:"",
                min_count:"",
                min_unit:"",
            },
            total: 0,
            editdialogVisible:false,
            limit:10,
            page:1,
            start_time:"",
            end_time:"",
            tableList:[],
            doctorList:[],
            checkDialogVisible:false,
            ids:"",
            checker:this.$store.getters.xt_user.user.id,
            check_time:new Date(),
            id:"",
            modifydialogVisible:false,
            numberList:[],
            profdialogVisible:false,
            index:"",
            unitList:[],
            reasonList:[
              {id:6,name:"默认"},
              {id:1,name:"到期退货"},
              {id:2,name:"异常退货"},
              {id:3,name:"退货"},
              {id:4,name:"人为损坏"},
              {id:5,name:"不计入报损分析"},
            ],
            activeNames: [0,1,2,3,4,5,6,7,8,9,10],
            showTable:false,
            showSearch:true,
            inventory_total:0,
            WarehouseList:[],
            showTableOne:false,
            panShow:true,
            panOneShow:true,
        }
    },
    methods:{
        changeNumber(val){
          this.form.warehouse_info_id = val
          this.getDrugWarehouseInfoTotal(val)
        },
        search(){
          this.getlist()
        },
        print(){
          if(this.ids == ""){
           this.$message.error("请勾选打印数据")
           return false
          }
          this.$router.push({path:'/stock/drugs/inventoryPrint?ids='+this.ids})
            
        },
        handleSizeChange(val){
          this.limit = val
          this.getlist()
        },
        handleCurrentChange(val){
          this.page = val
          this.getlist()
        },
        querySearchAsync(keyword, cb) {
            let key = '';
            if (keyword != undefined) {
                key = keyword
            }
             var params = {
                keyword:key,
             }
            
            postSearchDrugWarehouseList(params).then(response => {
            if (response.data.state == 1) {
            
                var list = response.data.data.list
                // console.log("列表数据",list)
                this.drugList = list
                var manufacturerList = response.data.data.manufacturerList
                this.manufacturerList = manufacturerList
                var dealer =  response.data.data.dealerList
                this.dealerList = dealer
                for(let i=0;i<this.drugList.length;i++){
                    for(let j=0;j<this.manufacturerList.length;j++){
                    if(this.drugList[i].manufacturer == this.manufacturerList[j].id){
                        this.drugList[i].manufacturer = this.manufacturerList[j].manufacturer_name
                    }
                    }
                }

                for(let i=0;i<this.drugList.length;i++){
                    for(let j=0;j<this.dealerList.length;j++){
                        if(this.drugList[i].dealer == this.dealerList[j].id){
                        this.drugList[i].dealer = this.dealerList[j].dealer_name
                        }
                    }
                }
            
                cb(this.drugList)
            } else {
                cb([])
             }
          })
         },
         handleSelect(val){
           var drugIds = []
          for(let i=0;i<this.tableData.length;i++){
             drugIds.push(this.tableData[i].drug_id)
           }
          var strId = drugIds.join(",")
          if (strId.indexOf(val.drug_id)!=-1){
            this.$message.error("不能添加重复药品!")
            return
          }
           var params = {
              id:val.drug_id
            }
           getDrugWarehouseInfoList(params).then(response=>{
             if(response.data.state == 1){
                var list =  response.data.data.list
               
                for(let i=0;i<list.length;i++){
                   if(list[i].XtBaseDrug.max_unit == list[i].XtBaseDrug.min_unit){
                      list[i].stock_max_number += list[i].stock_min_number
                      list[i].stock_min_number = 0
                      this.panShow = false
                      this.panOneShow = false
                   }
                   list[i].drug_name = list[i].XtBaseDrug.drug_name
                   list[i].specification_name = list[i].XtBaseDrug.dose +list[i].XtBaseDrug.dose_unit + "*"+list[i].XtBaseDrug.min_number +list[i].XtBaseDrug.min_unit+"/"+list[i].XtBaseDrug.max_unit
                   list[i].warehouseing_unit = list[i].max_unit
                   list[i].max_unit = list[i].XtBaseDrug.max_unit
                   list[i].min_unit = list[i].XtBaseDrug.min_unit
                   list[i].last_stock_max_number = ""
                   list[i].last_stock_min_number = ""
                   list[i].min_number = list[i].XtBaseDrug.min_number
                   list[i].manufacturer_name  = ""
                   list[i].type = 6
                   for(let j=0;j<this.manufacturerList.length;j++){
                     if(list[i].manufacturer == this.manufacturerList[j].id){
                        list[i].manufacturer_name = this.manufacturerList[j].manufacturer_name
                     }
                   }
                   this.WarehouseList.push(list[i])
                }
                console.log("lisrt2332323223322323232323",list)
                  let objInfo = {}
                  this.WarehouseList.forEach((item,index)=>{
                    let { drug_id } = item
                    if(!objInfo[drug_id]){
                      objInfo[drug_id] = {
                        drug_id,
                        child:[],
                        drug_name:item.drug_name,
                        specification_name:item.specification_name,
                        warehouseing_unit:item.max_unit,
                        price:item.price,
                        max_unit:item.max_unit,
                        min_unit:item.min_unit,
                        batch_number:item.batch_number,
                        expiry_date:item.expiry_date,
                        type:0,
                        
                      }
                    }
                  })
                 let newList = Object.values(objInfo);
                
                 for(let i=0;i<this.WarehouseList.length;i++){
                   for(let j=0;j<newList.length;j++){
                      if(this.WarehouseList[i].drug_id == newList[j].drug_id){
                          newList[j].child.push(this.WarehouseList[i])
                      }
                   }
                 }
               
                this.tableData = newList
                this.showSearch = false
                this.showTable = true
                if(this.tableData.length >=1){
                  this.showTableOne = true
                }
                this.inventory_total  = this.tableData.length
             }
           })
        },
        getTime(val) {
         if(val < 0){
            return ""
          }
         if(val == ""){
            return ""
          }else {
            return uParseTime(val, '{y}-{m}-{d}')
         }
        },
        getTotal(total,max_unit,min_unit,min_number){
            var str = ""
            var min_str = ""
            if(total<min_number){
            str = ""
            min_str = total + min_unit
            }
            if(total >= min_number){
            if(parseInt(total/min_number)!=0){
                str = parseInt(total/min_number)+ max_unit
            }
            if((total%min_number)!=0){
                min_str =  total%min_number + min_unit
            }
            }
        
            return str + min_str
        },
        addInventory(){
      
          if(this.form.drug_name == ""){
            this.$message.error("请输入药品名称")
            return
          }
          if(this.form.batch_number == undefined){
            this.$message.error("请选择批次")
            return
          }
          if(this.form.count == ""){
            this.$message.error("请输入盘点数量")
            return
          }
          if(this.form.min_count == ""){
            this.form.min_count = 0
          }
          var obj = {
            drug_id:this.form.drug_id,
            drug_name:this.form.drug_name,
            warehousing_unit:this.form.warehousing_unit,
            count:this.form.count,
            min_count:this.form.min_count,
            min_unit:this.form.min_unit,
            retail_price:this.form.retail_price.toString(),
            manufacturer:this.form.manufacturer,
            drug_origin_place:this.form.drug_origin_place,
            remark:this.form.remark,
            new_price:this.form.new_price.toString(),
            warehousing_order:this.form.warehousing_order,
            dealer:this.form.dealer,
            last_price:this.form.last_price,
            start_time:this.getTime(new Date()),
            number:this.form.number,  
            total:this.form.total,
            specification_name:this.form.specification_name,
            batch_number:this.form.batch_number,
            expiry_date:this.form.expiry_date,
            product_date:this.form.product_date,
            warehouse_info_id:this.form.warehouse_info_id,
         } 
      
         this.tableData.push(obj) 
         this.form.drug_name = ""  
         this.form.warehousing_unit = ""
         this.form.count = ""
         this.form.retail_price = ""
         this.form.manufacturer = ""
         this.form.drug_origin_place = ""
         this.form.number = ""
         this.form.remark = ""
         this.form.new_price = ""
         this.form.warehousing_order = ""
         this.form.dealer = ""
         this.form.last_price = ""
         this.form.number = ""
         this.form.total = ""
         this.form.specification_name = ""
         this.form.batch_number = ""
         this.form.expiry_date = ""
         this.form.product_date = ""
        },
        toEdit(val,index){
           this.form.id= val.id
           this.form.drug_id = val.drug_id,
           this.form.drug_name = val.drug_name
           this.form.retail_price = val.retail_price
           this.form.warehousing_order = val.warehousing_order
           this.form.number = val.number
           this.form.dealer = val.dealer
           this.form.manufacturer = val.manufacturer
           this.form.remark = val.remark
           this.form.warehousing_unit = val.warehousing_unit  
           this.form.total = val.total
           this.form.batch_number = val.batch_number
           this.form.last_price = val.last_price
           this.form.specification_name =  val.specification_name
           this.form.expiry_date = val.expiry_date
           this.form.product_date =val.product_date  
           this.form.count = val.count
           this.form.min_count = val.min_count
           this.form.min_unit = val.min_unit
           this.index = index
           this.editdialogVisible = true
        },
        saveInventory(){
          for(let i=0;i<this.tableData.length;i++){
             if(this.form.id == this.tableData[i].id){
               this.tableData[i].drug_id = this.form.drug_id
               this.tableData[i].drug_name = this.form.drug_name
               this.tableData[i].retail_price = this.form.retail_price
               this.tableData[i].warehousing_order = this.form.warehousing_order
               this.tableData[i].number = this.form.number
               this.tableData[i].dealer = this.form.dealer
               this.tableData[i].manufacturer = this.form.manufacturer
               this.tableData[i].remark = this.form.remark
               this.tableData[i].warehouseing_unit = this.form.warehousing_unit
               this.tableData[i].total = this.form.total
               this.tableData[i].batch_number = this.form.batch_number
               this.tableData[i].last_price = this.form.last_price
               this.tableData[i].specification_name = this.form.specification_name
               this.tableData[i].expiry_date = this.form.expiry_date
               this.tableData[i].product_date = this.form.product_date
               this.tableData[i].count =this.form.count
               this.tableData[i].min_unit = this.form.min_unit
               this.tableData[i].min_count = this.form.min_count
             }
            if(this.index == i){
               this.tableData[i].drug_id = this.form.drug_id
               this.tableData[i].drug_name = this.form.drug_name
               this.tableData[i].retail_price = this.form.retail_price
               this.tableData[i].warehousing_order = this.form.warehousing_order
               this.tableData[i].number = this.form.number
               this.tableData[i].dealer = this.form.dealer
               this.tableData[i].manufacturer = this.form.manufacturer
               this.tableData[i].remark = this.form.remark
               this.tableData[i].warehouseing_unit = this.form.warehousing_unit
               this.tableData[i].total = this.form.total
               this.tableData[i].batch_number = this.form.batch_number
               this.tableData[i].last_price = this.form.last_price
               this.tableData[i].specification_name = this.form.specification_name
               this.tableData[i].expiry_date = this.form.expiry_date
               this.tableData[i].product_date = this.form.product_date
               this.tableData[i].count =this.form.count
               this.tableData[i].min_unit = this.form.min_unit
               this.tableData[i].min_count = this.form.min_count
             }
          }   
          this.editdialogVisible = false
        },
        saveDrugInventory(){
           for(let i=0;i<this.tableData.length;i++){
              this.tableData[i].retail_price = this.tableData[i].retail_price.toString()
              this.tableData[i].last_price = this.tableData[i].last_price.toString()
              this.tableData[i].count = parseInt(this.tableData[i].count)
              this.tableData[i].min_count = parseInt(this.tableData[i].min_count)
              if(this.tableData[i].expiry_date == undefined){
                this.tableData[i].expiry_date  = 0
              }else{
               this.tableData[i].expiry_date = this.tableData[i].expiry_date
              }
              if( this.tableData[i].product_date  == undefined){
                this.tableData[i].product_date = 0
              }else{
               this.tableData[i].product_date = this.tableData[i].product_date
              }
              
             
              if(this.tableData[i].dealer == 0){
                this.tableData[i].dealer = ""
              }
               if(this.tableData[i].manufacturer == 0){
                this.tableData[i].manufacturer = ""
              }
              if(this.tableData[i].batch_number == undefined){
                 this.tableData[i].batch_number = ""
              }
           }
           var params = {
              tableData:this.tableData
           }
          saveDrugInventory(params).then(response=>{
            if(response.data.state == 1){
              var msg = response.data.data.msg
              this.$message.success("保存成功")
              this.getlist()
              this.editdialogVisible = false
              this.dialogVisible = false
            }
          })
        },
        getlist(){
          var params = {
            limit:this.limit,
            page:this.page,
            keyword:this.searchKey, 
            start_time:this.start_time,
            end_time:this.end_time,
          }
          getDrugInventoryList(params).then(response=>{

            if(response.data.state == 1){
              this.total =  response.data.data.total
              
              this.tableList = response.data.data.list
              
              this.doctorList =  response.data.data.doctorList
              

            }
          })
        },
        getDoctorName(id){
          var name = ""
          for(let i=0;i<this.doctorList.length;i++){
            if(id == this.doctorList[i].admin_user_id){
                name = this.doctorList[i].user_name
            }
          }
          return name
        },
     changePrice(val){  
        var arr = []
        for(let i=0;i<val.length;i++){
            arr.push(val[i].id)
        }
        var str = arr.join(",") 
        this.ids = str
        },
      toCheck(){
        if(this.ids.length <=0){
            this.$message.error("请勾选核对数据")
            return
        }else{
            this.checkDialogVisible = true
        }
     },
      SaveDrugCheckedInventory(){
         var params = {
            ids:this.ids,
            check_time:this.getTime(this.check_time),
            checker:this.checker,
         }
       SaveDrugCheckedInventory(params).then(response=>{
         if(response.data.state == 1){
           this.$message.success("保存成功")
           var msg = response.data.data.msg
           this.checkDialogVisible = false
           this.tableData = []
           this.getlist()
         }
       })
     },
     editInventory(id,check_status){
      if(check_status == 1){
        this.$message.error("已核对不能编辑")
        return false
      }
       getDrugInventoryDetail(id).then(response=>{
         if(response.data.state == 1){
            var detail = response.data.data.detail
            
            this.id = detail.id
            this.form.drug_id = detail.drug_id
            this.form.drug_name = detail.drug_name
            this.form.specification_name = detail.specification_name
            this.form.retail_price = detail.retail_price
            this.form.warehousing_order =detail.warehousing_order
            this.form.number = detail.number
        
            this.form.manufacturer = detail.manufacturer
            this.form.remark = detail.remark
            this.form.warehousing_unit = detail.warehousing_unit
            this.form.total = detail.total
        
            this.form.last_price = detail.last_price
           
            this.form.count =  parseInt(detail.count)
            this.form.drug_origin_place = detail.drug_origin_place
            this.form.stock_max_number = detail.stock_max_number
            this.form.stock_min_number = detail.stock_min_number
            this.form.warehouse_info_id = detail.warehouse_info_id
            this.form.min_unit = detail.min_unit
            this.form.min_count = detail.min_count
            this.modifydialogVisible = true
         }
       })
     },
     modifyInventory(){
       var params = {
          id:this.id,
          drug_id:this.form.drug_id,
          drug_name:this.form.drug_name,
          specification_name:this.form.specification_name,
          warehousing_unit:this.form.warehousing_unit,
          last_price:this.form.last_price,
          retail_price:this.form.retail_price,
          count:parseInt(this.form.count),
          total:this.form.total,
          drug_origin_place:this.form.drug_origin_place,
          batch_number:this.form.batch_number,
          manufacturer:this.form.manufacturer,
          remark:this.form.remark,
          stock_max_number:this.form.stock_max_number,
          stock_min_number:this.form.stock_min_number,
          warehouse_info_id:this.form.warehouse_info_id,
          min_count:parseInt(this.form.min_count),
          min_unit:this.form.min_unit,
       }
    
      
      modifyInventory(params).then(response=>{
         if(response.data.state == 1){
           var inventory =  response.data.data.inventory
           this.modifydialogVisible = false
           this.getlist()
         }
      })
     },
     deleteDrugInventory(id,check_status,index){
       if(check_status == 1){
          this.$message.error("已核对的不能编辑")
          return
        }
        this.$confirm('是否删除所选内容?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
        
       deleteDrugInventory(id).then(response => {
            if (response.data.state == 1) {
              var msg = response.data.data.msg
              this.$message.success("保存成功")
              this.tableList.splice(index,1)
            } 
          })
        }).catch(() => {
          this.loading = false
        });   
     },
    toMove(item,index){
      
       this.$confirm('此操作将移除药品整个批次, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.tableData.splice(index,1)
          this.WarehouseList = []
          this.activeNames = [0,1,2,3,4,5,6,7,8,9,10]
         
        }).catch(() => {
          this.loading = false
        });   
    },
    toDelete(index,drug_id){
      
      for(let i=0;i<this.tableData.length;i++){
         if(drug_id == this.tableData[i].drug_id){
           this.tableData[i].child.splice(index,1)
           if(this.tableData[i].child.length == 0){
              this.tableData.splice(index,1)

           }
         }
      }
     this.WarehouseList = []
     this.activeNames = [0,1,2,3,4,5,6,7,8,9,10]
    },
    exportList(){
       
         import('@/vendor/Export2Excel').then(excel => {
         const tHeader = ['药品名称', '规格', '单位','零售价','当前库存','盘点数','亏损金额']
         const filterVal = ['drug_name', 'spe', 'count','retail_price','total','count','']
         const data = this.formatJson(filterVal, this.tableList)
      
         excel.export_json_to_excel({
           header: tHeader,
           data,
           filename: '耗材盘点'
         })
          this.downloadLoading = false
        })
       },
       formatJson(filterVal, jsonData) {
        return jsonData.map(v => filterVal.map(j => v[j]));
      },
      getDrugBatchNumber(id){
            var params = {
              id:id
            }
          getDrugBatchNumber(params).then(response=>{
            if(response.data.state == 1){
              var list = response.data.data.list
              this.numberList = list
            }
          })
       },
       toProof(id){
         getDrugInventoryDetail(id).then(response=>{
          if(response.data.state == 1){
            var detail = response.data.data.detail
            this.id = detail.id
            this.form.drug_id = detail.drug_id
            this.form.drug_name = detail.drug_name
            this.form.specification_name = detail.specification_name
            this.form.retail_price = detail.retail_price
            this.form.warehousing_order =detail.warehousing_order
            this.form.number = detail.number
            this.form.manufacturer = detail.manufacturer
            this.form.remark = detail.remark
            this.form.warehousing_unit = detail.warehousing_unit
            this.form.total = detail.total
            this.form.last_price = detail.last_price
            this.form.count =  parseInt(detail.count)
            this.form.drug_origin_place = detail.drug_origin_place
            this.form.warehouse_info_id = detail.warehouse_info_id
            this.profdialogVisible = true
         }
        })
       },
       proofInventory(){
           var params = {
             id:this.id,
             warehouseing_unit:this.form.warehousing_unit,
             warehouse_info_id:this.form.warehouse_info_id,
             total:this.total.toString(),
             remark:this.remark,
             proof_count:parseInt(this.form.proof_count),
           }
         
         SaveDrugProofInventory(params).then(response=>{
            if(response.data.state == 1){
              var inventory = response.data.data.inventory
             
              this.profdialogVisible = false
              this.getlist()
            }
         })
       },
       changeStartTime(val){
          this.start_time = this.getTime(val)
          this.getlist()
       },
       changeEndTime(val){
         this.end_time = this.getTime(val)
         this.getlist()
       },
      getDrugWarehouseInfoTotal(val){
        var params = {
          id:val,
        }
      getDrugWarehouseInfoTotal(params).then(response=>{
         if(response.data.state == 1){
           var list = response.data.data.list
          
          var max_total = 0
          var min_total = 0
          for(let i=0;i<list.length;i++){
            max_total += list[i].stock_max_number
            min_total +=list[i].stock_min_number
          }
          
          if(max_total >0 ){
            this.form.total = max_total + list[0].XtBaseDrug.max_unit
          }
          if(min_total > 0){
            this.form.total =  this.form.total + min_total + list[0].XtBaseDrug.min_unit 
          }
         }
      })
     },
     getDataConfig(module, filed_name) {
       return getDataConfig(module, filed_name)
      },  
    changeMaxUnit(val){
      this.form.warehousing_unit = val
    },
    saveInentoryList(){
       var arr = []
       for(let i=0;i<this.tableData.length;i++){
         for(let j=0;j<this.tableData[i].child.length;j++){
            arr.push(this.tableData[i].child[j])
         }
       }
       for(let i=0;i<arr.length;i++){
         if(arr[i].last_stock_max_number == ""){
           arr[i].last_stock_max_number = 0
         }
         if(arr[i].last_stock_min_number == ""){
           arr[i].last_stock_min_number = 0
         }
         arr[i].last_stock_max_number = parseInt(arr[i].last_stock_max_number)
         arr[i].last_stock_min_number = parseInt(arr[i].last_stock_min_number)

         
       }

       var params = {
         tableData:arr
       }
      console.log("paramse32323232",params)
      saveInentoryList(params).then(response=>{
        if(response.data.state == 1){
           var inventory = response.data.data.inventory
           this.$message.success("保存成功")
           this.WarehouseList = []
           this.tableData = []
          
        }
      })
    }
    },
    created(){
      this.unitList =  this.getDataConfig('hemodialysis','units')
      console.log("单位",this.unitList)
      this.getlist()
    },
    mounted() {
    },
};
</script>
<style lang="scss">
    .addDrugsDialog {

      .el-dialog__body {
          padding-top: 0px;
      }
      .noMargin{
        .el-form-item__content{
          margin-left: 0 !important;
        }
      }

    }
</style>