<template>
  <div class="container"  style="padding: 30px!important; ">
    <el-table v-loading="loading" :data="ApplicationReviewList">
      <el-table-column label="用户编号" align="center" prop="userId" />
      <el-table-column label="用户名" align="center" prop="userName" />
      <el-table-column label="手机号码" align="center" prop="phonenumber" />
      <el-table-column
        label="注册时间"
        align="center"
        prop="loginDate"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="现用户类型" align="center" prop="cur" />
      <el-table-column label="申请认证类型" align="center" prop="apply" />
      <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"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:ApplicationReview:edit']"
            >查看详情</el-button
          >
        </template>
      </el-table-column>
    </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="800px"
      append-to-body
      :close-on-click-modal="false"
    >
      <!-- 认证公司 -->
      <el-form
        v-if="companyApply"
        :model="companyApply"
        label-width="200px"
      >
        <el-form-item label="公司名称" prop="name" v-if="cA || tAcA">
          <el-input readonly
            v-model="companyApply.name"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="统一信用代码" prop="creditCode" v-if="cA || tAcA">
          <el-input readonly
            v-model="companyApply.creditCode"
            placeholder="请输入统一信用代码"
          />
        </el-form-item>
        <el-form-item label="通讯地址" prop="address" v-if="cA || tAcA">
          <el-input readonly
            v-model="companyApply.address"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="网址" prop="webSite" v-if="cA || tAcA">
          <el-input readonly v-model="companyApply.webSite" placeholder="请输入网址" />
        </el-form-item>
        <el-form-item label="营业执照" prop="filePath" v-if="cA || tAcA">
          <!-- <multifile-link
            v-if="allFileGetFlag"
            v-model="companyApply.businessLicenseUrl"
            :serverAddr="materialServerAddr"
            fileName="营业执照"
            :limit="1"
          /> -->
          <AuthImg :authSrc="companyApply.businessLicenseUrl" alt=""></AuthImg>
        </el-form-item>
        <el-form-item
          label="认证公司补充材料"
          prop="filePath"
          v-if="cA || tAcA"
        >
          <multifile-link
            v-if="allFileGetFlag"
            v-model="companyApply.otherCQList"
            :serverAddr="materialServerAddr"
            fileName="认证公司补充材料"
            :limit="10"
          />
        </el-form-item>
        <el-form-item label="公司简介" prop="companyDesc" v-if="cA || tAcA">
          <el-input readonly
            v-model="companyApply.companyDesc"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
        <el-form-item label="联系人姓名" prop="contactName" v-if="cA || tAcA">
          <el-input readonly
            v-model="companyApply.contactName"
            placeholder="请输入联系人姓名"
          />
        </el-form-item>
        <el-form-item
          label="联系人职位"
          prop="contactPosition"
          v-if="cA || tAcA"
        >
          <el-input readonly
            v-model="companyApply.contactPosition"
            placeholder="请输入联系人职位"
          />
        </el-form-item>
      </el-form>
      <!-- 测试者 p2pt p2ct c2ct 三合一 -->
      <el-form
        v-if="testerApply"
        :model="testerApply"
        label-width="200px"
      >
        <el-form-item label="真实姓名" prop="name" v-if="tA">
          <el-input readonly v-model="testerApply.name" placeholder="请输入真实姓名" />
        </el-form-item>
        <el-form-item label="所在城市" prop="city" v-if="tA">
          <el-input readonly v-model="testerApply.city" placeholder="请输入所在城市" />
        </el-form-item>
        <el-form-item label="身份证号码" prop="idNumber" v-if="tA">
          <el-input readonly
            v-model="testerApply.idNumber"
            placeholder="请输入身份证号码"
          />
        </el-form-item>
        <el-form-item label="身份证正面" prop="filePath" v-if="tA">
          <!-- <multifile-link
            v-if="allFileGetFlag"
            v-model="testerApply.idCardFrontUrl"
            :serverAddr="materialServerAddr"
            fileName="身份证正面"
            :limit="1"
          /> -->
          <AuthImg :authSrc="testerApply.idCardFrontUrl" alt=""></AuthImg>
        </el-form-item>
        <el-form-item label="身份证反面" prop="filePath" v-if="tA">
          <!-- <multifile-link
            v-if="allFileGetFlag"
            v-model="testerApply.idCardBackUrl"
            :serverAddr="materialServerAddr"
            fileName="身份证反面"
            :limit="1"
          /> -->
          <AuthImg :authSrc="testerApply.idCardBackUrl" alt=""></AuthImg>
        </el-form-item>
        <el-form-item label="测试能力" prop="testSkills" v-if="tA">
          <el-input readonly
            v-model="testerApply.testSkills"
            type="textarea"
            placeholder="请输入内容"
          />
          <el-popover
            placement="top-start"
            title="示例："
            width="400"
            trigger="hover"
          >
            <span>性能测试LoadRunner, Jmeter，自动化测试Selenium</span>
            <el-link slot="reference" style="color: #46a6ff;">查看示例</el-link>
          </el-popover>
        </el-form-item>
        <el-form-item label="奖项和证书" prop="certificate" v-if="tA">
          <el-input readonly
            v-model="testerApply.certificate"
            type="textarea"
            placeholder="请输入内容"
          />
          <el-popover
            placement="top-start"
            title="示例："
            width="570"
            trigger="hover">
            <span>STQB认证软件测试工程师<br>国家软考认证软件评测师<br>或国家等级考试软件测试工程师证书 <br>
             2019年全国大学生软件测试大赛二等奖
            </span>
            <el-link slot="reference" style="color: #46a6ff;">查看示例</el-link>
          </el-popover>
        </el-form-item>
        <el-form-item label="工作经历" prop="workExperience" v-if="tA">
          <el-input readonly
            v-model="testerApply.workExperience"
            type="textarea"
            placeholder="请输入内容"
          />
          <el-popover
            placement="top-start"
            title="示例："
            width="570"
            trigger="hover">
            <span>2017.5-2021.7 西安未央软件有限公司 软件测试工程师<br>
             2013.9-2017.4. 西安大唐软件有限公司 软件测试工程师<br>
             2009.9-2013.7 西安电子科技大学 软件工程专业 本科
            </span>
            <el-link slot="reference" style="color: #46a6ff;">查看示例</el-link>
          </el-popover>
        </el-form-item>

        <el-form-item
          label="测试项目案例"
          prop="testProjects"
          v-if="tA || tAcA || c2ct"
        >
          <el-input readonly
            v-model="testerApply.testProjects"
            type="textarea"
            placeholder="请输入内容"
          />
          <el-popover
            placement="top-start"
            title="示例："
            width="530"
            trigger="hover">
            <span>2021年 航空仿真软件单元测试<br>
