<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="参会人" prop="usrName">
        <el-input
            v-model="queryParams.usrName"
            placeholder="请输入参会人"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="联系电话" prop="tel">
        <el-input
            v-model="queryParams.tel"
            placeholder="请输入联系电话"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="参会人单位" prop="orgUnitName">
        <el-input
            v-model="queryParams.orgUnitName"
            placeholder="请输入参会人单位"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="参会人职务" prop="uniLevel">
        <el-input
            v-model="queryParams.uniLevel"
            placeholder="请输入参会人职务"
            clearable
            @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="参会人角色" prop="usrType">
        <el-select v-model="queryParams.usrType" placeholder="请选择参会人角色" clearable>
          <el-option
              v-for="dict in dict.type.met_usr_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="现场调研" prop="conferenceFlag">
        <el-select v-model="queryParams.conferenceFlag" placeholder="请选择现场调研" clearable>
          <el-option
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="申请类型" prop="applyType">
        <el-select v-model="queryParams.applyType" placeholder="请选择申请类型" clearable>
          <el-option
              v-for="dict in dict.type.apply_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="审核状态" prop="reviewState">
        <el-select v-model="queryParams.reviewState" placeholder="请选择审核状态" clearable>
          <el-option
              v-for="dict in dict.type.review_state"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </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>
    </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"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="usrApplyList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" />
      <el-table-column label="参会人" align="center" prop="usrName" />
      <el-table-column label="联系电话" align="center" prop="tel" />
      <el-table-column label="参会人单位" align="center" prop="orgUnitName" />
      <el-table-column label="参会人职务" align="center" prop="uniLevel" />
      <el-table-column label="邮箱信息" align="center" prop="email" />
      <el-table-column label="参会人角色" align="center" prop="usrType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.met_usr_type" :value="scope.row.usrType" />
        </template>
      </el-table-column>
      <el-table-column label="现场调研" align="center" prop="conferenceFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_yes_no" :value="scope.row.conferenceFlag" />
        </template>
      </el-table-column>
      <el-table-column label="申请类型" align="center" prop="applyType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.apply_type" :value="scope.row.applyType" />
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="reviewState">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.review_state" :value="scope.row.reviewState" />
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="reviewMsg" />
      <el-table-column label="与会内容" align="center" prop="contentTitle" />
      <el-table-column label="创建时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </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" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
          >修改
          </el-button>
          <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
          >删除
          </el-button>
          <el-dropdown @command="(comd)=>handleCommand(comd, scope.row)">
            <span class="el-dropdown-link">
              更多<i class="el-icon-arrow-down el-icon--right"></i>
            </span>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item icon="el-icon-check" command="pass">通过</el-dropdown-item>
              <el-dropdown-item icon="el-icon-close" command="reject">拒绝</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改cps_usr_apply与会申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="参会人" prop="usrName">
          <el-input v-model="form.usrName" placeholder="请输入参会人" />
        </el-form-item>
        <el-form-item label="联系电话" prop="tel">
          <el-input v-model="form.tel" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="参会人单位" prop="orgUnitName">
          <el-input v-model="form.orgUnitName" placeholder="请输入参会人单位" />
        </el-form-item>
        <el-form-item label="参会人职务" prop="uniLevel">
          <el-input v-model="form.uniLevel" placeholder="请输入参会人职务" />
        </el-form-item>
        <el-form-item label="邮箱信息" prop="email">
          <el-input v-model="form.email" placeholder="请输入邮箱信息" />
        </el-form-item>
        <el-form-item label="参会人角色" prop="usrType">
          <el-radio-group v-model="form.usrType">
            <el-radio
                v-for="dict in dict.type.met_usr_type"
                :key="dict.value"
                :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="现场调研" prop="conferenceFlag">
          <el-radio-group v-model="form.conferenceFlag">
            <el-radio
                v-for="dict in dict.type.sys_yes_no"
                :key="dict.value"
                :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="申请类型" prop="applyType">
          <el-select v-model="form.applyType" placeholder="请选择申请类型">
            <el-option
                v-for="dict in dict.type.apply_type"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核状态" prop="reviewState">
          <el-radio-group v-model="form.reviewState">
            <el-radio
                v-for="dict in dict.type.review_state"
                :key="dict.value"
                :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="reviewMsg">
          <el-input v-model="form.reviewMsg" type="textarea" rows="3" 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>
    <el-dialog title="拒绝用户参会申请" :visible.sync="openReject" width="700px" append-to-body>
      <el-form ref="rejectForm" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="审核状态" prop="reviewState">
          <el-radio-group v-model="form.reviewState" :disabled="true">
            <el-radio
                v-for="dict in dict.type.review_state"
                :key="dict.value"
                :label="dict.value"
            >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="备注" prop="reviewMsg">
          <el-input v-model="form.reviewMsg" type="textarea" rows="3" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="rejectSubmitForm">确 定</el-button>
        <el-button @click="rejectCancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addUsrApply, delUsrApply, getUsrApply, listUsrApply, rejectUsrApply, updateUsrApply } from '@/api/cp/usrApply';

