<template>
  <div class="content-box">
    <!-- 添加项目 -->
    <el-dialog
      title="添加项目"
      width="90%"
      top="8vh"
      :visible.sync="userVisible"
      :before-close="handleUserClose"
    >
      <el-row :gutter="20">
        <el-col :span="16">
          <div ref="contentPanel">
            <el-form v-model="searchForm" :inline="true">
              <el-form-item label="项目编号">
                <el-input
                  size="small"
                  v-model="searchForm.projectNumber"
                  clearable
                  placeholder="请输入项目编号"
                ></el-input>
              </el-form-item>
              <el-form-item label="项目名称">
                <el-input
                  size="small"
                  v-model="searchForm.noticeTitle"
                  clearable
                  placeholder="请输入项目名称"
                ></el-input>
              </el-form-item>
              <el-form-item label="是否有效期内">
                <el-select
                  size="small"
                  clearable
                  v-model="isEffective"
                  placeholder="请选择项目是否效期内"
                >
                  <el-option
                    v-for="item in options"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  >
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button
                  size="small"
                  type="danger"
                  icon="el-icon-search"
                  @click="handleSearch"
                  >搜索</el-button
                >
                <el-button
                  size="small"
                  icon="el-icon-refresh"
                  @click="handleReset"
                  >重置</el-button
                >
              </el-form-item>
            </el-form>
          </div>
          <el-table
            ref="multipleTable"
            row-key="id"
            :data="noticeList"
            tooltip-effect="dark"
            style="width: 100%"
            size="mini"
            border
            height="380"
            @select="handleSelectionChange"
            @select-all="selectAll"
          >
            <el-table-column type="selection" width="55" align="center" />
            <el-table-column
              type="index"
              label="序号"
              width="50"
              align="center"
            />
            <el-table-column
              prop="projectNumber"
              show-overflow-tooltip
              min-width="100px"
              align="center"
              label="项目编号"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="noticeTitle"
              show-overflow-tooltip
              min-width="120px"
              align="center"
              label="项目名称"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="procureOrgName"
              show-overflow-tooltip
              min-width="130px"
              align="center"
              label="采购单位"
              resizable
            >
            </el-table-column>
            <el-table-column
              show-overflow-tooltip
              min-width="100px"
              prop="procureType"
              align="center"
              label="采购类型"
              resizable
            >
              <template v-slot="{row}">
                <div v-if="row.procureType == 6 || row.procureType == 4">
                  谈判采购
                </div>
                <div v-else>
                  {{ getDictName('cglx-cglx', row.procureType) }}
                </div>
              </template>
            </el-table-column>
            <el-table-column
              prop="quotationMethod"
              show-overflow-tooltip
              min-width="100px"
              align="center"
              label="付款方式"
              resizable
            >
              <template v-slot="{row}">
                {{ getDictName('cglx-fkfs', row.quotationMethod) }}
              </template>
            </el-table-column>
            <el-table-column
              prop="affiliatedUserName"
              min-width="100px"
              label="立项人"
              align="center"
              show-overflow-tooltip
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="contactsPhone"
              min-width="120px"
              label="立项人联系方式"
              align="center"
              show-overflow-tooltip
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="winBidValidityTime"
              min-width="120px"
              label="是否有效期内"
              align="center"
              show-overflow-tooltip
              resizable
            >
              <template slot-scope="scope">
                <div
                  v-if="
                    new Date(scope.row.winBidValidityTime) >= new Date(dateTime)
                  "
                >
                  是
                </div>
                <div v-else>否</div>
              </template>
            </el-table-column>
            <el-table-column
              prop="bond"
              show-overflow-tooltip
              min-width="100px"
              align="center"
              label="保证金(元)"
              resizable
            >
              <template slot-scope="scope">
                <div v-if="scope.row.hasBond == 1">
                  {{ scope.row.bond }}
                </div>
                <div v-else>0</div>
              </template>
            </el-table-column>
          </el-table>
          <el-pagination
            ref="paginationPanel"
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="params.pageBean.page"
            :page-sizes="[20, 30, 40, 50]"
            :page-size="params.pageBean.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="params.pageBean.total"
          >
          </el-pagination>
        </el-col>
        <el-col :span="8">
          <div style="font-size: 14px">
            已选中项目（{{ selectUsers.length }}）
          </div>
          <el-table
            :data="selectUsers"
            style="width: 100%"
            size="mini"
            border
            height="400"
          >
            <el-table-column
              prop="projectNumber"
              show-overflow-tooltip
              min-width="100px"
              align="center"
              label="项目编号"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="noticeTitle"
              show-overflow-tooltip
              min-width="120px"
              align="center"
              label="项目名称"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="address"
              label="操作"
              align="center"
              width="80"
            >
              <template slot-scope="scope">
                <el-link
                  :underline="false"
                  @click.native.prevent="deleteUser(scope.row)"
                  >移除</el-link
                >
              </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="handleUserClose">取 消</el-button>
        <el-button size="small" type="primary" @click="submitUsers"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
