<template>
    <el-dialog
        :title="formTitle"
        width="900px"
        :visible.sync="visible"
        :before-close="_close"
    >
        <div style="margin-bottom:10px;">
            <el-date-picker
                v-model="start_time"
                prefix-icon="el-icon-date"
                :editable="false"
                style="width: 196px;margin-right:10px;"
                type="date"
                placeholder="选择日期时间"
                align="right"
                format="yyyy-MM-dd"
                value-format="yyyy-MM-dd"
            ></el-date-picker>
            <el-select v-model="admin_user_id" placeholder="请选择">
                <el-option
                v-for="item in staffTable"
                :key="item.admin_user_id"
                :label="item.user_name"
                :value="item.admin_user_id">
                </el-option>
            </el-select>
        </div>
        <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>
           
            <el-table-column align="center" label="药品名称">
                <template slot-scope="scope">{{scope.row.drug_name}}</template>
            </el-table-column>
            <el-table-column align="center" label="规格名称">
                <template slot-scope="scope">{{scope.row.drug_spec}}</template>
            </el-table-column>
            <el-table-column align="center" label="入库数量">
                <template slot-scope="scope">
                    <el-input v-model="scope.row.store_number" placeholder="请输入内容"></el-input>
                </template>
            </el-table-column>
            <el-table-column align="center" label="备注">
                <template slot-scope="scope">
                    <el-input v-model="scope.row.remarks" placeholder="请输入内容"></el-input>
                </template>
            </el-table-column>
        </el-table>
         <div slot="footer" class="dialog-footer">
            <el-button @click="hide">取 消</el-button>
            <el-button type="primary" @click="saveStock">保 存</el-button>
        </div>
    </el-dialog>         
</template>

<script>
const moment = require('moment')
import {getCurrentOrgAllStaff,getAllDrugNameList,saveStock,getDrugDataByPatientId} from "@/api/drug/drug"
export default {
    data(){
        return{
            visible:false,
            formTitle:'',
            tableData: [],
            staffTable: [],
            value: '',
            start_time: moment(new Date()).add('year',0).format("YYYY-MM-DD"),
            admin_user_id:this.$store.getters.xt_user.user.id,
            patient_name:"",
            patient_id:""
        }
    },
    methods:{
        _close: function(done) {
            // this.clear()
            done()
        },
        clear: function() {
            this.form.id = 0;
            this.form.name = "";
            this.form.intro = "";
        },
        show(name,id) {
            this.patient_id = id
            this.formTitle = "自备药入库("+name+")"
            this.visible = true
           //根据患者ID 获取该患者的自备药
            this.getDrugDataByPatientId()
        },
        hide() {
           this.visible = false
        },
        getCurrentOrgAllStaff(){
          getCurrentOrgAllStaff().then(response=>{
             if(response.data.state == 1){
               var staff = response.data.data.staff
               this.staffTable = staff
             }
          })   
        },
    
        saveStock(){
            var arr=[]
            for(let i=0;i<this.tableData.length;i++){
              if(this.tableData[i].store_number){
                  arr.push(this.tableData[i])
              }
            }
            const params ={
               stocks:arr
            }
            if(this.admin_user_id ==''){
              this.$message.error("请选择入库人")
              return
            }
          saveStock(params,this.start_time,this.admin_user_id,this.patient_id).then(response=>{
              if(response.data.state == 1){
                 var msg = response.data.data.msg
                 this.$message.success("保存成功")
                 this.visible = false
                 this.$emit('getlist');
              }
          })
        },

        getDrugDataByPatientId(){
           const params = {
              patient_id:this.patient_id
           }
           console.log("params",params)
          getDrugDataByPatientId(params).then(response=>{
             if(response.data.state == 1){
               var medicalList = response.data.data.medicalList
                for(let i=0;i<medicalList.length;i++){
                  medicalList[i].outstore_number = ""
                  medicalList[i].remarks = ""
                }
                this.tableData = medicalList
             }
          })  
        }
    },
    created(){
      //获取当前机构的所有人员
      this.getCurrentOrgAllStaff() 
       //获取药品名称规格
      // this.getAllDrugNameList()
      
    }
}
</script>

