<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="90px">
      <el-form-item label="消息类型0个人1群" prop="type">
        <el-select v-model="queryParams.type" placeholder="请选择消息类型0个人1群" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="任务ID" prop="sopId">
        <el-input
          v-model="queryParams.sopId"
          placeholder="请输入任务ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="营期ID" prop="sopUserId">
        <el-input
          v-model="queryParams.sopUserId"
          placeholder="请输入营期ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发送类型" prop="sendType">
        <el-select v-model="queryParams.sendType" placeholder="请选择发送类型" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="生成类型(0自动1手动)" prop="generateType">
        <el-select v-model="queryParams.generateType" placeholder="请选择生成类型(0自动1手动)" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="发送账号ID" prop="accountId">
        <el-input
          v-model="queryParams.accountId"
          placeholder="请输入发送账号ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发送对象ID" prop="wxContactId">
        <el-input
          v-model="queryParams.wxContactId"
          placeholder="请输入发送对象ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发送对象名称" prop="wxContactName">
        <el-input
          v-model="queryParams.wxContactName"
          placeholder="请输入发送对象名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发送群聊ID" prop="wxRoomId">
        <el-input
          v-model="queryParams.wxRoomId"
          placeholder="请输入发送群聊ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发送群聊名称" prop="wxRoomName">
        <el-input
          v-model="queryParams.wxRoomName"
          placeholder="请输入发送群聊名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="小程序ID" prop="fsUserId">
        <el-input
          v-model="queryParams.fsUserId"
          placeholder="请输入小程序ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发送状态0待发送1发送成功2发送失败3消息作废" prop="sendStatus">
        <el-select v-model="queryParams.sendStatus" placeholder="请选择发送状态0待发送1发送成功2发送失败3消息作废" clearable size="small">
          <el-option label="请选择字典生成" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="发送备注" prop="sendRemark">
        <el-input
          v-model="queryParams.sendRemark"
          placeholder="请输入发送备注"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发送排序" prop="sendSort">
        <el-input
          v-model="queryParams.sendSort"
          placeholder="请输入发送排序"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="消息过期时间" prop="expirationTime">
        <el-date-picker clearable size="small"
          v-model="queryParams.expirationTime"
          type="date"
          value-format="yyyy-MM-dd"
          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>
    </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="['wx:wxSopLogs:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wx:wxSopLogs:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wx:wxSopLogs:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport"
          v-hasPermi="['wx:wxSopLogs:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table border v-loading="loading" :data="wxSopLogsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="消息类型0个人1群" align="center" prop="type" />
      <el-table-column label="任务ID" align="center" prop="sopId" />
      <el-table-column label="营期ID" align="center" prop="sopUserId" />
      <el-table-column label="发送类型" align="center" prop="sendType" />
      <el-table-column label="生成类型(0自动1手动)" align="center" prop="generateType" />
      <el-table-column label="发送账号ID" align="center" prop="accountId" />
      <el-table-column label="发送对象ID" align="center" prop="wxContactId" />
      <el-table-column label="发送对象名称" align="center" prop="wxContactName" />
      <el-table-column label="发送群聊ID" align="center" prop="wxRoomId" />
      <el-table-column label="发送群聊名称" align="center" prop="wxRoomName" />
      <el-table-column label="小程序ID" align="center" prop="fsUserId" />
      <el-table-column label="发送状态0待发送1发送成功2发送失败3消息作废" align="center" prop="sendStatus" />
      <el-table-column label="发送备注" align="center" prop="sendRemark" />
      <el-table-column label="发送排序" align="center" prop="sendSort" />
      <el-table-column label="消息过期时间" align="center" prop="expirationTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expirationTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <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="['wx:wxSopLogs:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wx:wxSopLogs:remove']"
          >删除</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="80px">
        <el-form-item label="消息类型0个人1群" prop="type">
          <el-select v-model="form.type" placeholder="请选择消息类型0个人1群">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="任务ID" prop="sopId">
          <el-input v-model="form.sopId" placeholder="请输入任务ID" />
        </el-form-item>
        <el-form-item label="营期ID" prop="sopUserId">
          <el-input v-model="form.sopUserId" placeholder="请输入营期ID" />
        </el-form-item>
        <el-form-item label="发送类型" prop="sendType">
          <el-select v-model="form.sendType" placeholder="请选择发送类型">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="生成类型(0自动1手动)" prop="generateType">
          <el-select v-model="form.generateType" placeholder="请选择生成类型(0自动1手动)">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
        <el-form-item label="发送账号ID" prop="accountId">
          <el-input v-model="form.accountId" placeholder="请输入发送账号ID" />
        </el-form-item>
        <el-form-item label="发送对象ID" prop="wxContactId">
          <el-input v-model="form.wxContactId" placeholder="请输入发送对象ID" />
        </el-form-item>
        <el-form-item label="发送对象名称" prop="wxContactName">
          <el-input v-model="form.wxContactName" placeholder="请输入发送对象名称" />
        </el-form-item>
        <el-form-item label="发送群聊ID" prop="wxRoomId">
          <el-input v-model="form.wxRoomId" placeholder="请输入发送群聊ID" />
        </el-form-item>
        <el-form-item label="发送群聊名称" prop="wxRoomName">
          <el-input v-model="form.wxRoomName" placeholder="请输入发送群聊名称" />
        </el-form-item>
        <el-form-item label="小程序ID" prop="fsUserId">
          <el-input v-model="form.fsUserId" placeholder="请输入小程序ID" />
        </el-form-item>
        <el-form-item label="发送状态0待发送1发送成功2发送失败3消息作废">
          <el-radio-group v-model="form.sendStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="发送备注" prop="sendRemark">
          <el-input v-model="form.sendRemark" placeholder="请输入发送备注" />
        </el-form-item>
        <el-form-item label="发送排序" prop="sendSort">
          <el-input v-model="form.sendSort" placeholder="请输入发送排序" />
        </el-form-item>
        <el-form-item label="消息过期时间" prop="expirationTime">
          <el-date-picker clearable size="small"
            v-model="form.expirationTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择消息过期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" 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 { listWxSopLogs, getWxSopLogs, delWxSopLogs, addWxSopLogs, updateWxSopLogs, exportWxSopLogs } from "@/api/wx/wxSopLogs";

export default {
  name: "WxSopLogs",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 个微发送记录表格数据
      wxSopLogsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        sopId: null,
        sopUserId: null,
        sendType: null,
        generateType: null,
        accountId: null,
        wxContactId: null,
        wxContactName: null,
        wxRoomId: null,
        wxRoomName: null,
        fsUserId: null,
        sendStatus: null,
        sendRemark: null,
        sendSort: null,
        expirationTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询个微发送记录列表 */
    getList() {
      this.loading = true;
      listWxSopLogs(this.queryParams).then(response => {
        this.wxSopLogsList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        sopId: null,
        sopUserId: null,
        sendType: null,
        generateType: null,
        accountId: null,
        wxContactId: null,
        wxContactName: null,
        wxRoomId: null,
        wxRoomName: null,
        fsUserId: null,
        sendStatus: 0,
        sendRemark: null,
        sendSort: null,
        expirationTime: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        remark: 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 = "添加个微发送记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getWxSopLogs(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改个微发送记录";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateWxSopLogs(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWxSopLogs(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除个微发送记录编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delWxSopLogs(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有个微发送记录数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportWxSopLogs(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    }
  }
};
</script>
