<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item label="编写日期" prop="nowTime">
        <el-input v-model="dataForm.nowTime" placeholder="参数名" clearable></el-input>
      </el-form-item>
      <el-form-item label="时间区间">
        <span>{{ dataForm.startTime }} 至 {{ dataForm.endTime }}</span>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('business:businessplantwo:save')" type="primary" @click="addTwo()">新增</el-button>
        <el-button v-if="isAuth('business:businessplantwo:save')" type="primary" @click="addTwo()">修改</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataOldList"
      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="projectYear"
        header-align="center"
        align="center"
        label="施工年份">
      </el-table-column>
      <el-table-column
        prop="jsdw"
        header-align="center"
        align="center"
        label="建设单位">
      </el-table-column>
      <el-table-column
        prop="project"
        header-align="center"
        align="center"
        label="立项名称">
      </el-table-column>
      <el-table-column
        prop="sgdd"
        header-align="center"
        align="center"
        label="施工地点">
      </el-table-column>
      <el-table-column
        prop="sgjd"
        header-align="center"
        align="center"
        label="施工进度">
      </el-table-column>
      <el-table-column
        prop="fzgz"
        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" @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>
    <el-dialog
      :title="'更改计划'"
      :visible.sync="addShow"
      append-to-body>
      <planAddTwo ref="addPT" @refreshDataList="getDataList"></planAddTwo>
    </el-dialog>
  </div>
</template>

<script>
  import AddOrUpdate from './businessplantwo-add-or-update'
  import planAddTwo from './planaddtwo'
  export default {
    data () {
      return {
        dataForm: {
          key: '',
          nowTime: '',
          startTime: '', // 区间开始时间传输数据用
          endTime: '' // 区间结束时间传输数据用
        },
        dataList: [],
        dataOldList: [], // 初始列表数据
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        addShow: false,
        dataListSelections: [],
        addOrUpdateVisible: false
      }
    },
    components: {
      AddOrUpdate,
      planAddTwo
    },
    activated () {
      this.getDataList()
    },
    methods: {
      // 获取数据列表
      getDataList () {
        this.dataListLoading = true
        // 获取当前时间
        let nowDate = new Date()
        let date = {
          year: nowDate.getFullYear(),
          month: nowDate.getMonth() + 1,
          date: nowDate.getDate()
        }
        this.dataForm.nowTime = date.year + '-' + date.month + '-' + date.date
        // +1天
        let startNow = new Date(new Date().getTime() + 24 * 60 * 60 * 1000)
        let nowOF = new Date(new Date().getTime() + 15 * 24 * 60 * 60 * 1000)
        this.dataForm.startTime = startNow.getFullYear() + '-' + (startNow.getMonth() + 1) + '-' + startNow.getDate()
        this.dataForm.endTime = nowOF.getFullYear() + '-' + (nowOF.getMonth() + 1) + '-' + nowOF.getDate()
        this.$http({
          url: this.$http.adornUrl('/business/businessplantwo/list'),
          method: 'get',
          params: this.$http.adornParams({
            'startTime': this.dataForm.startTime,
            'endTime': this.dataForm.endTime
          })
        }).then(({data}) => {
          if (data && data.code === 0) {
            data.page.forEach((item) => {
              if (item.projectNewName !== null) {
                item.project = item.projectNewName
              }
              this.dataList.push(item)
              this.dataOldList.push(item)
            })
          } else {
            this.dataList = []
          }
          this.dataListLoading = false
        })
      },
      addTwo () {
        this.addShow = true
        this.$nextTick(() => {
          this.$refs.addPT.getDataList()
        })
      },
      // 每页数
      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/businessplantwo/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>
