<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"
  >
    <!--     :default-sorter="[{direction: 'DESC', property: 'acceptanceDate'}]" -->
    <!-- <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%"
        ></project-info-selector>
        <el-date-picker
          v-model="searchForm.acceptanceDate"
          type="date"
          placeholder="选择验收时间"
          value-format="yyyy-MM-dd"
          format="yyyy-MM-dd"
          style="width: 100%"
        >
        </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"
        :loading="loading2"
        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.projectName"
        label="项目名称"
        :show-overflow-tooltip="true"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="completionAcceptanceDate"
        label="竣工验收时间"
        align="center"
        width="150"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="name"
        label="竣工验收名称"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="projectAcceptanceScope"
        label="工程验收范围"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="completionAcceptanceOpinions"
        label="竣工验收意见"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="remarks"
        label="备注"
        align="center"
        width="100"
        :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"
        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"
                :disabled="!isShow"
                clearable
              >
                <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="completionAcceptanceDate">
              <el-date-picker
                v-model="form.completionAcceptanceDate"
                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-form-item label="竣工验收名称" prop="name">
          <el-input
            v-model="form.name"
            placeholder="请输入竣工验收名称"
            clearable
          />
        </el-form-item>

        <el-form-item label="工程验收范围" prop="projectAcceptanceScope">
          <el-input
            type="textarea"
            v-model="form.projectAcceptanceScope"
            placeholder="请输入工程验收范围"
            clearable
          />
        </el-form-item>
        <el-form-item label="竣工验收意见" prop="completionAcceptanceOpinions">
          <el-input
            type="textarea"
            v-model="form.completionAcceptanceOpinions"
            placeholder="请输入竣工验收意见"
            clearable
          />
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input
            v-model="form.remarks"
            placeholder="请输入备注"
            type="textarea"
          ></el-input>
        </el-form-item>
        <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: 'CompletionAcceptanceSub',
  components: {
    ProjectInfoDialog,
  },
  props: {
    pid: {
      type: String,
      default: '',
    },
    isShow: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      importData: {
        title: '竣工验收',
        templateName: '竣工验收模板.xls',
        downloadUrl: '/../fvue/static/excel/completion.xls',
        importUrl:
          window.context.cqxy +
          '/biz/completionAcceptance/completionAcceptance/v1/importExcelData',
        showProject: true,
        projectId: ''
      },
      projectObj: [],
      loading: false,
      loading2:false,
      searchForm: {
        projectId: '',
        completionAcceptanceDate: '',
        approvalFilingAuthorityDate: '',
      }, //顶部搜索参数
      data: [], //表格数据
      pageResult: {
        page: 1,
        pageSize: 20,
        total: 0,
      },
      form: {},
      dialogVisible: false,
      dialogTitle: '添加',
      indexArr: [],
      querySearch: [],
      rules: {
        'projectInfo.projectName': {
          required: true,
          message: '请选择项目名称',
          trigger: 'blur',
        },
        completionAcceptanceDate: {
          required: true,
          message: '请选择竣工验收时间',
          trigger: 'blur',
        }
      },
    }
  },
  computed: {
    deleteUrl: function () {
      return (
        window.context.cqxy +
        '/biz/completionAcceptance/completionAcceptance/v1/delete'
      )
    },
  },
  mounted() {
    this.initProjectAll()
  },
  methods: {
    handleReset() {
      this.searchForm.projectId = ''
      this.searchForm.acceptanceDate = ''
      this.$refs['searchForm'].resetFields()
      this.pageResult = {
        page: 1,
        pageSize: 20,
        total: 0,
      }
      this.$refs['searchForm'].resetFields()
      this.$nextTick(() => {
        this.$refs.AtTable.load()
      })
    },
    //导入后重新加载数据
    load() {
      this.$refs.AtTable.load()
    },
    //导出
    handleExport() {
      this.loading2=true
      let param = {
        pageBean: {
          page: 1,
          pageSize: -1,
          showTotal: true,
        },
        querys: [
          {
          group: 'advance',
          operation: 'LIKE',
          property: 'PROJECT_ID_',
          relation: 'AND',
          value: this.pid,
        },
        ],
        sorter: [],
      }
      req
        .downloadPostCopy(
          '${cqxy}/biz/completionAcceptance/completionAcceptance/v1/exportDataToExcel',
          param
        )
        .then((res) => {
          if (res.status === 200) {
            this.loading2=false
            this.$message.success('导出成功')
          } else {
            this.loading2=false
            this.$message.error('导出失败')
          }
        })
    },
    handleFile(file) {
      this.form.accessoryInfo = file
    },
    handleAdd() {
      this.dialogTitle = '添加'
      this.form = {
         name:'',
         completionAcceptanceDate:'',
         completionAcceptanceOpinions:'',
         projectAcceptanceScope:'',
        projectId: '',
        projectInfo: {
          projectName: '',
          projectCode: '',
        },
        accessoryInfo: [],
      }
      this.dialogVisible = true

      this.form.projectInfo.projectName = this.projectObj.projectName
      this.form.projectInfo.projectCode = this.projectObj.projectCode
      this.form.handlerName = this.projectObj.projectManager
      this.form.projectId = this.projectObj.id
    },
    handleRowDBClick(data) {
      this.dialogTitle = '修改'
      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.handlerName = data.projectManager
      this.$refs.form.clearValidate()
    },
    handleSelect(selection) {},
    handleDialogCancel() {
      this.dialogVisible = false
    },
    handleDialogClose() {
      this.dialogVisible = false
    },
    //初始获取单个项目数据
    initProjectAll() {
      this.$http
        .post('${cqxy}/biz/projectManagement/pm/v1/detail/' + this.pid)
        .then(
          (res) => {
            this.projectObj = res.data.value
          },
          (error) => {
            reject(error)
          }
        )
    },
    //提交保存
    handleDialogSubmit() {
      this.$refs.form.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.loading = true
          this.$http
            .post(
              '${cqxy}/biz/completionAcceptance/completionAcceptance/v1/save',
              this.form
            )
            .then((res) => {
              if (res.data.state) {
                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 {
          this.$message({
            message: '*号必填项,不能为空!',
            type: 'warning',
          })
          return false
        }
      })
    },
    loadData(param, cb) {
      let query = [
        {
          group: 'advance',
          operation: 'LIKE',
          property: 'PROJECT_ID_',
          relation: 'AND',
          value: this.pid,
        },
      ]
      if (param.querys && param.querys.length) {
        param.querys.push(...query)
      } else {
        param.querys = query
      }
      this.$http
        .post(
          '${cqxy}/biz/completionAcceptance/completionAcceptance/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(() => {
          if (cb) {
            cb()
          }
        })
    },
    findinfo() {
      this.$refs.AtTable.load()
    },
  },
}
</script>
<style lang="scss" scoped>
@import '@/assets/css/common.scss';
</style>
