<template>
  <at-table
    @load="loadData"
    :data="data"
    :page-result="pageResult"
    ref="AtTable"
    :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"
          @keyup.enter.native="handleSearch"
        ></project-info-selector>
        <el-input
          placeholder="请输入勘察单位"
          @keyup.enter.native="handleSearch"
          v-model="searchForm.geologicalProspectingUnit"
        ></el-input>
        <el-date-picker
          placeholder="请选择勘察日期"
          type="date"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          style="width: 100%"
          @keyup.enter.native="handleSearch"
          v-model="searchForm.prospectingDate"
        ></el-date-picker>
        <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"
        :htTable="$refs.AtTable"
        :requestMethod="'POST'"
        >删除</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="geologicalProspectingUnit"
        label="勘察单位"
        align="center"
        min-width="130"
        show-overflow-tooltip
      >
      </el-table-column>
      <el-table-column
        prop="prospectingDate"
        label="勘察日期"
        align="center"
        min-width="130"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="constructionScale"
        label="勘察结果"
        align="center"
        min-width="150"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="establishmentRemarks"
        label="备注"
        align="center"
        min-width="150"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
    </template>
    <!-- 弹框 -->
    <template v-slot:dialogContent>
      <el-form
        ref="ruleForm"
        data-vv-scope="form"
        :model="form"
        :rules="rules"
        label-width="130px"
        v-if="dialogVisible"
        :label-position="labelPosition"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="勘察单位" prop="geologicalProspectingUnit">
              <el-input
                v-model="form.geologicalProspectingUnit"
                placeholder="请输入勘察单位"
                clearable
              >
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="勘察日期" prop="prospectingDate">
              <el-date-picker
                v-model="form.prospectingDate"
                type="date"
                placeholder="请输入勘察日期"
                class="basisType"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item label="勘察结果" prop="constructionScale">
              <el-input
                type="textarea"
                v-model="form.constructionScale"
                placeholder="请输入勘察结果"
                clearable
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="备注" prop="establishmentRemarks">
          <el-input
            type="textarea"
            v-model="form.establishmentRemarks"
            placeholder="请输入备注"
          ></el-input>
        </el-form-item>
        <file-table
          @handleFile="accessoryInfo"
          :file="form.accessoryInfo"
          :info="'上传勘察相关手续及文件'"
        />
      </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 req from '@/request.js'