import utils from '@/utils.js'
import {label} from 'three/examples/jsm/nodes/Nodes.js'
export default {
  data() {
    return {
      userVisible: false, // 分配员工弹窗状态
      noticeList: [], // 所有为分配公司的员工
      selectUsers: [], // 选中的员工
      params: {
        pageBean: {
          page: 1,
          pageSize: 20,
          total: 0,
        },
        querys: [],
      },
      noticeList: [],
      searchForm: {
        releaseStatus: '2',
        winBidValidityTime:''
      },
      isEffective: 1,
      options: [
        {value: 0, label: '否'},
        {value: 1, label: '是'},
      ],
      dateTime: '',
    }
  },
  created() {
    let momnet = require('moment')
    this.dateTime = momnet().format('YYYY-MM-DD HH:mm:ss')
  },
  methods: {
    open() {
      this.userVisible = true
      this.handleSearch()
    },
    loadData() {
      this.$http
        .post('${portal}/matApproval/v1/pageCurUser', this.params)
        .then((res) => {
          if (res.data.state) {
            this.noticeList = res.data.value.rows
            this.params.pageBean.total = res.data.value.total
            this.params.pageBean.page = res.data.value.page
            this.params.pageBean.pageSize = res.data.value.pageSize
          }
        })
    },
    // 项目查询
    handleSearch() {
      let obj = this.searchForm
      let querys = []
      for (let i in obj) {
        if(i == 'winBidValidityTime'){
            if (this.isEffective == '1') {
          querys.push({
            group: 'main',
            operation: 'GREAT_EQUAL',
            parentGroup: '',
            property: 'winBidValidityTime',
            relation: 'AND',
            value: this.dateTime,
          })
        } else if (this.isEffective == '0') {
          querys.push({
            group: 'main',
            operation: 'LESS',
            parentGroup: '',
            property: 'winBidValidityTime',
            relation: 'AND',
            value: this.dateTime,
          })
        }
        }
         else {
          querys.push({
            group: 'main',
            operation:
              i == 'projectNumber' ||
              i == 'noticeTitle' ||
              i == 'matCompanyName'
                ? 'LIKE'
                : 'EQUAL',
            parentGroup: '',
            property: i,
            relation: 'AND',
            value: obj[i],
          })
        }
      }
      this.params.querys = querys
      this.loadData()
    },
    // 项目重置
    handleReset() {
      this.searchForm = {
        releaseStatus: '2',
      }
      let momnet = require('moment')
      this.searchForm.winBidValidityTime = momnet().format(
        'YYYY-MM-DD HH:mm:ss'
      )
      this.isEffective = 1
      this.params.pageBean.page = 1
      this.handleSearch()
    },
    handleUserClose() {
      // 关闭项目弹窗
      this.userVisible = false
      this.noticeList = []
      this.selectUsers = []
    },
    selectAll(arr) {
      // 是否全选
      if (arr.length !== 0) {
        // 全选
        arr.forEach((item) => {
          const bool = this.selectUsers.some((user) => user.id === item.id) // 存在返回true 否则返回false
          if (!bool) {
            // 不存在添加
            this.selectUsers.push(item)
          }
        })
      } else {
        // 取消全选
        this.noticeList.forEach((item) => {
          this.selectUsers = this.selectUsers.filter(
            (user) => user.id !== item.id
          )
        })
      }
    },
    handleSelectionChange(arr, row) {
      // 选择
      const bool = this.selectUsers.some((user) => user.id === row.id) // 存在返回true 否则返回false
      if (bool) {
        // 存在删除
        this.selectUsers = this.selectUsers.filter((user) => user.id !== row.id)
      } else {
        // 添加
        this.selectUsers.push(row)
      }
    },
    deleteUser(node) {
      // 移除
      this.selectUsers = this.selectUsers.filter((user) => user.id !== node.id)
      this.noticeList.forEach((item) => {
        if (node.id === item.id) {
          // 存在添加
          this.$refs.multipleTable.toggleRowSelection(item, false)
        }
      })
    },
    handleCurrentChange(page) {
      this.params.pageBean = {...this.params.pageBean, page}
      this.handleSearch()
    },
    handleSizeChange(pageSize) {
      this.params.pageBean = {...this.params.pageBean, pageSize}
      this.handleSearch()
    },
    echoSelected() {
      // 回显选中
      if (this.selectUsers.length > 0) {
        this.$nextTick(() => {
          this.noticeList.forEach((item) => {
            if (this.selectUsers.some((user) => user.id === item.id)) {
              // 存在添加
              this.$refs.multipleTable.toggleRowSelection(item)
            }
          })
        })
      }
    },
    submitUsers() {
      // 确认添加项目
      if (this.selectUsers.length > 0) {
        this.$emit('load',this.selectUsers)
        this.handleUserClose()
      } else {
        this.$message.error('请选择项目')
      }
    },
    getDictName(code, value) {
      return utils.getDictName(code, value)
    },
  },
}
</script>
  