<template>
  <div class="mod-config">
    <el-form :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
      <el-form-item>
        <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="dataForm.projectJsdwId"
          placeholder="请选择建设单位"
          :clearable="true"
        >
          <el-option
            v-for="item in projectJsdwList"
            :key="item.dictId"
            :label="item.name"
            :value="item.dictId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="dataForm.projectSgddId"
          placeholder="请选择地点"
          :clearable="true"
        >
          <el-option
            v-for="item in projectSgddList"
            :key="item.dictId"
            :label="item.name"
            :value="item.dictId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select
          v-model="dataForm.projectGclbId"
          placeholder="请选择工程类别"
          :clearable="true"
        >
          <el-option
            v-for="item in projectGclbList"
            :key="item.dictId"
            :label="item.name"
            :value="item.dictId"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="dataForm.projectYear"
          type="date"
          value-format="yyyy"
          placeholder="选择年份"/>
      </el-form-item>
      <el-form-item>
        <el-select v-model="dataForm.isContant" placeholder="请选择导入合同">
          <el-option
            v-for="item in isContantList"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button @click="getDataList()">查询</el-button>
        <el-button v-if="isAuth('business:businessproject:save')" type="primary" @click="addOrUpdateHandle()">新增
        </el-button>
        <el-button v-if="isAuth('business:businessproject:delete')" type="danger" @click="deleteHandle()"
                   :disabled="dataListSelections.length <= 0">批量删除
        </el-button>
      </el-form-item>
    </el-form>
    <div style="display: flex;margin-bottom: 2%">
      <div v-for="item in totalNumList" :key="item.project_sgjd_id"
           style="padding: 1%; border: 1px solid black; border-radius: 8px;margin-left: 12%;text-align: center;width: 10%">
        <span>{{ projectSgjdDict[item.project_sgjd_id].name }}</span><br>
        <span>{{ item.numTotal }}/{{ totalNum }}</span>
      </div>
    </div>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      style="width: 100%;">
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>
      <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="projectNewName"
        header-align="center"
        align="center"
        label="立项名称">
      </el-table-column>
      <el-table-column
        prop="projectJsdwId"
        header-align="center"
        align="center"
        label="建设单位">
        <template slot-scope="scope">
          <template v-for="item in projectJsdwList" v-if="scope.row.projectJsdwId === item.dictId">
            {{ item.name }}
          </template>
        </template>
      </el-table-column>
      <el-table-column
        prop="projectSgddId"
        header-align="center"
        align="center"
        label="施工地点">
        <template slot-scope="scope">
          <template v-for="item in projectSgddList" v-if="scope.row.projectSgddId === item.dictId">
            {{ item.name }}
          </template>
        </template>
      </el-table-column>

      <el-table-column
        prop="projectSgjdId"
        header-align="center"
        align="center"
        label="施工进度">
        <template slot-scope="scope">
          <template v-for="item in projectSgjdDict" v-if="scope.row.projectSgjdId === item.dictId">
            {{ item.name }}
          </template>
        </template>
      </el-table-column>
      <el-table-column
        prop="projectPhotoConut"
        header-align="center"
        align="center"
        label="照片数量">
      </el-table-column>
      <el-table-column
        prop="isContant"
        header-align="center"
        align="center"
        label="合同">
        <template slot-scope="scope">
          <template v-if="scope.row.isContant===0">
            否
          </template>
          <template v-else>
            是
          </template>
        </template>
      </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.projectId)">修改</el-button>
          <el-button type="text" size="small" @click="deleteHandle(scope.row.projectId)">删除</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 './businessproject-add-or-update'

export default {
  data () {
    return {
      dataForm: {
        key: '',
        projectJsdwId: '',
        projectSgddId: '',
        projectGclbId: '',
        isContant: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      domain: '',
      token: '',

      projectJsdwList: [],
      projectSgddList: [],
      projectGclbList: [],
      projectSgjdDict: {},
      projectYear: '',
      isContantList: [{value: 0, label: '未录入'}, {value: 1, label: '已录入'}],
      totalNumList: [],
      totalNum: ''
    }
  },
  components: {
    AddOrUpdate
  },
  activated () {
    this.getDataList()
  },
  methods: {
    // 获取数据列表
    getDataList () {
      this.dataListLoading = true
      this.totalNumList = []
      this.$http({
        url: this.$http.adornUrl('/business/businessproject/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'gjz': this.dataForm.key,
          'projectJsdwId': this.dataForm.projectJsdwId,
          'projectSgddId': this.dataForm.projectSgddId,
          'projectGclbId': this.dataForm.projectGclbId,
          'projectYear': this.dataForm.projectYear,
          'isContant': this.dataForm.isContant
        })
      }).then(({data}) => {
        console.log(data.page.list)
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }

        // 获取统计数据
        this.$http({
          url: this.$http.adornUrl('/business/businessproject/getTotal'),
          method: 'get',
          params: this.$http.adornParams({
            'gjz': this.dataForm.key,
            'projectJsdwId': this.dataForm.projectJsdwId,
            'projectSgddId': this.dataForm.projectSgddId,
            'projectGclbId': this.dataForm.projectGclbId,
            'projectYear': this.dataForm.projectYear,
            'isContant': this.dataForm.isContant
          })
        }).then(({data}) => {
          data.page.forEach((item) => {
            console.log(item)
            if (item.project_sgjd_id === 0) {
              this.totalNum = item.numTotal
            } else {
              this.totalNumList.unshift(item)
            }
          })
        })

        this.dataListLoading = false
      })
    },
    // 每页数
    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, this.projectJsdwList, this.projectSgddList, this.projectGclbList, this.projectSgjdDict, this.domain, this.token)
      })
    },
    // 删除
    deleteHandle (id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.projectId
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/business/businessproject/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)
          }
        })
      })
    }
  },
  mounted () {
    this.token = this.$cookie.get('token')
    this.domain = this.$http.adornUrl('') + '/'
    this.$http({
      url: this.$http.adornUrl('/business/businessdict/list'),
      method: 'get'
    }).then(({data}) => {
      if (data && data.code === 0) {
        data.data.forEach((item) => {
          if (item.dictCode === 'construction_unit') {
            this.projectJsdwList = item.children
          } else if (item.dictCode === 'work_place') {
            this.projectSgddList = item.children
          } else if (item.dictCode === 'work_type') {
            this.projectGclbList = item.children
          } else if (item.dictCode === 'work_tatus') {
            item.children.forEach((obj) => {
              this.projectSgjdDict[obj.dictId] = obj
            })
          }
        })
      }
      this.dataListLoading = false
    })
  }
}
</script>
