<template>
  <div class="card-container">
    <el-card class="card-heard">
      <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="88px">
        <el-form-item label="业主手机号" prop="phone">
          <el-input
            v-model="queryParams.param.phone"
            placeholder="请输入业主手机号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-select
            v-model="queryParams.param.state"
            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 label="申请日期" >
          <el-date-picker
            v-model="dateRange"
            size="small"
            style="width: 280px"
            value-format="yyyy-MM-dd"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
          ></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-card>
    <el-card class="card-body">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-download"
            :loading="downloadLoading"
            @click="handleExport"
            size="mini"
            v-hasPermi="['apply:export']"
          >导出</el-button>
        </el-col>
      </el-row>

      <el-table v-loading="loading" :data="applyList" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center"/>
        <el-table-column label="业主手机号" align="center" prop="phone"/>
        <el-table-column label="账号余额(元)" align="center" prop="acountAmount"/>
        <el-table-column label="提现金额(元)" align="center" prop="amount"/>
        <el-table-column label="收款渠道" align="center" prop="payType"/>
        <el-table-column label="申请时间" align="center" prop="createTime"/>
        <el-table-column label="状态" align="center" prop="state"/>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              v-if="scope.row.state=='待审核'"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['apply:edit']"
            >审核
            </el-button>
            <el-button
              size="mini"
              type="text"
              v-if="scope.row.state=='已提现' || scope.row.state=='提现失败'"
              v-hasPermi="['apply:edit']"
            >--
            </el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              v-if="scope.row.state=='被拒绝'"
              @click="viewReason(scope.row)"
              v-hasPermi="['apply:edit']"
            >查看原因
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <pagination
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @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-radio v-model="radio" label="1">同意</el-radio>
        <el-radio v-model="radio" label="2">拒绝</el-radio>
        <el-form-item label="拒绝原因"  v-if="radio=='2'" style="margin-top:30px" prop="reason">
          <el-input type="textarea" :rows="4" placeholder="请填写拒绝原因" v-model="form.reason" maxlength="100">
          </el-input>
        </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="title" :visible.sync="openReasonFlag" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        {{form.reason}}
      </el-form>
      <div slot="footer" class="dialog-footer">
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listApply,
  getApply,
  delApply,
  addApply,
  updateApply,
  exportApply
} from '@/api/orderManage/apply'

export default {
  name: 'Apply',
  components: {},
  data() {
    return {
      radio: "1",
      reason: "",
      openReasonFlag: false,
      downloadLoading: false,
      // 状态数据字典
      statusOptions: [],
      // 日期范围
      dateRange: [],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 提现申请表格数据
      applyList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        param: {
          userId: null,
          amount: null,
          state: null,
          reason: null,
          operator: null,
          phone: null
        }
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        reason: [
          { required: true, message: "拒绝原因不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getDicts("apply_status").then(response => {
      this.statusOptions = response.data;
      // 状态002小程序端还未开发暂时过滤掉
      this.statusOptions = this.statusOptions.filter(item=>item.dictValue!='002')
    });
    this.initTime();
    this.getList()
  },
  methods: {
    initTime() {
      // 初始化查询，默认为前30天到今天
      let date = new Date();
      let startDate = new Date(date.getTime() - 24 * 60 * 60 * 1000 * 30);
      let endDate = date;
      this.dateRange = [
        this.getDateDay(startDate),
        this.getDateDay(endDate),
      ];
    },
    getDateDay(date) {
      //获取当前时间的年份转为字符串
      let year = date.getFullYear().toString(); //'2019'
      //获取月份，由于月份从0开始，此处要加1，判断是否小于10，如果是在字符串前面拼接'0'
      let month =
        date.getMonth() + 1 < 10
          ? "0" + (date.getMonth() + 1).toString()
          : (date.getMonth() + 1).toString(); //'04'
      //获取天，判断是否小于10，如果是在字符串前面拼接'0'
      let da =
        date.getDate() < 10
          ? "0" + date.getDate().toString()
          : date.getDate().toString(); //'12'
      //字符串拼接，开始时间，结束时间
      return year + "-" + month + "-" + da; //当天'2019-04-12'
    },
    /** 查询提现申请列表 */
    getList() {
      this.loading = true
      if (null != this.dateRange && '' != this.dateRange) {
        this.queryParams.param["beginTime"] = this.dateRange[0];
        this.queryParams.param["endTime"] = this.dateRange[1];
      }
      listApply(this.queryParams).then(response => {
        this.applyList = response.records
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        amount: null,
        state: null,
        reason: null,
        createTime: null,
        updateTime: null,
        operator: null,
        phone: null
      }

      this.resetForm('form')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.queryParams = {
        current: 1,
        size: 10,
        param: {},
      };
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id =
        row.id || this.ids
      getApply(id).then(response => {
        this.form = response.result
        this.open = true
        this.title = '审核'
      })
    },
    /** 查看拒绝原因 */
    viewReason(row) {
      this.reset()
      const id =
        row.id || this.ids
      getApply(id).then(response => {
        this.form = response.result
        this.openReasonFlag = true
        this.title = '查看原因'
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if(this.radio=="1"){
            this.msgError('暂不支持')
            return false;
            this.form.state ="003";//通过
          }else if(this.radio=="2"){
            this.form.state ="004";//拒绝
          }
          if (this.form.id != null) {
            updateApply(this.form).then(response => {
              this.msgSuccess('审核成功')
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /*导出数据 */
    handleExport() {
      exportApply(this.queryParams.param)
        .then((res) => {
          if (res.code === 200) {
            if (res.result) {
              this.msgSuccess("导出成功");
              window.open(res.result)
            } else {
              this.msgSuccess("导出失败");
            }
          } else {
            this.msgError(res.msg);
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
  }
}
</script>
