<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="" v-if="doctor!=null" >
          <span style="font-weight:bold;" >{{doctor.doctorName}}</span>
      </el-form-item>
      <el-form-item label="可提现金额" v-if="doctor!=null" >
          <span style="font-size:20px;color:red;">{{doctor.balance.toFixed(2)}}</span>元
      </el-form-item>
      <el-form-item label="提现类型" prop="extractType">
        <el-select v-model="queryParams.extractType" placeholder="请选择提现类型" clearable size="small">
          <el-option
            v-for="dict in extractTypeOptions"
            :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"
          
          size="mini"
          @click="handleExtract"
        >提现申请</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          
          size="mini"
          @click="handleEditExtract"
        >提现方式</el-button>
      </el-col>
	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
  <el-tabs type="card" v-model="queryParams.status" @tab-click="handleClickX">
      <el-tab-pane v-for="(item,index) in statusOptions" :label="item.dictLabel" :name="item.dictValue"></el-tab-pane>
    </el-tabs>
    <el-table v-loading="loading" border  :data="doctorextractList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="提现类型" align="center" prop="extractType">
        <template slot-scope="scope">
          <dict-tag :options="extractTypeOptions" :value="scope.row.extractType"/>
        </template>
      </el-table-column>
     
      <el-table-column label="提现状态" align="center" prop="status">
        <template slot-scope="scope">
            <el-tag v-for="(item, index) in statusOptions" :type="scope.row.status==-1?'danger':'success'"  v-if="scope.row.status==item.dictValue" :value="scope.row.status">{{item.dictLabel}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="提现金额" align="center" prop="extractPrice" />
     
      <el-table-column label="余额" align="center" prop="balance" />
      <el-table-column label="无效原因" align="center" prop="failMsg" />
     
      
      <el-table-column label="创建时间" align="center" prop="createTime" 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="100px">
        <el-form-item label="提现类型" prop="extractType">
              <el-radio-group v-model="extractJson.extractType">
                  <el-radio :label="item.dictValue" v-for="item in extractTypes" >{{item.dictLabel}}</el-radio>
              </el-radio-group>
         </el-form-item>
        <el-form-item label="姓名" prop="userName">
          <el-input v-model="extractJson.userName" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="联系电话" prop="phone">
          <el-input v-model="extractJson.phone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="微信号" prop="wechat" v-if="extractJson.extractType == 'wx'">
          <el-input v-model="extractJson.wechat" placeholder="请输入微信号" />
        </el-form-item>
        <el-form-item label="真实姓名" prop="alipayName" v-if="extractJson.extractType == 'alipay'">
          <el-input v-model="extractJson.alipayName" placeholder="请输入真实姓名" />
        </el-form-item>
        <el-form-item label="支付宝账户" prop="alipayAccount" v-if="extractJson.extractType == 'alipay'">
          <el-input v-model="extractJson.alipayAccount" placeholder="请输入支付宝账户" />
        </el-form-item>
        <el-form-item label="银行卡号" prop="bankNo" v-if="extractJson.extractType == 'bank'">
          <el-input v-model="extractJson.bankNo" placeholder="请输入银行卡号" />
        </el-form-item>
        <el-form-item label="银行名称" prop="bankName" v-if="extractJson.extractType == 'bank'">
          <el-input v-model="extractJson.bankName" placeholder="请输入银行名称" />
        </el-form-item>
        <el-form-item label="支行名称" prop="bankSubName" v-if="extractJson.extractType == 'bank'">
          <el-input v-model="extractJson.bankSubName" placeholder="请输入银行支行名称" />
        </el-form-item>
        
        <el-form-item label="验证码" prop="code">
          <el-input v-model="extractForm.code" placeholder="请输入验证码">
              <template #append>
                  <el-button @click="handleGetCode" type="primary" :disabled="counting" style="margin-left: -1px;">
                      {{ counting ? `${countdown} 秒后重新获取` : '获取验证码' }}
                  </el-button>
              </template>
          </el-input>
      </el-form-item>
      
       </el-form>
       <div style="margin-top: 5px; color: #999; font-size: 12px;">注：请核对并确认提现账号信息</div>
      <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="show.title" :visible.sync="show.open" width="500px" append-to-body>
      <el-form ref="extractForm" :model="extractForm" :rules="rules" label-width="100px">
        <el-form-item label="提现方式"  v-if="doctor.extractJson==null"> 
          <el-input  >
                <template #append>
                    <el-button @click="handleEditExtract" type="primary" style="margin-left: -1px;">请选择提现方式</el-button>
                </template>
            </el-input>
         </el-form-item>
         <el-form-item label="提现金额(元)" prop="money">
          <el-input v-model="extractForm.money" placeholder="请输入提现金额"/>
        </el-form-item>
        
        <el-form-item label="验证码" prop="code">
          <el-input v-model="extractForm.code" placeholder="请输入验证码">
              <template #append>
                  <el-button @click="handleGetCode" type="primary" :disabled="counting" style="margin-left: -1px;">
                      {{ counting ? `${countdown} 秒后重新获取` : '获取验证码' }}
                  </el-button>
              </template>
          </el-input>
      </el-form-item>
      
       </el-form>
       <div style="display: flex; align-items: center; margin-top: 10px;">
        <div style="color: #999; font-size: 12px;">可提现金额：{{ doctor.balance.toFixed(2) }}元</div>
        <div style="margin-left: 10px;">
            <el-button @click="handleAllExtract" type="text" style="color: green; font-size: 12px;">全部提现 ({{ doctor.balance.toFixed(2) }})</el-button>
        </div>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitExtractForm">立即提现</el-button>
        <el-button @click="cancel2">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { getDoctorExtractList } from "@/api/moneys";
import { extract, editExtract,sendSmsCode} from "@/api/doctor.js";
export default {
  name: "Doctorextract",
  data() {
    return {
      show:{
              title:"提现申请",
              open:false,
            },

      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      countdown: 0,
      counting: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 医生提现管理表格数据
      doctorextractList: [],
      extractTypes:[
      { dictValue: "wx", dictLabel: '微信' },
      { dictValue: "alipay", dictLabel: '支付宝' },
      { dictValue: "bank", dictLabel: '银行账户' },
      ],
      extractJson:{
        
      },
      extractForm:{
        money:null,
        code:null
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // bank = 银行卡 alipay = 支付宝wx=微信字典
      extractTypeOptions: [],
      // -1 未通过 0 审核中 1 已提现字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        doctorId: null,
        realName: null,
        extractType: null,
        bankCode: null,
        bankAddress: null,
        alipayCode: null,
        extractPrice: null,
        mark: null,
        balance: null,
        failMsg: null,
        failTime: null,
        createTime: null,
        status: "0",
        wechat: null,
        beginTime:null,
        endTime:null,
      },
       createTime:null,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        code: [
          { required: true, message: "验证码不能为空", trigger: "blur" }
        ]
      }
    };
  },
  computed: {
    doctor() {
      return this.$store.state.user.doctor;
    }
  },
  created() {
    this.getList();
    this.getDicts("sys_extract_type").then(response => {
      this.extractTypeOptions = response.data;
    });
    this.getDicts("sys_extract").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    handleGetCode(){
      if (this.counting) return; // 防止重复点击

        // 发送验证码请求的逻辑
        sendSmsCode().then(response => {
            if(response.code==200){
              this.msgSuccess(response.msg);
              }
            });

        this.countdown = 60;
        this.counting = true;

        const timer = setInterval(() => {
            if (this.countdown > 0) {
                this.countdown--;
            } else {
                this.counting = false;
                clearInterval(timer);
            }
        }, 1000);

    },
    handledetails(row){
        this.show.open=true;
        setTimeout(() => {
             this.$refs.Details.getDetails(row.extractId);
        }, 1);
    },
    change(){
          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;
          }

        },

    /** 查询医生提现管理列表 */
    getList() {
      this.loading = true;
      getDoctorExtractList(this.queryParams).then(response => {
        this.doctorextractList = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancel2(){
      this.show.open=false;
    },
    // 表单重置
    reset() {
      this.form = {
        extractId: null,
        doctorId: null,
        realName: null,
        extractType: null,
        bankCode: null,
        bankAddress: null,
        alipayCode: null,
        extractPrice: null,
        mark: null,
        balance: null,
        failMsg: null,
        failTime: null,
        createTime: null,
        updateTime: null,
        status: "0",
        wechat: null,
        isDel: null
      };
      this.extractJson={
        extractType:"wx"
      }
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleClickX(){
      this.handleQuery();
    },
    /** 重置按钮操作 */
    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.extractId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleEditExtract() {
      this.reset();
      this.open = true;
      if(this.doctor.extractJson!=null){
        this.extractJson = JSON.parse(this.doctor.extractJson)
      }
      this.title = "提现方式";
    },
    
    handleExtract() {
      this.extractForm.money = null;
      this.extractForm.code = null;
      this.show.open = true;
    },
    handleAllExtract(){
      this.extractForm.money = this.doctor.balance;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.extractJson = JSON.stringify(this.extractJson);
          editExtract(this.form).then(response => {
            if(response.code==200){
              this.msgSuccess("保存成功");
              this.open = false;
              this.getList();
            }
            });
        }
      });
    },
    /** 提交按钮 */
    submitExtractForm() {
      this.$refs["extractForm"].validate(valid => {
        if (valid) {
          extract(this.extractForm).then(response => {
            if(response.code==200){
              this.msgSuccess(response.msg);
              this.open = false;
              this.getList();
            }
            });
        }
      });
    },

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