<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="patientId">
        <el-input
          v-model="queryParams.patientId"
          placeholder="请输入患者姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="患者手机号" prop="patientPhone" label-width="90px">
        <el-input
          v-model="queryParams.patientPhone"
          placeholder="请输入患者手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="消息发送时间" prop="sendTime" label-width="98px">
        <el-date-picker
          clearable
          v-model="queryParams.sendTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择消息发送时间"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="关联账单" prop="billId">
        <el-input
          v-model="queryParams.billId"
          placeholder="请输入关联账单id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:shortMessageSendRecord:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        style="margin-right: 20px"
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="shortMessageSendRecordList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="姓名" align="center" prop="patientId" />
      <el-table-column label="患者手机号" align="center" prop="patientPhone" />
      <el-table-column
        label="消息发送时间"
        align="center"
        prop="sendTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sendTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="模板名称"
        align="center"
        prop="messageTemplateId"
      />

      <el-table-column label="短信长度" align="center" prop="messageLength" />
      <el-table-column label="短信数量" align="center" prop="messageQuantity" />
      <el-table-column
        label="短信单价"
        align="center"
        prop="messageUnitPrice"
      />
      <el-table-column label="短信价格" align="center" prop="messagePrice" />
      <el-table-column label="关联账单" align="center" prop="billId" />
      <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:shortMessageSendRecord:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:shortMessageSendRecord: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="患者id" prop="patientId">
          <el-input v-model="form.patientId" placeholder="请输入患者id" />
        </el-form-item>
        <el-form-item label="签约患者管理任务节点表id" prop="manageRouteNodeId">
          <el-input
            v-model="form.manageRouteNodeId"
            placeholder="请输入签约患者管理任务节点表id"
          />
        </el-form-item>
        <el-form-item label="患者手机号" prop="patientPhone">
          <el-input
            v-model="form.patientPhone"
            placeholder="请输入患者手机号"
          />
        </el-form-item>
        <el-form-item label="消息发送时间" prop="sendTime">
          <el-date-picker
            clearable
            v-model="form.sendTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择消息发送时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="消息模板id" prop="messageTemplateId">
          <el-input
            v-model="form.messageTemplateId"
            placeholder="请输入消息模板id"
          />
        </el-form-item>
        <el-form-item label="消息内容">
          <editor v-model="form.messageNodeContent" :min-height="192" />
        </el-form-item>
        <el-form-item label="推送结果状态码" prop="errorCode">
          <el-input
            v-model="form.errorCode"
            placeholder="请输入推送结果状态码"
          />
        </el-form-item>
        <el-form-item label="短信长度" prop="messageLength">
          <el-input v-model="form.messageLength" placeholder="请输入短信长度" />
        </el-form-item>
        <el-form-item label="短信数量" prop="messageQuantity">
          <el-input
            v-model="form.messageQuantity"
            placeholder="请输入短信数量"
          />
        </el-form-item>
        <el-form-item label="短信单价" prop="messageUnitPrice">
          <el-input
            v-model="form.messageUnitPrice"
            placeholder="请输入短信单价"
          />
        </el-form-item>
        <el-form-item label="短信价格" prop="messagePrice">
          <el-input v-model="form.messagePrice" placeholder="请输入短信价格" />
        </el-form-item>
        <el-form-item label="关联账单id" prop="billId">
          <el-input v-model="form.billId" placeholder="请输入关联账单id" />
        </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 {
  listShortMessageSendRecord,
  getShortMessageSendRecord,
  delShortMessageSendRecord,
  addShortMessageSendRecord,
  updateShortMessageSendRecord,
} from "@/api/system/shortMessageSendRecord";

export default {
  name: "ShortMessageSendRecord",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 短信发送结果记录表格数据
      shortMessageSendRecordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        patientId: null,
        patientPhone: null,
        sendTime: null,
        messageNodeContent: null,
        errorStatus: null,
        messageType: null,
        billId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询短信发送结果记录列表 */
    getList() {
      this.loading = true;
      listShortMessageSendRecord(this.queryParams).then((response) => {
        this.shortMessageSendRecordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        patientId: null,
        patientPhone: null,
        sendTime: null,
        messageNodeContent: null,
        errorStatus: "0",
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        messageType: null,
        billId: 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;
      getShortMessageSendRecord(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) {
            updateShortMessageSendRecord(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addShortMessageSendRecord(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除短信发送结果记录编号为"' + ids + '"的数据项？')
        .then(function () {
          return delShortMessageSendRecord(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/shortMessageSendRecord/export",
        {
          ...this.queryParams,
        },
        `shortMessageSendRecord_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
