<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="诊所名称" prop="hospitalName">
        <el-input
        style="width: 220px"
          v-model="queryParams.hospitalName"
          placeholder="请输入诊所名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="companyMobile">
        <el-input
          v-model="queryParams.companyMobile"
          placeholder="请输入联系电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
     
      <el-form-item label="状态" prop="status">
        <el-select style="width: 220px" v-model="queryParams.status" placeholder="请选择状态" clearable size="small">
              <el-option
                v-for="dict in statusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
        </el-select>
      </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="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['hospital:hospital:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['hospital:hospital:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['hospital:hospital:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['hospital:hospital:export']"
        >导出</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table  height="660" border v-loading="loading" :data="hospitalList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="hospitalId" />
      <el-table-column label="诊所名称" align="center" prop="hospitalName" />
      <el-table-column label="管理员帐号" width="100px" align="center" prop="userName" />
      <el-table-column label="余额" align="center" prop="money" />
      <el-table-column label="联系电话" align="center" prop="hospitalMobile" />
      <el-table-column label="用户数量" align="center" prop="limitUserCount" />
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="执照" align="center" prop="licenseUrl" width="120">
         <template slot-scope="scope">
           <el-popover
             placement="right"
             title=""
             trigger="hover"
           >
             <img slot="reference" :src="scope.row.licenseUrl" width="100">
             <img :src="scope.row.licenseUrl" style="max-width: 150px;">
           </el-popover>
         </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
              <el-tag prop="status" v-for="(item, index) in statusOptions"  :type="scope.row.status==1?'success':'danger'"  v-if="scope.row.status==item.dictValue">{{item.dictLabel}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime">
      </el-table-column>
      <!-- <el-table-column label="到期时间" align="center" prop="limitTime">
      </el-table-column> -->
      <el-table-column label="操作" align="center"  fixed="right" width="200px" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            style="margin-left:10px"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['hospital:hospital:edit']"
          >编辑</el-button>
           
           <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleResetPwd(scope.row)"
            v-hasPermi="['hospital:hospital:resetPwd']"
          >重置密码</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['hospital:hospital:remove']"
          >删除</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleRecharge(scope.row)"
            v-hasPermi="['hospital:hospital:recharge']"
          >充值</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDeduct(scope.row)"
            v-hasPermi="['hospital:hospital:deduct']"
          >扣款</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-dialog :title="title" :visible.sync="open" width="650px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="诊所名称" prop="hospitalName">
          <el-input v-model="form.hospitalName" placeholder="请输入诊所名称" />
        </el-form-item>
        <el-form-item label="诊所执照" prop="licenseUrl">
          <el-upload
              v-model="form.licenseUrl"
              class="avatar-uploader"
              :action="uploadUrl"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload">
              <img v-if="form.licenseUrl" :src="form.licenseUrl" class="avatar" width="200px">
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="联系电话" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="员工数量" prop="limitUserCount">
          <el-input-number v-model="form.limitUserCount"  :min="1" :max="10000"  ></el-input-number>
        </el-form-item>
        <el-form-item  label="管理员帐号" prop="userName" v-if="form.hospitalId==null">
          <el-input v-model="form.userName" placeholder="请输入管理员帐号" />
        </el-form-item>
         <el-form-item  label="管理员密码" prop="password" v-if="form.hospitalId==null">
          <el-input type="password" v-model="form.password" placeholder="请输入管理员密码" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
           <el-radio-group v-model="form.status">
            <el-radio v-for="dict in statusOptions" :label="dict.dictValue">{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </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>
    <el-dialog :title="recharge.title" :visible.sync="recharge.open" width="500px" append-to-body>
      <el-form ref="rechargeForm" :rules="rechargeRules" :model="rechargeForm"  label-width="80px">
        <el-form-item label="诊所" >
          <el-input v-model="rechargeForm.hospitalName" disabled />
        </el-form-item>
        <el-form-item label="余额" >
          <el-input v-model="rechargeForm.balance" disabled />
        </el-form-item>
        <el-form-item label="充值金额" prop="money">
          <el-input-number v-model="rechargeForm.money" :min="0" placeholder="请输入充值金额" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="rechargeForm.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitRechargeForm">确 定</el-button>
        <el-button @click="recharge.open=false">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="deduct.title" :visible.sync="deduct.open" width="500px" append-to-body>
      <el-form ref="deductForm" :rules="deductRules" :model="deductForm"  label-width="80px">
        <el-form-item label="诊所" >
          <el-input v-model="deductForm.hospitalName" disabled />
        </el-form-item>
        <el-form-item label="余额" >
          <el-input v-model="deductForm.balance" disabled />
        </el-form-item>
        <el-form-item label="扣款金额" prop="money">
          <el-input-number v-model="deductForm.money" :min="0" placeholder="请输入扣款金额" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="deductForm.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitDeductForm">确 定</el-button>
        <el-button @click="deduct.open=false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {recharge,deduct, resetPwd,listHospital, getHospital, delHospital, addHospital, updateHospital, exportHospital } from "@/api/hospital/hospital";


export default {
  name: "hospital",
  data() {
    return {
      uploadUrl:process.env.VUE_APP_BASE_API+"/common/uploadOSS",
      // 表单参数
      rechargeForm: {
        money:0,
      },
      // 表单校验
      rechargeRules: {
        money: [
          { required: true, message: "充值金额不能为空", trigger: "blur" }
        ],
      },
      recharge:{
        open:false,
        title:"后台充值"
      },
      // 表单参数
      deductForm: {
        money:0,
      },
      // 表单校验
      deductRules: {
        money: [
          { required: true, message: "扣款金额不能为空", trigger: "blur" }
        ],
      },
      deduct:{
        open:false,
        title:"后台扣款"
      },
      packageIds: [],
      packages:[],
      typeOptions:[],
      statusOptions:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 企业表格数据
      hospitalList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        hospitalName: null,
        mobile: null,
        address: null,
        status: null,
        startTime: null,
        endTime: null,
        money: null,
        times: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        hospitalName: [
          { required: true, message: "诊所名称不能为空", trigger: "blur" }
        ],
        licenseUrl:[
          { required: true, message: "诊所执照不能为空", trigger: "blur" }
        ],
        mobile: [
          { required: true, message: "诊所电话不能为空", trigger: "blur" }
        ],
        address: [
          { required: true, message: "诊所地址不能为空", trigger: "blur" }
        ],
        limitUserCount: [
          { required: true, message: "用户数量不能为空", trigger: "blur" }
        ],
        userName: [
          { required: true, message: "帐号不能为空", trigger: "blur" }
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getDicts("sys_company_status").then((response) => {
      this.statusOptions = response.data;
    });
    this.getDicts("sys_hospital_type").then((response) => {
      this.typeOptions = response.data;
    });
    this.getList();
  },
  methods: {
    handleAvatarSuccess(res, file) {
            if(res.code==200){
              this.form.licenseUrl=res.url;
              console.log(this.form.licenseUrl)
              // self.$forceUpdate()
            }
            else{
              this.msgError(res.msg);
            }
        },
        beforeAvatarUpload(file) {
          const isLt1M = file.size / 1024 / 1024 < 1;
          if (!isLt1M) {
            this.$message.error('上传图片大小不能超过 1MB!');
          }
          return   isLt1M;
        },
    handleDeduct(row) {
      const hospitalId = row.hospitalId  
      this.deductForm.hospitalId=row.hospitalId;
      this.deductForm.hospitalName=row.hospitalName;
      this.deductForm.balance=row.money;
      this.deduct.open = true;
    },
    /** 提交按钮 */
    submitDeductForm() {
      this.$refs["deductForm"].validate(valid => {
        if (valid) {
          deduct(this.deductForm).then(response => {
            if (response.code === 200) {
              this.msgSuccess(response.msg);
              this.deduct.open = false;
              this.getList();
            }
          });
        }
      });
    },
    handleRecharge(row) {
      const hospitalId = row.hospitalId  
      this.rechargeForm.hospitalId=row.hospitalId;
      this.rechargeForm.hospitalName=row.hospitalName;
      this.rechargeForm.balance=row.money;
      this.recharge.open = true;
    },
    /** 提交按钮 */
    submitRechargeForm() {
      this.$refs["rechargeForm"].validate(valid => {
        if (valid) {
          recharge(this.rechargeForm).then(response => {
            if (response.code === 200) {
              this.msgSuccess(response.msg);
              this.recharge.open = false;
              this.getList();
            }
          });
        }
      });
    },
    /** 查询企业列表 */
    getList() {
      this.loading = true;
      listHospital(this.queryParams).then(response => {
        this.hospitalList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        hospitalId: null,
        hospitalName: null,
        hospitalMobile: null,
        hospitalAddress: null,
        createTime: null,
        updateTime: null,
        status: '0',
        startTime: null,
        endTime: null,
        money: null,
        times: null,
        voiceApiId: null
      };
      this.packageIds=[];
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.hospitalId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "创建诊所";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const hospitalId = row.hospitalId || this.ids
      getHospital(hospitalId).then(response => {
        this.form = response.data;
        this.form.status = response.data.status.toString();
        this.open = true;
        this.title = "修改诊所";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.hospitalId != null) {
            updateHospital(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addHospital(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    
    handleResetPwd(row) {
      const hospitalIds = row.hospitalId || this.ids;
      this.$confirm('是否确认重复密码为123456?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return resetPwd(hospitalIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("修改成功");
        }).catch(function() {});
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const hospitalIds = row.hospitalId || this.ids;
      this.$confirm('是否确认删除诊所编号为"' + hospitalIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delhospital(hospitalIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },

    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有诊所数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exporthospital(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
    
    
  }
};
</script>

<style>
.avatar-uploader .el-upload {
     border: 1px dashed #d9d9d9;
     border-radius: 6px;
     cursor: pointer;
     position: relative;
     overflow: hidden;
   }
   .avatar-uploader .el-upload:hover {
     border-color: #409EFF;
   }

   .avatar-uploader-icon {
     font-size: 28px;
     color: #8c939d;
     width: 150px;
     height: 150px;
     line-height: 150px;
     text-align: center;
   }
</style>
