<template>
  <at-table
    @load="loadData"
    :data="data"
    :page-result="pageResult"
    ref="AtTable"
    :dialogTitle="dialogTitle"
    :dialogVisible="dialogVisible"
    :default-sorter="[{direction: 'DESC',property: 'establishmentDate'}]"
    @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="findinfo"
        ></project-info-selector>
        <el-input
          v-model="searchForm.code"
          style="width: 100%"
          placeholder="请输入证件编号"
          clearable
          @keyup.enter.native="findinfo"
        ></el-input>
        <el-date-picker
          v-model="searchForm.establishmentDate"
          type="date"
          placeholder="选择发证日期"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          style="width: 100%"
          @keyup.enter.native="findinfo"
        >
        </el-date-picker>
        <!-- <el-date-picker
          v-model="searchForm.contractDuration"
          unlink-panels
          placeholder="请选择合同工期"
          type="daterange"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width: 100%; padding: 0; padding-left: 10px; margin-left: 10px"
          @keyup.enter.native="findinfo"
        >
        </el-date-picker> -->
        <el-button
          size="small"
          @click="findinfo"
          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="项目名称"
        :show-overflow-tooltip="true"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="code"
        label="证件编号"
        align="center"
        width="220"
        :show-overflow-tooltip="true"
      >
      </el-table-column>

      <el-table-column
        prop="contractDuration"
        label="工期"
        align="center"
        width="120"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <!-- <el-table-column
        prop="contractPrice"
        label="合同价格(万元)"
        align="center"
        width="130"
        :show-overflow-tooltip="true"
      >
      </el-table-column> -->
      <el-table-column
        prop="establishmentApprovalUnit"
        label="发证机关"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="establishmentDate"
        label="发证日期"
        align="center"
        width="150"
        sortable
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <!-- <el-table-column
        prop="name"
        label="建设单位"
        align="center"
        min-width="100"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="constructionUnit"
        label="施工单位"
        align="center"
        width='120'
        :show-overflow-tooltip="true"
      >
      <template v-slot="{row}">
        {{getDictName('sgxk-sgdw',row.constructionUnit)}}
      </template>
      </el-table-column>
      <el-table-column
        prop="designUnit"
        label="设计单位"
        align="center"
        width="130"
        :show-overflow-tooltip="true"
      >
      <template v-slot="{row}">
        {{getDictName('sgxk-sjdw',row.designUnit)}}
      </template>
      </el-table-column>
      <el-table-column
        prop="supervisorUnit"
        label="监理单位"
        align="center"
        width="120"
        :show-overflow-tooltip="true"
      >
      <template v-slot="{row}">
        {{getDictName('sgxk-jldw',row.supervisorUnit)}}
      </template>
      </el-table-column>
      <el-table-column
        prop="surveyUnit"
        label="勘察单位"
        align="center"
        width="120"
        :show-overflow-tooltip="true"
      >
       <template v-slot="{row}">
        {{getDictName('sgxk-kcdw',row.surveyUnit)}}
      </template>
      </el-table-column> -->

      <el-table-column
        prop="establishmentRemarks"
        label="备注"
        align="center"
        width="120"
        :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="190px"
        v-if="dialogVisible"
        v-loading.fullscreen="loading"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="项目名称"
              prop="projectInfo.projectName"
            >
              <el-input
                v-model="form.projectInfo.projectName"
                placeholder="请选择项目名称"
                @focus="handleFocusProject"
                clearable
                :disabled="!isShow"
              >
                <el-button
                  type="primary"
                  @click="openProject"
                  slot="append"
                
                  icon="el-icon-search"
                ></el-button>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="证件编号"
              prop="code"
            >
            <!--  v-pinyin="form.name" -->
              <el-input
                v-model="form.code"
                placeholder="请输入证件编号"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="发证机关"
              prop="establishmentApprovalUnit"
            >
              <el-input
                v-model="form.establishmentApprovalUnit"
                placeholder="请输入发证机关"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="发证日期"
              prop="establishmentDate"
            >
              <el-date-picker
                v-model="form.establishmentDate"
                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="contractDuration"
            >
             <el-input
                v-model="form.contractDuration"
                placeholder="请输入工期"
                clearable
              />
              <!-- <el-date-picker
                v-model="form.contractDuration"
                unlink-panels
                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-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="person"
            >
              <el-input
                v-model="form.person"
                placeholder="请输入建设单位"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="建设单位负责人联系电话"
              prop="phone"
            >
              <el-input
                v-model="form.phone"
                placeholder="请输入建设单位负责人联系电话"
                clearable
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="勘察单位"
              prop="surveyUnit"
            >
              <dict-select
                dictCode="sgxk-kcdw"
                :hasDefaultOption="true"
                clearable
                placeholder="请输入勘察单位"
                v-model="form.surveyUnit"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="勘察单位负责人"
              prop="surveyUnitPerson"
            >
              <el-input
                v-model="form.surveyUnitPerson"
                placeholder="请输入勘察单位负责人"
                clearable
                filterable
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="勘察单位负责人联系电话"
              prop="surveyUnitPhone"
            >
              <el-input
                v-model="form.surveyUnitPhone"
                placeholder="请输入勘察单位负责人联系电话"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="设计单位"
              prop="designUnit"
            >
              <dict-select
                dictCode="sgxk-sjdw"
                :hasDefaultOption="true"
                clearable
                filterable
                placeholder="请输入设计单位"
                v-model="form.designUnit"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="设计单位负责人"
              prop="designUnitPerson"
            >
              <el-input
                v-model="form.designUnitPerson"
                placeholder="请输入设计单位负责人"
                clearable
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="设计单位负责人联系电话"
              prop="designUnitPhone"
            >
              <el-input
                v-model="form.designUnitPhone"
                placeholder="请输入设计单位负责人联系电话"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="施工单位"
              prop="constructionUnit"
            >
              <dict-select
                dictCode="sgxk-sgdw"
                :hasDefaultOption="true"
                clearable
                filterable
                placeholder="请输入施工单位"
                v-model="form.constructionUnit"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="施工单位负责人"
              prop="constructionUnitPerson"
            >
              <el-input
                v-model="form.constructionUnitPerson"
                placeholder="请输入施工单位负责人"
                clearable
                
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="施工单位负责人联系电话"
              prop="constructionUnitPhone"
            >
              <el-input
                v-model="form.constructionUnitPhone"
                placeholder="请输入施工单位负责人联系电话"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="监理单位"
              prop="supervisorUnit"
            >
              <dict-select
                dictCode="sgxk-jldw"
                :hasDefaultOption="true"
                clearable
                filterable
                placeholder="请输入监理单位"
                v-model="form.supervisorUnit"
                style="width: 100%"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="监理单位负责人"
              prop="supervisorUnitPerson"
            >
              <el-input
                v-model="form.supervisorUnitPerson"
                placeholder="请输入监理单位负责人"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="监理单位负责人联系电话"
              prop="supervisorUnitPhone"
            >
              <el-input
                v-model="form.supervisorUnitPhone"
                placeholder="请输入监理单位负责人联系电话"
                clearable
              >
              </el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <!-- <el-row>
          <el-col :span="12"> -->
            <el-form-item
              label="建设地址"
              prop="proposedLocation"
            >
              <el-input
                v-model="form.proposedLocation"
                placeholder="请输入建设地址"
                clearable
              ></el-input>
            </el-form-item>
          <!-- </el-col>
        </el-row> -->
        <el-row>
          <el-col :span="24">
            <el-form-item
              label="建设规模"
              prop="proposedLandArea"
            >
              <el-input
                v-model="form.proposedLandArea"
                placeholder="请输入建设规模"
                clearable
                type="textarea"
              ></el-input>
            </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"
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <file-table
          @handleFile="handleFile"
          :file="form.accessoryInfo"
          :info='"上传施工许可证书等文件"'
        />
        <project-info-dialog
          ref="project"
          :single="true"
          @select="handleSelectProject"
        ></project-info-dialog>
      </el-form>
    </template>
  </at-table>