export default {
  name: 'UsrApply',
  dicts: ['apply_type', 'sys_yes_no', 'review_state', 'met_usr_type'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // cps_usr_apply与会申请表格数据
      usrApplyList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      openReject: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        usrName: null,
        tel: null,
        orgUnitName: null,
        uniLevel: null,
        usrType: null,
        conferenceFlag: null,
        applyType: null,
        reviewState: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleCommand(comd, row) {
      switch (comd) {
        case 'pass':
          this.handlePass(row);
          break;
        case 'reject':
          this.handleReject(row);
          break;
        default:
          break;
      }
    },
    rejectSubmitForm() {
      this.$refs.rejectForm.validate(valid => {
        if (valid) {
          rejectUsrApply(this.form).then(response => {
            this.$modal.msgSuccess('审核完成');
            this.openReject = false;
            this.getList();
          });
        }
      });
    },
    rejectCancel() {
      this.openReject = false;
      this.reset();
    },

    handlePass(row) {
      updateUsrApply({
        id: row.id,
        reviewState: '0',
        reviewMsg: '通过',
      }).then(response => {
        this.$modal.msgSuccess('审核完成');
        this.openReject = false;
        this.getList();
      });
    },
    handleReject(row) {
      this.reset();
      const id = row.id || this.ids;
      getUsrApply(id).then(response => {
        this.form = response.data;
        this.openReject = true;
        this.form.reviewState = '1';
      });
    },
    /** 查询cps_usr_apply与会申请列表 */
    getList() {
      this.loading = true;
      listUsrApply(this.queryParams).then(response => {
        this.usrApplyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        usrName: null,
        tel: null,
        orgUnitName: null,
        uniLevel: null,
        email: null,
        usrType: null,
        conferenceFlag: null,
        conferenceRel: null,
        applyType: null,
        reviewState: null,
        reviewMsg: null,
        usrWechatId: null,
        contentId: null,
        updateTime: null,
        createTime: null,
        createBy: null,
        updateBy: null,
      };
      this.resetForm('form');
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm('queryForm');
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = '添加cps_usr_apply与会申请';
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getUsrApply(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = '修改cps_usr_apply与会申请';
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUsrApply(this.form).then(response => {
              this.$modal.msgSuccess('修改成功');
              this.open = false;
              this.getList();
            });
          } else {
            addUsrApply(this.form).then(response => {
              this.$modal.msgSuccess('新增成功');
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除cps_usr_apply与会申请编号为"' + ids + '"的数据项？').then(function () {
        return delUsrApply(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess('删除成功');
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('cp/usrApply/export', {
        ...this.queryParams,
      }, `usrApply_${new Date().getTime()}.xlsx`);
    },
  },
};
</script>
