<template>
  <div class="common-page-content">
    <div class="common-search-container" ref="contentPanel">
      <el-form
        ref="form"
        label-position="right "
        :inline="true"
        type="flex"
        justify="left"
      >
        <el-form-item label="公告编号">
          <el-input
            size="small"
            v-model="querys[0].value"
            clearable
            placeholder="请输入公告编号"
          ></el-input>
        </el-form-item>
        <el-form-item label="公告标题">
          <el-input
            size="small"
            v-model="querys[1].value"
            clearable
            placeholder="请输入公告标题"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-select
            style="width: 100%"
            clearable
            v-model="querys[2].value"
            size="small"
          >
            <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>
      <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
            :disabled="handleDeleteActive"
            size="small"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete"
          >
            删除
          </el-button>
          <el-button
            :disabled="handleDeleteActive"
            size="small"
            icon="el-icon-plus"
            type="primary"
            class="btn btn-plus"
            @click="publishBtn"
          >
            发布
          </el-button>
          <el-button
            :disabled="handleDeleteActive"
            size="small"
            icon="el-icon-minus"
            v-if="querys[2].value!=0"
            type="warning"
            class="btn btn-plus"
            @click="shelvesTaken"
          >
            下架
          </el-button>
        </div>
        <!-- LJ：表格高度自适应需配合样式  btn-pagenation 有按钮有分页  btngroup只有按钮  pagenation只有分页 -->
        <div class="common-table-content btn-pagenation" ref="tableContent">
          <el-table
            :border="true"
            stripe
            fit
            size="mini"
            v-loading="tableLoading"
            ref="multipleSelection"
            :data="tableData"
            @selection-change="handleSelectionChange"
            @row-dblclick="handleDetail"
          >
            <el-table-column type="selection" min-width="5%" align="center">
            </el-table-column>
            <el-table-column
              prop="noticeCode"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="公告编号"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="noticeTitle"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="公告标题"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="projectLedgerCode"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="项目编号"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="projectLedgerName"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="项目名称"
              resizable
            >
            </el-table-column>
            
            <el-table-column
              prop="procureOrgName"
              min-width="10%"
              show-overflow-tooltip
              label="采购单位"
              align="center"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="applicationType"
              min-width="10%"
              label="项目类型"
              align="center"
              resizable
            >
            <template slot-scope="scope">
              <div v-if="scope.row.applicationType==1">
                服务
              </div>
              <div v-if="scope.row.applicationType==2">
                工程
              </div>
            </template>
            </el-table-column>
            <el-table-column
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="发布状态"
              resizable
            >
            <template v-slot="{row}">
                <el-tag type="danger" v-if="row.releaseStatus == 0"
                  >草稿</el-tag
                >
                <el-tag type="warning" v-else-if="row.releaseStatus == 3"
                  >已下架</el-tag
                >
                <el-tag type="" v-else-if="row.releaseStatus == 1"
                  >审核中</el-tag
                >
                <el-tag type="success" v-else-if="row.releaseStatus == 2"
                  >已发布</el-tag
                >
                <el-tag type="danger" v-else>驳回</el-tag>
              </template>
            </el-table-column>
            
            <el-table-column min-width="10%" align="center" label="操作">
              <template v-slot="{row}">
                <template>
                  <el-button
                    icon="el-icon-edit"
                    type="primary"
                    plain
                    size="small"
                    v-if="row.releaseStatus==0||row.releaseStatus==3"
                    @click="handleEdit(row)"
                    >编辑</el-button
                  >
                  <el-button
                    icon="el-icon-view"
                    type="primary"
                    plain
                    size="small"
                    v-else
                    @click="handleDetail(row)"
                    >详情</el-button
                  >
                </template>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <div class="common-pagenation">
          <el-pagination
            ref="paginationPanel"
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryParam.pageBean.page"
            :page-sizes="[20, 30, 40, 50]"
            :page-size="queryParam.pageBean.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="queryParam.pageBean.total"
          >
          </el-pagination>
        </div>
      </el-col>
    </el-row>
    <!-- 流程弹窗 -->
    <purchaseDialog ref="flowDialog" @reload="loadData()"></purchaseDialog>
  </div>
</template>

