<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="处方单号" prop="prescribeCode">
        <el-input
          v-model="queryParams.prescribeCode"
          placeholder="请输入处方单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="患者姓名" prop="patientName">
        <el-input
          v-model="queryParams.patientName"
          placeholder="请输入患者姓名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处方类型" prop="prescribeType">
        <el-select v-model="queryParams.prescribeType" placeholder="请选择处方类型" clearable size="small">
          <el-option
            v-for="dict in prescribeTypeOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="开方时间">
        <el-date-picker v-model="createTime" size="small" style="width: 220px" value-format="yyyy-MM-dd" type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" @change="changeTime"></el-date-picker>
          </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table v-loading="loading"  border :data="prescribeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="处方单号" align="center" prop="prescribeCode" />
      <el-table-column label="处方类型" align="center" prop="prescribeType" width="100px">
        <template slot-scope="scope">
          <dict-tag :options="prescribeTypeOptions" :value="scope.row.prescribeType"/>
        </template>
      </el-table-column>
      <el-table-column label="患者姓名" align="center" prop="patientName" />
      <el-table-column label="患者年龄" align="center" prop="patientAge" />
      <el-table-column label="患者性别" align="center" prop="patientGender" >
        <template slot-scope="scope">
            <span v-if="scope.row.patientGender == 1">男</span>
            <span v-if="scope.row.patientGender == 2">女</span>
        </template>
      </el-table-column>
      <el-table-column label="诊断" align="center" prop="diagnose" />
      <el-table-column label="药品名称" align="center">
        <template slot-scope="scope">
          <span v-for="(size, index) in scope.row.drugs">
            {{ size.drugName }}
            <span v-if="index < scope.row.drugs.length - 1">, </span>
          </span>
        </template>
      </el-table-column>
      <el-table-column label="药品种类数量" align="center">
        <template slot-scope="scope">
          {{ scope.row.drugs.length }}
        </template>
      </el-table-column>
      <el-table-column label="开方时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="150px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleDetails(scope.row)"
          >详情</el-button>
        </template>
      </el-table-column>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <el-drawer
            :with-header="false"
            size="75%"
             :title="show.title" :visible.sync="show.open">
         <prescribeDetails  ref="Details" />
    </el-drawer>
 

  </div>
</template>

<script>
import { getPrescribeList } from "@/api/prescribe";
import prescribeDetails from '@/views/components/myDrugStore/prescribeDetails.vue';
export default {
  name: "Doctorbill",
  components:{prescribeDetails},
  data() {
    return {
      show:{
              title:"账单详情",
              open:false,
            },
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      dateRange: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 医生账单记录表格数据
      prescribeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 类型字典
      prescribeTypeOptions: [],
      createTime:null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("sys_prescribe_type").then(response => {
      this.prescribeTypeOptions = response.data;
    });
  },
  methods: {
    changeTime(){
        
        if(this.createTime!=null){
          this.queryParams.beginTime=this.createTime[0];
          this.queryParams.endTime=this.createTime[1];
        }else{
          this.queryParams.beginTime=null;
          this.queryParams.endTime=null;
        }

    },
    handleDetails(row){
        this.show.open=true;
        setTimeout(() => {
              this.$refs.Details.getDetails(row.prescribeId);
        }, 1);
    },
    /** 查询医生账单记录列表 */
    getList() {
      this.loading = true;
      this.queryParams.type = 1;
      this.queryParams.status = 2;
      getPrescribeList(this.queryParams).then(response => {
        this.prescribeList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        billId: null,
        doctorId: null,
        title: null,
        money: null,
        balance: null,
        billType: null,
        remark: null,
        createTime: null,
        businessId: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
       this.createTime=null;
      this.queryParams.beginTime=null;
      this.queryParams.endTime=null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.billId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加医生账单记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const billId = row.billId || this.ids
      getDoctorbill(billId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改医生账单记录";
      });
    },
   
  }
};
</script>
