<template>
  <div class="common-page-shadow">
    <div class="common-search-container" ref="contentPanel">
      <el-form
        ref="form"
        :model="searchForm"
        label-width="auto"
        label-position="right "
        :inline="true"
        type="flex"
        justify="left"
      >
        <el-form-item label="项目编号">
          <el-input
            size="small"
            v-model="searchForm.code"
            clearable
            placeholder="请输入项目编号"
          ></el-input>
        </el-form-item>
        <el-form-item label="发布状态">
          <dict-select
            dictCode="cjjgzt"
            :hasDefaultOption="true"
            size="small"
            style="width:100%"
            v-model="searchForm.status"
            placeholder="请选择发布状态"
          />
        </el-form-item>
      </el-form>
      <div class="container-btn">
        <el-button
          size="small"
          icon="el-icon-search"
          type="danger"
          class="btn btn-plus"
          @click="handleSearch"
        >
          搜索
        </el-button>
        <el-button
          size="small"
          icon="el-icon-refresh-right"
          class="btn btn-plus"
          @click="clearSearchForm"
        >
          重置
        </el-button>
      </div>
    </div>
    <el-row
      class="common-table-container flex searchPanel"
      :style="SearchPanel"
    >
      <el-col :span="24" class="common-table-wrapper">
        <div class="common-btn-group">
          <el-button
            size="small"
            icon="el-icon-plus"
            type="primary"
            class="btn btn-plus"
            @click="handleAdd"
          >
            新增单一来源公示
          </el-button>
          <el-button
            size="small"
            icon="el-icon-position"
            type="primary"
            v-if="searchForm.status==3"
            class="btn btn-plus"
            @click="handleRelease"
          >
            发布
          </el-button>
          <el-button
            size="small"
            slot="reference"
            icon="el-icon-delete"
            type="danger"
            v-if="searchForm.status==3"
            class="btn btn-delete"
            @click="handleDelete"
          >
            {{ this.$t('common.delete') }}
          </el-button>
        </div>
        <!-- LJ：表格高度自适应需配合样式  btn-pagenation 有按钮有分页  btngroup只有按钮  pagenation只有分页 -->
        <div class="common-table-content btn-pagenation">
          <el-table
            :border="true"
            stripe
            fit
            size="mini"
            v-loading="tableLoading"
            ref="multipleSelection"
            :data="tableData"
            @select="selectChange"
            @select-all="selectAll"
            @selection-change="handleSelectionChange"
          >
            <!-- @row-dblclick="handleDetail" -->
            <el-table-column type="selection" min-width="5%" align="center">
            </el-table-column>
            <el-table-column
              prop="noticeName"
              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
              prop="procureContact"
              show-overflow-tooltip
              min-width="120px"
              align="center"
              label="采购企业联系人"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="procurePhone"
              show-overflow-tooltip
              min-width="110px"
              align="center"
              label="采购企业联系方式"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="procureAddress"
              show-overflow-tooltip
              min-width="120px"
              align="center"
              label="采购企业联系地址"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="vendorName"
              show-overflow-tooltip
              min-width="120px"
              align="center"
              label="供应商名称"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="vendorAddress"
              show-overflow-tooltip
              min-width="120px"
              align="center"
              label="供应商联系地址"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="validityDate"
              show-overflow-tooltip
              min-width="120px"
              align="center"
              label="公示有效期至"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="status"
              show-overflow-tooltip
              min-width="100px"
              align="center"
              label="发布状态"
              resizable
            >
            <template slot-scope="scope">
              <el-tag v-if="scope.row.status==1" type="success">已发布</el-tag>
              <el-tag v-if="scope.row.status==3" type="danger">未发布</el-tag>
              <el-tag v-if="scope.row.status==2" type="">审核中</el-tag>
            </template>
            </el-table-column>
            <el-table-column min-width="180px" align="center" label="操作">
              <template v-slot="{row}">
                  <el-button
                    icon="el-icon-Edit"
                    type="primary"
                    plain
                    size="small"
                    v-if="row.status==3"
                    @click="handleEdit(row)"
                  >
                    编辑
                  </el-button>
                  <el-button
                    icon="el-icon-view"
                    type="primary"
                    plain
                    size="small"
                    @click="handleView(row)"
                  >
                    查看明细
                  </el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="common-pagenation">
          <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>
        </div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import {mapState} from 'vuex'
