<template>
  <div class="main-contain">
    <div class="position">
      <bread-crumb :crumbs="crumbs"></bread-crumb>
    </div>
    <div class="app-container">
        <div style="display: flex;align-items: center;justify-content: space-between;margin-bottom:10px;">
            <div>
                <el-input
                size="small"
                style="width: 200px;"
                class="filter-item"
                v-model.trim="searchKey"
                placeholder="请输入单据编号或操作人姓名"
                />
                <el-button
                size="small"
                class="filter-item"
                type="primary"
                icon="el-icon-search"
                @click="search"
                >搜索</el-button
                >
                <el-date-picker
                size="small"
                v-model="start_time"
                type="date"
                style="margin-left:5px;width:140px;"
                placeholder="选择日期"
                @change="changeStartTime">
                </el-date-picker>
                <el-date-picker
                size="small"
                v-model="end_time"
                type="date"
                style="margin-left:5px;width:140px;"
                placeholder="选择日期"
                @change="changeEndTime">
                </el-date-picker>
            </div>
            <div>
                <el-button size="small" type="primary" @click="dialogVisible = true">新增</el-button>
                <el-button size="small" type="primary" @click="toCheck">核对</el-button>
                <!-- <el-button size="small" type="primary" @click="print">打印</el-button>
                <el-button size="small" type="primary">导出</el-button> -->
            </div>
        </div>
        <el-table :data="tableList" border :header-cell-style="{ backgroundColor: 'rgb(245, 247, 250)' }"  @selection-change="changePrice">
            <el-table-column type="selection" width="55" align="center"> </el-table-column>
            <el-table-column prop="date" label="报损编码" align="center">
               <template slot-scope="scope">
                  {{scope.row.warehousing_order}}
                </template>
            </el-table-column>
            <el-table-column  prop="date" label="报损日期" align="center">
               <template slot-scope="scope">
                  {{getTime(scope.row.start_time)}}
                </template>
            </el-table-column>
            <el-table-column prop="name" label="药品名称" align="center">
               <template slot-scope="scope">
                  {{scope.row.drug_name}}
                </template>
            </el-table-column>
            <el-table-column prop="name" label="规格" align="center">
               <template slot-scope="scope">
                  {{scope.row.specification_name}}
                </template>
            </el-table-column>
            <el-table-column prop="name" label="报损数量" align="center">
               <template slot-scope="scope">
                  {{scope.row.count}}
                </template>
            </el-table-column>
            <el-table-column prop="name" label="操作人" align="center">
               <template slot-scope="scope">
                  {{getDoctorName(scope.row.creater)}}
                </template>
            </el-table-column>
            <el-table-column prop="name" label="审核日期" align="center">
              <template slot-scope="scope">
                {{getTime(scope.row.checker_time)}}
               </template>
            </el-table-column>
            <el-table-column prop="name" label="核对人" align="center">
              <template slot-scope="scope">
                {{getDoctorName(scope.row.checker)}}
              </template>
            </el-table-column>
            <el-table-column prop="name" label="状态" align="center">
              <template slot-scope="scope">
                <span v-if="scope.row.checker_status == 1">已核对</span>
                <span v-if="scope.row.checker_status == 2">未核对</span>
              </template>   
            </el-table-column>
            <el-table-column label="操作" align="center" width="200">
                <template slot-scope="scope">
                  <el-button type="primary" size="small" @click="moDrugDamage(scope.row.id,scope.row.checker_status)">编辑</el-button>
                  <el-button type="danger" size="small" @click="deleteDrugDamage(scope.row.id,scope.row.checker_status,scope.$index)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-pagination
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :page-sizes="[10, 20, 40, 100]"
            :page-size="10"
            background
            style="margin-top:20px;text-align: right"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total">
        </el-pagination>
    </div>
    <el-dialog
        title="药品报损"
        :visible.sync="dialogVisible"
        width="1200px">
        <el-form :model="form" class="modifyDialog" label-width="120px">
            <el-form-item label="药品ID">
                <el-input v-model="form.drug_id" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="药品名称">
              <el-autocomplete
                  class="checkSearch"
                  popper-class="my-autocomplete"
                  v-model="form.drug_name"
                  :fetch-suggestions="querySearchAsync"
                  :trigger-on-focus="true"
                  placeholder="请输入药品名称"
                  @select="handleSelect"
                  @input="changeGoodName(scope.$index)"
                  style="width:260px;"
                  >
                  <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-form-item>

            <el-form-item label="批次">
               <el-select v-model="form.batch_number" filterable placeholder="请选择" @change="changeNumber">
                  <el-option
                    v-for="(item,index) in numberList"
                    :key="index"
                    :label="item.batch_number"
                    :value="item.id">
                  </el-option>
               </el-select>
            </el-form-item>
         
            <el-form-item label="单位">
                <el-input v-model="form.warehousing_unit" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="进货价">
                <el-input v-model="form.last_price" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="零售价">
                <el-input v-model="form.retail_price" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="报损数量">
                <el-input v-model="form.count"></el-input>
            </el-form-item>
            <el-form-item label="库存">
                <el-input v-model="form.total" :disabled="true" ></el-input>
            </el-form-item>
            <el-form-item label="产地">
                <el-input v-model="form.drug_origin_place" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="批准文号">
                <el-input v-model="form.number" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="生产厂商">
                <el-input v-model="form.manufacturer" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="备注">
                <div style="display:flex;">
                    <el-input v-model="form.remark"></el-input>
                    <el-button style="margin-left:5px;" type="primary" @click="addPrice">添加</el-button>
                </div>
            </el-form-item>
        </el-form>
        <el-table :data="tableData" border :header-cell-style="{ backgroundColor: 'rgb(245, 247, 250)' }">
            <el-table-column  prop="date" label="药品ID" width="100" align="center">
                <template slot-scope="scope">
                  {{scope.row.drug_id}}
                </template>
            </el-table-column>
            <el-table-column prop="date" label="药品名称"  width="100" 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_unit}}
              </template> 
            </el-table-column>
            <el-table-column prop="name" label="报损数量" width="100" align="center">
              <template slot-scope="scope">
                 {{scope.row.count}}
              </template> 
            </el-table-column>
            <el-table-column prop="name" label="原进货价"  width="100" align="center">
             <template slot-scope="scope">
                 {{scope.row.last_price}}
              </template> 
            </el-table-column>
            <el-table-column  prop="name" label="原零售价" width="100" align="center">
               <template slot-scope="scope">
                {{scope.row.retail_price}}
              </template>
            </el-table-column>
        
            <el-table-column  prop="name"  label="生产厂商"  width="100" align="center">
              <template slot-scope="scope">
                {{scope.row.manufacturer}}
              </template>
            </el-table-column>
            <el-table-column prop="name" label="产地" width="100" align="center">
              <template slot-scope="scope">
                {{scope.row.drug_origin_place}}
              </template>
            </el-table-column>
            <el-table-column prop="name" label="批准文号" width="100" align="center">
             <template slot-scope="scope">
                {{scope.row.number}}
              </template>
            </el-table-column>
            <el-table-column  prop="name" label="备注"  width="100" align="center">
               <template slot-scope="scope">
                {{scope.row.remark}}
              </template>
            </el-table-column>
            <el-table-column
            fixed="right"
            width="100"
            label="操作">
                <template slot-scope="scope">
                    <el-button type="text" size="small" @click="toDel(scope.$index)">删除</el-button>
                    <el-button type="text" size="small" @click="toEdit(scope.row,scope.$index)">编辑</el-button>
                </template>
            </el-table-column>
        </el-table>
        <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="saveDrugDamage">确 定</el-button>
        </span>
    </el-dialog>
   

    <el-dialog
        title="药品盘点核对"
        :visible.sync="checkDialogVisible"
        width="30%"
        >
        <span>
          <el-form :model="form">
            <el-row>
              <el-col>
                <el-form-item label="核对时间">
                 <el-date-picker
                    size="small"
                    v-model="check_time"
                    type="date"
                    style="margin-left:5px;width:140px;"
                    placeholder="选择日期">
                  </el-date-picker>
                </el-form-item>  
              </el-col>
            </el-row>  
             <el-row>
              <el-col>
                <el-form-item label="核对人">
                  <el-select v-model="checker" placeholder="请选择">
                    <el-option
                    v-for="item in doctorList"
                    :key="item.admin_user_id"
                    :label="item.user_name"
                    :value="item.admin_user_id">
                    </el-option>
                 </el-select>
                </el-form-item>  
              </el-col>
            </el-row>  
          </el-form>  
        </span>
        <span slot="footer" class="dialog-footer">
            <el-button @click="checkDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="SaveDrugDamagePrice">保 存</el-button>
        </span>
    </el-dialog>

    
     <el-dialog
        title="库房调价"
        :visible.sync="editDialogVisible"
        width="1200px">
        <el-form :model="form" class="modifyDialog" label-width="120px">
            <el-form-item label="药品ID">
                <el-input v-model="form.drug_id" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="药品名称">
            <el-autocomplete
                class="checkSearch"
                popper-class="my-autocomplete"
                v-model="form.drug_name"
                :fetch-suggestions="querySearchAsync"
                :trigger-on-focus="true"
                placeholder="请输入药品名称"
                @select="handleSelect"
                @input="changeGoodName(scope.$index)"
                style="width:160px;"
                >
                <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-form-item>
            <el-form-item label="批次">
               <el-select v-model="form.batch_number" filterable placeholder="请选择" @change="changeNumber">
                  <el-option
                    v-for="(item,index) in numberList"
                    :key="index"
                    :label="item.batch_number"
                    :value="item.id">
                  </el-option>
               </el-select>
            </el-form-item>
            <el-form-item label="规格">
                <el-input v-model="form.specification_name" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="单位">
                <el-input v-model="form.warehousing_unit" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="进货价">
                <el-input v-model="form.last_price" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="零售价">
                <el-input v-model="form.retail_price" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="报损数量">
                <el-input v-model="form.count"></el-input>
            </el-form-item>
            <el-form-item label="库存">
                <el-input v-model="form.total" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="产地">
                <el-input v-model="form.drug_origin_place" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="批准文号">
                <el-input v-model="form.number" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="生产厂商">
                <el-input v-model="form.manufacturer" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="备注">
                <div style="display:flex;">
                    <el-input v-model="form.remark"></el-input>
                </div>
            </el-form-item>
        </el-form>
        
        <span slot="footer" class="dialog-footer">
            <el-button @click="editDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="EditDrugDamage">确 定</el-button>
        </span>
    </el-dialog>


      <el-dialog
        title="库房调价"
        :visible.sync="modifyDialogVisible"
        width="1200px">
        <el-form :model="form" class="modifyDialog" label-width="120px">
            <el-form-item label="药品ID">
                <el-input v-model="form.drug_id" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="药品名称">
            <el-autocomplete
                class="checkSearch"
                popper-class="my-autocomplete"
                v-model="form.drug_name"
                :fetch-suggestions="querySearchAsync"
                :trigger-on-focus="true"
                placeholder="请输入药品名称"
                @select="handleSelect"
                @input="changeGoodName(scope.$index)"
                style="width:160px;"
                >
                <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-form-item>
            <!-- <el-form-item label="规格">
                <el-input v-model="form.name"></el-input>
            </el-form-item> -->
            <el-form-item label="单位">
                <el-input v-model="form.warehousing_unit" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="进货价">
                <el-input v-model="form.last_price" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="零售价">
                <el-input v-model="form.retail_price" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="报损数量">
                <el-input v-model="form.count"></el-input>
            </el-form-item>
            <el-form-item label="库存">
                <el-input v-model="form.total" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="产地">
                <el-input v-model="form.drug_origin_place" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="批准文号">
                <el-input v-model="form.number" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="生产厂商">
                <el-input v-model="form.manufacturer" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="备注">
                <div style="display:flex;">
                    <el-input v-model="form.remark"></el-input>
                </div>
            </el-form-item>
        </el-form>
        
        <span slot="footer" class="dialog-footer">
            <el-button @click="modifyDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="modifyDrugDamage">确 定</el-button>
        </span>
    </el-dialog>
    
  </div>
