<template>
  <div>
    <base-layout
      ref="baseLayout"
      :buttonList="buttonList"
      @onFuncBtn="onFuncBtn"
      :querySwitch="true"
      :searchList="requirementList"
      @search="handleSearchEvent"
      :isPage="true"
      @pageChange="handlePageChange"
    >
      <div slot="main" slot-scope="{ tableHeight }">
        <base-table
          ref="baseTable"
          :showIndex="true"
          :funWidth="215"
          :funData="funData"
          @onFunc="onFunc"
          :tabLoading.sync="tabLoading"
          :tableHeight="tableHeight"
          :tableData="tableData"
          :tableColumn="tableColumnData"
        >
          <template v-slot:status="{ row }">
            <div style="width: 100%">
              <span v-if="row.status == '1'">待发送</span>
              <span v-else-if="row.status == '2'">发送中</span>
              <span v-else-if="row.status == '3'">发送成功</span>
              <span v-else-if="row.status == '4'">发送失败</span>
              <span v-else></span>
            </div>
          </template>
        </base-table>
      </div>
    </base-layout>
    <right-dialog ref="rightDialog" @resetTable="resetTable"></right-dialog>
  </div>
</template>

<script>
import baseLayout from "@/components/base/baseLayout";
import baseTable from "@/components/base/baseTable";
import rightDialog from "./rightDialog";
import configData from "./configData";
import { authApi } from "@/api/apis/auth";
export default {
  components: {
    baseLayout,
    baseTable,
    rightDialog,
  },
  data() {
    return {
      buttonList: [
        {
          menuName: "刷新",
          icon: "el-icon-refresh",
          btnFunction: "resetLoad",
        },
      ], //按钮组
      requirementList: [
        {
          placeholder: "消息管理名称",
          prop: "theme",
          tag: "elInput",
        },
        {
          placeholder: "发送者应用",
          prop: "sendApp",
          tag: "elSelect",
          options: [],
          optionValue: "id",
          optionLabel: "name",
        },
        {
          placeholder: "发送者",
          prop: "sendApi",
          tag: "elSelect",
          options: [],
          optionValue: "id",
          optionLabel: "apiName",
        },
        {
          placeholder: "接收者应用",
          prop: "receiveApp",
          tag: "elSelect",
          options: [],
          optionValue: "id",
          optionLabel: "name",
        },
        {
          placeholder: "接收者",
          prop: "receiveApi",
          tag: "elSelect",
          options: [],
          optionValue: "id",
          optionLabel: "apiName",
        },
        {
          placeholder: "状态",
          prop: "status",
          tag: "elSelect",
          options: [
            { id: "1", label: "待发送" },
            { id: "2", label: "发送中" },
            { id: "3", label: "发送成功" },
            { id: "4", label: "发送失败" },
          ],
          optionValue: "id",
          optionLabel: "label",
        },
      ], //查询模板list
      tabLoading: false,
      tableColumnData: configData.tableColumnData, //表头数据
      funData: [
        {
          color: "#6a9af1",
          text: "查看",
        },
        {
          color: "#6a9af1",
          text: "编辑",
        },
        {
          color: "#75b67e",
          text: "重新发送",
        },
      ],
      tableData: [], //表格数据
      pageModel: {
        pageNum: 1,
        pageSize: 10,
      },
      queryModel: {
        theme: "", 
        sendApp: "", 
        sendApi: "", 
        receiveApp: "", 
        receiveApi: "",
        status: "",
      },
    };
  },
  mounted() {
    this.querysysAppApiService();
    this.querysysAppService();
    this.GetMessageLogTableData();
  },
  methods: {
    // 表格数据
    async GetMessageLogTableData() {
      this.tabLoading = true;
      let param = {
        ...this.pageModel,
        ...this.queryModel,
      };
      let res = await authApi(
        "sysMessageManageLogService",
        "messageManage",
        "queryPagedVo",
        "",
        param
      );
      this.tabLoading = false;
      if (res.status == "200") {
        this.tableData = res.attribute.list;
        this.$refs.baseLayout.setPageTotal(res.attribute.total);
      }
    },
    // 分页变化
    handlePageChange(val) {
      this.pageModel.pageNum = val.pageIndex;
      this.pageModel.pageSize = val.pageSize;
      this.GetMessageLogTableData();
    },
    // 按钮组
    onFuncBtn(btn) {
      this[btn.btnFunction]();
    },
    // 表格操作事件 查看 编辑 重新推送
    onFunc(index, row) {
      // 查看
      if (index == 0) {
        this.openLoading("detail");
        this.$refs.rightDialog.openDialog("show", row);
      }
      // 编辑
      if (index == 1) {
        this.openLoading("detail");
        this.$refs.rightDialog.openDialog("edit", row);
      }
      // 重新推送
      if (index == 2) {
        this.$confirm('确认重新发送吗?', '提示', {
					confirmButtonText: '确定',
					cancelButtonText: '取消',
					type: 'warning'
				}).then(() => {
          this.openLoading("detail");
          this.messageResendData(row.id)
				}).catch(() => {
					this.$message({
						type: 'info',
						message: '取消重新发送'
					});
				});
      }
    },
    async messageResendData(id) {
      let param = {
        id: id,
      };
      let res = await authApi(
        "sysMessageManageLogService",
        "messageManage",
        "resendData",
        "",
        param
      );
      if (res.status == "200") {
        this.$vmNews("重新发送成功！", "success");
        this.resetTable();
      }
    },
    // 重置表格
    resetTable() {
      this.pageModel.pageNum = 1;
      this.$refs.baseLayout.pageClear();
      this.GetMessageLogTableData();
    },
    // 搜索
    handleSearchEvent() {
      let data = this.$refs.baseLayout.ruleForm;
      this.queryModel.theme = data.theme;
      this.queryModel.sendApp = data.sendApp;
      this.queryModel.sendApi = data.sendApi;
      this.queryModel.receiveApp = data.receiveApp;
      this.queryModel.receiveApi = data.receiveApi;
      this.queryModel.status = data.status;
      this.resetTable();
    },
    // 应用
    async querysysAppService() {
      let params = {
        pageSize: 9999,
        pageNum: 1,
      };
      let res = await authApi(
        "sysAppService",
        "app",
        "queryPageApp",
        "",
        params
      );
      if (res.status == "200") {
        this.requirementList[1].options = res.attribute.list;
        this.requirementList[3].options = res.attribute.list;
      }
    },
    // 应用者
    async querysysAppApiService() {
      let params = {
        pageSize: 9999,
        pageNum: 1,
      };
      let res = await authApi(
        "appApiService",
        "appApi",
        "queryPage",
        "",
        params
      );
      if (res.status == "200") {
        this.requirementList[2].options = res.attribute.list;
        this.requirementList[4].options = res.attribute.list;
      }
    },
  },
};
</script>

<style scoped>
.clickTitle {
  color: #409eff;
  cursor: pointer;
}
</style>