<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">
          <el-button
              :disabled="handleDeleteActive"
              size="small"
              icon="el-icon-thumb"
              type="primary"
              class="btn btn-plus"
              @click="publishBtn"
            >
              澄清提问
            </el-button>
            <!-- <el-button
              :disabled="handleDeleteActive"
              size="small"
              icon="el-icon-minus"
              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"
            :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="projectApprovalType"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="立项类型"
              resizable
            >
              <template slot-scope="scope">
                <div>
                  {{scope.row.projectApprovalType==1?'物资':'项目' }}
                </div>
              </template>
            </el-table-column>
            <el-table-column
              prop="describe"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="问题描述"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="operateName"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="操作人姓名"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="operateOrgName"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="操作人企业名称"
              resizable
            >
            </el-table-column>
            <el-table-column
              prop="operateTime"
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="时间"
              resizable
            >
            </el-table-column>
            <el-table-column
              show-overflow-tooltip
              min-width="10%"
              align="center"
              label="附件"
              resizable
            >
            <!-- <template v-slot="{row}">
                <template>
                  <el-button
                    icon="el-icon-view"
                    type="primary"
                    plain
                    size="small"
                    @click="handleEdit(row)"
                    >查看附件</el-button
                  >
                </template>
              </template> -->
              <template v-slot="{row}">
                <a v-if="row.accessoryList.length" :href="row.accessoryList[0].url" target="_blank">{{ row.accessoryList[0].name }}</a>
              </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="handleQuery(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="visible"
      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="visible = false">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 澄清 -->
    <el-dialog
      title="澄清提问"
      :visible.sync="dialogVisible"
      width="width"
      :before-close="dialogBeforeClose"
    >
      <el-form :model="form" :rules="rules">
        <el-form-item label="澄清描述" label-width="90px" prop="describe">
          <el-input
            type="textarea"
            autosize
            placeholder="请输入澄清描述"
            v-model="form.describe"
          ></el-input>
        </el-form-item>
        <el-form-item label="附件上传" label-width="90px">
          <el-upload
            class="upload-demo"
            ref="upload"
            :headers="headers"
            :action="action"
            :file-list="form.accessoryList"
            :multiple="false"
            style="text-align: left;"
            accept=".png,.jpg,.jpeg,.xlsx,.xls,.pdf,.docx,.doc,.zip,.rar"
            :show-file-list="false"
            :before-upload="beforeUpload"
            :on-success="handleSuccess"
          >
            <el-button slot="trigger" size="small" type="primary"
              >上传文件</el-button
            >
          </el-upload>
        </el-form-item>
      </el-form>
      <el-table
        class="down"
        :data="form.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 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">
              <a
                target="_blank"
                @click="downloadFile(scope.row.name, scope.row.url)"
                >下载</a
              >
            </el-button>
            <el-button
              size="small"
              type="text"
              @click="deleteHandle(scope.$index)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" :loading="submiting" @click="handleQuestion"
          >确 定</el-button
        >
      </div>
    </el-dialog>
    <!-- 澄清提问查询 -->
    <questionDialog ref="question"></questionDialog>
  </div>
</template>
    
    <script>
import pagination from '@/components/pagination'
import {mapState} from 'vuex'
import {TableMixin} from '@/mixins/tableMixin'
import procurementManagement from '@/api/procurementManagement'
import questionDialog from './component/questionDialog'

export default {
  name: 'DemandEntry',
  components: {
    pagination,
    questionDialog
  },
  mixins: [TableMixin],
  data() {
    return {
      tableHeight: 1000,
      tableData: [],
      handleDeleteActive: true,
      handleDeleteId: [],
      submiting: false,
      tableLoading: false,
      visible: false,
      loading: false,
      form: {
        describe: '',
        accessoryList: [],
      },
      rules: {
        describe: [
          {
            required: true,
            message: '请输入问题描述',
            trigger: 'blur',
          },
        ],
      },
      multipleSelection:[],
      fileList: [],
      queryParam: {
        pageBean: {
          page: 1,
          pageSize: 20,
          total: 0,
        },
        sorter: [
          {
            direction: 'DESC',
            property: 'create_time_',
          },
        ],
      },
      dialogVisible: false,
      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: 'projectApprovalType',
        //   relation: 'AND',
        //   value: 1
        // }
        {
          group: 'main',
          operation: 'IN',
          parentGroup: '',
          property: 'bidStatus',
          relation: 'AND',
          value: '',
        },
        {
          group: 'main',
          operation: 'EQUAL',
          parentGroup: '',
          property: 'projectCode',
          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: {
    handleQuery(row){
      this.$refs.question.open(row)
    },
    // 动态计算表格的高度，自适应当前容器
    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])
      }
      this.$http
        .post('${portal}/clarifyQuestions/v1/page', 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,
            }
          }
        })
    },
    handleSuccess(res, fileList) {
      this.form.accessoryList.push(res.value)
    },
    beforeUpload(file) {
      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
      }
    },
    //下载按钮回调
    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(index) {
      this.$confirm('是否确认删除该附件', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }).then(() => {
        this.form.accessoryList.splice(index, 1)
      })
    },
    // 编辑
    handleEdit(rows) {
      this.visible = true
      this.accessoryList = rows.accessoryList
    },
    dialogClose() {
      this.visible = false
    },
    //重置
    clearSearchForm() {
      this.querys[0].value = ''
      this.querys[1].value = ''
      this.querys[2].value = ''
      this.loadData()
    },
    dialogBeforeClose() {
      this.dialogVisible = false
      this.visible = false
    },
    publishBtn(){
      this.dialogVisible = true
    },
    handleQuestion() {
      this.submiting = true
      let data ={
        questionsId:this.multipleSelection[0].id,
        describe:this.form.describe,
        accessoryList:this.form.accessoryList,
        projectApprovalType:1,
      }
      this.$http
        .post(
          '${portal}/clarifyAnswer/v1/saveOrUpdate',data
        )
        .then((res) => {
          this.submiting = false
          if (res.data.state) {
            this.$message.success(res.data.message)
            this.loadData()
            this.dialogBeforeClose()
          }
        })
    },
    // 顶部搜索
    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
      }
      if (this.multipleSelection.length > 1) {
        this.$refs.multipleSelection.toggleRowSelection(
          this.multipleSelection[0],
          false
        )
      }
    },
    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',
      }
    },
    ...mapState({
      headers: (state) => {
        return {Authorization: `Bearer ${state.login.currentUser.token}`}
      },
      action: function () {
        return window.context.portal + '/biz/universal/v1/minioUpload'
      },
    }),
  },
}
</script>
    
  <style lang="scss" scoped>
.dialog {
  /deep/.el-dialog__body {
    height: 650px;
  }
}
</style>
    