<template>
  <div class="mod-config">
    <div style="text-align: center;padding: 1%;font-size: 18px;font-weight: bold">{{ sysYear }}收支总结表</div>
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item label="开始时间" prop="start">
        <el-date-picker
          v-model="dataForm.start"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="start">
        <el-date-picker
          v-model="dataForm.end"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button @click="onExport()">导出</el-button>
        <el-button v-if="isAuth('business:businessoutlayincome:save')" type="primary" @click="addOrUpdateHandle()">新增</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="index"
        header-align="center"
        align="center"
        label="序号"
        width="50">
      </el-table-column>
      <el-table-column
        prop="outinDate"
        header-align="center"
        align="center"
        label="日期">
      </el-table-column>
      <el-table-column
        prop="outinYdayBalance"
        header-align="center"
        align="center"
        label="昨日余额">
      </el-table-column>
      <el-table-column
        prop="outinTdayIncome"
        header-align="center"
        align="center"
        label="今日收入">
      </el-table-column>
      <el-table-column
        prop="outinTdayOut"
        header-align="center"
        align="center"
        label="今日支出">
      </el-table-column>
      <el-table-column
        prop="outinTdayBalance"
        header-align="center"
        align="center"
        label="今日余额">
      </el-table-column>
      <el-table-column
        prop="outinCreditingWay"
        header-align="center"
        align="center"
        label="收款方式">
        <template slot-scope="scope">
          <template v-for="item in payList" v-if="item.dictId === scope.row.outinCreditingWay">
            {{ item.name }}
          </template>
        </template>
      </el-table-column>
      <el-table-column
        prop="outType"
        header-align="center"
        align="center"
        label="收入类型">
        <template slot-scope="scope">
          <template v-for="item in expenseList" v-if="item.dictId === scope.row.outType">
            {{ item.name }}
          </template>
        </template>
      </el-table-column>
      <el-table-column
        prop="outinRemark"
        header-align="center"
        align="center"
        label="备注">
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" size="small" v-if="scope.row.approval != 2" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
  </div>
</template>

<script>
  /* eslint-disable spaced-comment */

  import AddOrUpdate from './businessoutlayincome-add-or-update'
  export default {
    data () {
      return {
        dataForm: {
          key: '',
          start: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        payList: [], // 收款方式
        expenseList: [], // 收入类别
        addOrUpdateVisible: false,
        userId: '',
        authenticName: '',
        sysYear: ''
      }
    },
    components: {
      AddOrUpdate
    },
    async mounted () {
      if (new Date().getMonth() + 1 === 1 || new Date().getMonth() + 1 === 2) {
        this.sysYear = new Date().getFullYear() - 1
      } else {
        this.sysYear = new Date().getFullYear()
      }
      await this.$http.request({
        url: this.$http.adornUrl('/sys/user/info'),
        method: 'get'
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.userId = data.user.userId
          this.authenticName = data.user.authenticName
        }
      })
      await this.$http.request({
        url: this.$http.adornUrl('/business/businessoutlay/dictList'),
        method: 'get'
      }).then(({data}) => {
        // console.log(data);
        if (data && data.code === 0) {
          data.data.forEach((item) => {
            if (item.dictCode === 'pay_type') {
              this.payList = item.children
            }
            if (item.dictCode === 'expense_type') {
              this.expenseList = item.children
            }
          })
        }
      })

      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        // eslint-disable-next-line eqeqeq
        if (!(this.authenticName == '管理员' || this.authenticName == '侯德生' || this.authenticName == '侯吉庆' || this.authenticName == '李现举' || this.authenticName === '侯吉光' || this.authenticName === '肖爽')) {
          return
        }

        this.dataListLoading = true

        // 获取系统用户信息
        /*this.$http.request({
          url: this.$http.adornUrl('/sys/user/listNoPage'),
          method: 'get'
        }).then(({data}) => {
          // console.log(data);
          if (data && data.code === 0) {
            data.page.forEach((item) => {
              if (item.authenticName == '侯德生' || item.authenticName == '侯吉庆' || item.authenticName == '李现举' || item.authenticName === '侯吉光') {
                this.userId = item.userId
                this.authenticName = item.authenticName
              }
            })
          }
        })*/
        this.dataList = []
        this.$http({
          url: this.$http.adornUrl('/business/businessoutlayincome/pcListPage'),
          method: 'get',
          params: this.$http.adornParams({
            'start': this.dataForm.start,
            'end': this.dataForm.end,
            'writeUserId': this.userId,
            'page': this.pageIndex,
            'limit': this.pageSize
          })
        }).then(({data}) => {
          this.totalPage = data.page.totalCount
          if (this.authenticName === '管理员') {
            this.dataList = data.page.list
          } else {
            data.page.list.forEach((item) => {
              if (item.writeUserId === this.userId) { // 谁登陆查找谁的信息
                this.dataList.push(item)
              }
            })
          }
        })
        this.dataListLoading = false
      },
      onExport () {
        if (this.authenticName === '管理员') {
          window.open(this.$http.adornUrl('/business/businessoutlayincome/export'))
        } else {
          window.open(this.$http.adornUrl('/business/businessoutlayincome/export?writeUserId=' + this.userId))
        }
      },
      // 每页数
      sizeChangeHandle (val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle (val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle (val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle (id) {
        this.addOrUpdateVisible = true
        this.$nextTick(() => {
          this.$refs.addOrUpdate.init(id)
        })
      },
      // 删除
      deleteHandle (id) {
        var ids = id ? [id] : this.dataListSelections.map(item => {
          return item.id
        })
        this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/business/businessoutlayincome/delete'),
            method: 'post',
            data: this.$http.adornData(ids, false)
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.getDataList()
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      }
    }
  }
</script>
