<template>
  <a-card size="small" :bordered="false" class="customerTypeList-wrap">
    <a-spin :spinning="spinning" tip="Loading...">
      <!-- 搜索条件 -->
      <div ref="tableSearch" class="table-page-search-wrapper"></div>
      <!-- 列表 -->
      <a-table
        class="sTable fixPagination"
        ref="table"
        :style="{ height: tableHeight+84.5+'px' }"
        size="small"
        :rowKey="(record) => record.id"
        :columns="columns"
        :data-source="loadData"
        :scroll="{ y: tableHeight }"
        bordered>
        <!-- 操作 -->
        <template slot="action1" slot-scope="text, record, index">
          <a-checkbox :id="'ac1-'+record.id" :checked="text==1" @change="onChange(0,record)"></a-checkbox>
        </template>
        <template slot="action2" slot-scope="text, record, index">
          <a-checkbox :id="'ac2-'+record.id" :checked="text==1" @change="onChange(1,record)"></a-checkbox>
        </template>
        <template slot="action3" slot-scope="text, record, index">
          <a-checkbox :id="'ac3-'+record.id" :checked="text==1" @change="onChange(2,record)"></a-checkbox>
        </template>
        <template slot="action4" slot-scope="text, record, index">
          <a-checkbox :id="'ac4-'+record.id" :checked="text==1" @change="onChange(3,record)"></a-checkbox>
        </template>
        <template slot="action5" slot-scope="text, record, index">
          <a-checkbox :id="'ac5-'+record.id" :checked="text==1" @change="onChange(4,record)"></a-checkbox>
        </template>
      </a-table>
    </a-spin>
  </a-card>
</template>

<script>
import { commonMixin } from '@/utils/mixin'
import { noticeUserBizQuery, noticeUserBizSave } from '@/api/noticeUser'
import { STable, VSelect } from '@/components'
export default {
  name: 'NotificationManageList',
  components: { STable, VSelect },
  mixins: [commonMixin],
  data () {
    return {
      spinning: false,
      tableHeight: 0,
      disabled: false, //  查询、重置按钮是否可操作
      columns: [
        { title: '序号', dataIndex: 'no', width: '5%', align: 'center' },
        { title: '用户名称', dataIndex: 'userName', width: '45%', align: 'center', customRender: function (text) { return text || '--' }, ellipsis: true },
        { title: '急送提醒', dataIndex: 'tempOrderFlag', scopedSlots: { customRender: 'action1' }, width: '10%', align: 'center' },
        { title: '补货提醒', dataIndex: 'shelfReplenishFlag', scopedSlots: { customRender: 'action2' }, width: '10%', align: 'center' },
        { title: '订单提醒', dataIndex: 'shelfOrderFlag', scopedSlots: { customRender: 'action3' }, width: '10%', align: 'center' },
        { title: '货架异常提醒', dataIndex: 'shelfWarnFlag', scopedSlots: { customRender: 'action4' }, width: '10%', align: 'center' },
        { title: '全选', dataIndex: 'allFlag', scopedSlots: { customRender: 'action5' }, width: '10%', align: 'center' }
      ],
      loadData: [], // 列表数据
      openModal: false, //  新增编辑客户类型  弹框
      itemId: '', //  当前id
      nowData: null //  当前记录数据
    }
  },
  methods: {
    // 选择
    onChange (index, record) {
      const key = ['tempOrderFlag', 'shelfReplenishFlag', 'shelfOrderFlag', 'shelfWarnFlag', 'allFlag']
      record[key[index]] = record[key[index]] ? 0 : 1
      // 全选
      if (index == 4) {
        const flag = record[key[index]]
        key.map(item => {
          record[item] = flag ? 1 : 0
        })
      } else {
        const ret = []
        key.map(item => {
          if (item != 'allFlag') {
            ret.push(record[item])
          }
        })
        record['allFlag'] = ret.filter(item => item == 1).length == 4 ? 1 : 0
      }
      // 保存数据
      const noticeTypeList = []
      const keys = ['temp_order', 'shelf_replenish', 'shelf_order', 'shelf_warn']
      key.map((item, i) => {
        if (item != 'allFlag' && record[item] == 1) {
          noticeTypeList.push(keys[i])
        }
      })
      this.spinning = true
      noticeUserBizSave({
        userSn: record.userSn,
        noticeTypeList: noticeTypeList
      }).then(res => {
        if (res.status == 200) {
          this.$message.success(res.message)
        }
        this.spinning = false
        this.getData()
      })
    },
    // 获取列表数据
    getData () {
      this.spinning = true
      noticeUserBizQuery().then(res => {
        const data = res.data
        if (res.status == 200) {
          const no = 0
          for (var i = 0; i < data.length; i++) {
            data[i].no = no + i + 1
            const key = ['tempOrderFlag', 'shelfReplenishFlag', 'shelfOrderFlag', 'shelfWarnFlag', 'allFlag']
            const ret = []
            key.map(item => {
              if (data[i][item] == 1) {
                ret.push(1)
              }
            })
            data[i].allFlag = ret.length == 4 ? 1 : 0
          }
        }
        this.loadData = data
        this.spinning = false
      })
    },
    pageInit () {
      const _this = this
      this.$nextTick(() => { // 页面渲染完成后的回调
        _this.setTableH()
      })
      this.getData()
    },
    setTableH () {
      const tableSearchH = this.$refs.tableSearch.offsetHeight
      this.tableHeight = window.innerHeight - tableSearchH - 200
    }
  },
  watch: {
    '$store.state.app.winHeight' (newValue, oldValue) { //  窗口变更时，需同时更改表格高度
      this.setTableH()
    }
  },
  mounted () {
    if (!this.$store.state.app.isNewTab) { // 页签刷新时调用
      this.pageInit()
    }
  },
  activated () {
    // 如果是新页签打开，则重置当前页面
    if (this.$store.state.app.isNewTab) {
      this.pageInit()
    }
  }
}
</script>
