<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="关键字" prop="nickname">
        <el-input v-model="queryParams.nickname" placeholder="搜索昵称、姓名、手机、Email" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="成交" prop="mobile">
        <el-input v-model="queryParams.mobile" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="认证" prop="password">
        <el-input v-model="queryParams.password" placeholder="请输入密码" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="审核状态" prop="registerIp">
        <el-input v-model="queryParams.registerIp" placeholder="请输入注册 IP" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['member:user:create']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport" :loading="exportLoading"
                   v-hasPermi="['member:user:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="公司名称" align="center" prop="enterpriseName" />
      <el-table-column label="用户昵称" align="center" prop="nickname" />
      <el-table-column label="真实姓名" align="center" prop="identityName" />
      <el-table-column label="手机号" show-overflow-tooltip align="center" prop="mobile" >
     </el-table-column>
      <el-table-column label="成交" align="center" prop="" show-overflow-tooltip />
      <el-table-column label="认证" align="center" prop="" />
      <el-table-column label="状态" align="center" prop="status" />
      <el-table-column label="身份证审核状态" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
        <dict-tag :type="DICT_TYPE.AUDIT_STATUS" :value="scope.row.identityAuditStatus" ></dict-tag>
        </template>
      </el-table-column>
      <el-table-column label="营业执照审核状态" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.AUDIT_STATUS" :value="scope.row.enterpriseAuditStatus" ></dict-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200px">
        <template slot-scope="scope" >
          <el-button size="mini" type="text"  @click="guarantee(scope.row)">保函/证书</el-button>
          <el-button size="mini" type="text"  @click="operationLogFn(scope.row)">操作日志</el-button>
          <el-button size="mini" type="text"  @click="identityFn(scope.row , '1')">身份证</el-button>
          <el-button size="mini" type="text"  @click="identityFn(scope.row, '2')">营业执照</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>
    <el-dialog
      title="保函/证书"
      :visible.sync="guaranteeShow"
      :before-close="()=>{guaranteeUrl = ''; guaranteeShow = false;}"
      width="50%">
      <el-form>
        <el-form-item label="保函/证书">
          <el-row :gutter="20" type="flex" justify="center" >
            <el-col :span="12"><el-input v-model="guaranteeUrl" ></el-input></el-col>
            <el-col :span="4"><div style="height: 45px;overflow: hidden;">
              <file-upload v-model="guaranteeUrl" :fileType="['png', 'jpg', 'jpeg','pdf']" :limit="1" :isShowTip="false"></file-upload>
            </div></el-col>
            <el-col :span="6">
              <div>可上传图片,pdf文档</div>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
       <div style="text-align: center;"><el-button @click="setGuarantee">保 存</el-button></div>
    </el-dialog>
    <el-dialog
      title="操作日志"
      :visible.sync="operationLogShow"
      width="50%">
      <el-table :data="operationLogList">
        <el-table-column label="标题" prop="title"></el-table-column>
        <el-table-column label="操作内容" prop="content"></el-table-column>
        <el-table-column label="操作人" prop="userId"></el-table-column>
        <el-table-column label="操作时间">
          <template v-slot:default ="scope">
            {{parseTime(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column label="操作ip" prop="ip"></el-table-column>
      </el-table>
    </el-dialog>
        <el-dialog
          title="认证"
          :visible.sync="attestationShow"
          width="80%">
           <div>
             <el-tabs v-model="activeName" type="border-card">
               <el-tab-pane label="身份证" name="1">
                <div style="width: 500px;padding:0 40px;box-sizing: border-box;">
                  <el-form label-position="left" label-width="100px">
                    <el-form-item label="姓名">
                      <el-input></el-input>
                    </el-form-item>
                    <el-form-item label="证件类型">
                      <el-input></el-input>
                    </el-form-item>
                    <el-form-item label="证件号码">
                      <el-input></el-input>
                    </el-form-item>
                    <el-form-item label="证件正面照">
                      <div>
                        <el-input style="margin-bottom: 20px"></el-input>
                        <ImageUpload :isShowTip="false" :limit="1"></ImageUpload>
                      </div>
                    </el-form-item>
                    <el-form-item label="证件背面照">
                      <div>
                        <el-input style="margin-bottom: 20px"></el-input>
                        <ImageUpload :isShowTip="false" :limit="1"></ImageUpload>
                      </div>
                    </el-form-item>
                    <el-form-item label="审核状态">
                     {{}}
                    </el-form-item>
                    <el-form-item label="审核时间">
                     {{}}
                    </el-form-item>
                  </el-form>
                </div>
               </el-tab-pane>
               <el-tab-pane label="营业执照" name="2">
                <div style="width: 500px;padding:0 40px;box-sizing: border-box;">
                  <el-form label-position="left" label-width="100px">
                    <el-form-item label="企业名称">
                        <el-input></el-input>
                    </el-form-item>
                    <el-form-item label="企业法人">
                      <el-input></el-input>
                    </el-form-item>
                    <el-form-item label="证件号码">
                      <el-input></el-input>
                    </el-form-item>
                    <el-form-item label="证件照">
                      <div>
                        <el-input style="margin-bottom: 20px"></el-input>
                        <ImageUpload :isShowTip="false" :limit="1"></ImageUpload>
                      </div>
                    </el-form-item>
                    <el-form-item label="附件">
                      <el-input></el-input>
                    </el-form-item>
                    <el-form-item label="审核状态">{{}}</el-form-item>
                    <el-form-item label="审核时间">{{}}</el-form-item>
                  </el-form>
                </div>
               </el-tab-pane>
             </el-tabs>
             <div style="text-align: center;margin-top: 20px;">
               <el-button type="primary">提交审核</el-button>
               <el-button>取 消</el-button>
             </div>
           </div>
        </el-dialog>
  </div>
</template>

<script>
import {
  createUser,
  updateUser,
  deleteUser,
  getUser,
  getUserPage,
  exportUserExcel,
  operationLogApi,
  seTupdateBackletter,
  memberGetAuthIdcardInfo
} from "@/api/member/user";
import {DICT_TYPE, getDictDatas} from "@/utils/dict";
import FileUpload  from "@/components/FileUpload"
import ImageUpload from "@/components/ImageUpload";
import uploadImage from "@/components/UploadImage";
let stateTitle = {
     0:'未审核',
    1:'审核中',
   2:'审核通过',
  3:'审核不通过'
}
export default {
  name: "User",
  components: {
    FileUpload,
    ImageUpload,uploadImage
  },
  data() {
    return {
      guaranteeUrl:undefined,
      DICT_TYPE,
      // audit_status
      activeName:"1",
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dateRangeLoginDate: [],
      dateRangeCreateTime: [],
      // 查询参数
      queryParams: {
        pageNo: 1,
        pageSize: 10,
        isDeal:undefined,//	是否成交
        searchKey:undefined,//关键字
        auditStatus:undefined,//审核状态
        authType:undefined,//关键字
      },
      stateTitle,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        nickname: [{ required: true, message: "用户昵称不能为空", trigger: "blur" }],
        avatar: [{ required: true, message: "头像不能为空", trigger: "blur" }],
        status: [{ required: true, message: "状态不能为空", trigger: "blur" }],
        mobile: [{ required: true, message: "手机号不能为空", trigger: "blur" }],
        password: [{ required: true, message: "密码不能为空", trigger: "blur" }],
        registerIp: [{ required: true, message: "注册 IP不能为空", trigger: "blur" }],
      },
      publicObj:undefined,
      operationLogShow:false,
      guaranteeShow:false,
      operationLogFrom:{
        page:1,
        row:10,
        userId:undefined
      },
      operationLogList:[],
      attestationShow:false
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 处理查询参数
      let params = {...this.queryParams};
      this.addBeginAndEndTime(params, this.dateRangeLoginDate, 'loginDate');
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime');
      // 执行查询
      getUserPage(params).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        nickname: undefined,
        avatar: undefined,
        status: undefined,
        mobile: undefined,
        password: undefined,
        registerIp: undefined,
        loginIp: undefined,
        loginDate: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRangeLoginDate = [];
      this.dateRangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户";
    },
    // 保函证书
    guarantee(row) {
         this.publicObj = row;
         this.guaranteeShow = true;
    },
    // 操作日志
    operationLogFn(row){
      this.publicObj = row;
      this.operationLogFrom.page = 1;
      this.operationLogFrom.userId = row.id
      this.operationLogShow = true;
      this.operationLogList = [];
      this.getOperationLogList();

    },
    getOperationLogList(){
      operationLogApi(this.operationLogFrom).then(r=>{
        if(r.code === 0){
          this.operationLogList = r.data.list
        }
      })
    },
    setGuarantee(){
      seTupdateBackletter({userId:this.publicObj.id,backLetter:this.guaranteeUrl}).then(r=>{
        if(r.code === 0){
          this.guaranteeShow = false;
          this.guaranteeUrl = true;
          this.$message.success('保存成功');
        }
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateUser(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createUser(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除用户编号为"' + id + '"的数据项?').then(function() {
          return deleteUser(id);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.addBeginAndEndTime(params, this.dateRangeLoginDate, 'loginDate');
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime');
      // 执行导出
      this.$modal.confirm('是否确认导出所有用户数据项?').then(() => {
          this.exportLoading = true;
          return exportUserExcel(params);
        }).then(response => {
          this.$download.excel(response, '${table.classComment}.xls');
          this.exportLoading = false;
        }).catch(() => {});
    },
    identityFn(row,val){
      this.attestationShow = true;
      this.activeName = val;
      this.publicObj = row;
      if(val == 1){
        this.getIdentityDetails();
      }
    },
    getIdentityDetails(){
      memberGetAuthIdcardInfo({userId:this.publicObj.id}).then(r=>{
        console.log(r);
      })
    }
  }
};
</script>