</template>

<script>
import BreadCrumb from "../../components/bread-crumb";
import { postSearchDrugWarehouseList,saveDrugDamage,getDrugDamageList,SaveDrugDamagePrice,getDrugDamageDetail,modifyDrugDamage,deleteDrugDamage,getDrugWarehouseInfoList,getDrugWarehouseInfoTotal } from "@/api/drug/drug"
import { getDrugBatchNumber } from  "@/api/drug/drug_stock"
import { uParseTime } from '@/utils/tools'
import { getDataConfig } from '@/utils/data'
export default {
    name: "stockModifyPrice",
    components:{
        BreadCrumb
    },
    data() {
        return{
            crumbs: [
                { path: false, name: "库存管理" },
                { path: false, name: "药品管理" },
                { path: false, 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:"",
                batch_number:"",
                last_price:"",
                warehouse_info_id:"",
                expiry_date:"",
                product_date:"",
                stock_max_number:"",
                stock_min_number:"",
            },
            drugList:[],
            manufacturerList:[],
            dealerList:[],
            currentIndex: 0,
            limit:10,
            page:1,
            tableList:[],
            total: 0,
            start_time:"",
            end_time:"",
            doctorList:[],
            ids:"",
            checkDialogVisible:false,
            checker:this.$store.getters.xt_user.user.id,
            check_time:new Date(),
            editDialogVisible:false,
            modifyDialogVisible:false,
            id:"",
            numberList:[],
            index:0,
            unitList:[],
        }
    },
    methods:{
      
        search(){
          this.getlist()
        },
        print(){
          if(this.ids == ""){
            this.$message.error("请勾选打印数据")
            return false
          }else{
            this.$router.push({path:'/stock/drugs/drugDamagePrint?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
            }
            postSearchDrugWarehouseList(key).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([])
             }
          })
         },
        
        changeGoodName(val){
          this.currentIndex = val
        },
         handleSelect(val){
            console.log("val232323223",val)
            this.getDrugBatchNumber(val.drug_id)
            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++){
                  list[i].stock_max_number = val.min_number * list[i].stock_max_number
                }
                var total = 0
                for(let i=0;i<list.length;i++){
                   total += (list[i].stock_max_number+list[i].stock_min_number)
                }
                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.max_unit 
                this.form.total = "" 
                // this.form.total = this.getTotal(total,val.max_unit,val.min_unit,val.min_number)
                // this.form.batch_number = val.batch_number
                this.form.last_price = val.last_price
                this.form.expiry_date = val.expiry_date
                this.form.product_date = val.product_date
                this.form.specification_name =  val.dose + val.dose_unit +"*"+val.min_number+val.min_unit+"/"+val.max_unit
                this.form.drug_origin_place = val.drug_origin_place
                this.form.batch_number = ""
                this.form.total = ""
             }
           }) 
        },

        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
        },
        changeNumber(val){
          console.log("val",val)
          this.form.warehousing_info_id = val
          this.getDrugWarehouseInfoTotal(val)
        },
        getDrugBatchNumber(id){
            var params = {
              id:id
            }
          getDrugBatchNumber(params).then(response=>{
            if(response.data.state == 1){
              var list = response.data.data.list
              console.log("list2222",list)
              this.numberList = list
            }
          })
        },
        getTime(val) {
         if(val < 0){
            return ""
          }
         if(val == ""){
            return ""
          }else {
            return uParseTime(val, '{y}-{m}-{d}')
         }
        },
        addPrice(){
          console.log("批次号",this.form.batch_number)
          if(this.form.drug_name == ""){
             this.$message.error("请输入药品名称")
             return
          }
          if(this.form.batch_number == ""){
            this.$message.error("请选择批次号")
            return false
          }
          if(this.form.count == ""){
             this.$message.error("请输入报损数量")
             return false
          }
          var obj = {
            drug_id:this.form.drug_id,
            drug_name:this.form.drug_name,
            warehousing_unit:this.form.warehousing_unit,
            count:this.form.count,
            retail_price:this.form.retail_price.toString(),
            manufacturer:this.form.manufacturer,
            drug_origin_place:this.form.drug_origin_place,
            number:this.form.number,
            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,
            expiry_date:this.form.expiry_date,
            product_date:this.form.product_date,
            specification_name:this.form.specification_name,
            drug_origin_place:this.form.drug_origin_place,
            warehousing_info_id:this.form.warehousing_info_id,
            batch_number:this.form.batch_number,
            stock_max_number:this.form.stock_max_number,
            stock_min_number:this.form.stock_min_number,
            total:this.form.total,
          } 
          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.expiry_date = ""
          this.form.product_date = ""
          this.form.specification_name = ""
          this.form.drug_origin_place = "" 
          this.form.total = ""
        },
        saveDrugDamage(){
        
          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].new_price = this.tableData[i].new_price.toString()
              this.tableData[i].count =  parseInt(this.tableData[i].count)
              if(this.tableData[i].stock_max_number == ""){
                this.tableData[i].stock_max_number = 0
              }else{
                this.tableData[i].stock_max_number = parseInt(this.tableData[i].stock_max_number)
              }
              if(this.tableData[i].stock_min_number == ""){
                this.tableData[i].stock_min_number = 0
              }else{
                this.tableData[i].stock_min_number = parseInt(this.tableData[i].stock_min_number)
              }
              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){
                console.log("你啦")
                this.tableData[i].dealer = ""
             }
            
          }
          var params= {
            tableData:this.tableData,
          }
          console.log("parasm222",params)
   
        saveDrugDamage(params).then(response=>{
            if(response.data.state == 1){
            var msg =  response.data.data.msg
            console.log("msg",msg)
            this.$message.success("保存成功!")
            this.dialogVisible = false
            this.tableData = []
            this.getlist()
          }
         })  
      },
      getlist(){
        var params = {
          limit:this.limit,
          page:this.page, 
          start_time:this.start_time,
          end_time:this.end_time,
          keyword:this.searchKey,
        }
        getDrugDamageList(params).then(response=>{
           if(response.data.state == 1){
             this.tableList = response.data.data.list
             this.total = response.data.data.total
             this.doctorList = response.data.data.doctorList
           }
        })
      },
      getDoctorName(id){
        var user_name = ""
        for(let i=0;i<this.doctorList.length;i++){
          if(id == this.doctorList[i].admin_user_id){
            user_name = this.doctorList[i].user_name
          }
        } 
        return user_name
     },
    changePrice(val){  
       var arr = []
      
       for(let i=0;i<val.length;i++){
         arr.push(val[i].id)
       }
      var str = arr.join(",")
    
      console.log("str",str)
      this.ids = str
     
     },
     toCheck(){
      if(this.ids.length <=0){
         this.$message.error("请勾选核对数据")
         return
       }else{
         this.checkDialogVisible = true
       }
     },
      SaveDrugDamagePrice(){
         var params = {
            ids:this.ids,
            check_time:this.getTime(this.check_time),
            checker:this.checker,
         }
       console.log("params",params)
       SaveDrugDamagePrice(params).then(response=>{
         if(response.data.state == 1){
           this.$message.success("保存成功")
           var damage = response.data.data.damage
           this.checkDialogVisible = false
           this.getlist()
         }
       })
     },
     toDel(index){
       this.tableList.splice(index,1)
     },
     toEdit(val,index){
       this.index = index
       this.form.drug_name = val.drug_name
       this.form.drug_origin_place = val.drug_origin_place
       this.form.last_price = val.last_price
       this.form.drug_id = val.drug_id,
       this.form.manufacturer = val.manufacturer
       this.form.new_price = val.new_price
       this.form.number = val.number
       this.form.retail_price = val.retail_price
       this.form.remark = val.remark
       this.form.id = val.id
       this.form.count = val.count
       this.form.specification_name = val.specification_name
       this.form.warehousing_unit = val.warehousing_unit
       this.form.stock_max_number = val.stock_max_number
       this.form.stock_min_number = val.stock_min_number,
       this.editDialogVisible = true
     },
     EditDrugDamage(){
       for(let i=0;i<this.tableData.length;i++){
          if(this.form.id == this.tableData[i].id){
            this.tableData[i].drug_name = this.form.drug_name
            this.tableData[i].drug_origin_place = this.form.drug_origin_place
            this.tableData[i].last_price = this.form.last_price
            this.tableData[i].new_price = this.form.new_price
            this.tableData[i].number = this.form.number
            this.tableData[i].retail_price = this.form.retail_price
            this.tableData[i].remark = this.form.remark
            this.tableData[i].count = this.form.count
            this.tableData[i].stock_max_number = this.form.stock_max_number
            this.tableData[i].stock_min_number = this.form.stock_min_number
            this.tableData[i].manufacturer = this.form.manufacturer
            this.tableData[i].dealer = this.form.dealer
          }
          if(this.index == i){
            this.tableData[i].drug_name = this.form.drug_name
            this.tableData[i].drug_origin_place = this.form.drug_origin_place
            this.tableData[i].last_price = this.form.last_price
            this.tableData[i].new_price = this.form.new_price
            this.tableData[i].number = this.form.number
            this.tableData[i].retail_price = this.form.retail_price
            this.tableData[i].remark = this.form.remark
            this.tableData[i].count = this.form.count
            this.tableData[i].stock_max_number = this.form.stock_max_number
            this.tableData[i].stock_min_number = this.form.stock_min_number
            this.tableData[i].manufacturer = this.form.manufacturer
            this.tableData[i].dealer = this.form.dealer
          }
       }
       this.editDialogVisible = false
       this.form.drug_name = ""
       this.form.drug_origin_place = ""
       this.form.last_price = ""
       this.form.manufacturer = ""
       this.form.new_price = ""
       this.form.number = ""
       this.form.retail_price = ""
       this.form.remark = ""
       this.form.count = ""
     },
     moDrugDamage(id,checker_status){
         if(checker_status == 1){
          this.$message.error("已核对不能编辑")
          return false
         }
       getDrugDamageDetail(id).then(response=>{
         if(response.data.state ==1){
           var detail = response.data.data.detail
           console.log("系准给",detail)
           this.form.drug_id = detail.drug_id
           this.form.drug_name = detail.drug_name
           this.form.drug_origin_place = detail.drug_origin_place
           this.form.last_price = detail.last_price
           this.form.manufacturer = detail.manufacturer
           this.form.new_price = detail.new_price
           this.form.number = detail.number
           this.form.retail_price = detail.retail_price
           this.form.remark = detail.remark
           this.form.count = detail.count
           this.id = detail.id
           this.form.warehousing_unit = detail.warehousing_unit
           this.form.stock_max_number = detail.stock_max_number
           this.form.stock_min_number = detail.stock_min_number
           this.form.total = detail.total
           this.form.warehousing_info_id = detail.warehousing_info_id
           this.modifyDialogVisible = true
         }
       })
     },
     modifyDrugDamage(){
       if(this.form.stock_max_number == ""){
         this.form.stock_max_number = 0
       }else{
         this.form.stock_max_number = parseInt(this.form.stock_max_number)
       }
       if(this.form.stock_min_number == ""){
         this.form.stock_min_number = 0
       }else{
         this.form.stock_min_number = parseInt(this.form.stock_min_number)
       }
         var params = {
           drug_id:this.form.drug_id,
           drug_name:this.form.drug_name,
           drug_origin_place:this.form.drug_origin_place,
           last_price:this.form.last_price,
           manufacturer:this.form.manufacturer,
           new_price:this.form.new_price.toString(),
           number:this.form.number,
           retail_price:this.form.retail_price,
           remark:this.form.remark,
           count:parseInt(this.form.count),
           warehousing_unit:this.form.warehousing_unit,
           id:this.id,
           total:this.form.total,
           stock_max_number:this.form.stock_max_number,
           stock_min_number:this.form.stock_min_number,
         }
       modifyDrugDamage(params).then(response=>{
          if(response.data.state == 1){
            var damage =  response.data.data.damage
            this.$message.success("保存成功")
            this.modifyDialogVisible = false
            this.getlist()
          }
       })
     },

     deleteDrugDamage(id,checker_status,index){
       if(checker_status == 1){
         this.$message.error("已核对不能删除")
         return false
       }
       this.$confirm('是否删除所选内容?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          let params = {
            id:id,
          }
         deleteDrugDamage(params).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
        });
     },
     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
           console.log("list",list)
         
          if(list.stock_max_number >0 ){
            this.form.total = list.stock_max_number + list.XtBaseDrug.max_unit
          }
          if(list.stock_min_number > 0){
            this.form.total =  this.form.total + list.stock_min_number + list.XtBaseDrug.min_unit 
          }
          // this.form.total = list.stock_max_number + list.XtBaseDrug.max_unit + list.stock_min_number + list.XtBaseDrug.min_unit
         }
      })
     },
     changeMaxUnit(){

     }
    },
    created(){
     
      this.getlist()
    },
    mounted() {
    },
};
</script>

<style rel="stylesheet/scss" lang="scss">
.app-container {
  // margin: 20px;
  font-size: 15px;
}
.modifyDialog{
    display: flex;
    flex-wrap: wrap;
    margin-bottom: 10px;
    .el-form-item{
        width: 33%;
    }
}
.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;
}
::-webkit-scrollbar{
    height: 20px !important;
}
</style>
