<!-- 消息列表 -->
<template>
  <div class="container">
    <div style="margin-bottom:10px">
      <RadioGroup v-model="params.readState" style="border-right:1px solid #dcdcdc" @on-change="readStateChange">
          <Radio :label="1">已读</Radio>
          <Radio :label="0">未读</Radio>
      </RadioGroup>
      <RadioGroup v-model="params.sourceType" style="margin:0 10px"  @on-change="readStateChange2">
        <Radio :label="index" v-for="(item, index) in sourceTypeList"  :key="index">
          {{item}}<span style="color:red">({{ getNum(index)}})</span>
        </Radio>
      </RadioGroup>
    </div>
    <div style="height:calc(100vh - 200px)">
      <my-table :data="data" :columns="columns" :table-filter="tableFilter" :table-page="params" @event-handle="eventHandle" @on-change="changePage"></my-table>
    </div>
    <purchase-form v-model="purchaseShow" :data="formData" @getlist="getList"></purchase-form>
    <contract-form v-model="contractShow" :data="formData" @getlist="getList"></contract-form>
    <pay-form v-model="payShow" :data="formData" @getlist="getList"></pay-form>
    <material-approve-form v-model="materialApproveShow" :data="formData" @getlist="getList"></material-approve-form>
    <provider-price-form  v-model="providerPriceShow" :data="formData" @getlist="getList"></provider-price-form>
    <qty-form v-model="qtyCheckShow" :data="formData" @getlist="getList"></qty-form>
    <apply-material-form v-model="applyMaterialShow" :data="formData" @getlist="getList"></apply-material-form>
    <other-form v-model="otherShow" :data="formData" @getlist="getList"></other-form>
    <return-form v-model="returnShow" :data="formData" @getlist="getList"></return-form>
    <check-form v-model="checkShow" :data="formData" @getlist="getList"></check-form>
    <new-check-form v-model="newCheckShow" :data="formData" @getlist="getList"></new-check-form>
    <profit-form v-model="profitShow" :data="formData" @getlist="getList"></profit-form>
    <del-contract-form v-model="delContractShow" :data="formData"></del-contract-form>
  </div>
</template>

