<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="$t('来源')" prop="fromId">
        <el-input v-model="queryParams.fromId" :placeholder="$t('请输入站内信来源')" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>-->
      <el-form-item :label="$t('发送时间')">
        <el-date-picker v-model="dateRangeSendTime" style="width: 240px" value-format="yyyy-MM-dd"
                        type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" />
      </el-form-item>
      <el-form-item :label="$t('标题')" prop="title">
        <el-input v-model="queryParams.title" :placeholder="$t('请输入标题')" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item :label="$t('消息类型')" prop="type">
        <el-select v-model="queryParams.type" :placeholder="$t('请选择')" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.INTERNAL_MESSAGE_TYPE)"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('状态')" prop="status">
        <el-select v-model="queryParams.status" :placeholder="$t('请选择')" clearable size="small">
          <el-option v-for="dict in this.getDictDatas(DICT_TYPE.INTERNAL_MESSAGE_READ_STATUS)"
                     :key="dict.value" :label="dict.label" :value="dict.value"/>
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('创建时间')">
        <el-date-picker v-model="dateRangeCreateTime" style="width: 240px" value-format="yyyy-MM-dd"
                        type="daterange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>


    <!-- 列表 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column :label="$t('编号')" align="center" prop="id" />
<!--      <el-table-column :label="$t('来源')" align="center" prop="fromId" />-->
      <el-table-column :label="$t('发送时间')" align="center" prop="sendTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.sendTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('标题')" align="center" prop="title" />
<!--      <el-table-column :label="$t('内容')" align="center" prop="content" />-->
      <el-table-column :label="$t('消息类型')" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.INTERNAL_MESSAGE_TYPE" :value="scope.row.type" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('状态')" align="center" prop="status" width="50">
        <template slot-scope="scope">
          <dict-tag :type="DICT_TYPE.INTERNAL_MESSAGE_READ_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <el-table-column :label="$t('创建时间')" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>

      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="viewMessage(scope.row)">{{ $t('查看') }}</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="deleteFn(scope.row)">{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.page" :limit.sync="queryParams.rows"
                @pagination="getList"/>

    <el-dialog
      :title="$t('站内信')"
      :visible.sync="dialogVisible"
      width="50%">
      <div class="from">
      <div class="from-item">
        <div class="label">{{ $t('标题：') }}</div>
        <div class="content">{{publicObj.title}}</div>
      </div>
      <div class="from-item">
        <div class="label">{{ $t('消息分类：') }}</div>
        <div class="content">{{type[publicObj.type]}}</div>
      </div>
      <div class="from-item">
        <div class="label">{{ $t('创建时间') }}</div>
        <div class="content">{{parseTime(publicObj.createTime)}}</div>
      </div>
      <div class="from-item">
        <div class="label">{{ $t('消息内容：') }}</div>
        <div class="content">{{publicObj.content}}</div>
      </div>
      </div>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">{{ $t('关 闭') }}</el-button>
  </span>
    </el-dialog>
  </div>
</template>

<script>
import {  getMyInternalMessagePage ,getRedInternalMessage, deleteInternalMessage} from "@/api/system/internalMessage";
export default {
  name: "MyInternalMessage",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 站内信列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      dateRangeSendTime: [],
      dateRangeCreateTime: [],
      // 查询参数
      queryParams: {
        page: 1,
        rows: 10,
        fromId: null,
        title: null,
        content: null,
        type: null,
        status: this.$route.query.status?this.$route.query.status:null,
      },
      // 表单参数
      form: {},
      dialogVisible:false,
      publicObj:{},
      type: {
        1:this.$t('订单消息'),
        2:this.$t('出货消息'),
        3:this.$t('提醒消息'),
      },
    };
  },
  computed:{
    notMessage() {
      return this.$store.state.user.notMessage;
    }
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 处理查询参数
      let params = {...this.queryParams};
      this.addBeginAndEndTime(params, this.dateRangeSendTime, 'sendTime');
      this.addBeginAndEndTime(params, this.dateRangeCreateTime, 'createTime');
      // 执行查询
      getMyInternalMessagePage(params).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        fromId: undefined,
        sendTime: undefined,
        title: undefined,
        content: undefined,
        type: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRangeSendTime = [];
      this.dateRangeCreateTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    viewMessage(row){
     this.publicObj = row;
     this.dialogVisible = true;
     if(row.status == 0)this.setNotMessage(row.id)
    },
    setNotMessage(id){
      getRedInternalMessage({messageId:id}).then(r =>{
      console.log(r)
        if(r.code === 0){
          this.getList()
          let num = Number(this.notMessage);
          this.$store.commit('NOt_MESSAGE',--num);
        }
      })
    },
    deleteFn(row){
      this.$confirm(`是否要删除编号为“${row.id}”的站内信?`, this.$t('提示'), {
        confirmButtonText: this.$t('确定'),
        cancelButtonText: this.$t('取消'),
        type: 'warning'
      }).then(() => {
        deleteInternalMessage(row.id).then(r => {
          if(r.code === 0){
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.getList();
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: this.$t('已取消删除')
        });
      });
    }
  }
};
</script>
<style lang="scss" scoped>
.from{
  .from-item{
    display: flex;
    margin: 10px 0;
    .label{
      width:80px;
    }
    .content{
      flex: 1;
    }
  }
}
</style>
