<template>
  <div class="page_drugs">
    <van-sticky>
      <div class="hisOrderTitle">
        <i class="iconfont icon-zuojiantou jiantou" @click="close"></i>
        <span class="titleName">选择药品</span>
        <span @click="save">确认添加</span>
      </div>
      <div class="searchBox">
        <van-search v-model="search_keyword" @keyup.enter.native='onSearch' @input='onSearch' placeholder="请输入搜索关键词"  style="width:80%;">
        </van-search>
        <div class="toolOne" @click="typeShow = true">
          {{ name }}
          <van-icon name="arrow-down"/>
        </div>
      </div>
      <van-checkbox class="allcheckTitle" v-model="allChecked" @click="toggle">
        <span style="width:70%;display:inline-block;line-height: initial;text-align:left;">名称/规格</span>
        <!-- <span style="width:20%;display:inline-block;line-height: initial;text-align:left;">规格</span> -->
        <span style="width:15%;display:inline-block;line-height: initial;text-align:left;">库存</span>
        <span style="width:15%;display:inline-block;line-height: initial;text-align:left;">单价</span>
      </van-checkbox>
    </van-sticky>
    <div class="drugsList">

      <van-checkbox-group v-model="result" ref="checkboxGroup" @change="checkGroup">
        <van-checkbox name="a" v-for="(item,index) in drug" :key="index" :name="item.id">
          <span style="width:70%;display:inline-block;line-height: initial;text-align:left;">{{item.drug_name}}&nbsp;{{item.drug_spec}}</span>
          <!-- <span style="width:20%;display:inline-block;line-height: initial;text-align:left;">{{item.drug_spec}}</span> -->
          <span style="width:15%;display:inline-block;line-height: initial;text-align:left;">{{item.total}}</span>
          <span style="width:15%;display:inline-block;line-height: initial;text-align:left;">{{item.min_price}}</span>
        </van-checkbox>

      </van-checkbox-group>
    </div>


    <van-popup v-model="typeShow" class="newPopup" position="bottom" :style="{ height: '40%' }">
      <van-picker
        show-toolbar
        :columns="drug_type_names"
        @confirm="chooseType"
        @cancel="typeShow = false"
      />
    </van-popup>
  </div>
</template>

<script>
  export default {

    props: {
      drug: Array,
      all_drug: Array,
      drug_type: Array,
      drug_type_names: Array,
    },
    data() {
      return {
        search_keyword:"",
        typeShow: false,
        name: '全部',
        typeColumns: ['杭州', '宁波', '温州', '绍兴', '湖州', '嘉兴', '金华', '衢州'],
        allChecked: false,
        result: [],
        lizi: ['a', 'b'] //例子判断全选的 用药品数组替换 可以删掉
      }
    },
    methods: {
      save(){
        let tempDrug = []
        let targetDrug = []

        for (let i = 0; i < this.all_drug.length; i++){
          for (let b = 0; b < this.result.length; b++){
            if(this.all_drug[i].id == this.result[b]){
              tempDrug.push(this.all_drug[i])
            }
          }
        }

        for (let b = 0; b < tempDrug.length; b++) {
          let obj = {
            advice_id: 0,
            drug_name: tempDrug[b].drug_name,
            single_dose: tempDrug[b].single_dose,
            delivery_way: tempDrug[b].delivery_way,
            execution_frequency: tempDrug[b].execution_frequency,
            retail_price: tempDrug[b].retail_price.toString(),
            remark: "",
            day: tempDrug[b].drug_day ? tempDrug[b].drug_day : 1,
            prescribing_number:tempDrug[b].prescribing_number.toString(),
            single_dose_unit: tempDrug[b].min_unit,
            prescribing_number_unit: tempDrug[b].max_unit,
            medical_insurance_number: tempDrug[b].medical_insurance_number,
            id: tempDrug[b].id,
            drug:tempDrug[b],
            hosp_appr_flag:"1",

          }
          targetDrug.push(obj)
        }

        this.$emit('add_drug', targetDrug)
        this.result = []
        this.close()

      },

      stockInCount: function(row) {
        let total = 0
        if (row.stock_in != null) {
          for (let i = 0; i < row.stock_in.length; i++) {
            total = total + row.stock_in[i].warehousing_count
          }
        }
        return total
      },
      salesReturnCount: function(row) {
        let total = 0
        if (row.sales_return != null) {

          for (let i = 0; i < row.sales_return.length; i++) {
            total = total + row.sales_return[i].count
          }
        }
        return total
      },
      stockOutCount: function(row) {
        let total = 0
        if (row.stock_out != null) {

          for (let i = 0; i < row.stock_out.length; i++) {
            total = total + row.stock_out[i].count
          }
        }
        return total
      },
      cancelStockCount: function(row) {
        let total = 0
        if (row.cancel_stock != null) {
          for (let i = 0; i < row.cancel_stock.length; i++) {
            total = total + row.cancel_stock[i].count
          }
        }
        return total
      },

      close() {
        this.result = []
        this.$parent.close(1);
      },
      onSearch() {
        if (this.search_keyword.length == 0) {
          this.drug = this.all_drug
        } else {
          let arr = []
          for (let i = 0; i < this.all_drug.length; i++) {
            if (this.all_drug[i].drug_name.indexOf(this.search_keyword) != -1 || this.all_drug[i].wubi.indexOf(this.search_keyword) != -1 || this.all_drug[i].pinyin.indexOf(this.search_keyword) != -1) {
              arr = arr.concat(this.all_drug[i])
            }
          }
          this.drug = arr

        }

      },
      chooseType(val) {
        this.name = val
        let select_id = 0
        let select_result = []
        for (let i = 0; i < this.drug_type.length; i++){
          if(this.drug_type[i].name == val){
            select_id = this.drug_type[i].id
          }
        }
        this.drug = []
        if(this.name == "全部"){
          this.drug = this.all_drug
        }else{
          for(let i = 0; i < this.all_drug.length; i++){
            if(this.all_drug[i].drug_type == select_id){
              select_result.push(this.all_drug[i])
            }
          }
          this.drug = select_result
        }

        this.typeShow = false

      },
      toggle() {
        if (this.allChecked) {
          for (let i = 0;i < this.drug.length;i++){
            this.result.push(this.drug[i].id)
          }
        } else {
          this.result = []
        }
      },
      checkGroup(arr) {
        if(arr.length == this.drug.length) {
          this.allChecked = true
        } else {
          this.allChecked = false
        }
      }
    }
  }
</script>
