<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" :row-style="{ color: '#303133' }" :header-cell-style="{backgroundColor: 'rgb(245, 247, 250)',color: '#606266'}" 
                 highlight-current-row  @selection-change="handleSelectionChange">
                    <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:[]
        }
    },
    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)
               this.tableData = rullername
             }
           }) 
        },
        handleCurrentChange(val){
           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){
           this.medicineData = val
        },

        SaveSelfMedicines(){
          if(this.medicineData.length == 0){
             this.$message.error("请添加药品规格")
             return
          }
          const params = {
             patient_id:this.patient_id,
             medicineData:this.medicineData
          }
         console.log("params----",params)
       
         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>

