<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-model="searchForm.projectId"
          @keyup.enter.native="handleSearch"
          :disabled="!isShow"
        ></project-info-selector>
        <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="projectInfo.projectCode"
        label="项目代码"
        align="center"
        width="150"
        :show-overflow-tooltip="true"
      >
      </el-table-column> -->
      <el-table-column
        prop="projectInfo.projectName"
        label="项目名称"
        align="center"
        min-width="200"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <!-- <el-table-column
        prop="documentNumber"
        label="文号"
        align="center"
        width="140"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="establishmentName"
        label="名称"
        align="center"
        width="200"
        :show-overflow-tooltip="true"
      >
      </el-table-column> -->
      <!-- <el-table-column
        prop="establishmentApprovalUnit"
        label="审批单位"
        align="center"
        width="180"
        :show-overflow-tooltip="true"
      >
      <template v-slot="{row}">
        {{getDictName('xmgl-lxspjg',row.establishmentApprovalUnit)}}
      </template>
      </el-table-column>
       <el-table-column
        prop="establishmentDate"
        label="批复日期"
        align="center"
        width="130"
        :show-overflow-tooltip="true"
      >
      </el-table-column> -->
      <el-table-column
        prop="establishmentInvestmentAmount"
        label="总投资(万元)"
        align="center"
        min-width="120"
      >
      </el-table-column>
      <el-table-column
        prop="engineeringCost"
        label="工程费用(万元)"
        align="center"
        min-width="130"
      >
      </el-table-column>
      <el-table-column
        prop="reserveCost"
        label="预备费用(万元)"
        align="center"
        min-width="130"
      >
      </el-table-column>
      <el-table-column
        prop="engineeringOtherCost"
        label="建设其他费用(万元)"
        align="center"
        min-width="160"
      >
      </el-table-column>
      <!-- establishmentName -->
      <el-table-column
        prop="establishmentName"
        label="建设期贷款利息(万元)"
        align="center"
        min-width="180"
      >
      </el-table-column>
      <el-table-column
        prop="landCost"
        label="土地成本(万元)"
        align="center"
        min-width="130"
      >
      </el-table-column>

      <!-- <el-table-column
        prop="constructionLength"
        label="建设长度"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="constructionWidth" label="建设宽度" align="center">
      </el-table-column>
      <el-table-column
        prop="constructionHeight"
        label="建设高度"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="establishmentScale"
        label="建设规模和主要内容"
        align="center"
        :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="ruleForm"
        data-vv-scope="form"
        :model="form"
        :rules="rules"
        label-width="140px"
        v-if="dialogVisible"
        :label-position="labelPosition"
      >
        <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="engineeringCost"
            >
              <el-input
                type="number"
                placeholder="请输入工程费用"
                v-model="form.engineeringCost"
                @change="countTotal"
              >
                <i slot="suffix">万元</i>
              </el-input>
            </el-form-item>
            <!-- <el-form-item label="审批单位" prop="establishmentApprovalUnit">
               <dict-select
                v-model="form.establishmentApprovalUnit"
                dictCode="xmgl-lxspjg"
                placeholder="请选择审批单位"
                :clearable="true"
                style="width:100%"
              />
            </el-form-item> -->
            <!-- <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 :gutter="20">
          <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="establishmentName">
              <el-input
                v-model="form.establishmentName"
                placeholder="请输入名称"
                clearable
              />
            </el-form-item>
          </el-col>
        </el-row> -->
        <el-row>
          <el-col :span="12">
            <!-- <el-form-item label="批复日期" prop="establishmentDate">
              <el-date-picker
                v-model="form.establishmentDate"
                type="date"
                placeholder="请输入批复日期"
                class="basisType"
                value-format="yyyy-MM-dd"
                format="yyyy-MM-dd"
              />
            </el-form-item> -->
          </el-col>
          <el-col :span="12">

          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="预备费用"
              prop="reserveCost"
            >
              <el-input
                type="number"
                placeholder="请输入预备费用"
                v-model="form.reserveCost"
                @change="countTotal"
              >
                <i slot="suffix">万元</i>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="土地成本"
              prop="landCost"
            >
              <el-input
                type="number"
                placeholder="请输入土地成本"
                v-model="form.landCost"
                @change="countTotal"
              >
                <i slot="suffix">万元</i>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="建设其他费用"
              prop="engineeringOtherCost"
            >
              <el-input
                type="number"
                placeholder="请输入建设其他费用"
                v-model="form.engineeringOtherCost"
                @change="countTotal"
              >
                <i slot="suffix">万元</i>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="建设期贷款利息"
              prop="establishmentName"
            >
              <el-input
                placeholder="请输入建设期贷款利息"
                v-model="form.establishmentName"
                @change="countTotal"
              >
                <i slot="suffix">万元</i>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="总投资"
              prop="establishmentInvestmentAmount"
            >
              <el-input
                placeholder="请输入总投资"
                v-model="form.establishmentInvestmentAmount"
              >
                <i slot="suffix">万元</i>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="建筑面积"
              prop="constructionHeight"
            >
              <el-input
                type="number"
                placeholder="请输入建筑面积"
                v-model="form.constructionHeight"
              >
                <i slot="suffix">㎡</i>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="建设长度"
              prop="constructionLength"
            >
              <el-input
                type="number"
                placeholder="请输入建设长度"
                v-model="form.constructionLength"
              >
                <i slot="suffix">m</i>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="建设宽度"
              prop="constructionWidth"
            >
              <el-input
                type="number"
                placeholder="请输入建设宽度"
                v-model="form.constructionWidth"
              >
                <i slot="suffix">m</i>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-row>
          <el-col :span="12">
           
          </el-col>
          <el-col :span="12">
            <el-form-item label="建设高度" prop="constructionHeight">
              <el-input
                type="number"
                placeholder="请输入建设高度"
                v-model="form.constructionHeight"
              >
                <i slot="suffix">m</i>
              </el-input>
            </el-form-item>
          </el-col>
        </el-row> 
        <el-form-item label="建设规模" prop="establishmentScale">
          <el-input
            type="textarea"
            v-model="form.establishmentScale"
            placeholder="请输入建设规模"
            :rows="3"
          ></el-input>
        </el-form-item>-->

        <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'
