<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[1].value"
            clearable
            placeholder="请输入项目编号"
          ></el-input>
        </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"></div>
        <!-- LJ：表格高度自适应需配合样式  btn-pagenation 有按钮有分页  btngroup只有按钮  pagenation只有分页 -->
        <div class="common-table-content btn-pagenation" ref="tableContent">
          <el-table
            :border="true"
            stripe
            fit
            size="mini"
            :max-height="tableHeight"
            v-loading="tableLoading"
            ref="multipleSelection"
            :data="tableData"
            @selection-change="handleSelectionChange"
          >
            <el-table-column type="selection" min-width="5%" align="center">
            </el-table-column>
            <el-table-column
              prop="projectNumber"
              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="procureOrgName"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="采购单位"
              resizable
            >
            </el-table-column>
            <el-table-column
              show-overflow-tooltip
              min-width="10%"
              prop="procureType"
              align="center"
              label="采购类型"
              resizable
            >
              <template v-slot="{row}">
                <div v-if="row.procureType != 6">
                  {{ getDictName('cglx-cglx', row.procureType) }}
                </div>
                <div v-else>谈判采购</div>
              </template>
            </el-table-column>
            <el-table-column min-width="10%" align="center" label="操作">
              <template v-slot="{row}">
                <template>
                  <el-button
                    icon="el-icon-view"
                    type="primary"
                    plain
                    size="small"
                    @click="handleEdit(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>
    <el-dialog
      title="补遗管理"
      :visible.sync="dialogVisible"
      width="800px"
      v-loading="loading"
      class="dialog"
      fullscreen
      :before-close="dialogBeforeClose"
    >
      <div>
        <el-button
          size="small"
          icon="el-icon-plus"
          type="primary"
          v-if="showStatus == true ? false : true"
          class="btn btn-plus"
          style="width: 90px !important"
          @click="handleAdd"
        >
          新增补遗
        </el-button>
        <el-button
          :disabled="handleDeleteActive"
          size="small"
          icon="el-icon-plus"
          type="primary"
          v-if="showStatus == true ? false : true"
          class="btn btn-plus"
          style="width: 90px !important"
          @click="publishBtn"
        >
          发布补遗
        </el-button>
        <el-button
          :disabled="handleDeleteActive"
          size="small"
          icon="el-icon-minus"
          type="warning"
          v-if="showStatus == true ? false : true"
          class="btn btn-plus"
          @click="shelvesTaken"
        >
          下架补遗
        </el-button>
        <el-table
          class="down"
          :data="dataList"
          border
          @selection-change="handleSelectionChange"
          stripe
          style="width: 100%; margin-top: 20px"
        >
          <el-table-column
            type="selection"
            min-width="50px"
            align="center"
          ></el-table-column>
          <el-table-column
            prop="describe"
            min-width="300px"
            label="描述"
          ></el-table-column>
          <el-table-column prop="isVendorConfirm" label="是否需要供应商确认">
            <template slot-scope="scope">
              <span v-if="scope.row.isVendorConfirm == 1">否</span>
              <span v-if="scope.row.isVendorConfirm == 2">是</span>
            </template>
          </el-table-column>
          <el-table-column prop="releaseStatus" label="发布状态">
            <template slot-scope="scope">
              <el-tag type="danger" v-if="scope.row.releaseStatus == 1"
                >未发布</el-tag
              >
              <el-tag type="success" v-if="scope.row.releaseStatus == 2"
                >已发布</el-tag
              >
            </template>
          </el-table-column>
          <el-table-column prop="createTime" label="上传时间">
            <template slot-scope="scope">
              <div>
                {{ scope.row.createTime }}
              </div>
            </template>
          </el-table-column>
          <el-table-column width="150px" label="操作">
            <template slot-scope="scope">
              <el-button size="small" type="text">
                <el-button
                  size="small"
                  type="text"
                  @click="handleCheck(scope.row)"
                  >查看附件</el-button
                >
              </el-button>
              <el-button
                size="small"
                type="text"
                v-if="scope.row.releaseStatus == 1"
                @click="deleteHandle(scope.row.id, scope.$index)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div slot="footer">
        <el-button @click="dialogBeforeClose">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 补遗附件上传 -->
    <el-dialog
      title="补遗附件"
      :visible.sync="visible"
      width="600px"
      :before-close="cancelConfirm"
    >
      <el-form
        ref="form"
        :model="form"
        label-width="auto"
        label-position="right"
        :inline="true"
      >
        <el-row :gutter="24">
          <el-col :span="24">
            <el-form-item label="是否需要供应商确认" prop="isVendorConfirm">
              <el-select v-model="form.isVendorConfirm" placeholder="">
                <el-option
                  v-for="item in isOptions"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="描述" prop="describe">
              <el-input
                clearable
                type="areatext"
                :autosize="{minRows: 2, maxRows: 4}"
                v-model="form.describe"
                placeholder="请输入描述"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="文件导入">
              <el-upload
                style="display: inline-block"
                :action="actionBz"
                :on-success="hadleUploadResult"
                :on-error="hadleUploadResult"
                :headers="headers"
                :multiple="false"
                accept=".png,.jpg,.jpeg,.xlsx,.xls,.pdf,.docx,.doc,.zip,.rar"
                :before-upload="beforeUpload"
                :data="form.fileList"
                :auto-upload="false"
                ref="upload"
              >
                <el-button
                  type="primary"
                  class="download-btn"
                  icon="el-icon-upload"
                  style="width: 100% !important"
                  >选择文件</el-button
                >
              </el-upload>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click="cancelConfirm()">取 消</el-button>
        <el-button
          size="small"
          type="primary"
          @click="submitImport"
          plain
          element-loading-text="拼命导入中"
          v-loading.fullscreen.lock="fullscreenLoading"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <el-dialog
      title="附件"
      :visible.sync="showVisible"
      width="width"
      :before-close="dialogClose"
    >
      <div>
        <el-table
          class="down"
          :data="accessoryList"
          border
          stripe
          style="width: 100%; margin-top: 20px"
        >
          <el-table-column prop="name" label="文件名称"></el-table-column>
          <el-table-column prop="size" label="文件大小">
            <template slot-scope="scope">
              <span>{{ scope.row.fileSize }}</span>
            </template>
          </el-table-column>
          <el-table-column width="150px" label="操作">
            <template slot-scope="scope">
              <el-button size="small" type="text">
                <a
                  target="_blank"
                  @click="downloadFile(scope.row.name, scope.row.url)"
                  >下载</a
                >
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <div slot="footer">
        <el-button @click="showVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
  
  <script>