</template>

<script>
import req from '@/request.js'
import ProjectInfoDialog from '@/components/dialog/ProjectInfoDialog.vue'
export default {
  name: 'ConstructionPermit',
  components: {
    ProjectInfoDialog
  },
  props: {
    pid: {
      type: String,
      default: ''
    },
    isShow: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      importData: {
        title: '施工许可导入',
        templateName: '施工许可模板.xls',
        downloadUrl: './../static/excel/ExportConstructionPermit.xlsx',
        importUrl:
          window.context.cqxy + '/biz/constructionPermit/cp/v1/importExcelData',
        showProject: true,
        projectId: ''
      },
      projectObj: [],
      loading: false,
      searchForm: {
        projectId: '',
        establishmentDate: '',
        contractDuration: '',
        name: '',
        code:''
      }, //顶部搜索参数
      data: [], //表格数据
      pageResult: {
        page: 1,
        pageSize: 20,
        total: 0
      },
      form: {},
      dialogVisible: false,
      dialogTitle: '添加',
      indexArr: [],
      querySearch: [],
      rules: {
        'projectInfo.projectName': {
          required: true,
          message: '请选择项目名称',
          trigger: 'blur'
        },
        // 'projectInfo.projectCode': {
        //   required: true,
        //   message: '请选择项目代码',
        //   trigger: 'blur'
        // },
        name: {
          required: true,
          message: '请输入名称',
          trigger: 'blur'
        },
        code: {
          required: true,
          message: '请输入编号',
          trigger: 'blur'
        },
        // constructionUnit: {
        //   required: true,
        //   message: '请输入施工单位',
        //   trigger: 'blur'
        // },
        contractPrice: {
          required: true,
          message: '请输入合同价格',
          trigger: 'blur'
        },
        createCompanyName: {
          required: true,
          message: '请输入创建人单位名称',
          trigger: 'blur'
        },
        createName: {
          required: true,
          message: '请输入创建人姓名',
          trigger: 'blur'
        },
        createOrgName: {
          required: true,
          message: '请输入创建人组织名称',
          trigger: 'blur'
        },
        // designUnit: {
        //   required: true,
        //   message: '请输入设计单位',
        //   trigger: 'blur'
        // },
        establishmentApprovalUnit: {
          required: true,
          message: '请输入发证机关',
          trigger: 'blur'
        },
        establishmentDate: {
          required: true,
          message: '请选择发证日期',
          trigger: 'blur'
        }
      }
    }
  },
  computed: {
    deleteUrl: function() {
      return window.context.cqxy + '/biz/constructionPermit/cp/v1/delete'
    }
  },
  mounted() {
    this.initProjectAll()
  },
  methods: {
    //重置
    handleReset() {
      this.searchForm.projectId = ''
      this.searchForm.establishmentDate = ''
      this.searchForm.name = ''
      this.searchForm.contractDuration = ''
      this.searchForm.code=''
      this.$refs['searchForm'].resetFields()
      this.pageResult = {
        page: 1,
        pageSize: 20,
        total: 0
      }
      this.$nextTick(() => {
        this.$refs.AtTable.load()
      })
    },
    //导入后重新加载数据
    load() {
      this.$refs.AtTable.load()
    },
    //导出
    handleExport() {
      let query = []
      let search = {
        project_id_: this.searchForm.projectId,
        name_: this.searchForm.name,
        establishment_date_: this.searchForm.establishmentDate,
        contract_duration_: this.searchForm.contractDuration,
        code_:this.searchForm.code,
      }
      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/constructionPermit/cp/v1/exportDataToExcel',
          param
        )
        .then(res => {
          if (res.status === 200) {
            this.$message.success('导出成功')
          } else {
            this.$message.error('导出失败')
          }
        })
    },
    handleFile(file) {
      this.form.accessoryInfo = file
    },
    handleAdd() {
      this.dialogTitle = '添加'
      this.form = {
        name: '重庆西永微电子产业园区开发有限公司',
        code: '',
        projectInfo: {
          projectName: '',
          projectCode: ''
        },
        accessoryInfo: [],
        proposedLocation:''
      }
      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
        this.form.proposedLocation = selectedProject.projectAddress
      } 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
        this.form.proposedLocation = this.projectObj[0].projectAddress
      }
    },
    handleRowDBClick(data) {
      this.dialogTitle = '修改'
      // let time = data.contractDuration
      // if (time) {
      //   this.form.contractDuration = time.split('~')
      // }

      this.form = data
      this.dialogVisible = true
    },
    handleFocusProject() {
      this.$refs.project.open()
    },
    openProject() {
      this.$refs.project.open()
    },
    handleSelectProject(data) {
      this.form.projectId = data.id
      this.form.projectInfo.projectCode = data.projectCode
      this.form.projectInfo.projectName = data.projectName
      this.form.proposedLocation = data.projectAddress
      this.$refs.form.clearValidate()
    },
    handleSelect(selection) {},
    handleDialogCancel() {
      this.dialogVisible = false
    },
    handleDialogClose() {
      this.dialogVisible = false
    },
    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
            }
          })
      }
    },
    //提交保存
    handleDialogSubmit() {
      this.$refs.form.validate(valid => {
        if (valid && this.form.accessoryInfo.length !== 0) {
          if (this.form.accessoryInfo && this.form.accessoryInfo.length) {
            this.form.accessoryInfo.forEach(item => {
              if (item.id) {
                delete item.id
              }
            })
          }
          this.loading = true
          delete this.form.projectName
          delete this.form.projectCode
          // let time = this.form.contractDuration
          // if (typeof time == 'object') {
          //   this.form.contractDuration = time.join('~')
          // }
          this.$http
            .post('${cqxy}/biz/constructionPermit/cp/v1/save', this.form)
            .then(res => {
              if (res.status == 200) {
                this.$refs.form.resetFields()
                this.$message.success(res.data.message)
                this.dialogVisible = false
                this.$refs.AtTable.load()
              } else {
                this.$message.error(res.data.message)
              }
              this.loading = false
            })
        } else if (this.form.accessoryInfo.length === 0) {
          this.$message.error('请至少上传一个附件')
        } else {
          this.$message({
            message: '*号必填项,不能为空!',
            type: 'warning'
          })
          return false
        }
      })
    },
    //查询
    findinfo() {
      this.$refs.AtTable.load()
    },
    loadData(param, cb) {
      let query = []
      let search = {
        project_id_: this.searchForm.projectId,
        name_: this.searchForm.name,
        establishment_date_: this.searchForm.establishmentDate,
        contract_duration_: this.searchForm.contractDuration,
        code_:this.searchForm.code
      }
      if (!this.isShow) {
        search['project_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/constructionPermit/cp/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';
/deep/ .el-range-editor--small .el-range-separator {
  display: contents;
}
</style>
