<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="医生账户" prop="doctorAccount">
        <el-input
          v-model="queryParams.doctorAccount"
          placeholder="请输入医生账户"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="医生名称" prop="doctorName">
        <el-input
          v-model="queryParams.doctorName"
          placeholder="请输入医生名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户ID" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入用户ID"
          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="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入电话号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属公司" prop="companyId" >
      <el-select v-model="queryParams.companyId" placeholder="请选择所属公司" filterable size="small">
            <el-option v-for="(option, index) in companyList" :key="index" :value="option.dictValue" :label="option.dictLabel"></el-option>
       </el-select>
      </el-form-item>
      <el-form-item label="店铺" prop="storeId">
         <el-select v-model="queryParams.storeId" placeholder="店铺" clearable size="small" filterable>
        <el-option
            v-for="dict in storeOPtions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="月份" prop="month">
          <el-date-picker
              v-model="month"
              type="month"
              placeholder="选择月份"
              format="yyyy-MM"
            ></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-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['his:drugReportCount:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="drugReportCountList" @selection-change="handleSelectionChange" border height="550">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户名称" align="center" prop="nickName" />
      <el-table-column label="用户电话" align="center" prop="phone" />
      <el-table-column label="就诊人" align="center" prop="patientName" />
      <el-table-column label="医生账户" align="center" prop="doctorAccount" />
      <el-table-column label="医生名称" align="center" prop="doctorName" />
      <el-table-column label="所属公司" align="center" prop="companyName" />
      <el-table-column label="药店" align="center" prop="storeName" />
      <el-table-column label="咨询次数" align="center" prop="count" />
      <el-table-column label="月份" align="center" prop="month" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180"/>
    </el-table>

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

    <!-- 添加或修改咨询统计对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入用户id" />
        </el-form-item>
        <el-form-item label="病人id" prop="patientId">
          <el-input v-model="form.patientId" placeholder="请输入病人id" />
        </el-form-item>
        <el-form-item label="病人名称" prop="patientName">
          <el-input v-model="form.patientName" placeholder="请输入病人名称" />
        </el-form-item>
        <el-form-item label="医生id" prop="doctorId">
          <el-input v-model="form.doctorId" placeholder="请输入医生id" />
        </el-form-item>
        <el-form-item label="医生账户" prop="doctorAccount">
          <el-input v-model="form.doctorAccount" placeholder="请输入医生账户" />
        </el-form-item>
        <el-form-item label="医生名称" prop="doctorName">
          <el-input v-model="form.doctorName" placeholder="请输入医生名称" />
        </el-form-item>
        <el-form-item label="所属公司" prop="companyId">
          <el-input v-model="form.companyId" placeholder="请输入所属公司" />
        </el-form-item>
        <el-form-item label="药店id" prop="storeId">
          <el-input v-model="form.storeId" placeholder="请输入药店id" />
        </el-form-item>
        <el-form-item label="咨询次数" prop="count">
          <el-input v-model="form.count" placeholder="请输入咨询次数" />
        </el-form-item>
        <el-form-item label="月份" prop="month">
          <el-input v-model="form.month" placeholder="请输入月份" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDrugReportCount, getDrugReportCount, delDrugReportCount, addDrugReportCount, updateDrugReportCount, exportDrugReportCount } from "@/api/his/drugReportCount";
import {listStore} from "@/api/his/storeProduct";
import {allList}from "@/api/company/company";
export default {
  name: "DrugReportCount",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 咨询统计表格数据
      drugReportCountList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      month:null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        patientId: null,
        patientName: null,
        doctorId: null,
        doctorAccount: null,
        doctorName: null,
        companyId: null,
        storeId: null,
        count: null,
        month: null,
      },
      // 表单参数
      form: {},
      storeOPtions: [],
      companyList:[],
      // 表单校验
      rules: {
      }
    };
  },
  created() {

    listStore().then(response => {
      this.storeOPtions = response.rows;
    });
    allList().then(response => {
      this.companyList = response.rows;
    });
    this.getList();
  },
  methods: {
    /** 查询咨询统计列表 */
    getList() {
      this.loading = true;
      listDrugReportCount(this.queryParams).then(response => {
        this.drugReportCountList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        patientId: null,
        patientName: null,
        doctorId: null,
        doctorAccount: null,
        doctorName: null,
        companyId: null,
        storeId: null,
        count: null,
        month: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if(this.month!=null){
        const year = this.month.getFullYear();
        const month = (this.month.getMonth() + 1).toString().padStart(2, '0');
        this.queryParams.month = year+month
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.month=null;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加咨询统计";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDrugReportCount(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) {
            updateDrugReportCount(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDrugReportCount(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 delDrugReportCount(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有咨询统计数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportDrugReportCount(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