import pagination from '@/components/pagination'
import {TableMixin} from '@/mixins/tableMixin'
import routerManagement from '@/api/BasicDataManagement/routerManagement.js'
import req from '@/request.js'
import utils from '@/utils.js'
const portal = window.context.portal
export default {
  name: 'DemandEntry',
  components: {
    pagination
  },
  mixins: [TableMixin],
  data() {
    return {
      tableMaxHeight: 400,
      loading: false,
      dialogVisible: false,
      tableLoading: false,
      tableData: [],
      multipleSelection: [],
      params: {
        pageBean: {
          page: 1,
          pageSize: 20,
          total: 0
        }
      },
      searchForm: {
        status:3,
        procureType:5,
        procureOrgId:'',
      }, // 顶部搜索对象
      selectArr: [],
      currentUser:{}
    }
  },
  mounted() {},
  computed: {
    ...mapState({
      headers: state => {
        return {Authorization: `Bearer ${state.login.currentUser.token}`}
      },
      action: function() {
        return window.context.portal + '/biz/universal/v1/minioUpload'
      }
    })
  },
  created() {
    this.currentUser = JSON.parse(sessionStorage.getItem('currentUserDetail'))
    this.searchForm.procureOrgId = this.currentUser.org[0].orgId
    this.handleSearch()
  },
  methods: {
    handleAdd(){
      this.$router.push({
        name:'transactionPublicity'
      })
    },
    // 发布
    handleRelease() {
      let selection = this.$refs.multipleSelection.selection
      this.dialogVisible = true
        if (selection.length !== 0) {
          this.$confirm('确定要发布吗?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
          }).then(() => {
            this.$http.post('${portal}/bizWinBiddingNotice/v1/updateStatusById?id='+selection[0].id+'&status=1').then(res=>{
              if(res.data.state){
                this.$message.success(res.data.message)
                this.handleSearch()
              }
            })
          })
        } else {
          this.$message.info('请选择要发布的数据！')
        }
    },
    // 删除
    handleDelete() {
      let ids = this.currentDataDel.map(item=>{
        return item.id
      })
      this.$confirm(this.$t('common.sureDelete'), this.$t('common.tips'), {
        confirmButtonText: this.$t('common.confirm'),
        cancelButtonText: this.$t('common.cancel'),
        type: 'warning',
      }).then(() => {
        this.$http.post('${portal}/bizWinBiddingNotice/v1/removeIdList',ids).then(res=>{
          if(res.data.state){
            this.$message.success(res.data.message)
            this.handleSearch()
          }
        })
      })
    },
    clearSearchForm(){
      this.searchForm={
        status:3,
        procureType:5,
        procureOrgId : this.currentUser.org[0].orgId
      }
      this.handleSearch()
    },
    // 导出数据
    handleDownload() {
      let selection = this.$refs.multipleSelection.selection
      if (selection.length !== 0) {
        this.$confirm('确定要导出选中数据吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$message.success('导出数据成功！')
        })
      } else {
        this.$message.info('请选择要导出的数据！')
      }
    },
    getDictName(code, value) {
      return utils.getDictName(code, value)
    },
    //获取数据
    loadData(params) {
      this.tableLoading = true
      this.$http.post('${portal}/bizWinBiddingNotice/v1/pageAll',params).then(res=>{
        console.log(res, 'res')
        this.tableLoading = false
        const data = (res && res.data) || {}
        this.tableData = data.value.rows || []
        console.log('返回的表格数据', this.tableData)
        this.pageResult = {
          page: data.value.page,
          pageSize: data.value.pageSize,
          total: data.value.total
        }
      })
    },
    dialogBeforeClose() {
      this.dialogVisible = false
    },
    //添加
    handleView(row) {
      this.$router.push({
        name:'announcementWinningBidView',
        query:{id:row.id}
      })
    },
    handleEdit(row){
      this.$router.push({
        name:'transactionPublicity',
        query:{id:row.id}
      })
    },
    // 顶部搜索
    handleSearch() {
      let obj = this.searchForm
      console.log(obj)
      let querys = []
      for (let i in obj) {
        if (obj[i]) {
          querys.push({
            group: 'main',
            operation:
              i == 'name' || i == 'code' || i == 'companyName'
                ? 'LIKE'
                : 'EQUAL',
            parentGroup: '',
            property: i,
            relation: 'AND',
            value: obj[i]
          })
        }
      }
      this.params.querys = querys
      this.loadData(this.params)
    },
    handleSelectionChange(currentData) {
      this.currentDataDel = currentData
      if (currentData.length === 0) return (this.deleteIds = '')
      let ids = []
    },
    selectChange(selection, row) {
      if (selection.length > 1) {
        const del_row = selection.shift()
        this.$refs.multipleSelection.toggleRowSelection(del_row, false)
      }
    },
    selectAll(selection) {
      if (selection.length > 1) {
        selection.length = 1
      }
    },
    handleCurrentChange(page) {
      this.params.pageBean = {...this.params.pageBean, page}
      this.loadData(this.params)
    },
    handleSizeChange(pageSize) {
      this.params.pageBean = {...this.params.pageBean, pageSize}
      this.loadData(this.params)
    }
  },
  computed: {
    SearchPanel() {
      return {
        '--searchPanel': this.searchPanel + 'px'
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.cloumn-btn {
  border: 1px solid #2761ff;
  padding: 3px 10px;
  border-radius: 5px;
  &:nth-child(1) {
    margin-right: 5px;
  }
  &:nth-child(2) {
    margin-right: 5px;
  }
  &:nth-child(1):hover {
    background-color: #2761ff;
    color: #fff;
  }
  &:nth-child(2):hover {
    background-color: #2761ff;
    color: #fff;
  }
  /deep/ i,
  span {
    font-size: 16px;
  }
}

/deep/ .el-tree-node__content {
  padding: 5px 0;
}
/deep/.el-dialog .el-button--primary{
    width: 100px;
}
/deep/ .el-table thead tr th {
  background-color: #eee !important;
  border-right: 1px solid #dfe2e8 !important;
}
/deep/ .el-table {
  border-collapse: collapse !important;
}
</style>