<script>
import pagination from '@/components/pagination'
import {TableMixin} from '@/mixins/tableMixin'
import procurementManagement from '@/api/procurementManagement'
import dictUtils from '@/components/dict/DictSelectUtils.js'
import purchaseDialog from './module/flowDialog.vue'
import utils from '@/utils.js'
export default {
  name: 'DemandEntry',
  components: {
    pagination,
    purchaseDialog,
  },
  mixins: [TableMixin],
  data() {
    return {
      tableHeight: 1000,
      tableData: [],
      handleDeleteActive: true,
      handleDeleteId: [],
      tableLoading: false,
      showApproval:false,
      queryParam: {
        pageBean: {
          page: 1,
          pageSize: 20,
          total: 0
        },
        sorter: [
          {
            direction: 'DESC',
            property: 'create_time_',
          },
        ],
      },
      cgfbztState: [],
      multipleSelection:[],
      querys: [
        {
          group: 'main',
          operation: 'LIKE',
          parentGroup: '',
          property: 'noticeCode',
          relation: 'AND',
          value: ''
        },
        {
          group: 'main',
          operation: 'LIKE',
          parentGroup: '',
          property: 'noticeTitle',
          relation: 'AND',
          value: ''
        },
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'releaseStatus',
          relation: 'AND',
          value: '0'
        }
      ],
      options: [
        {label: '草稿', value: '0'},
        {label: '审批中', value: '1'},
        {label: '已发布', value: '2'},
        {label: '已下架', value: '3'},
        {label: '驳回', value: '4'},
      ],
    }
  },
  mounted() {
    this.calcTableHeight()
    //页面缩放从新计算表格的高度
    window.addEventListener('resize', this.calcTableHeight)
  },
  created() {
    this.getAlias()
    this.loadData()
    // 数据单位
    this.cgfbztState = dictUtils.getDictItemsFromCache('cgfbzt')
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.calcTableHeight)
  },
  methods: {
    getAlias() {
      let params = {
        pageBean: {
          page: -1,
          pageSize: -1,
        },
        querys: [],
      }
      params.querys.push({
        group: 'main',
        operation: 'EQUAL',
        parentGroup: '',
        property: 'a.APPROVAL_MENU_ALIAS_',
        relation: 'AND',
        value: this.$route.name,
      })
      this.$http
        .post('${portal}/purchasingRulesDetail/v1/pageAllByOrg', params)
        .then((res) => {
          if (res.data.state) {
            let data = res.data.value.rows
            if (data.length != 0) {
              this.showApproval = true
            } else {
              this.showApproval = false
            }
          }
        })
    },
    // 获取字典值翻译
    getDictName(code, value) {
      return utils.getDictName(code, value)
    },
    // 动态计算表格的高度，自适应当前容器
    calcTableHeight() {
      this.tableHeight = this.$refs.tableContent.offsetHeight
    },
    //获取数据
    loadData() {
      this.tableLoading = true
      this.queryParam.querys = []
      if (this.querys[0].value) {
        this.queryParam.querys.push(this.querys[0])
      }
      if (this.querys[1].value) {
        this.queryParam.querys.push(this.querys[1])
      }
      if (this.querys[2].value) {
        this.queryParam.querys.push(this.querys[2])
      }
      procurementManagement
        .projectApprovalProDetailsPage(this.queryParam)
        .then(res => {
          this.queryParam.pageBean.total = res.value.total
          this.tableLoading = false
          this.tableData = res.value.rows
          console.log(this.tableData)
        })
    },
    //发布
    publishBtn() {
      if (this.showApproval == true) {
        let ids = this.handleDeleteId.join(',')
      this.$confirm('确定发布选中数据？', '提示', {
        confirmButtonText: '确 定',
        cancelButtonText: '取 消',
        type: 'warning'
      }).then(() => {
        this.$http.post('${portal}/projectApproval/v1/modifyReleaseStatus?ids='+ids+'&status=2')
          .then(res => {
            console.log(res,'res');
            if (res.data.state) {
              this.$message.success(res.data.message)
              this.loadData()
            }
          })
          .catch(res => {
            this.$message.error(res.data.message)
          })
      })
      } else {
        if (this.multipleSelection.length > 1) {
          this.$message.warning('请选择单条数据！')
          return
        }
        this.$refs.flowDialog.handleOpen(this.multipleSelection)
      }
      
    },
    //下架
    shelvesTaken() {
      let ids = this.handleDeleteId.join(',')
      this.$confirm('确定下架选中数据？', '提示', {
        confirmButtonText: '确 定',
        cancelButtonText: '取 消',
        type: 'warning'
      }).then(() => {
        this.$http.post('${portal}/projectApproval/v1/modifyReleaseStatus?ids='+ids+'&status=3')
          .then(res => {
            if (res.data.state) {
              this.$message.success(res.data.message)
              this.loadData()
            }
          })
          .catch(res => {
            this.$message.error(res.data.message)
          })
      })
    },
    //添加
    handleAdd() {
      this.$router.push('projectApprovalAddDialog')
    },
    // 编辑
    handleEdit(rows) {
      this.$refs.form.clearValidate()
      this.$router.push({
        name: 'projectApprovalAddDialog',
        query: {editRows: rows, pageBean: this.params}
      })
    },
    // 详情
    handleDetail(rows) {
      this.$router.push({
        name: 'projectApprovalDetail',
        query: {editRows: rows, pageBean: this.params}
      })
    },
    // 删除
    handleDelete() {
      this.$confirm('确定删除选中数据？', '提示', {
        confirmButtonText: '确 定',
        cancelButtonText: '取 消',
        type: 'warning'
      }).then(() => {
        this.$http.post('${portal}/projectApproval/v1/removeByIds',this.handleDeleteId)
          .then(res => {
            if (res.data.state) {
              this.$message.success(res.data.message)

              this.loadData()
            }
          })
      })
    },
    //重置
    clearSearchForm() {
      this.querys[0].value = ''
      this.querys[1].value = ''
      this.querys[2].value = '0'
      this.loadData()
    },
    // 顶部搜索
    handleSearch() {
      this.loadData()
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
      if (val.length !== 0) {
        this.handleDeleteActive = false
        this.handleDeleteId = val.map(item => {
          return item.id
        })
      } else {
        this.handleDeleteActive = true
      }
    },
    handleCurrentChange(page) {
      this.queryParam.pageBean = {...this.queryParam.pageBean, page}
      this.loadData(this.queryParam)
    },
    handleSizeChange(pageSize) {
      this.queryParam.pageBean = {...this.queryParam.pageBean, pageSize}
      this.loadData(this.queryParam)
    }
  },
  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-table thead tr th {
  background-color: #eee !important;
  border-right: 1px solid #dfe2e8 !important;
}
/deep/ .el-table {
  border-collapse: collapse !important;
}
</style>