2020年 企业供应链管理平台安全测试<br>
2020年 B2C的电商平台系统前后台会员管理、订单、支付及后台订单处理相关模块测试
</span>
            <el-link slot="reference" style="color: #46a6ff;">查看示例</el-link>
          </el-popover>
        </el-form-item>
        <el-form-item label="其它补充材料" prop="filePath" v-if="tA || tAcA || c2ct">
          <multifile-link
            v-if="allFileGetFlag"
            v-model="testerApply.otherTQList"
            :serverAddr="materialServerAddr"
            fileName="其它补充材料"
            :limit="10"
          />
        </el-form-item>
        <el-form-item label="补充说明" prop="otherInfo" v-if="tAcA || c2ct">
          <el-input readonly
            v-model="testerApply.otherInfo"
            type="textarea"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitPass">审核通过</el-button>
        <el-button type="primary" @click="submitUnpass">审核不通过</el-button>
      </div>
    </el-dialog>

    <!-- 添加或修改企业认证申请对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="unpassOpen"
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="审核意见" prop="auditOpinion">
          <el-input
            v-model="form.auditOpinion"
            type="textarea"
            placeholder="请输入内容"
          />
        </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>
  </div>
</template>

<script>
import {
  listApplicationReview,
  delApplicationReview,
  addApplicationReview,
  updateApplicationReview,
  exportApplicationReview,
} from "@/api/system/ApplicationReview";
import {
  getUnauditInfo,
  changeAllStatusOnce,
  user_status,
  apple_type,
} from "@/api/system/user";
import { changeCAStatus } from "@/api/tester/CompanyApply";
import { changeTAStatus } from "@/api/tester/TesterApply";
import MultifileLink from "@/views/components/MultifileLink";
import AuthImg from "@/views/components/AuthImg";