export default {
  name: 'GeologicalProspecting',
  components: {
    ProjectInfoDialog,
  },
  props: {
    pid: {
      type: String,
      default: '',
    },
    isShow: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      importData: {
        title: '地勘导入',
        templateName: '地勘模板.xls',
        downloadUrl: '/../fvue/static/excel/PreliminaryDesign.xlsx',
        importUrl:
          window.context.cqxy +
          '/biz/designEstimate/geologicalProspecting/v1/importExcelData',
        showProject: true,
        projectId: '',
      },
      projectObj: [], //获取的所有项目数组
      labelPosition: 'right', //表单对齐方式
      searchForm: {
        projectId: '',
        geologicalProspectingUnit: '',
        prospectingDate: '',
      }, //顶部搜索参数
      data: [], //表格数据
      pageResult: {
        page: 1,
        pageSize: 20,
        total: 0,
      },
      form: {},
      dialogVisible: false,
      dialogTitle: '添加',
      //表单验证
      rules: {
        geologicalProspectingUnit: {
          required: true,
          message: '请输入勘察单位',
          trigger: 'blur',
        },
        prospectingDate: {
          required: true,
          message: '请输入勘察日期',
          trigger: 'blur',
        },
      },
      obj: {},
    }
  },
  computed: {
    deleteUrl: function () {
      return (
        window.context.cqxy +
        '/biz/designEstimate/geologicalProspecting/v1/delete'
      )
    },
  },
  created() {},
  mounted() {
    this.initProjectAll()
  },
  methods: {
    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
            }
          })
      }
    },
    //重置
    handleReset() {
      this.searchForm.geologicalProspectingUnit = ''
      this.searchForm.prospectingDate = ''
      this.$refs['searchForm'].resetFields()
      this.pageResult = {
        page: 1,
        pageSize: 20,
        total: 0,
      }
      this.$nextTick(() => {
        this.$refs.AtTable.load()
      })
    },
    // handleSelectProject(data) {
    // //   this.form.constructionScale = data.projectContent
    // //   this.form.proposedLocation = data.projectAddress
    // //   this.form.projectInfo.projectName = data.projectName
    // //   this.form.projectInfo.projectCode = data.projectCode
    //   this.form.projectId = data.id
    //   this.$refs.ruleForm.clearValidate()
    // },
    //导入后重新加载数据
    load() {
      this.$refs.AtTable.load()
    },
    //导出
    handleExport() {
      let query = []
      let search = {
        'pm.ID_': this.searchForm.projectId,
        // document_number_: this.searchForm.documentNumber,
        'bgp.GEOLOGICAL_PROSPECTING_UNIT_':
          this.searchForm.geologicalProspectingUnit,
        prospectingDate: this.searchForm.prospectingDate,
      }
      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/designEstimate/geologicalProspecting/v1/exportDataToExcel',
          param
        )
        .then((res) => {
          if (res.status == 200) {
            this.$message.success('导出成功')
          } else {
            this.$message.error('导出失败')
          }
        })
    },
    //获取附件信息
    accessoryInfo(info) {
      this.form.accessoryInfo = info
    },
    //查询
    handleSearch() {
      this.$refs.AtTable.load()
    },
    afterSaveData() {
      setTimeout(() => {
        this.form = {
          projectInfo: {
            projectName: '',
            projectCode: '',
          },
          accessoryInfo: [],
        }
        // this.dialogVisible = falseconstructionScale
        this.$refs.AtTable.load()
      }, 200)
    },
    handleAdd() {
      this.dialogTitle = '添加'
      this.dialogVisible = true
      this.form = {
        // establishmentApprovalUnit: '重庆市高新区建设局',
        // establishmentDate: this.getDate(),
        // totalEstimate: '',
        // proposedLocation: '',
        // constructionScale: '',
        // projectInfo: {},
        accessoryInfo: [],
        projectId: this.pid,
      }
      if (this.isShow) {
        let selectedProject = this.projectObj.find((item) => {
          if (item.currentProject && item.currentProject == 1) {
            return item.currentProject == 1
          }
        })
        // this.form.type = 1
        // this.form.constructionScale = selectedProject.projectContent
        // this.form.proposedLocation = selectedProject.projectAddress
        // this.form.projectInfo.projectName = selectedProject.projectName
        // this.form.projectInfo.projectCode = selectedProject.projectCode
        // this.form.projectId = selectedProject.id
      } else {
        //根据项目id查询
        // this.form.type = 1
        // this.form.constructionScale = this.projectObj[0].projectContent
        // this.form.proposedLocation = this.projectObj[0].projectAddress
        // 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/designEstimate/geologicalProspecting/v1/detail/' +
            data.id
        )
        .then(
          (res) => {
            this.dialogVisible = true
            let resp = res.data.value
            this.form = resp
          },
          (error) => {
            reject(error)
          }
        )
    },
    handleSelect(selection) {
      if (selection.length == 1) {
        this.importData.projectId = selection[0].projectId
      } else {
        this.importData.projectId = ''
      }
    },
    handleDialogCancel() {
      this.dialogVisible = false
      this.form = {
        projectInfo: {
          projectName: '',
          projectCode: '',
        },
        accessoryInfo: [],
      }
    },
    handleDialogClose() {
      this.dialogVisible = false
      this.form = {
        projectInfo: {
          projectName: '',
          projectCode: '',
        },
        accessoryInfo: [],
      }
    },
    //提交保存
    handleDialogSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if (this.form.accessoryInfo && this.form.accessoryInfo.length > 0) {
            this.form.accessoryInfo.forEach((item) => {
              if (item.id) {
                delete item.id
              }
            })
          }
          this.$http
            .post(
              '${cqxy}/biz/designEstimate/geologicalProspecting/v1/save',
              this.form
            )
            .then((res) => {
              if (res.status == 200) {
                setTimeout(() => {
                  this.$message.success(res.data.message)
                  this.afterSaveData()
                }, 200)
                this.dialogVisible = false
                this.$refs.AtTable.load()
              } else {
                this.$message.error(res.data.message)
              }
            })
            .catch(() => {
              this.$message.error('请求出错了')
            })
        } else if (this.form.accessoryInfo.length === 0) {
          this.$message.error('至少上传一个附件')
        } else {
          this.$message({
            message: '*号必填项,不能为空!',
            type: 'warning',
          })
          return false
        }
      })
    },
    loadData(param, cb) {
      // let param = {
      //   pageBean: {page: 1, pageSize: 20, showTotal: true},
      //   querys: []
      // }
      let query = []
      let search = {
        'pm.ID_': this.searchForm.projectId,
        'bgp.GEOLOGICAL_PROSPECTING_UNIT_':
          this.searchForm.geologicalProspectingUnit,
        prospectingDate: this.searchForm.prospectingDate,
      }
      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/designEstimate/geologicalProspecting/v1/page', param)
        .then(
          (resp) => {
            // this.dialogVisible = false
            this.data = resp.data.value.rows
            this.pageResult = {
              page: resp.data.value.page,
              pageSize: resp.data.value.pageSize,
              total: resp.data.value.total,
            }
          },
          (error) => {
            reject(error)
          }
        )
        .finally(() => {
          if (cb) {
            cb()
          }
        })
    },
  },
}
</script>
<style lang="scss" scoped>
@import '@/assets/css/common.scss';
</style>
