<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      :class="showSearch ? '' : ' hide'"
      label-width="68px"
    >
      <el-form-item label="应用名称" prop="appName">
        <el-input
          v-model="queryParams.appName"
          placeholder="请输入应用名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入申请人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="申请时间">
        <el-date-picker
          v-model="daterangeCreateTime"
          style="width: 240px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <br />
      <el-form-item label="审核状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择审核状态"
          clearable
        >
          <el-option
            v-for="dict in dict.type.sys_audit_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核人" prop="auditBy">
        <el-input
          v-model="queryParams.auditBy"
          placeholder="请输入审核人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核时间">
        <el-date-picker
          v-model="daterangeAuditTime"
          style="width: 240px"
          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>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-form>
    <el-table v-loading="loading" :data="applicationList">
      <el-table-column label="编号" align="center" prop="id" width="80" />
      <el-table-column label="应用名称" prop="appName" />
      <el-table-column label="联系人" align="center" prop="linkMan" />
      <el-table-column
        label="申请人"
        align="center"
        prop="createBy"
      ></el-table-column>
      <el-table-column
        label="申请时间"
        align="center"
        prop="createTime"
        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="status">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.sys_audit_status"
            :value="scope.row.status"
          />
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="auditBy" />
      <el-table-column
        label="审核时间"
        align="center"
        prop="auditTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.auditTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            :icon="scope.row.status === '0' ? 'el-icon-edit' : 'el-icon-view'"
            @click="handleAudit(scope.row, scope.row.status === '0')"
            v-hasPermi="['system/audit:application:audit']"
          >
            <span v-if="scope.row.status === '0'">审核</span>
            <span v-else>查看</span>
          </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>
      <el-form ref="form" :model="form" label-width="100px" class="detail">
        <el-row>
          <el-col :span="24">
            <el-form-item label="应用名称:" prop="appName">
              {{ form.appName }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="应用图标:">
              <image-preview :src="form.coverImg" :width="88" :height="50" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="联系人:" prop="linkMan">
              {{ form.linkMan }}
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="联系电话:" prop="linkPhone">
              {{ form.linkPhone }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="应用描述:" prop="remark">
              {{ form.remark }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-divider></el-divider>
        <el-row>
          <el-col :span="12">
            <el-form-item label="申请人:" prop="tag">
              <span style="margin-right: 10px;">{{ form.applyName }}</span>
              <el-tag size="mini" type="primary" v-if="form.userType === '00'">内部用户</el-tag>
              <el-tag size="mini" type="success" v-if="form.userType === '01'">外部企业</el-tag>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="申请时间:" prop="remark">
              {{ parseTime(form.createTime, "{y}-{m}-{d} {h}:{i}:{s}") }}
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="!isAudit">
          <el-col :span="24">
            <el-form-item label="申请结果:" prop="status">
              <dict-tag
                :options="dict.type.sys_audit_status"
                :value="form.status"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row v-if="!isAudit">
          <el-col :span="24">
            <el-form-item label="原因说明:" prop="auditRemark">
              {{ form.auditRemark }}
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <el-form
        ref="form"
        :rules="rules"
        :model="form"
        label-width="100px"
        v-if="isAudit"
      >
        <el-divider></el-divider>
        <el-row>
          <el-col :span="24">
            <el-form-item label="申请结果:" prop="status">
              <el-radio v-model="form.status" label="1">通过</el-radio>
              <el-radio v-model="form.status" label="2">驳回</el-radio>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="原因说明:" prop="auditRemark">
              <el-input
                v-model="form.auditRemark"
                placeholder="请输入原因说明"
                type="textarea"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button
          type="primary"
          @click="submitForm"
          v-if="isAudit"
          :loading="submitting"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listApplication,
  auditApplication,
  delApplication,
  addApplication,
  updateApplication,
} from "@/api/system/customer/application";

import { getUser } from "@/api/system/user";

export default {
  name: "Application",
  dicts: ["sys_audit_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      submitting: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 应用表格数据
      applicationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 审核意见时间范围
      daterangeCreateTime: [],
      // 审核意见时间范围
      daterangeAuditTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        appId: null,
        appSecret: null,
        appName: null,
        linkMan: null,
        linkPhone: null,
        coverImg: null,
        status: "0",
        statusRemark: null,
        createBy: null,
        createTime: null,
        auditBy: null,
        auditTime: null,
        auditRemark: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        status: [
          {
            required: true,
            message: "请选择审核结果",
            trigger: "change",
          },
        ],
        auditRemark: [
          { required: true, message: "请填写原因说明", trigger: "change" },
        ],
      },
      isAudit: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询应用列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeCreateTime && "" != this.daterangeCreateTime) {
        this.queryParams.params["beginCreateTime"] =
          this.daterangeCreateTime[0] + " 00:00:00";
        this.queryParams.params["endCreateTime"] =
          this.daterangeCreateTime[1] + " 23:59:59";
      }
      if (null != this.daterangeAuditTime && "" != this.daterangeAuditTime) {
        this.queryParams.params["beginAuditTime"] =
          this.daterangeAuditTime[0] + " 00:00:00";
        this.queryParams.params["endAuditTime"] =
          this.daterangeAuditTime[1] + " 23:59:59";
      }
      listApplication(this.queryParams).then((response) => {
        this.applicationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        appId: null,
        appSecret: null,
        appName: null,
        linkMan: null,
        linkPhone: null,
        coverImg: null,
        status: "0",
        statusRemark: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
        auditBy: null,
        auditTime: null,
        auditRemark: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeCreateTime = [];
      this.daterangeAuditTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    handleAudit(row, isAudit) {
      // console.log(isAudit);
      this.reset();

      // 获取应用所属用户
      getUser(row.userId).then((res) => {
        this.open = true;
        this.form = JSON.parse(JSON.stringify(row));
        this.form.userType = res.data.userType;
        this.form.applyName = res.data.nickName;
        this.isAudit = isAudit;
        if (this.isAudit) {
          this.form.auditRemark = "";
        }
      });

      this.title = "审核应用";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          // console.log(this.form);
          this.submitting = true;
          auditApplication(this.form).then((response) => {
            this.submitting = false;
            this.$modal.msgSuccess("审核成功");
            this.open = false;
            this.getList();
          }).catch(() => {
              this.submitting = false;
            });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除应用编号为"' + ids + '"的数据项？')
        .then(function () {
          return delApplication(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
  },
};
</script>
