<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="runStatus">
        <el-select v-model="queryParams.runStatus" size="small" clearable>
          <el-option
              v-for="dict in runStatusList"
              :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"
          v-hasPermi="['cai:batchAddUser:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="batchAddUserList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
      <el-table-column label="新增用户数" align="center" prop="addNum" />
      <el-table-column label="性别" align="center" prop="gender" >
        <template v-slot="scope">
          <cai-dict-tag :options="genderList" :value="scope.row.gender" />
        </template>
      </el-table-column>
      <el-table-column label="邀请人code" align="center" prop="inviteUserCode" />
      <el-table-column label="手机前缀" align="center" prop="phonePrefix" />
      <el-table-column label="手机后缀起点" align="center" prop="phoneSuffixStart" />
      <el-table-column label="密码生成规则" align="center" prop="passwordType" >
        <template v-slot="scope">
          <cai-dict-tag :options="passwordTypeList" :value="scope.row.passwordType" />
        </template>
      </el-table-column>
      <el-table-column label="密码前缀" align="center" prop="passwordPrefix" />
      <el-table-column label="密码后缀起点" align="center" prop="passwordSuffixStart" />
      <el-table-column label="状态" align="center" prop="runStatus" >
        <template v-slot="scope">
          <cai-dict-tag :options="runStatusList" :value="scope.row.runStatus" />
        </template>
      </el-table-column>
      <el-table-column label="执行失败原因" align="center" prop="runFail" />
      <el-table-column label="执行时间" align="center" prop="runTime" width="180" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" />
      <el-table-column label="操作" align="center" fix="right" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            v-if="scope.row.runStatus === 0"
            @click="handleRun(scope.row)"
            v-hasPermi="['cai:batchAddUser:edit']"
          >执行</el-button>
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.runStatus === 3"
            @click="handleExport(scope.row)"
          >下载</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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="170px">
        <el-form-item label="新增用户数" prop="addNum">
          <el-input-number style="width: 100%" :min="1" :max="200" v-model="form.addNum" placeholder="请输入新增用户数" />
        </el-form-item>
        <el-form-item label="性别" prop="gender">
          <el-select v-model="form.gender" placeholder="请输入性别" size="small">
            <el-option key="1" value="1" label="女" />
            <el-option key="2" value="2" label="男" />
          </el-select>
        </el-form-item>
        <el-form-item label="邀请人code" prop="inviteUserCode">
          <el-input v-model="form.inviteUserCode" placeholder="请输入邀请人code" />
        </el-form-item>
        <el-form-item label="手机前缀(前7位)" prop="phonePrefix">
          <el-input v-model="form.phonePrefix" placeholder="请输入手机前缀(前7位)" />
        </el-form-item>
        <el-form-item label="手机后缀起点(后4位)" prop="phoneSuffixStart">
          <el-input-number :min="0" :max="9999" style="width: 100%" v-model="form.phoneSuffixStart" placeholder="请输入手机后缀起点(后4位)" />
        </el-form-item>
        <el-form-item label="密码生成规则" prop="passwordType">
          <el-select v-model="form.passwordType" placeholder="密码生成规则" clearable size="small">
            <el-option
                v-for="dict in passwordTypeList"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="密码前缀" v-if="form.passwordType === 1" prop="passwordPrefix">
          <el-input v-model="form.passwordPrefix" placeholder="请输入密码前缀" />
        </el-form-item>
        <el-form-item label="密码后缀起点" v-if="form.passwordType === 1" prop="passwordSuffixStart">
          <el-input-number :min="0" style="width: 100%" v-model="form.passwordSuffixStart" placeholder="请输入密码后缀起点" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {addBatchAddUser, delBatchAddUser, listBatchAddUser, runBatchAddUser} from "@/api/cai/batchAddUser";
import {genderList, passwordTypeList, runStatusList} from "@/constant/statusMap";

export default {
  name: "BatchAddUser",
  data() {
    return {
      runStatusList,passwordTypeList,genderList,
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 批量新增用户脚本表格数据
      batchAddUserList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        runStatus: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        addNum: [
          { required: true, message: "新增用户数不能为空", trigger: "blur" }
        ],
        gender: [
          { required: true, message: "性别不能为空", trigger: "blur" }
        ],
        passwordType: [
          { required: true, message: "密码生成规则不能为空", trigger: "blur" }
        ],
        phonePrefix: [
          { required: true, message: "手机前缀(前7位)不能为空", trigger: "blur" }
        ],
        phoneSuffixStart: [
          { required: true, message: "手机后缀起点(后4位)不能为空", trigger: "blur" }
        ],
        passwordPrefix: [
          { required: true, message: "密码前缀不能为空", trigger: "blur" }
        ],
        passwordSuffixStart: [
          { required: true, message: "密码后缀起点不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询批量新增用户脚本列表 */
    getList() {
      this.loading = true;
      listBatchAddUser(this.queryParams).then(response => {
        this.batchAddUserList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        addNum: undefined,
        gender: undefined,
        inviteUserCode: undefined,
        phonePrefix: undefined,
        phoneSuffixStart: undefined,
        passwordPrefix: undefined,
        passwordSuffixStart: undefined,
        runStatus: undefined,
        runFail: undefined,
        runResult: undefined,
        createTime: undefined,
        runTime: undefined,
        passwordType: 1
      };
      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 = "添加新增用户";
    },
    /** 修改按钮操作 */
    handleRun(row) {
      this.$modal.confirm('是否执行改脚本').then(() => {
        this.loading = true;
        return runBatchAddUser({id:row.id});
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("正在执行中，请稍后下载账户密码");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    handleExport(row){
      this.download('cai/batchAddUser/exportUser', {
        id: row.id
      }, `导出用户_${row.id}.txt`)
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          addBatchAddUser(this.form).then(response => {
            this.$modal.msgSuccess("新增成功");
            this.open = false;
            this.getList();
          }).finally(() => {
            this.buttonLoading = false;
          });
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除批量新增用户脚本编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delBatchAddUser(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
  }
};
</script>
