<!--
  @description: 选择建设项目
  @Modifier: haoFx/肖文龙
  @routerPath:
  @date: 2019-12-20 09:36:14
-->
<template>
  <el-dialog
    v-dialogDrag
    :visible.sync="dialogData.buildingProjectVisible"
    :close-on-click-modal="false"
    :title="title+'选择'"
    width="840px"
    custom-class="el-dialog__body-p8 el-dialog_table"
  >
    <!--搜索-->
    <section class="clearfix">
      <el-form ref="searchForm" :model="searchForm" inline>
        <el-form-item :label="title+'编号'" prop="projectNo">
          <el-input v-model.trim="searchForm.projectNo" :placeholder="title+'编号'" @keyup.enter.native="search" />
        </el-form-item>
        <el-form-item :label="title+'名称'" prop="projectName">
          <el-input v-model.trim="searchForm.projectName" :placeholder="title+'名称'" @keyup.enter.native="search" />
        </el-form-item>
        <el-form-item class="fr">
          <el-button
            type="primary"
            @click="search('searchForm')"
          >
            搜索
          </el-button>
          <el-button
            plain
            type="info"
            @click="resetForm('searchForm')"
          >
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </section>
    <!--查询列表-->
    <div>
      <el-table
        ref="singleTable"
        v-loading="loading"
        :data="list"
        class="pointer"
        highlight-current-row
        size="mini"
        @current-change="currentChange"
      >
        <el-table-column type="index" label="序号" align="center" width="80" />
        <el-table-column :label="title+'编号'" prop="projectNo" align="center" width="200" />
        <el-table-column :label="title+'名称'" prop="projectName" />
      </el-table>
      <!--分页组件-->
      <pagination :pagination-data="paginationData" @change="loadData" />
    </div>
    <!--底部操作按钮-->
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="ensure">确 定</el-button>
      <el-button @click="dialogData.buildingProjectVisible = false">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
/* 接口*/
import { getBuildingProjectByPage } from '@/api/purchasePlan/taskplan'// 委托合同选择
/* 公用方法*/
import { filterObj } from '@/assets/js/utils'// 过滤空的key
/* 混合开发方法-参数字段*/
import { ListMixin } from '@/mixins/ListMixin'// 分页
/* 组件*/
import pagination from '@comp/common/pagination/pagination'
// 分页
export default {
  name: 'BuildingProjectSelectDialog',
  components: {
    pagination// 分页
  },
  mixins: [ListMixin],
  props: {
    // 弹框展示状态
    dialogData: {
      type: Object,
      default: function() {

      }
    }
  },
  data() {
    return {
      loading: true,
      title: '建设项目', // 标题
      // 搜索信息
      searchForm: {
        projectNo: '', // 编号
        projectName: ''// 名称
      },
      list: [], // 列表数据
      currentData: {}// 当前选择的信息
    }
  },
  watch: {
    // 监听-弹框展示状态
    'dialogData.buildingProjectVisible'(val) {
      if (val) {
        // 弹框已打开
        this.searchForm.projectNo = ''
        this.searchForm.projectName = ''
        // 加载数据
        this.loadData()
      }
    }
  },
  methods: {
    // 当前选择的信息
    currentChange(val) {
      this.currentData = val
    },
    // 加载数据
    loadData() {
      const params = this.getParams()// 查询条件
      getBuildingProjectByPage(params).then(response => {
        this.loading = false
        this.list = response.data.records
        this.paginationData.total = response.data.total
      }).catch(() => {
        this.loading = false
      })
    },
    // 查询条件
    getParams() {
      const status = {
        useStatus: '01', // 有效建设项目状态
        auditStatus: '01' // 审核状态
      }
      const param = Object.assign({}, this.searchForm, status)
      param.pageNo = this.paginationData.pageNo
      param.pageSize = this.paginationData.pageSize
      return filterObj(param)// 过滤空的key
    },
    // 选择信息-确定
    ensure() {
      if (this.currentData) {
        const data = {
          name: this.currentData.projectName,
          code: this.currentData.projectNo,
          id: this.currentData.id
        }
        this.$emit('buildingProjectInfo', data)
        this.dialogData.buildingProjectVisible = false
      } else {
        this.$message({
          message: `请选择${this.title}`,
          type: 'warning'
        })
      }
    },
    // 搜索重置
    resetForm(formName) {
      this.paginationData.pageNo = 1
      this.paginationData.pageSize = 10
      this.$refs[formName].resetFields()
      this.loadData()
    },
    // 搜索
    search() {
      this.paginationData.pageNo = 1
      this.paginationData.pageSize = 10
      this.loadData()
    }
  }
}
</script>

<style scoped lang="less">

</style>
