<template>
  <at-table
    @load="loadData"
    :data="data"
    :page-result="pageResult"
    ref="AtTable"
    :default-sorter="[
      {direction: 'DESC', property: 'project_code_'},
      {direction: 'DESC', property: 'project_name_'},
    ]"
    :dialogTitle="dialogTitle"
    :dialogVisible="dialogVisible"
    @select-all="handleSelect"
    @select="handleSelect"
    @handleDialogCancel="handleDialogCancel"
    @handleDialogClose="handleDialogClose"
    @handleDialogSubmit="handleDialogSubmit"
    @row-dblclick="handleRowDBClick"
  >
    <template v-slot:searchInput>
      <el-form
        ref="searchForm"
        :model="searchForm"
        label-width="80px"
        class="search-top"
        @submit.native.prevent
      >
        <project-info-selector
          v-if="isShow"
          v-model="searchForm.projectId"
          style="width: 100%"
          @keyup.enter.native="handleSearch"
        ></project-info-selector>
        <el-input
          v-model="searchForm.documentNumber"
          placeholder="请输入文件编号"
          @keyup.enter.native="handleSearch"
          clearable
        />
        <el-input
          v-model="searchForm.name"
          placeholder="请输入投标名称"
          @keyup.enter.native="handleSearch"
          clearable
        />
        <el-button
          size="small"
          @click="handleSearch"
          type="primary"
          class="el-icon-search search-btn"
          >查询</el-button
        >
        <el-button
          size="small"
          @click="handleReset"
          type="primary"
          plain
          class="el-icon-refresh"
          >重置</el-button
        >
      </el-form>
    </template>
    <template v-slot:operationBtn>
      <el-button
        size="small"
        type="primary"
        @click="handleAdd"
        icon="el-icon-plus"
        >添加</el-button
      >
      <ht-delete-button
        :url="deleteUrl"
        :requestMethod="'POST'"
        :htTable="$refs.AtTable"
        >删除</ht-delete-button
      >
      <import-file :importData="importData" @load="load"></import-file>
      <el-button
        size="small"
        type="primary"
        @click="handleExport"
        icon="el-icon-download"
        plain
        style="margin-left: 10px"
        >导出</el-button
      >
    </template>
    <template>
      <el-table-column type="selection" width="55" align="center">
      </el-table-column>
      <el-table-column type="index" label="序号" width="55" align="center">
      </el-table-column>
      <el-table-column
        prop="projectInfo.projectCode"
        label="项目代码"
        align="center"
        width="150"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="projectInfo.projectName"
        label="项目名称"
        align="center"
        width="180"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="documentNumber"
        label="文件编号"
        align="center"
        width="110"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="name"
        label="投标名称"
        align="center"
        width="120"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="bidAmount"
        label="投标金额(万元)"
        align="center"
        min-width="130"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="bidDate"
        label="投标日期"
        align="center"
        width="100"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <ht-table-column
        prop="bidOf"
        label="是否中标"
        align="center"
        width="100"
        :show-overflow-tooltip="true"
        :filters="getDictData('tbgl-sfzb')"
      >
        <template v-slot="{row}">
          <el-tag :type="row.bidOf == 1 ? 'success' : 'danger'">{{
            getDictName('tbgl-sfzb', row.bidOf)
          }}</el-tag>
        </template>
      </ht-table-column>
      <el-table-column
        prop="biddingChargePerson"
        label="招标负责人"
        align="center"
        width="110"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="biddingAgency"
        label="招标代理机构"
        align="center"
        min-width="130"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="biddingChargePerson"
        label="招标负责人"
        align="center"
        width="110"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="biddingDate"
        label="招标日期"
        align="center"
        width="100"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="biddingFixedPrice"
        label="招标限价(万元)"
        align="center"
        width="130"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="biddingRange"
        label="招标范围"
        align="center"
        width="140"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="constructionControlUnit"
        label="监理单位"
        align="center"
        width="130"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="plannedConstructionPeriod"
        label="计划工期"
        align="center"
        width="150"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="establishmentRemarks"
        label="备注"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
    </template>
    <template v-slot:dialogContent>
      <el-form
        ref="form"
        v-form
        data-vv-scope="form"
        :model="form"
        :rules="rules"
        label-width="130px"
        v-if="dialogVisible"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="项目名称" prop="projectInfo.projectName">
              <el-input
                v-model="form.projectInfo.projectName"
                placeholder="请选择项目名称"
                @focus="handleFocusProject"
                :disabled="!isShow"
                clearable
              >
                <el-button
                  type="primary"
                  @click="handleFocusProject"
                  slot="append"
                  icon="el-icon-search"
                ></el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="项目代码" prop="projectInfo.projectCode">
              <el-input
                v-model="form.projectInfo.projectCode"
                placeholder="请输入项目代码"
                :disabled="!isShow"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="文件编号" prop="documentNumber">
              <el-input
                v-model="form.documentNumber"
                placeholder="请输入文件编号"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="投标名称" prop="name">
              <el-input
                v-model="form.name"
                placeholder="请输入投标名称"
                clearable
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="投标金额" prop="bidAmount">
              <el-input
                v-model="form.bidAmount"
                placeholder="请输入投标金额"
                type="number"
              >
                <i slot="suffix">万元</i>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="投标日期" prop="bidDate">
              <el-date-picker
                v-model="form.bidDate"
                type="date"
                placeholder="选择投标日期"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
                style="width: 100%"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="是否中标" prop="bidOf">
              <dict-select
                dictCode="tbgl-sfzb"
                :hasDefaultOption="true"
                clearable
                v-model="form.bidOf"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="招标代理机构" prop="biddingAgency">
              <el-input
                v-model="form.biddingAgency"
                placeholder="请输入招标代理机构"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="招标负责人" prop="biddingChargePerson">
              <el-input
                v-model="form.biddingChargePerson"
                placeholder="请输入招标负责人"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="招标日期" prop="biddingDate">
              <el-date-picker
                v-model="form.biddingDate"
                type="date"
                placeholder="选择招标日期"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
                style="width: 100%"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="招标限价" prop="biddingFixedPrice">
              <el-input
                v-model="form.biddingFixedPrice"
                placeholder="请输入招标限价"
              >
                <i slot="suffix">万元</i>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="招标范围" prop="biddingRange">
              <el-input
                v-model="form.biddingRange"
                placeholder="请输入招标范围"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="监理单位" prop="constructionControlUnit">
              <el-input
                v-model="form.constructionControlUnit"
                placeholder="请输入监理单位"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="计划工期" prop="plannedConstructionPeriod">
              <el-date-picker
                v-model="form.plannedConstructionPeriod"
                type="daterange"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
                style="width: 100%"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="备注" prop="establishmentRemarks">
              <el-input
                v-model="form.establishmentRemarks"
                placeholder="请输入备注"
                type="textarea"
                rows="3"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <file-table @handleFile="accessoryInfo" :file="form.accessoryInfo" />
      </el-form>
    </template>
    <project-info-dialog
      ref="project"
      :single="true"
      @select="handleSelectProject"
    ></project-info-dialog>
  </at-table>
