<!--
  @description: 项目信息-选择招标人/代理机构-弹框
  @Modifier: haoFx
  @routerPath:
  @date: 2019/10/26-13:27
-->
<template>
  <el-dialog
    v-dialogDrag
    :visible.sync="organizationDialog"
    :close-on-click-modal="false"
    :title="title+'选择'"
    width="840px"
    append-to-body
    custom-class="el-dialog__body-p8 el-dialog_table"
  >
    <div class="clearfix">
      <el-form ref="searchForm" :model="searchForm" inline>
        <el-form-item :label="title+'编码'" prop="orgCode">
          <el-input
            v-model.trim="searchForm.orgCode"
            :placeholder="title+'编码'"
            @keyup.enter.native="search"
          />
        </el-form-item>
        <el-form-item :label="title+'名称'" prop="departName">
          <el-input
            v-model.trim="searchForm.departName"
            :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>
    </div>
    <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="orgCode" align="center" width="200" />
        <el-table-column :label="title+'名称'" prop="departName" />
      </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="organizationDialog = false">取 消</el-button>
    </span>
  </el-dialog>
</template>

<script>
/* 接口*/
import { getDepartListByDepartRoleName } from '@/api/common/user'
/* 公用方法*/
import { filterObj } from '@/assets/js/utils'// 过滤空的key
/* 混合开发方法-参数字段*/
import { ListMixin } from '@/mixins/ListMixin'// 分页
/* 组件*/
import pagination from '@comp/common/pagination/pagination'// 分页
export default {
  name: 'BidProjectDepart',
  components: {
    pagination// 分页
  },
  mixins: [ListMixin],
  props: {
    // 表格列表标题
    titleName: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      loading: true,
      organizationDialog: false, // 弹框打开状态
      buyersIndex: '', // 多招标人当前选择索引
      moreTenderType: '', // 是否多招标人
      title: '', // 标题
      // 搜索信息
      searchForm: {
        departRoleName: '', // 10采购人 11代理机构(项目经理)
        orgCode: '',
        departName: ''
      },
      isAgent: false,
      list: [], // 列表数据
      currentData: {}// 当前选择的信息
    }
  },
  methods: {
    // 当前选择的信息
    currentChange(val) {
      this.currentData = val
    },
    // 打开弹框
    openOrganizationDialog(departRoleName, buyersIndex, type) {
      this.searchForm.orgCode = '' // 打开弹框清空
      this.searchForm.departName = '' // 打开弹框清空
      this.isAgent = ''
      // this.searchForm.departRoleName = departRoleName // 10采购人 11代理机构
      this.departRoleName = departRoleName
      /* =============多招标人添加===============*/
      this.buyersIndex = buyersIndex // 多招标人赋值索引
      this.moreTenderType = type // 是否多招标人
      /* ================多招标人添加=============*/
      // 加载数据
      if (departRoleName === '10' && this.titleName) {
        this.title = this.titleName
      } else if (departRoleName === '10') {
        this.title = '招标人'
      } else if (departRoleName === '11') {
        this.title = '代理机构'
        this.isAgent = true
      }
      this.searchForm.departRoleName = departRoleName

      this.loadData()
      this.organizationDialog = true // 打开弹框
    },
    // 加载数据
    loadData() {
      const params = this.getParams()// 查询条件
      getDepartListByDepartRoleName(params).then(response => {
        this.loading = false
        this.list = response.data.results
        this.paginationData.total = response.data.total
      }).catch(() => {
        this.loading = false
      })
    },
    // 查询条件
    getParams() {
      const param = Object.assign({}, this.searchForm)
      param.pageNo = this.paginationData.pageNo
      param.pageSize = this.paginationData.pageSize
      param.departRoleName = '10,11'
      return filterObj(param)// 过滤空的key
    },
    // 选择信息-确定
    ensure() {
      if (this.currentData) {
        // 如果有下标为多招标人选择信息 否则为主招标人或代理机构信息（触发父组件方法不同）
        if (this.moreTenderType) {
          this.$emit('dialogDataBuyersInfo', this.currentData, this.buyersIndex)
        } else {
          if (this.isAgent) {
            this.searchForm.departRoleName = '11'
          }
          this.$emit('dialogDataInfo', this.currentData, this.searchForm.departRoleName)
        }
        this.organizationDialog = 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>
