<template>
  <div class="app-container">
    <el-card shadow="never">
      <div>
        <i class="el-icon-search"></i>
        <span>筛选搜索</span>
        <div style="float:right">
          <el-button type="primary" @click="handleSearchList" size="small">
            查询
          </el-button>
        </div>
      </div>
      <div style="margin-top: 15px">
        <el-form
          size="small"
          :model="queryParams"
          ref="queryForm"
          :inline="true"
          @submit.native.prevent
        >
          <el-form-item label="手机号">
            <el-input v-model="queryParams.phone" placeholder="请输入" />
          </el-form-item>
          <el-form-item label="处理状态" prop="status">
            <el-select
              v-model="queryParams.status"
              clearable
              placeholder="请选择"
            >
              <el-option
                v-for="item in statusOptions"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </el-form-item>
        </el-form>
      </div>
    </el-card>
    <el-card class="operate-container" shadow="never">
      <i class="el-icon-tickets"></i>
      <span>数据列表</span>
      <div class="btn-add">
        <el-button
          @click="handleExport"
          size="small"
          :disabled="!total ? true : false"
          >报表导出</el-button
        >
        <el-button @click="handleImport" size="small">报表导入</el-button>
      </div>
    </el-card>
    <el-table style="width: 100%" class="table-container" :data="dataList">
      <el-table-column
        label="订单号"
        align="center"
        prop="id"
      ></el-table-column>
      <el-table-column
        label="手机号"
        align="center"
        prop="phone"
      ></el-table-column>
      <el-table-column
        label="姓名"
        align="center"
        prop="username"
      ></el-table-column>
      <el-table-column
        label="银行卡号"
        align="center"
        prop="moneyCar"
      ></el-table-column>
      <el-table-column
        label="对应金额"
        align="center"
        prop="money"
      ></el-table-column>
      <el-table-column
        label="申请时间"
        align="center"
        prop="createTime"
      ></el-table-column>
      <el-table-column
        label="处理状态"
        align="center"
        prop="status"
        width="120px"
      >
        <template slot-scope="scope">
          <el-select
            v-model="scope.row.status"
            size="mini"
            placeholder="请选择"
            @change="handleChange($event, scope.row)"
            @visible-change="aaa($event, scope.row.status)"
          >
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getDataList"
    />
    <!-- 添加修改 -->
    <el-dialog :title="title" :visible.sync="dialogVisible" width="25%">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" maxlength="11"></el-input>
        </el-form-item>
        <el-form-item label="返现金额" prop="money">
          <el-input-number v-model="form.money" :min="1"></el-input-number>
        </el-form-item>
        <el-form-item label="机构">
          <el-input v-model="form.company"></el-input>
        </el-form-item>
        <el-form-item label="城市">
          <el-input v-model="form.city"></el-input>
        </el-form-item>
        <el-form-item label="渠道">
          <el-input v-model="form.channel"></el-input>
        </el-form-item>
        <el-form-item label="车牌号">
          <el-input v-model="form.carNum"></el-input>
        </el-form-item>
        <el-form-item label="客户经理">
          <el-input v-model="form.own"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 用户导入对话框 -->
    <el-dialog
      :title="upload.title"
      :visible.sync="upload.open"
      width="400px"
      append-to-body
    >
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">
          提示：仅允许导入“xls”或“xlsx”格式文件！
        </div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { getList3, handleOrder, addUser } from '@/api/index';
import { getToken } from '@/utils/auth';
export default {
  name: 'Index3',
  data() {
    return {
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: '',
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: 'Bearer ' + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + '/webOrder/importDate'
      },
      status: null,
      statusOptions: [
        {
          value: 0,
          label: '处理中'
        },
        {
          value: 1,
          label: '已打款'
        },
        {
          value: 2,
          label: '打款失败'
        }
      ],
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        phone: '',
        status: ''
      },
      total: 0,
      dataList: [],
      dialogVisible: false,
      title: '',
      form: {},
      rules: {
        phone: [{ required: true, message: '请输入', trigger: 'blur' }],
        money: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    };
  },
  methods: {
    handleSearchList() {
      this.queryParams.pageNum = 1;
      this.getDataList();
    },
    getDataList() {
      getList3(this.queryParams).then(({ data }) => {
        this.dataList = data.list;
        this.total = data.total;
      });
    },
    handleChange(e, row) {
      this.$confirm(
        `确定将该提取订单的处理状态改为 “ ${
          this.statusOptions.filter(item => item.value == e)[0].label
        } ” ？`,
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(() => {
          handleOrder(row).then(res => {
            this.msgSuccess('操作成功');
          });
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消修改'
          });
          row.status = this.status;
        });
    },
    aaa(e, status) {
      if (e) this.status = status;
    },
    /** 导出按钮操作 */
    handleExport() {
      this.$confirm('是否确认导出所有数据项?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(res => {
        const baseURL = process.env.VUE_APP_BASE_API;
        window.location.href =
          baseURL + '/webOrder/export?status=' + this.queryParams.status;
      });
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = '导入订单';
      this.upload.open = true;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      console.log(this.upload);
      this.upload.open = false;
      this.upload.isUploading = false;
      this.$refs.upload.clearFiles();
      this.$alert(response.data, '导入结果', {
        dangerouslyUseHTMLString: true
      });
      this.getDataList();
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit();
    }
  },
  created() {
    this.getDataList();
  }
};
</script>