import utils from '@/utils.js'
export default {
  name: 'FeasibilityStudyReply',
  components: {
    ProjectInfoDialog
  },
  props: {
    pid: {
      type: String,
      default: ''
    },
    isShow: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      importData: {
        title: '可研批复导入',
        templateName: '可研批复模板.xls',
        downloadUrl: '/../fvue/static/excel/ExportReply.xlsx',
        importUrl:
          window.context.cqxy +
          '/biz/feasiblePlan/feasibilityStudyReply/v1/importExcelData',
        showProject: true,
        projectId: ''
      },
      projectObj: [], //获取的所有项目数组
      labelPosition: 'right', //表单对齐方式
      searchForm: {
        projectId: '',
        establishmentName: '',
        documentNumber: '',
        establishmentApprovalUnit: ''
      }, //顶部搜索参数
      data: [], //表格数据
      pageResult: {
        page: 1,
        pageSize: 20,
        total: 0
      },
      form: {
        establishmentName: '',
        projectInfo: {
          projectName: '',
          projectCode: ''
        },
        accessoryInfo: []
      },
      dialogVisible: false,
      dialogTitle: '添加',
      //表单验证
      rules: {
        'projectInfo.projectName': {
          required: true,
          message: '项目名称不能为空',
          trigger: 'blur'
        }
        // 'projectInfo.projectCode': {
        //   required: true,
        //   message: '项目代码不能为空',
        //   trigger: 'blur',
        // },
        // documentNumber: {
        //   required: true,
        //   message: '文号不能为空',
        //   trigger: 'blur',
        // },
        // establishmentName: {
        //   required: true,
        //   message: '项目不能为空',
        //   trigger: 'blur',
        // },
        // establishmentDate: {
        //   required: true,
        //   message: '批复日期不能为空',
        //   trigger: 'blur',
        // },
        // establishmentApprovalUnit: {
        //   required: true,
        //   message: '审批单位不能为空',
        //   trigger: 'blur',
        // },
        // engineeringCost: {
        //   required: true,
        //   message: '工程费用不能为空',
        //   trigger: 'blur',
        // },
        // reserveCost: {
        //   required: true,
        //   message: '预备费用不能为空',
        //   trigger: 'blur',
        // },
        // landCost: {
        //   required: true,
        //   message: '土地成本不能为空',
        //   trigger: 'blur',
        // },
        // engineeringOtherCost: {
        //   required: true,
        //   message: '建设其他费用不能为空',
        //   trigger: 'blur',
        // },
        // establishmentInvestmentAmount: {
        //   required: true,
        //   message: '总投资不能为空',
        //   trigger: 'blur',
        // },
      }
    }
  },
  computed: {
    deleteUrl: function() {
      return (
        window.context.cqxy +
        '/biz/feasiblePlan/feasibilityStudyReply/v1/delete'
      )
    }
  },
  mounted() {
    this.initProjectAll()
  },
  methods: {
    //重置
    handleReset() {
      this.searchForm.projectId = ''
      // this.searchForm.documentNumber = ''
      // this.searchForm.establishmentName = ''
      this.searchForm.establishmentApprovalUnit = ''
      this.$refs['searchForm'].resetFields()
      this.pageResult = {
        page: 1,
        pageSize: 20,
        total: 0
      }
      this.$refs['searchForm'].resetFields()
      this.$nextTick(() => {
        this.$refs.AtTable.load()
      })
    },
    countTotal() {
      let a = FormMath.toNumber(this.form.engineeringCost)
      let b = FormMath.toNumber(this.form.reserveCost)
      let c = FormMath.toNumber(this.form.landCost)
      let d = FormMath.toNumber(this.form.engineeringOtherCost)
      let e = FormMath.toNumber(this.form.establishmentName)
      let ary = [a, b, c, d, e]
      this.form.establishmentInvestmentAmount = FormMath.tofixed(
        FormMath.sum(ary),
        2
      )
      // this.form.establishmentInvestmentAmount = total.toFixed(2)
    },
    handleSelectProject(data) {
      this.form.establishmentApprovalUnit = data.projectApprovalAuthority
      this.form.establishmentScale = data.projectContent
      this.form.projectInfo.projectName = data.projectName
      this.form.projectInfo.projectCode = data.projectCode
      this.form.projectId = data.id
      this.$refs.ruleForm.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,
        // establishment_name_: this.searchForm.establishmentName,
        establishment_approval_unit_: this.searchForm.establishmentApprovalUnit
      }
      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/feasiblePlan/feasibilityStudyReply/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
            }
          })
      }
    },
    //查询
    handleSearch() {
      this.$refs.AtTable.load()
    },
    afterSaveData() {
      setTimeout(() => {
        this.beforeCloseDialog()
        this.$refs.AtTable.load()
      }, 500)
    },
    beforeCloseDialog() {
      this.dialogVisible = false
    },
    handleAdd() {
      this.dialogTitle = '添加'
      this.dialogVisible = true
      let date = new Date()
      this.form = {
        documentNumber: '',
        establishmentName: '',
        establishmentApprovalUnit: '',
        establishmentDate: utils.formatDateYear(date),
        engineeringCost: 0,
        reserveCost: 0,
        landCost: 0,
        engineeringOtherCost: 0,
        establishmentName: 0,
        establishmentInvestmentAmount: 0,
        establishmentScale: '',
        projectId: '',
        projectInfo: {
          projectName: '',
          projectCode: ''
        },
        accessoryInfo: []
      }
      if (this.isShow) {
        let selectedProject = this.projectObj.find(item => {
          if (item.currentProject && item.currentProject == 1) {
            return item.currentProject == 1
          }
        })
        // this.form.establishmentApprovalUnit=selectedProject.projectApprovalAuthority
        this.form.establishmentScale = selectedProject.projectContent
        this.form.projectInfo.projectName = selectedProject.projectName
        this.form.projectInfo.projectCode = selectedProject.projectCode
        this.form.projectId = selectedProject.id
      } else {
        //根据项目id查询
        // this.form.establishmentApprovalUnit=this.projectObj[0].projectApprovalAuthority
        this.form.establishmentScale = this.projectObj[0].projectContent
        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/feasiblePlan/feasibilityStudyReply/v1/detail/' + data.id
        )
        .then(res => {
          this.dialogVisible = true
          let response = res.data.value
          this.form = response
        })
    },

    handleSelect(selection) {},
    handleDialogCancel() {
      this.dialogVisible = false
    },
    handleDialogClose() {
      this.dialogVisible = false
    },
    //提交保存
    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/feasiblePlan/feasibilityStudyReply/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,
        establishment_name_: this.searchForm.establishmentName,
        // establishment_date_: this.searchForm.establishmentDate,
        // document_number_: this.searchForm.documentNumber,
        establishment_approval_unit_: this.searchForm.establishmentApprovalUnit
      }
      if (!this.isShow) {
        search['pm.ID_'] = this.pid
        this.importData.projectId = this.pid
        this.searchForm.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/feasiblePlan/feasibilityStudyReply/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>