<script>
import { GetMessageRemindPage, ReadMessageRemind } from '@/api/storageHome'
import MyTable from '_c/my-table/my-table'
import PurchaseForm from '@/view/index/schedule/purchase-form'
import ContractForm from '@/view/index/schedule/contract-form'
import PayForm from '@/view/index/schedule/pay-form'
import MaterialApproveForm from '@/view/index/schedule/material-approve-form'
import ProviderPriceForm from '@/view/index/schedule/provider-price-form'
import ApplyMaterialForm from '@/view/index/schedule/apply-material-form'
import QtyForm from '@/view/index/schedule/qty-form'
import OtherForm from '@/view/index/schedule/other-form'
import ReturnForm from '@/view/index/schedule/return-form'
import CheckForm from '@/view/index/schedule/check-form'
import NewCheckForm from '@/view/index/schedule/new-check-form'
import ProfitForm from '@/view/index/schedule/profit-form'
import DelContractForm from '@/view/index/schedule/del-contract-form'
import {
  GetApplyPurchasePageList,
  GetPurchaseContractDel,
  GetPurContractPageList,
  GetQuantityCheckLake
} from '@/api/applyPurchase'
import { GetApplyStockInBackRequestDto, GetApplyStockOutBill, GetApplyTakePageList } from '@/api/stock'
import { GetChangePriceCheck, GetMaterialEditCheckData } from '@/api/baseData'
import axios from 'axios'
export default {
  name: 'msg',
  components: {
    MyTable,
    PurchaseForm,
    ContractForm,
    PayForm,
    MaterialApproveForm,
    ProviderPriceForm,
    QtyForm,
    ApplyMaterialForm,
    OtherForm,
    ReturnForm,
    CheckForm,
    NewCheckForm,
    ProfitForm,
    DelContractForm
  },
  data () {
    return {
      formData: {},
      sourceTypeList:['物料申购','入库管理','库存管理','出库管理','系统提醒','盘点提醒'],
      show: false,
      storeInShow: false,
      delContractShow: false,
      profitShow: false,
      newCheckShow: false,
      dispatchShow: false,
      checkShow: false,
      returnShow: false,
      otherShow: false,
      applyMaterialShow: false,
      qtyCheckShow: false,
      providerPriceShow: false,
      materialApproveShow: false,
      purchaseShow: false,
      contractShow: false,
      payShow: false,
      data: [],
      columns: [
        {
          title: '序号',
          type: 'index',
          width: 60,
          align: 'center'
        },
        {
          title: '提醒来源',
          key: 'sourcetype',
          minWidth: 150,
          render:(h,p)=>{
            return h('span',{},
              this.sourceTypeList[p.row.sourcetype]
              
            )
          }
        },
        {
          title: '提醒内容',
          key: 'content',
          minWidth: 350,
          render: (h, params) => {
            let _this = this
            if ((params.row.dealWithType || params.row.dealWithType === 0) && params.row.linkId) {
              return h('a', {
                on: {
                  click (e) {
                    e.stopPropagation()
                    _this.formData = {}
                    if (params.row.dealWithType === 0) {
                      // 请购单审批
                      GetApplyPurchasePageList({
                        id: params.row.linkId,
                        pageIndex: 1,
                        pageSize: 1
                      }).then(res => {
                        if (res.code === 0) {
                          _this.formData = { ...res.result.list[0], ...params.row }
                          _this.purchaseShow = true
                        }
                      })
                    } else if (params.row.dealWithType === 1) {
                      // 合同审批
                      GetPurContractPageList({
                        id: params.row.linkId,
                        pageIndex: 1,
                        pageSize: 1
                      }).then(res => {
                        if (res.code === 0) {
                          _this.formData = { ...res.result.list[0], ...params.row }
                          _this.contractShow = true
                        }
                      })
                    } else if (params.row.dealWithType === 2) {
                      // 付款审批
                      _this.formData = { ...params.row }
                      _this.payShow = true
                    } else if (params.row.dealWithType === 3) {
                      // 领料申请
                      GetApplyTakePageList({
                        id: params.row.linkId,
                        pageIndex: 1,
                        pageSize: 1
                      }).then(res => {
                        if (res.code === 0) {
                          _this.formData = { ...res.result.list[0], ...params.row }
                          _this.applyMaterialShow = true
                        }
                      })
                    } else if (params.row.dealWithType === 4) {
                      // 其他出库退货/报损
                      GetApplyStockOutBill({
                        id: params.row.linkId
                      }).then(res => {
                        if (res.code === 0) {
                          _this.formData = { ...res.result, ...params.row }
                          _this.otherShow = true
                        }
                      })
                    } else if (params.row.dealWithType === 5) {
                      // 到货质检数量修正
                      GetQuantityCheckLake({
                        checkAdjustId: params.row.linkId
                      }).then(res => {
                        if (res.code === 0) {
                          _this.formData = { ...res.result, ...params.row }
                          _this.qtyCheckShow = true
                        }
                      })
                    } else if (params.row.dealWithType === 6) {
                      // 物料审批
                      GetMaterialEditCheckData({
                        id: params.row.linkId
                      }).then(res => {
                        if (res.code === 0) {
                          _this.formData = { ...res.result, ...params.row }
                          _this.materialApproveShow = true
                        }
                      })
                    } else if (params.row.dealWithType === 7) {
                      // 供应商调价
                      GetChangePriceCheck({
                        priceChangeId: params.row.linkId
                      }).then(res => {
                        if (res.code === 0) {
                          _this.formData = { ...res.result, ...params.row }
                          _this.providerPriceShow = true
                        }
                      })
                    } else if (params.row.dealWithType === 8) {
                      // 审核盘点
                      _this.formData = { ...params.row }
                      _this.profitShow = true
                    } else if (params.row.dealWithType === 9) {
                      // 归还入库
                      GetApplyStockInBackRequestDto({
                        id: params.row.linkId
                      }).then(res => {
                        if (res.code === 0) {
                          _this.formData = { ...res.result, ...params.row }
                          _this.returnShow = true
                        }
                      })
                    } else if (params.row.dealWithType === 10) {
                      // 合同删除
                      GetPurchaseContractDel({
                        id: params.row.linkId
                      }).then(res => {
                        if (res.code === 0) {
                          _this.formData = { ...res.result, ...params.row }
                          _this.delContractShow = true
                        }
                      })
                    }
                  }
                }
              }, params.row.content)
            } else {
              return h('span', params.row.content)
            }
          }
        },
        {
          title: '时间',
          key: 'createdTime',
          minWidth: 150
        },
        {
          title: '消息操作',
          key: 'applyBillNo',
          align: 'center',
          fixed: 'right',
          width: 150,
          render: (h, params) => {
            let _this = this
            if (params.row.readState === 0) {
              if(params.row.routerPushUrl){
                return h('div',[
                  h('a', {
                    on: {
                      click (e) {
                        e.stopPropagation()
                        ReadMessageRemind({
                          id: params.row.id
                        }).then(res => {
                          if (res.code === 0) {
                            _this.$Message.info(res.msg || '操作成功')
                            _this.$store.commit('setMsgChange', new Date().getTime())
                            _this.getList()
                            _this.getMsgState()
                          }
                        })
                      }
                    }
                  }, '已阅'),
                  h('a',{
                    style:{
                      marginLeft:'10px'
                    },
                    on:{
                      'click':()=>{
                        this.$router.push('/' + params.row.routerPushUrl)
                      }
                    },
                  },'跳转')
                ])
              }else{
                return h('a', {
                  on: {
                    click (e) {
                      e.stopPropagation()
                      ReadMessageRemind({
                        id: params.row.id
                      }).then(res => {
                        if (res.code === 0) {
                          _this.$Message.info(res.msg || '操作成功')
                          _this.$store.commit('setMsgChange', new Date().getTime())
                          _this.getList()
                          _this.getMsgState()
                        }
                      })
                    }
                  }
                }, '已阅')
              }
              
            } else {
              if(params.row.routerPushUrl){
                return h('div',[
                  h('span','消息已读'),
                  h('a',{
                    style:{
                      marginLeft:'10px'
                    },
                    on:{
                      'click':()=>{
                        this.$router.push('/' + params.row.routerPushUrl)
                      }
                    },
                  },'跳转')
                ])
              }else{
                return h('span', '消息已读')
              }
              
            }
          }
        }
      ],
      tableFilter: [
        {
          name: 'Input',
          value: 'key',
          placeholder: '请输入关键字'
        },
        {
          name: 'Button',
          type: 'primary',
          text: '查询',
          e: 'search'
        },
        {
          name: 'Button',
          type: 'primary',
          text: '一键已读',
          align: 'right',
          e: 'allRead'
        },
        {
          name: 'Button',
          type: 'primary',
          text: '返回首页',
          align: 'right',
          e: 'toHome'
        },
        
      ],
      params: {
        pageIndex: 1,
        pageSize: 20,
        total: 0,
        userNo:window.localStorage.getItem('jobNo'),
        readState:0,
        search:'',
      },
      uMessageStatisticsClassify:[],
    }
  },
  methods: {
    readStateChange2(){
      const v = this
      v.params.pageIndex = 1
      v.getList()
    },
    readStateChange(){
      const v = this
      v.params.pageIndex = 1
      v.params.sourceType = null
      v.getList()
      v.getMsgState()
    },
    cfm (type, formData) {

    },
    // 检索条件事件处理
    eventHandle (option) {
      switch (option._evnet) {
        case 'search':
          this.params.pageIndex = 1
          this.params.keyWord = option.key
          this.getList()
          break
        case 'add':
          this.formType = 'add'
          this.show = true
          break
        case 'toHome':
          this.$router.push('Home')
          break
        case 'allRead':
          let promise = []
          this.data.forEach(item => {
            promise.push(
              ReadMessageRemind({
                id: item.id
              })
            )
          })
          Promise.all(promise).then(() => {
            this.$Message.info('操作成功')
            this.$store.commit('setMsgChange', new Date().getTime())
            this.getList()
          })
          break
      }
    },
    changePage (pageIndex) {
      this.params.pageIndex = pageIndex
      this.getList()
    },
    getMsgState(){
      const v = this
      v.loading = true
      axios
        .post('/cloudApi/uMessage/statisticsClassify', v.params)
        .then((res) => {
          v.uMessageStatisticsClassify = res.data.data
          console.log(res.data.data,'1111')
        })
    },
    getNum(index) {
      let data = this.uMessageStatisticsClassify.filter(item => item.sourceType === index)
      if (data && data.length > 0) {
        return data[0].count
      }
      return 0
    },
    getList () {
      const v = this
      axios.post('/cloudApi/uMessage/list/count',v.params).then(res=>{
        console.log(res)
        v.params.total = res.data.data
      })
      axios.post('/cloudApi/uMessage/list',v.params).then(res=>{
        
        res.data.data.forEach(item => {
            item.readState = this.params.readState
            if(item.content.indexOf('请尽快确认合同') != -1) item.routerPushUrl = 'material_contract'
            if(item.content.indexOf('退货审批') != -1) item.routerPushUrl = 'return_material'
            if(item.content.indexOf('退货申请') != -1) item.routerPushUrl = 'return_material'
            if(item.content.indexOf('盘点工作') != -1) item.routerPushUrl = 'inventory_record'
            if(item.content.indexOf('出库数量超标') != -1) item.routerPushUrl = 'error_out'
            if(item.content.indexOf('出库物料错误') != -1) item.routerPushUrl = 'error_out'
            if(item.content.indexOf('信息已发生变化') != -1) item.routerPushUrl = 'material_maintenance'
            if(item.content.indexOf('驳回') != -1) item.routerPushUrl = 'schedule'
            if(item.content.indexOf('进行处理') != -1) item.routerPushUrl = 'inventory_record'
          }) 
          this.data = res.data.data
          console.log(this.data)
          // this.params.total = res.result.totalCount
      })
      // GetMessageRemindPage(this.params).then(res => {
      //   if (res.code === 0) {
          
      //     res.result.list.forEach(item => {
      //       item.readState = this.params.readState
      //       if(item.content.indexOf('请尽快确认合同') != -1) item.routerPushUrl = 'material_contract'
      //       if(item.content.indexOf('退货审批') != -1) item.routerPushUrl = 'return_material'
      //       if(item.content.indexOf('退货申请') != -1) item.routerPushUrl = 'return_material'
      //       if(item.content.indexOf('盘点工作') != -1) item.routerPushUrl = 'inventory_record'
      //       if(item.content.indexOf('出库数量超标') != -1) item.routerPushUrl = 'error_out'
      //       if(item.content.indexOf('出库物料错误') != -1) item.routerPushUrl = 'error_out'
      //       if(item.content.indexOf('信息已发生变化') != -1) item.routerPushUrl = 'material_maintenance'
      //       if(item.content.indexOf('驳回') != -1) item.routerPushUrl = 'schedule'
      //       if(item.content.indexOf('进行处理') != -1) item.routerPushUrl = 'inventory_record'
      //     }) 
      //     this.data = res.result.list
      //     console.log(this.data)
      //     this.params.total = res.result.totalCount
      //   }
      // })
    }
  },
  mounted () {
    this.getList()
    this.getMsgState()
  }
}
</script>

<style lang="less" scoped>
.container {
  height: 100%;
}
</style>