<template>
    <el-dialog
        :title="title"
        width="900px"
        :visible.sync="visible"
        :before-close="_close"
    >
        <div style="display:flex;width:100%;">
            <div style="width:200px;">
              <el-table :data="tableData" border style="width: 100%;" height="300" :row-style="{ color: '#303133' }" 
                 :header-cell-style="{backgroundColor: 'rgb(245, 247, 250)',color: '#606266'}" highlight-current-row
                 ref="monthlyPlanTable" @current-change="handleCurrentChange">
                    <el-table-column align="center" prop="name" label="药品名称">
                        <template slot-scope="scope">
                           {{scope.row.drug_name}}
                         </template>
                    </el-table-column>
                </el-table>
            </div>


            <div style="flex:1;padding-left:10px;">
                <el-table :data="tableDatatwo" border style="width: 100%;" height="300"
                 ref="multipleTable"
                 :row-style="{ color: '#303133' }"
                 :header-cell-style="{backgroundColor: 'rgb(245, 247, 250)',color: '#606266'}" 
                  highlight-current-row
                  @select="selectGoodInfo"
                  @select-all="changeAllGoodInfoTableData">
                 <el-table-column type="selection" align="center" width="55"></el-table-column>
                 <el-table-column align="center" prop="name" label="规格名称">
                        <template slot-scope="scope">{{scope.row.drug_spec}}</template>
                    </el-table-column>
                </el-table>
            </div>
        </div>
        <div slot="footer" class="dialog-footer">
            <el-button @click="hide">取 消</el-button>
            <el-button type="primary" @click="SaveSelfMedicines()">保 存</el-button>
        </div>
    </el-dialog>         
</template>

<script>
import { getAllDrugNameList,getRullListByDrugName,SaveSelfMedicines } from "@/api/drug/drug"
export default {
    data(){
        return{
            visible:false,
            formTitle:'',
            tableData: [],
            patient_name:"",
            title:"",
            currentRow:"",
            tableDatatwo:[],
            patient_id:0,
            medicineData:[],
            standList:[],
            table_current_index:-1,
        }
    },
    methods:{
        _close: function(done) {
            done()
        },
        clear: function() {
            this.form.id = 0;
            this.form.name = "";
            this.form.intro = "";
        },
        show(name,id) {
           this.patient_id = id
           this.patient_name = name
           this.title = "添加自备药"+"("+name+")"
           this.visible = true
           this.$nextTick(() => {
            if( this.tableData.length > 0){
                this.$refs.monthlyPlanTable.setCurrentRow(this.tableData[0])
            }
          });
         },
        hide() {
           this.visible = false
        },

        getAllDrugNameList(){
           getAllDrugNameList().then(response=>{
             if(response.data.state == 1){
               var rullername =  response.data.data.rullerName
               console.log("rullerName",rullername)
               var list =   response.data.data.list
               console.log("list999999",list)
               this.standList = list
               this.tableData = list
             }
           }) 
        },
        handleCurrentChange(val){
          for(let i=0;i<this.tableData.length;i++){
             if(val.id == this.tableData[i].id){
                this.tableDatatwo = this.tableData[i].XtStandName
             }
          }

         for (let i = 0; i < this.tableDatatwo.length; i++) {
             console.log("222",this.tableDatatwo[i].isSelected)
            if (this.tableDatatwo[i].isSelected) {
                this.$nextTick(function() {
                this.$refs.multipleTable.toggleRowSelection(this.tableDatatwo[i])
                })
            }
         }
        //  this.currentRow = val
           
        //    this.getRullListByDrugName(val.id)
        },
        // getRullListByDrugName(id){
        //     const params = {
        //       id:id,
        //     }
        //    getRullListByDrugName(params).then(response=>{
        //       if(response.data.state ==  1){
        //         var drugName =  response.data.data.drugName
        //         this.tableDatatwo = drugName
        //       }
        //    })
        // },
        // handleSelectionChange(val){
        //    console.log("val22222",val)
        //    this.medicineData = val
        // },
        changeAllGoodInfoTableData(selection){  
        let selected = selection.length && selection.indexOf(selection) !== -1

        if (selection.length > 0) {
          for (let y = 0; y < this.tableDatatwo.length; y++) {
                for (let i = 0; i < selection.length; i++) {
                if (this.tableDatatwo[y].id == selection[i].id) {
                    this.tableDatatwo[y].isSelected = true
                }
               }
             }
           }else{
             for (let y = 0; y < this.tableDatatwo.length; y++) {
                    this.tableDatatwo[y].isSelected = false 
              }
           }
        },
        selectGoodInfo(rows, row){
          let selected = rows.length && rows.indexOf(row) !== -1
           if(selected == true){
            for (let y = 0; y < this.tableDatatwo.length; y++) {
                for (let i = 0; i < rows.length; i++) {
                if (this.tableDatatwo[y].id == rows[i].id) {
                    this.tableDatatwo[y].isSelected = true
                 }
                }
            }
           }
           if(selected == 0){
            for (let y = 0; y < this.tableDatatwo.length; y++) {
                this.tableDatatwo[y].isSelected = false 
            }
           }
        },
      SaveSelfMedicines(){
        this.multipleSelection = []
          for (let i = 0; i < this.tableData.length; i++) {
            for (let a = 0; a < this.tableData[i].XtStandName.length; a++) {
                if (this.tableData[i].XtStandName[a].isSelected) {
                    this.multipleSelection.push(this.tableData[i].XtStandName[a])
                }
            }
         }
          if(this.multipleSelection.length == 0){
             this.$message.error("请添加药品规格")
             return
          }
          const params = {
             patient_id:this.patient_id,
             medicineData:this.multipleSelection
          }
         SaveSelfMedicines(params).then(response=>{
             if(response.data.state == 1){
                var msg = response.data.data.msg
                this.$message.success("保存成功")
                this.visible = false
                this.$emit('getlist');
             }else {
                this.$message.error("药品名称规格已存在")
             }
           })   
        },
    },
  created(){
       //获取所有药品名称
      this.getAllDrugNameList()
  },
}
</script>

