<template>
  <div>
    <div  style="margin-bottom:10px;">
    <el-select size="small" v-model="storehouse_id" filterable placeholder="请选择仓库" style="width:200px" @change="changehouseList">
        <el-option
          v-for="(option, index) in houseList"
          :key="index"
          :label="option.storehouse_name"
          :value="option.id">
        </el-option>
      </el-select>   
      <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>
    <el-table :data="tableList" border :header-cell-style="{ backgroundColor: 'rgb(245, 247, 250)' }" >
        <el-table-column type="selection" width="55" align="center"> </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">
              {{getTotalCount(scope.row.drug_id,scope.row.max_unit,scope.row.min_unit,scope.row.min_number)}}
            </template>
        </el-table-column>
        <el-table-column prop="name" label="总报损金额" align="center">
            <template slot-scope="scope">
              {{getTotalPrice(scope.row.drug_id,scope.row.min_price)}}
            </template>
        </el-table-column>
        <el-table-column prop="name" label="仓库名称" align="center">
            <template slot-scope="scope">
              {{getHouseName(scope.row.storehouse_id)}}
            </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 label="操作" align="center" width="200">
            <template slot-scope="scope">
              <el-button type="primary" size="small" @click="clickDetai(scope.row.drug_id)">查看详情</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>

    <el-dialog
      title="盘点批次详情"
      :visible.sync="dialogVisible"
      width="80%">
      <span>
        <el-form>
          <el-row>
            <el-col :span="8">
              <el-form-item label="入库单号:">
                 <el-input v-model ="form.warehousing_order" style="width:200px"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="原因:">
                  <el-select v-model="form.type" placeholder="请选择" @change="changeReason">
                      <el-option
                        v-for="item in reasonList"
                        :key="item.id"
                        :label="item.name"
                        :value="item.id">
                      </el-option>
                  </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-button type="primary" @click="toQuery">查询</el-button>
            </el-col>
          </el-row>
          <el-row style="margin-bottom:10px;">
            <div>{{drug_name}}  &nbsp;&nbsp;总损耗数量: {{getDamagedCount(total_count)}}    &nbsp;&nbsp;总报损金额:{{(total_count * min_price).toFixed(2)}}</div>
          </el-row>
          <el-row>
            <el-table
              :data="goodList"
              border
              style="width: 100%">
              <el-table-column prop="date" label="序号" width="180" align="center">
               <template slot-scope="scope">
                  {{scope.$index + 1}}
                </template>
              </el-table-column>
              <el-table-column prop="name" label="入库单号" width="180" align="center">
                <template slot-scope="scope">
                  {{scope.row.warehousing_order}}
                </template>
              </el-table-column>
              <el-table-column prop="address" label="损耗数量" align="center">
                <template slot-scope="scope">
                  <span v-if="scope.row.count>0">{{scope.row.count}}{{scope.row.min_unit}}</span>
                </template>
              </el-table-column>
              <el-table-column prop="address" label="药损比" align="center">
                  <template slot-scope="scope">
                    {{getDrugDamageRadio(scope.row.count)}}%
                  </template>
              </el-table-column>
              <el-table-column prop="address" label="报损金额" align="center">
                <template slot-scope="scope">
                  <span v-if="scope.row.XtBaseDrug.min_price!=0">{{scope.row.XtBaseDrug.min_price}} </span>
                </template>
              </el-table-column>

              <el-table-column prop="address" label="盘点原因" align="center">
                <template slot-scope="scope">
                  <span v-if="scope.row.type == 6">默认</span>
                  <span v-if="scope.row.type == 1">到期退货</span>
                  <span v-if="scope.row.type == 2">异常退货</span>
                  <span v-if="scope.row.type == 3">退货</span>
                  <span v-if="scope.row.type == 4">损坏</span>
                  <span v-if="scope.row.type == 5">不计入报损分析</span>
                  <span v-if="scope.row.type == 7">有效期到期</span>
                </template>
              </el-table-column>
            </el-table>
          </el-row>
        </el-form>
      </span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import BreadCrumb from "../../components/bread-crumb";
