<template>
  <es-data-table
    ref="notify"
    checkbox
    page
    stripe
    numbers
    row-key="id"
    close
    :key="tableCount"
    :full="true"
    :fit="true"
    :thead="thead"
    :toolbar="toolbar"
    :url="sendList"
    :param="defaultParam"
    @success="successAfter"
  >
    <es-dialog
      v-if="showForm"
      title="查看"
      :visible.sync="showForm"
      :close-on-click-modal="false"
      :middle="true"
      height="600px"
      class="dialog-form"
    >
      <es-form
        ref="form"
        :model="formData"
        :param="params"
        :contents="form"
        :genre="2"
        readonly
        collapse
      />
    </es-dialog>
  </es-data-table>
</template>
<script>
import { sendList, sendInfo, sendBatch } from 'eoss-ui/src/config/api';
export default {
  name: 'EsNotify',
  componentName: 'EsNotify',
  mixins: [],
  props: {
    param: {
      type: Object,
      default() {
        return {};
      }
    },
    thead: {
      type: Array,
      default() {
        return [
          {
            title: '标题',
            align: 'left',
            field: 'title',
            minWidth: 300
          },
          {
            title: '发送者',
            field: 'senderInfo',
            align: 'center',
            width: 200,
            render: (h, params) => {
              return h('p', {}, [
                params.row.senderInfo == null ? '--' : params.row.senderInfo
              ]);
            }
          },
          {
            title: '接收人',
            field: 'msgTo',
            align: 'center',
            width: 200,
            render: (h, params) => {
              return h(
                'el-popover',
                {
                  props: {
                    placement: 'bottom',
                    trigger: 'hover',
                    content: '标识ID: ' + params.row.msgTo
                  }
                },
                [h('p', { slot: 'reference' }, params.row.accepterInfo)]
              );
            }
          },
          {
            title: '发送方式',
            align: 'center',
            field: 'sendType',
            width: 120,
            render: (h, params) => {
              return h(
                'el-popover',
                {
                  props: {
                    placement: 'bottom',
                    trigger: 'hover',
                    content: '标识: ' + params.row.sendType
                  }
                },
                [
                  h(
                    'el-tag',
                    { slot: 'reference', props: { type: '' } },
                    this.formartSendType(params.row.sendType)
                  )
                ]
              );
            }
          },
          {
            title: '消息状态',
            align: 'center',
            field: 'status',
            width: 200,
            render: (h, params) => {
              let sta = params.row.status;
              return h(
                'el-popover',
                {
                  props: {
                    placement: 'bottom',
                    trigger: 'hover',
                    content: sta == 2 ? '发送成功' : params.row.sendResponse
                  }
                },
                [
                  //-2-被终止 -1-发送失败 0-等待发送 1-重试中 2-发送成功 3-用户自定义忽略/拒收 4 发送时间超期 5 超过最大发送条数 6 第三方发送失败"
                  h(
                    'el-tag',
                    {
                      slot: 'reference',
                      props: {
                        type:
                          sta == 2
                            ? 'success'
                            : sta == -1 || sta == 6
                            ? 'danger'
                            : 'warning'
                      }
                    },
                    this.formartStatus(sta)
                  )
                ]
              );
            }
          },
          {
            title: '预约时间',
            align: 'center',
            field: 'startTime',
            width: 160
          },
          {
            title: '重发次数',
            align: 'center',
            field: 'retryCountAlready',
            width: 80,
            render: (h, params) => {
              return h(
                'p',
                {},
                params.row.retryCountAlready == null
                  ? '0'
                  : params.row.retryCountAlready
              );
            }
          },
          {
            title: '操作',
            type: 'handle',
            width: 180,
            // template: '',
            events: [
              {
                text: '查看',
                event: res => {
                  let { row } = res;
                  this.params = { id: row.id };
                  this.showForm = true;
                }
              },
              {
                text: '重发',
                format: false,
                method: 'post',
                action: sendBatch
              }
            ]
          }
        ];
      }
    },
    form: {
      type: Array,
      default() {
        return [
          {
            name: 'title',
            label: '标题',
            type: 'textarea',
            value: '',
            col: 12
          },
          {
            name: 'content',
            label: '内容',
            type: 'textarea',
            value: '',
            col: 12
          },
          {
            label: '发送者',
            name: 'senderInfo',
            data: [],
            col: 6
          },
          {
            name: 'msgTo',
            label: {
              text: '接收人标识ID',
              tips: '用于联系用户的标识ID, 可为手机号、邮箱、身份ID等'
            },
            value: '',
            col: 6
          },
          {
            name: 'accepterInfo',
            label: {
              text: '接收人信息',
              tips: '接收人的详细信息'
            },
            value: '',
            col: 6
          },
          {
            name: 'handleIdentityId',
            label: {
              text: '接收人身份id',
              tips: '接收人在系统中的身份ID, 可能为空。'
            },
            value: '',
            col: 6
          },
          {
            name: 'sendType',
            label: {
              text: '发送方式',
              tips:
                'SYS-系统消息 SMS-短信 EMAIL-邮件 APP-app消息推送 DING_DING-钉钉消息  WX_OFFICIAL_ACCOUNTS-微信公众号 等'
            },
            type: 'select',
            data: [
              {
                name: '系统消息',
                value: 'SYS'
              },
              {
                name: '短信',
                value: 'SMS'
              },
              {
                name: '邮件',
                value: 'EMAIL'
              },
              {
                name: 'app消息推送',
                value: 'APP'
              },
              {
                name: '钉钉消息',
                value: 'DING_DING'
              },
              {
                name: '微信公众号',
                value: 'WX_OFFICIAL_ACCOUNTS'
              }
            ],
            col: 6
          },
          {
            name: 'msgType',
            label: {
              text: '消息类型',
              tips: '由业务指定的消息类型, 也是业务类型'
            },
            value: '',
            col: 6
          },
          {
            name: 'attaches',
            label: {
              text: '附加信息',
              tips: '附件/办理地址'
            },
            value: '',
            col: 6
          },
          {
            name: 'startTime',
            label: '预约开始时间',
            value: '',
            col: 6
          },
          {
            name: 'status',
            label: {
              text: '状态',
              tips:
                '-2-被终止 -1-发送失败 0-等待发送 1-重试中 2-发送成功 3-用户自定义忽略/拒收 4 发送时间超期 5 超过最大发送条数 6 '
            },
            value: '',
            type: 'select',
            data: [
              {
                name: '被终止',
                value: -2
              },
              {
                name: '发送失败',
                value: -1
              },
              {
                name: '等待发送',
                value: 0
              },
              {
                name: '重试中',
                value: 1
              },
              {
                name: '发送成功',
                value: 2
              },
              {
                name: '用户自定义忽略/拒收',
                value: 3
              },
              {
                name: '发送时间超期',
                value: 4
              },
              {
                name: '超过最大发送条数',
                value: 5
              },
              {
                name: '用户自定义忽略/拒收',
                value: 6
              }
            ],
            col: 6
          },
          {
            name: 'sendedTime',
            label: '发送时间',
            value: '',
            col: 6
          },
          {
            name: 'businessId',
            label: { text: '业务id', tips: '关联的业务标识ID' },
            value: '',
            col: 6
          },
          {
            name: 'xcode',
            label: '短信xcode',
            value: '',
            col: 6
          },
          {
            name: 'templateCode',
            label: { text: '模版Code', tips: '使用的消息模版编码code' },
            value: '',
            col: 6
          },
          {
            name: 'tagText',
            label: '消息标签',
            value: '',
            col: 6
          },
          {
            type: 'textarea',
            name: 'metaData',
            label: '元数据',
            value: '',
            col: 12
          },
          {
            type: 'textarea',
            name: 'sendResponse',
            label: '发送响应结果',
            value: '',
            col: 12
          },
          {
            name: 'subsystemName',
            label: '子系统名称',
            value: '',
            col: 6
          },
          {
            name: 'updateTime',
            label: '更新时间',
            value: '',
            col: 6
          }
        ];
      }
    }
  },
  data() {
    return {
      sendList: sendList,
      sendBatch: sendBatch,
      tableCount: 0,
      selectData: [],
      formTitle: '',
      showForm: false,
      formData: sendInfo,
      params: {},
      sendTypeDict: [],
      statusDict: []
    };
  },
  computed: {
    defaultParam() {
      return { orderBy: 'startTime', asc: false, ...this.param };
    },
    toolbar() {
      return [
        {
          type: 'button',
          contents: [
            {
              text: '批量重发',
              type: 'primary',
              checkbox: true,
              format: false,
              action: sendBatch
            }
          ]
        },
        {
          type: 'search',
          reset: true,
          contents: [
            {
              name: 'title_like',
              placeholder: '输入接标题',
              col: 6
            },
            {
              name: 'msgTo_like',
              placeholder: '输入接收者',
              col: 6
            }
          ]
        },
        {
          type: 'filter',
          contents: [
            {
              type: 'select',
              name: 'sendType',
              col: 6,
              label: '发送方式',
              placeholder: '发送方式',
              event: 'multipled',
              data: this.sendTypeDict,
              'value-key': 'rawName',
              'label-key': 'text'
            },
            {
              type: 'select',
              name: 'status',
              col: 6,
              label: '消息状态',
              placeholder: '消息状态',
              event: 'multipled',
              data: this.statusDict,
              'value-key': 'status',
              'label-key': 'text'
            },
            {
              type: 'date',
              col: 6,
              name: 'startTime_begin',
              label: '预约开始时间',
              placeholder: '预约开始时间'
            },
            {
              type: 'date',
              col: 6,
              name: 'startTime_end',
              label: '预约结束时间',
              placeholder: '预约结束时间'
            }
          ]
        }
      ];
    }
  },
  methods: {
    successAfter(data) {
      this.sendTypeDict = data.results.extraData.sendType;
      this.statusDict = data.results.extraData.statusType;
    },
    formartStatus(data) {
      let text = '未知';
      this.statusDict.forEach(dict => {
        if (dict.status === data) {
          text = dict.text;
        }
      });
      return text;
    },
    formartSendType(data) {
      let text = '未知';
      this.sendTypeDict.forEach(dict => {
        if (dict.rawName === data) {
          text = dict.text;
        }
      });
      return text;
    }
  }
};
</script>
