<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :inline="true" :model="queryParams" label-width="68px">
      <el-form-item label="预约日期" prop="reservationDate">
        <el-date-picker v-model="queryParams.reservationDate" clearable placeholder="选择日期"
          size="small"
          style="width: 200px"
          type="date"
          value-format="yyyy-MM-dd">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="就诊类型" prop="type">
        <el-select v-model="queryParams.type" clearable placeholder="请选择类型" size="small">
          <el-option
              v-for="item in visitTypes"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="cyan" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['monitor:ApplyReport:remove']"
          :disabled="multiple"
          icon="el-icon-delete"
          size="mini"
          type="danger"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['monitor:ApplyReport:export']"
          icon="el-icon-download"
          size="mini"
          type="warning"
          @click="handleExport"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="ApplyReportList" @selection-change="handleSelectionChange">
      <el-table-column align="center" type="selection" width="55" />
       <el-table-column align="center" label="日期" prop="reservationDate" />
      <el-table-column :formatter="visitTypeFormat" align="center" label="类型" prop="type"/>
      <el-table-column align="center" label="已预约" prop="outUseSourceCount" />
      <el-table-column align="center" label="已完成" prop="outUseCount" />
      <el-table-column align="center" label="已完成百分比" prop="outUsePercent" />
      <el-table-column align="center" label="已失效" prop="invalid" />
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['monitor:ApplyReport:remove']"
            icon="el-icon-delete"
            size="mini"
            type="text"
            @click="handleDelete(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :limit.sync="queryParams.pageSize"
      :page.sync="queryParams.pageNum"
      :total="total"
      @pagination="getList"
    />

  </div>
</template>

<script>
import { listApplyReport, getApplyReport, delApplyReport, addApplyReport, updateApplyReport, exportApplyReport } from "@/api/monitor/ApplyReport";

export default {
  name: "ApplyReport",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 预约报表表格数据
      ApplyReportList: [],
      // 弹出层标题
      title: "",
      visitTypes: [
        {"id":"1",name:"门诊"},
         {"id":"2",name:"住院"},
          {"id":"3",name:"体检"},
      ],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        invalid: null,
        reservationDate: null,
        applyState: null,
        type: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询预约报表列表 */
    getList() {
      this.loading = true;
      listApplyReport(this.queryParams).then(response => {
        this.ApplyReportList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        outUseSourceCount: null,
        inUseSourceCount: null,
        phyUseSourceCount: null,
        outUseCount: null,
        inUseCount: null,
        phyUseCount: null,
        outUsePercent: null,
        inUsePercent: null,
        phyUsePercent: null,
        invalid: null,
        reservationDate: null,
        applyState: null,
        type: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    visitTypeFormat(row,column){
     var reservationResult = "";
      if(row.type != null){
        this.visitTypes.forEach( el => {
           if(row.type === el.id){
              reservationResult = el.name
           }else{
             return reservationResult;
           }
        });
      }

      return reservationResult;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加预约报表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getApplyReport(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改预约报表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateApplyReport(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addApplyReport(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除预约报表编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delApplyReport(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有预约报表数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportApplyReport(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