import pagination from '@/components/pagination'
import {TableMixin} from '@/mixins/tableMixin'
import {mapState} from 'vuex'
import procurementManagement from '@/api/procurementManagement'
import {label} from 'three/examples/jsm/nodes/Nodes.js'
export default {
  name: 'DemandEntry',
  components: {
    pagination,
  },
  mixins: [TableMixin],
  data() {
    return {
      tableHeight: 1000,
      tableData: [],
      showVisible: false,
      handleDeleteActive: true,
      handleDeleteId: [],
      submiting: false,
      tableLoading: false,
      visible: false,
      loading: false,
      fullscreenLoading: false,
      dialogvisible: false,
      fullscreen: false,
      describe: '',
      form: {
        isVendorConfirm: 1,
        describe: '',
      },
      fileList: [],
      dataList: [],
      isOptions: [
        {value: 1, label: '否'},
        {value: 2, label: '是'},
      ],
      queryParam: {
        pageBean: {
          page: 1,
          pageSize: 20,
          total: 0,
        },
        sorter: [
          {
            direction: 'DESC',
            property: 'create_time_',
          },
        ],
      },
      dialogVisible: false,
      showStatus: false,
      querys: [
        {
          group: 'main',
          operation: 'GREAT_EQUAL',
          parentGroup: '',
          property: 'bidStatus',
          relation: 'AND',
          value: 3,
        },
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'projectNumber',
          relation: 'AND',
          value: '',
        },
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'matIfPlat',
          relation: 'AND',
          value: '',
        },
      ],
      rows: {},
      accessoryList: [],
      handleDeleteId: [],
    }
  },
  mounted() {
    //页面缩放从新计算表格的高度
    window.addEventListener('resize', this.calcTableHeight)
  },
  created() {
    this.loadData()
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.calcTableHeight)
  },
  methods: {
    // 动态计算表格的高度，自适应当前容器
    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
      //   .bizProjectApprovalPage(this.queryParam)
      this.$http
        .post('${portal}/matApproval/v1/pageCurUser', this.queryParam)
        .then((res) => {
          this.tableLoading = false
          if (res.data.state) {
            this.tableData = res.data.value.rows
            this.queryParam.pageBean = {
              page: res.data.value.page,
              pageSize: res.data.value.pageSize,
              total: res.data.value.total,
            }
          }
        })
    },
    getByData(row) {
      let params = {
        pageBean: {
          page: -1,
          pageSize: -1,
        },
        querys: [
          {
            group: 'main',
            operation: 'EQUAL',
            parentGroup: '',
            property: 'type',
            relation: 'AND',
            value: 2,
          },
          {
            group: 'main',
            operation: 'EQUAL',
            parentGroup: '',
            property: 'projectApprovalType',
            relation: 'AND',
            value: 1,
          },
          {
            group: 'main',
            operation: 'EQUAL',
            parentGroup: '',
            property: 'projectApprovalId',
            relation: 'AND',
            value: row.id,
          },
        ],
      }
      this.loading = true
      this.$http
        .post('${portal}/tenderDocument/v1/pageAccessory', params)
        .then((res) => {
          if (res.data.value) {
            this.dataList = res.data.value.rows
          }
          this.loading = false
        })
    },
    //下载按钮回调
    downloadFile(name, url) {
      var a = document.createElement('a')
      var event = new MouseEvent('click')
      a.download = name
      a.target = '_blank'
      a.href = url
      a.dispatchEvent(event)
    },
    // 删除
    deleteHandle(id, index) {
      let ids = []
      ids.push(id)
      this.$confirm('是否确认删除该附件', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        if (id) {
          this.$http
            .post('${portal}/tenderDocument/v1/removeByIds', ids)
            .then((res) => {
              console.log(res, '11111111111')
              this.getByData(this.rows)
            })
        }
      })
    },
    handleCheck(row) {
      this.showVisible = true
      this.accessoryList = row.accessoryList
    },
    // 新增补遗
    handleAdd() {
      this.visible = true
      this.form = {
        isVendorConfirm: 1,
        describe: '',
        fileList: [],
      }
      console.log(this.form, 'lllllllll')
      this.fileList = []
    },
    //下架
    shelvesTaken() {
      let ids = this.handleDeleteId.join(',')
      this.$http
        .post(
          '${portal}/tenderDocument/v1/modifyReleaseStatus?ids=' +
            ids +
            '&status=1'
        )
        .then((res) => {
          if (res.data.state) {
            this.$message.success(res.data.message)
          }
          this.getByData(this.rows)
        })
    },
    // 编辑
    handleEdit(rows) {
      console.log(rows, 222222222)
      this.parameter = rows
      if (rows.bidStatus > 5) {
        this.showStatus = true
      } else {
        this.showStatus = false
      }
      console.log(this.showStatus)
      this.rows = rows
      this.getByData(rows)
      this.dialogVisible = true
      this.loading = true
    },
    dialogClose() {
      this.showVisible = false
    },
    //重置
    clearSearchForm() {
      this.querys[0].value = '3'
      this.querys[1].value = ''
      this.querys[2].value = ''
      this.loadData()
    },
    dialogBeforeClose() {
      this.dialogVisible = false
    },
    publishBtn() {
      let ids = this.handleDeleteId.join(',')
      this.submiting = true
      this.$http
        .post(
          '${portal}/tenderDocument/v1/modifyReleaseStatus?status=2' +
            '&ids=' +
            ids
        )
        .then((res) => {
          this.submiting = false
          if (res.data.state) {
            this.$message.success(res.data.message)
          }
          this.getByData(this.rows)
        })
    },
    // 顶部搜索
    handleSearch() {
      this.loadData()
    },
    handleSelectionChange(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)
    },
    cancelConfirm() {
      this.$refs.upload.clearFiles()
      this.visible = false
      this.$refs.upload.uploadFiles = []
    },
    submitImport() {
      if (
        !this.$refs.upload.uploadFiles ||
        this.$refs.upload.uploadFiles.length == 0
      ) {
        this.$message.warning('请选择要导入的数据文件!')
        return false
      }
      this.$refs.upload.submit()
    },
    hadleUploadResult(response) {
      this.form.fileList = []
      this.fullscreenLoading = false
      if (response.state) {
        this.form.fileList.push(response.value)
        this.form.fileList.forEach((item) => {
          item['type'] = 2
          let byDocument = {
            type: item.type,
            accessoryList: this.form.fileList,
            describe: this.form.describe,
            isVendorConfirm: this.form.isVendorConfirm,
            projectApprovalId: this.parameter.id,
            projectApprovalType: 1,
          }
          console.log(byDocument)
          let list = []
          list.push(byDocument)
          this.$http
            .post('${portal}/tenderDocument/v1/uploadByDoc', list)
            .then((res) => {
              this.visible = false
              if (res.data.state) {
                this.getByData(this.parameter)
                this.form = {
                  isVendorConfirm: 1,
                  describe: '',
                  fileList: [],
                }
                this.$refs.upload.clearFiles()
                this.$message.success(res.data.message)
              }
            })
        })
      } else {
        this.$message.error(response.message)
      }
    },
    beforeUpload(file) {
      this.fileInfo = file
      this.fullscreenLoading = true
      let acceptList = ['xlsx', 'xls','png','jpg','jpeg','xlsx','xls','pdf','docx','doc','zip','rar']
      // 根据文件名获取文件的后缀名
      let fileType = file.name.split('.').pop().toLowerCase()
      // 判断文件格式是否符合要求
      if (acceptList.indexOf(fileType) === -1) {
        this.$message.error('请上传 .png,.jpg,.jpeg,.xlsx,.xls,pdf,.docx,.doc,.zip,.rar 格式的文件 !')
        return false
      }
    },
  },
  computed: {
    ...mapState({
      headers: (state) => {
        return {Authorization: `Bearer ${state.login.currentUser.token}`}
      },
      actionBz: function () {
        return window.context.portal + '/biz/universal/v1/minioUpload'
      },
    }),
    SearchPanel() {
      return {
        '--searchPanel': this.searchPanel + 'px',
      }
    },
  },
}
</script>
  
<style lang="scss" scoped>
.dialog {
  /deep/.el-dialog__body {
    height: 650px;
  }
}
</style>
  