</template>

<script>
import ProjectInfoDialog from '@/components/dialog/ProjectInfoDialog.vue'
import utils from '@/utils.js'
import req from '@/request.js'
export default {
  name: 'TenderingManagement',
  components: {
    ProjectInfoDialog,
  },
  props: {
    pid: {
      type: String,
      default: '',
    },
    isShow: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      importData: {
        title: '投标管理导入',
        templateName: '投标管理模板.xls',
        downloadUrl: './../static/excel/ExportTenderingManagement.xlsx',
        importUrl:
          window.context.cqxy +
          '/biz/bidManagement/bidManagement/v1/importExcelData',
          showProject:true,
        projectId: '',
      },
      searchForm: {
        projectId: '',
        documentNumber: '',
        name: '',
      }, //顶部搜索参数
      data: [], //表格数据
      pageResult: {
        page: 1,
        pageSize: 20,
        total: 0,
      },
      form: {
        projectInfo: {
          projectName: '',
          proejctCode: '',
        },
        accessoryInfo: [],
      },
      dialogTitle: '添加',
      dialogVisible: false,
      rules: {
        'projectInfo.projectCode': {
          required: true,
          message: '请选择项目代码',
          trigger: 'blur',
        },
        'projectInfo.projectName': {
          required: true,
          message: '请选择项目名称',
          trigger: 'blur',
        },
        documentNumber: {
          required: true,
          message: '请输入文件编号',
          trigger: 'blur',
        },
        name: {
          required: true,
          message: '请输入投标名称',
          trigger: 'blur',
        },
        bidAmount: {
          required: true,
          message: '请输入投标金额',
          trigger: 'blur',
        },
        bidDate: {
          required: true,
          message: '请选择投标日期',
          trigger: 'blur',
        },
        bidOf: {
          required: true,
          message: '请选择是否中标',
          trigger: 'blur',
        },
        biddingAgency: {
          required: true,
          message: '请输入招标代理机构',
          trigger: 'blur',
        },
        biddingChargePerson: {
          required: true,
          message: '请输入招标负责人',
          trigger: 'blur',
        },
        biddingDate: {
          required: true,
          message: '请选择招标日期',
          trigger: 'blur',
        },
        biddingFixedPrice: {
          required: true,
          message: '请输入招标限价(单位:万元)',
          trigger: 'blur',
        },
        biddingRange: {
          required: true,
          message: '请输入招标范围',
          trigger: 'blur',
        },
        constructionControlUnit: {
          required: true,
          message: '请输入监理单位',
          trigger: 'blur',
        },
        plannedConstructionPeriod: {
          required: true,
          message: '请选择计划工期',
          trigger: 'blur',
        },
      },
    }
  },
  computed: {
    deleteUrl: function () {
      return window.context.cqxy + '/biz/bidManagement/bidManagement/v1/delete'
    },
  },
  mounted() {
    this.initProjectAll()
  },
  methods: {
    //重置
    handleReset() {
      this.searchForm.projectId = ''
      this.searchForm.documentNumber = ''
      this.searchForm.name = ''
      this.$refs['searchForm'].resetFields()
       this.pageResult={
        page:1,
        pageSize:20,
        total:0
      }
      this.$refs['searchForm'].resetFields()
      this.$nextTick(()=>{
        this.$refs.AtTable.load()
      })
    },
    handleSelectProject(data) {
      this.form.projectInfo.projectName = data.projectName
      this.form.projectInfo.projectCode = data.projectCode
      this.form.projectId = data.id
      this.$refs.form.clearValidate()
    },
    //打开选择项目弹框
    handleFocusProject() {
      this.$refs.project.open()
    },
    //导入后重新加载数据
    load() {
      this.$refs.AtTable.load()
    },
    //导出
    handleExport() {
      let query = []
      let search = {
        'pm.ID_': this.searchForm.projectId,
        document_number_: this.searchForm.documentNumber,
        name: this.searchForm.name,
      }
      if (!this.isShow) {
        search['pm.ID_'] = this.pid
      }
      for (let i in search) {
        if (search[i]) {
          query.push({
            group: 'advance',
            operation: 'LIKE',
            property: i,
            relation: 'AND',
            value: search[i],
          })
        }
      }
      let param = {
        pageBean: {
          page: 1,
          pageSize: -1,
          showTotal: true,
        },

        querys: query,
        sorter: [],
      }
      req
        .downloadPostCopy(
          '${cqxy}/biz/bidManagement/bidManagement/v1/exportDataToExcel',
          param
        )
        .then((res) => {
          if (res.status == 200) {
            this.$message.success('导出成功')
          } else {
            this.$message.error('导出失败')
          }
        })
    },
    //获取附件信息
    accessoryInfo(info) {
      this.form.accessoryInfo = info
    },
    initProjectAll() {
      //初始获取所有项目数据
      if (this.isShow) {
        this.$http.post('${cqxy}/biz/projectManagement/pm/v1/All').then(
          (res) => {
            this.projectObj = res.data.value
          },
          (error) => {
            reject(error)
          }
        )
      } else {
        let params = {
          pageBean: {
            page: 1,
            pageSize: 500,
            total: 0,
          },
          querys: [
            {
              group: 'main',
              operation: 'EQUAL',
              property: 'id',
              relation: 'AND',
              value: this.pid,
            },
          ],
        }
        this.$http
          .post('${cqxy}/biz/projectManagement/pm/v1/page', params)
          .then((res) => {
            if (res.data.state) {
              let resp = res.data.value
              this.projectObj = resp.rows
            }
          })
      }
    },
    //过滤
    filterHandler(value, row, column) {
      const property = column['property']
      return row[property] === value
    },
    //查询
    handleSearch() {
      this.$refs.AtTable.load()
    },
    afterSaveData() {
      setTimeout(() => {
        this.form = {
          projectInfo: {
            projectName: '',
            proejctCode: '',
          },
          accessoryInfo: [],
        }
        this.dialogVisible = false
        this.$refs.AtTable.load()
      }, 500)
    },
    handleAdd() {
      this.dialogTitle = '添加'
      this.dialogVisible = true
      if (this.isShow) {
        let selectedProject = this.projectObj.find((item) => {
          if (item.currentProject && item.currentProject == 1) {
            return item.currentProject == 1
          }
        })
        this.form.projectInfo.projectName = selectedProject.projectName
        this.form.projectInfo.projectCode = selectedProject.projectCode
        this.form.projectId = selectedProject.id
      } else {
        //根据项目id查询
        this.form.projectInfo.projectName = this.projectObj[0].projectName
        this.form.projectInfo.projectCode = this.projectObj[0].projectCode
        this.form.projectId = this.projectObj[0].id
      }
    },
    handleRowDBClick(data) {
      this.dialogTitle = '修改'
      this.$http
        .post('${cqxy}/biz/bidManagement/bidManagement/v1/detail/' + data.id)
        .then((res) => {
          this.dialogVisible = true
          let response = res.data.value
          let time = response.plannedConstructionPeriod
          if (time) {
            response.plannedConstructionPeriod = time.split('~')
          }
          this.form = response
        })
    },
    handleSelect(selection) {
    },
    handleDialogCancel() {
      this.dialogVisible = false
      this.form = {
        projectInfo: {
          projectName: '',
          proejctCode: '',
        },
        accessoryInfo: [],
      }
    },
    handleDialogClose() {
      this.dialogVisible = false
      this.form = {
        projectInfo: {
          projectName: '',
          proejctCode: '',
        },
        accessoryInfo: [],
      }
    },
    //提交保存
    handleDialogSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid && this.form.accessoryInfo.length !== 0) {
          let time = this.form.plannedConstructionPeriod
          if (typeof time == 'object') {
            this.form.plannedConstructionPeriod = time.join('~')
          }
          if (this.form.accessoryInfo && this.form.accessoryInfo.length) {
            this.form.accessoryInfo.forEach((item) => {
              if (item.id) {
                delete item.id
              }
            })
          }
          this.$http
            .post('${cqxy}/biz/bidManagement/bidManagement/v1/save', this.form)
            .then((res) => {
              if (res.status == 200) {
                setTimeout(() => {
                  this.$message.success(res.data.message)
                  this.afterSaveData()
                }, 500)
              } else {
                this.$message.error(res.data.message)
              }
            })
        } else if (this.form.accessoryInfo.length === 0) {
          this.$message.error('请至少上传一个附件')
        } else {
          this.$message({
            message: '*号必填项,不能为空!',
            type: 'warning',
          })
          return false
        }
      })
    },
    loadData(param, cb) {
      let query = []
      let search = {
        'pm.ID_': this.searchForm.projectId,
        document_number_: this.searchForm.documentNumber,
        name: this.searchForm.name,
      }
      this.searchObj = search
      if (!this.isShow) {
        search['pm.ID_'] = this.pid
        this.importData.projectId = this.pid
      }
      for (let i in search) {
        if (search[i]) {
          query.push({
            group: 'advance',
            operation: 'LIKE',
            property: i,
            relation: 'AND',
            value: search[i],
          })
        }
      }
     
        if (param.querys && param.querys.length) {
          param.querys.push(...query)
        } else {
          param.querys = query
        }
      this.$http
        .post('${cqxy}/biz/bidManagement/bidManagement/v1/page', param)
        .then(
          (resp) => {
            let response = resp.data.value
            this.data = response.rows
            this.pageResult = {
              page: response.page,
              pageSize: response.pageSize,
              total: response.total,
            }
          },
          (error) => {
            reject(error)
          }
        )
        .finally(() => cb())
    },
  },
}
</script>
<style lang="scss" scoped>
@import '@/assets/css/common.scss';
</style>
