<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="end">
        <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:businessoutlay: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="outTime"
        header-align="center"
        align="center"
        label="日期">
      </el-table-column>
      <el-table-column
        prop="outMoney"
        header-align="center"
        align="center"
        label="支出金额">
      </el-table-column>
      <el-table-column
        prop="outNr"
        header-align="center"
        align="center"
        label="内容">
      </el-table-column>
      <el-table-column
        prop="outWay"
        header-align="center"
        align="center"
        label="支付方式">
        <template slot-scope="scope">
          <template v-for="item in payList" v-if="item.dictId === scope.row.outWay">
            {{ 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="outProject"
        header-align="center"
        align="center"
        label="立项归属">
        <template slot-scope="scope">
          <template v-for="item in projectList" v-if="item.projectId === scope.row.outProject">
            <span v-if="item.projectNewName !== null">{{ item.projectNewName }}</span>
            <span v-else>{{ item.project }}</span>
          </template>
        </template>
      </el-table-column>
      <el-table-column
        v-if="isShow"
        prop="outPayeeStr"
        header-align="center"
        align="center"
        label="收款人">
      </el-table-column>
<!--      <el-table-column-->
<!--        prop="outPayeeAddress"-->
<!--        header-align="center"-->
<!--        align="center"-->
<!--        label="地址">-->
<!--      </el-table-column>-->
<!--      <el-table-column-->
<!--        prop="outPayeePhone"-->
<!--        header-align="center"-->
<!--        align="center"-->
<!--        label="电话">-->
<!--      </el-table-column>-->
<!--      <el-table-column-->
<!--        prop="outRemark"-->
<!--        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>
  import AddOrUpdate from './businessoutlay-add-or-update'
  export default {
    data () {
      return {
        dataForm: {
          key: '',
          start: ''
        },
        dataList: [],
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        projectList: [], // 立项信息
        payList: [], // 支付方式
        recipientList: [], // 收款人
        expenseList: [], // 支出类别
        addOrUpdateVisible: false,
        userId: '',
        authenticName: '',
        sysYear: '',
        isShow: false
      }
    },
    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
          if (data.user.userId === 10 || data.user.userId === 1) {
            this.isShow = true
          }
        }
      })
      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
            } else if (item.dictCode === 'recipient') {
              this.recipientList = item.children
            } else if (item.dictCode === 'expense_type') {
              this.expenseList = item.children
            }
          })
        }
      })
      // 获取立项数据
      await this.$http.request({
        url: this.$http.adornUrl('/business/businessoutlay/allList'),
        method: 'get'
      }).then(({data}) => {
        // console.log(data);
        if (data && data.code === 0) {
          this.projectList = data.page
          // console.log(this.projectList);
          this.projectList.forEach((item) => {
            if (item.projectNewName !== null) {
              item.project = item.projectNewName
            }
          })
        }
      })

      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        this.dataList = []
        this.$http({
          url: this.$http.adornUrl('/business/businessoutlay/pcListNoPage'),
          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
            this.recipientList.forEach((item) => {
              if (item.dictId.toString() === item.outPayee) {
                item.outPayee = item.name
              }
            })
          } else {
            data.page.list.forEach((item) => {
              if (item.writeUserId === this.userId) { // 谁登陆查找谁的信息
                this.recipientList.forEach((item) => {
                  if (item.dictId.toString() === item.outPayee) {
                    item.outPayee = item.name
                  }
                })
                this.dataList.push(item)
              }
            })
          }
        })
        this.dataListLoading = false
      },
      onExport () {
        if (this.authenticName === '管理员') {
          window.open(this.$http.adornUrl('/business/businessoutlay/export'))
        } else {
          window.open(this.$http.adornUrl('/business/businessoutlay/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/businessoutlay/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>