import { getDrugDamageList,getDamagedByDrugId } from "@/api/drug/drug"
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:"",
                storehouse_id:"",
            },
            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:[],
            datamageList:[],
            reasonList:[
              {id:6,name:"默认"},
              {id:1,name:"到期退货"},
              {id:2,name:"异常退货"},
              {id:3,name:"退货"},
              {id:4,name:"损坏"},
              {id:5,name:"不计入报损分析"},
              {id:7,name:"有效期到期"}
            ],
            goodList:[],
            drug_name:"",
            total_count:0,
            min_number:0,
            max_unit:"",
            min_unit:"",
            min_price:0,
            drug_id:0,
            storehouse_id:0,
            houseList:[],
        }
    },
    methods:{

        search(){
          this.getlist()
        },
        handleSizeChange(val){
          this.limit = val
          this.getlist()
        },
        handleCurrentChange(val){
          this.page = val
          this.getlist()
        },

        getTime(val) {
         if(val < 0){
            return ""
          }
         if(val == ""){
            return ""
          }else {
            return uParseTime(val, '{y}-{m}-{d}')
         }
        },
      changeReason(val){
        this.form.type = val
        this.clickDetai(this.drug_id)
      },
      getlist(){
        var params = {
          limit:this.limit,
          page:this.page,
          start_time:this.start_time,
          end_time:this.end_time,
          keyword:this.searchKey,
          storehouse_id:this.storehouse_id,
        }

        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
             var datamagelist = response.data.data.damagelist
             this.datamageList = datamagelist
             var obj = {id:0,storehouse_name:"全部"}
             this.houseList = []
             this.houseList.push(obj)
             for(let i=0;i<response.data.data.houseList.length;i++){
              this.houseList.push(response.data.data.houseList[i])
             }
           }
        })
      },
    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
     },
     changeStartTime(){
       this.start_time = this.getTime(this.start_time)
       this.getlist()
     },
     changeEndTime(){
       this.end_time = this.getTime(this.end_time)
       this.getlist()
     },
     getTotalCount(drugid,max_unit,min_unit,min_number){

        var total = 0
        var str = ""
        var str_min = ""
        for(let i=0;i<this.datamageList.length;i++){
          if(drugid == this.datamageList[i].drug_id){
             total +=this.datamageList[i].count
          }
        }
        if(parseInt(total/min_number)!=0){
          str = parseInt(total/min_number) + max_unit
        }
        if((total%min_number)!=0){
          str_min = total%min_number + min_unit
        }
        return str+str_min
     },
     getTotalPrice(drugid,minprice){
        var total = 0
        for(let i=0;i<this.datamageList.length;i++){
          if(drugid == this.datamageList[i].drug_id){
            total+=this.datamageList[i].count
          }
        }
        return (total*minprice).toFixed(2)
     },
     clickDetai(drugid){
        this.drug_id = drugid
         var params = {
           drug_id:drugid,
           warehouseing_order:this.form.warehousing_order,
           type:this.form.type,
         }

        this.dialogVisible = true
        getDamagedByDrugId(params).then(response=>{
           if(response.data.state ==1){
             var list =  response.data.data.list
             for(let i=0;i<list.length;i++){
               list[i].min_number = 0
               list[i].min_number == list[i].XtBaseDrug.min_number
               list[i].total = 0
             }


             for(let i=0;i<list.length;i++){
               this.drug_name = list[0].drug_name
               list[i].total = list[i].count
             }
             var total = 0
             for(let i=0;i<list.length;i++){
               total += list[i].total
               this.min_number = list[0].XtBaseDrug.min_number
               this.max_unit = list[0].XtBaseDrug.max_unit
               this.min_unit = list[0].XtBaseDrug.min_unit
               this.min_price = list[0].XtBaseDrug.min_price
             }
             this.total_count = total
             this.goodList = list
           }
        })
     },
     getDamagedCount(total){

      var str = ""
      var min_str = ""
      if(total< this.min_number){
        str = ""
        min_str = total + this.min_unit
      }
     if(total >= this.min_number){
        if(parseInt(total/this.min_number)!=0){
            str = parseInt(total/this.min_number)+ this.max_unit
        }
       if((total%this.min_number)!=0){
          min_str =  total%this.min_number + this.min_unit
        }
      }
      return str + min_str
     },
    getDamagedCountOne(total){

      var str = ""
      var min_str = ""
      if(total< this.min_number){
        str = ""
        min_str = total
      }
     if(total >= this.min_number){
        if(parseInt(total/this.min_number)!=0){
            str = parseInt(total/this.min_number)
        }
       if((total%this.min_number)!=0){
          min_str =  total%this.min_number
        }
      }
      return str + min_str
     },
     toQuery(){
       this.clickDetai(this.drug_id)
     },
     getDrugDamageRadio(count){
       var total_count =  this.getDamagedCountOne(this.total_count)
       var all_count = 0
       all_count =  total_count * this.min_number

       return (count / all_count).toFixed(2)*100
     },
     changehouseList(){
       this.houseList = []
       this.getlist()
     },
     getHouseName(id){
      var storehouse_name  = ""
      for(let i=0;i<this.houseList.length;i++){
        if(id == this.houseList[i].id){
          storehouse_name = this.houseList[i].storehouse_name
        }
      }
      if(storehouse_name == "全部"){
        return ""
      }else{
        return storehouse_name
      }
     }
    },
    created(){
      this.getlist()
    },
}
</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>
