<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>
      </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="handleAdd" size="small">添加顾客</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"
        type="index"
      ></el-table-column>
      <el-table-column
        label="手机号"
        prop="phone"
        align="center"
      ></el-table-column>
      <el-table-column
        label="返现金额"
        align="center"
        prop="money"
      ></el-table-column>
      <el-table-column
        label="机构"
        align="center"
        prop="company"
      ></el-table-column>
      <el-table-column
        label="城市"
        align="center"
        prop="city"
      ></el-table-column>
      <el-table-column
        label="渠道"
        align="center"
        prop="channel"
      ></el-table-column>
      <el-table-column
        label="车牌号"
        align="center"
        prop="carNum"
      ></el-table-column>
      <el-table-column
        label="客户经理"
        align="center"
        prop="own"
      ></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button @click="handleUpdate(scope.row)" type="text" size="small"
            >编辑</el-button
          >
          <el-button @click="handleDelete(scope.row)" type="text" size="small"
            >删除</el-button
          >
        </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="submitForm">确 定</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" slot="tip">
          <el-link type="info" style="font-size:12px" @click="importTemplate"
            >下载模板</el-link
          >
        </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 { getList, handleUser, addUser } from '@/api/index';
import { getToken } from '@/utils/auth';
export default {
  name: 'Index',
  data() {
    return {
      // 用户导入参数
      upload: {
        // 是否显示弹出层（用户导入）
        open: false,
        // 弹出层标题（用户导入）
        title: '',
        // 是否禁用上传
        isUploading: false,
        // 设置上传的请求头部
        headers: { Authorization: 'Bearer ' + getToken() },
        // 上传的地址
        url: process.env.VUE_APP_BASE_API + '/customer/importDate'
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        phone: ''
      },
      total: 0,
      dataList: [],
      dialogVisible: false,
      title: '',
      form: {},
      rules: {
        phone: [{ required: true, message: '请输入', trigger: 'blur' }],
        money: [{ required: true, message: '请输入', trigger: 'blur' }]
      }
    };
  },
  methods: {
    importTemplate() {
      window.location.href =
        'http://140.143.229.114:9000/pic/20210729/1420581901814599680.xlsx';
    },
    handleSearchList() {
      this.queryParams.pageNum = 1;
      this.getDataList();
    },
    getDataList() {
      getList(this.queryParams).then(({ data }) => {
        this.dataList = data.list;
        this.total = data.total;
      });
    },
    // 添加
    handleAdd() {
      this.form = {};
      this.resetForm('form');
      this.dialogVisible = true;
      this.title = '添加顾客';
    },
    // 编辑
    handleUpdate(row) {
      this.form = {};
      this.resetForm('form');
      row.status = 0;
      this.form = Object.assign({}, row);
      this.dialogVisible = true;
      this.title = '修改顾客';
    },
    // 提交
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.title == '修改顾客') {
            handleUser(this.form).then(response => {
              this.msgSuccess('修改成功');
              this.dialogVisible = false;
              this.getDataList();
            });
          } else {
            addUser(this.form).then(response => {
              this.msgSuccess('新增成功');
              this.dialogVisible = false;
              this.getDataList();
            });
          }
        }
      });
    },
    /** 导入按钮操作 */
    handleImport() {
      this.upload.title = '导入顾客';
      this.upload.open = true;
    },
    // 文件上传中处理
    handleFileUploadProgress(event, file, fileList) {
      this.upload.isUploading = true;
    },
    // 文件上传成功处理
    handleFileSuccess(response, file, fileList) {
      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();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm('确认删除该数据?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(function() {
          row.status = -1;
          return handleUser(row);
        })
        .then(() => {
          this.getDataList();
          this.msgSuccess('删除成功');
        });
    }
  },
  created() {
    this.getDataList();
  }
};
</script>