export default {
  name: "ApplicationReview",
  components: {
    MultifileLink,
    AuthImg,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 申请审核表格数据
      ApplicationReviewList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      unpassOpen: false,
      // 最后登录时间时间范围
      daterangeLoginDate: [],
      // 创建时间时间范围
      daterangeCreateTime: [],
      // 更新时间时间范围
      daterangeUpdateTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        userName: null,
        nickName: null,
        userType: null,
        email: null,
        phonenumber: null,
        sex: null,
        avatar: null,
        password: null,
        status: null,
        loginIp: null,
        loginDate: null,
        companyStatus: null,
        testerStatus: null,
      },
      // 表单参数
      testerApply: {},
      companyApply: {},
      form: {
        auditOpinion: null,
      },
      // 表单校验
      rules: {
        auditOpinion: [
          { required: true, message: "审核意见不能为空", trigger: "blur" },
        ],
      },
      materialServerAddr: "",
      allFileGetFlag: false,
      cA: false,
      tA: false,
      tAcA: false,
      c2ct: false,
    };
  },
  created() {
    this.getList();
    this.getClientConfigKey("material.server").then((response) => {
      this.materialServerAddr = response.msg;
    });
  },
  methods: {
    /** 查询申请审核列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      // if (null != this.daterangeLoginDate && "" != this.daterangeLoginDate) {
      //   this.queryParams.params["beginLoginDate"] = this.daterangeLoginDate[0];
      //   this.queryParams.params["endLoginDate"] = this.daterangeLoginDate[1];
      // }
      // if (null != this.daterangeCreateTime && "" != this.daterangeCreateTime) {
      //   this.queryParams.params["beginCreateTime"] =
      //     this.daterangeCreateTime[0];
      //   this.queryParams.params["endCreateTime"] = this.daterangeCreateTime[1];
      // }
      // if (null != this.daterangeUpdateTime && "" != this.daterangeUpdateTime) {
      //   this.queryParams.params["beginUpdateTime"] =
      //     this.daterangeUpdateTime[0];
      //   this.queryParams.params["endUpdateTime"] = this.daterangeUpdateTime[1];
      // }
      listApplicationReview(this.queryParams).then((response) => {
        this.ApplicationReviewList = this.makeApplyStatus(response.rows);
        this.total = response.total;
        this.loading = false;
      });
    },
    makeApplyStatus(responseRows) {
      if (responseRows && responseRows.length > 0) {
        var newArr = responseRows.map(function (item, idnex) {
          var cS = item.companyStatus;
          var tS = item.testerStatus;
          var result = 0;
          /***
           *
           *n: [0, '普通用户'],
           *c: [1, '公司用户'],
           *pt: [2, '个人测试者用户'],
           *ct: [3, '测试公司用户'],
           */
          if (cS == 2) {
            if (tS == 2) {
              result = 3;
            } else {
              result = 1;
            }
          } else {
            if (tS == 2) {
              result = 2;
            } else {
              result = 0;
            }
          }
          item.cur = user_status.getDescFromValue(result);
          item.userStatus = result;
          /***
           * 申请：
           * 0 p2c
           * 1 p2pt
           * 2 p2ct
           * 3 c2ct
           * 或更新：
           * 4 更新公司 uc
           * 5 更新测试公司 uct
           * 6 更新个人测试者 upt
           */
          if (cS == 2) {
            if (tS == 2) {
              result = 5; //uct
            } else if (tS == 1) {
              result = 3; //c2ct
            } else {
              result = 4; //uc
            }
          } else if (cS == 1) {
            if (tS == 2) {
              // 个人测试者无法再继续认证为公司
            } else if (tS == 1) {
              result = 2; //p2ct
            } else {
              result = 0; //p2c
            }
          } else {
            if (tS == 2) {
              result = 6; //upt
            } else if (tS == 1) {
              result = 1; //p2pt
            } else {
              // 啥都没做哦
            }
          }
          // console.log(apple_type.upt);
          // console.log(apple_type.getDesc('upt'));
          // console.log(apple_type.getDescFromValue(apple_type.upt));
          item.apply = apple_type.getDescFromValue(result);
          item.appleType = result;
          return item;
        });
        return newArr;
      } else {
        return responseRows;
      }
    },
    // 取消按钮
    cancel() {
      this.unpassOpen = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        auditOpinion: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeLoginDate = [];
      this.daterangeCreateTime = [];
      this.daterangeUpdateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // // 多选框选中数据
    // handleSelectionChange(selection) {
    //   this.ids = selection.map((item) => item.userId);
    //   this.single = selection.length !== 1;
    //   this.multiple = !selection.length;
    // },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加申请审核";
    },
    /** 审核按钮操作 */
    handleUpdate(row) {
      this.reset();
      const userId = row.userId || this.ids;
      this.allFileGetFlag = false;
      this.open = false;
      const userStatus = row.userStatus;
      const appleType = row.appleType;
      this.tAcA = false; //p2ct
      this.tA = false; //p2pt
      this.c2ct = false; //c2ct
      this.cA = false; //p2c
      getUnauditInfo(userId).then((response) => {
        if (response.testerApply) {
          if (response.companyApply) {
            //公司测试者认证审核??? p2ct
            this.tAcA = true;
          } else {
            if (userStatus && appleType && userStatus == user_status.c && appleType == apple_type.c2ct) {
              //c2ct
              this.c2ct = true;
            } else {
              //p2pt
              this.tA = true;
            }
          }
        } else {
          if (response.companyApply) {
            //公司认证审核??? p2c
            this.cA = true;
          }
        }

        if (response.testerApply) {
          this.testerApply = response.testerApply;
        } else {
          this.testerApply = {
            applyId: null,
            name: null,
            city: null,
            idNumber: null,
            testSkills: null,
            testProjects: null,
            workExperience: null,
            certificate: null,
            createTime: null,
            updateTime: null,
            idCardFrontUrl: [],
            idCardBackUrl: [],
            otherTQList: [],
            otherInfo: null,
          };
        }
        if (response.companyApply) {
          this.companyApply = response.companyApply;
        } else {
          this.companyApply = {
            applyId: null,
            name: null,
            creditCode: null,
            address: null,
            webSite: null,
            companyDesc: null,
            contactName: null,
            contactPosition: null,
            createTime: null,
            updateTime: null,
            businessLicenseUrl: [],
            otherCQList: [],
          };
        }
        this.allFileGetFlag = true;
        this.open = true;
        this.title = "申请审核";
      });
    },
    /** 审核通过 */
    submitPass() {
      if (this.tAcA) {
        this.testerApply.remark = "" + this.companyApply.applyId;
        this.testerApply.status = 1;
        this.doPostApplyMethod(changeAllStatusOnce, this.testerApply);
      }
      if (this.tA || this.c2ct) {
        this.testerApply.status = 1;
        this.doPostApplyMethod(changeTAStatus, this.testerApply);
      }
      if (this.cA) {
        this.companyApply.status = 1;
        this.doPostApplyMethod(changeCAStatus, this.companyApply);
      }
    },
    /** 审核不通过 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.tAcA) {
            this.testerApply.remark = "" + this.companyApply.applyId;
            this.testerApply.status = 2;
            this.testerApply.auditOpinion = this.form.auditOpinion;
            this.doPostApplyMethod(changeAllStatusOnce, this.testerApply);
          }
          if (this.tA || this.c2ct) {
            this.testerApply.status = 2;
            this.testerApply.auditOpinion = this.form.auditOpinion;
            this.doPostApplyMethod(changeTAStatus, this.testerApply);
          }
          if (this.cA) {
            this.companyApply.status = 2;
            this.companyApply.auditOpinion = this.form.auditOpinion;
            this.doPostApplyMethod(changeCAStatus, this.companyApply);
          }
        }
      });
    },
    /** 审核不通过 */
    submitUnpass() {
      this.unpassOpen = true;
    },
    doPostApplyMethod(methodPost, aData) {
      // if (this.unpassOpen) {
      //   aData.auditOpinion = this.form.auditOpinion;
      // }
      methodPost(aData).then((response) => {
        this.open = false;
        this.unpassOpen = false;
        this.getList();
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const userIds = row.userId || this.ids;
      this.$confirm(
        '是否确认删除申请审核编号为"' + userIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delApplicationReview(userIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        });
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有申请审核数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          return exportApplicationReview(queryParams);
        })
        .then((response) => {
          this.download(response.msg);
        });
    },
  },
};
</script>
