<template>
  <at-table
    @load="loadData"
    :data="data"
    :page-result="pageResult"
    ref="AtTable"
    :default-sorter="[{ direction: 'DESC', property: '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"
      >
        <project-info-selector
          v-model="searchForm.projectId"
          style="width: 100%"
        ></project-info-selector>
        <el-date-picker
          v-model="searchForm.establishmentDate"
          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="search-btn"
        >查询</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-data
        tempFiles=""
        bizCode=""
        :dataBaseOptions="dataBaseOptions"
        :originAPIOptions="originAPIOptions"
        @reload="load(exportQuery)"
      ></import-data>

      <export-data
        tempFiles=""
        bizCode=""
        :dataBaseOptions="dataBaseOptions"
        :originAPIOptions="originAPIOptions"
        :exportQuery="exportQuery"
      ></export-data>
    </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"
        :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="name"
        label="名称"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="documentNumber"
        label="编号"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="establishmentDate"
        label="审查日期"
        align="center"
        width="120"
        :show-overflow-tooltip="true"
      >
      </el-table-column>

      <el-table-column
        prop="reviewUnit"
        label="审查单位"
        align="center"
        width="120"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="proposal"
        label="建议"
        align="center"
        width="120"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="reviewComments"
        label="审查意见"
        align="center"
        width="120"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="conclusion"
        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="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="projectCode"
            >
              <el-input
                v-model="form.projectInfo.projectCode"
                placeholder="项目代码"
                disabled
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="项目名称"
              prop="projectInfo.projectName"
            >
              <el-input
                v-model="form.projectInfo.projectName"
                placeholder="请选择项目名称"
                @focus='handleFocusProject'
                clearable
              >
                <el-button
                  type="primary"
                  @click="handleFocusProject"
                  slot="append"
                  icon="el-icon-search"
                ></el-button>
              </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="reviewUnit"
            >
              <el-input
                v-model="form.reviewUnit"
                placeholder="请输入审查单位"
                clearable
              ></el-input>
            </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="选择审查日期"
                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="24">
            <el-form-item
              label="建议"
              prop="proposal"
            >
              <el-input
                v-model="form.proposal"
                placeholder="请输入建议"
                type='textarea'
                rows='3'
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item
              label="审查意见"
              prop="reviewComments"
            >
              <el-input
                v-model="form.reviewComments"
                placeholder="请输入审查意见"
                type='textarea'
                rows='3'
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-form-item
              label="结论"
              prop="conclusion"
            >
              <el-input
                v-model="form.conclusion"
                placeholder="请输入结论"
                type='textarea'
                rows='3'
                clearable
              ></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"
                rows='3'
                clearable
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <file-table
          @handleFile='handleFile'
          :file='form.accessoryInfo'
        />
      </el-form>
      <project-info-dialog
        ref="project"
        :single="true"
        @select="handleSelectProject"
      ></project-info-dialog>
    </template>
  </at-table>
</template>

<script>
import ProjectInfoDialog from '@/components/dialog/ProjectInfoDialog.vue'
export default {
  name: 'WaterConservation',
  components: {
    ProjectInfoDialog
  },
  data() {
    return {
      searchForm: {}, //顶部搜索参数
      dataBaseOptions: {
        dataBaseId: '',
        tableName: '', // 必填
        tableSchema: ''
      },
      originAPIOptions: {
        url: '', // 列表请求接口地址
        method: 'post', // 列表请求方法
        moduleRoot: '' // 微服务根路径
      },
      exportQuery: {},
      data: [], //表格数据
      pageResult: {
        page: 1,
        pageSize: 20,
        total: 0
      },
      form: {},
      dialogTitle: '添加',
      dialogVisible: false,
      loading: false,
      rules: {
        'projectInfo.projectName': {
          required: true,
          message: '请选择项目名称',
          trigger: 'blur'
        },
        documentNumber: {
          required: true,
          message: '请输入编号',
          trigger: 'blur'
        },
        reviewUnit: {
          required: true,
          message: '请输入审查单位',
          trigger: 'blur'
        },
        establishmentDate: {
          required: true,
          message: '请选择审查日期',
          trigger: 'blur'
        }
      }
    }
  },
  computed: {
    deleteUrl: function() {
      return (
        window.context.cqxy + '/biz/designEstimate/aquaticProtection/v1/delete'
      )
    }
  },
  methods: {
    handleAdd() {
      this.dialogTitle = '添加'
      this.form = {
        projectInfo: {
          projectCode: '',
          projectName: ''
        },
        accessoryInfo: []
      }
      this.dialogVisible = true
    },
    handleSelect(selection) {},
    handleDialogCancel() {
      this.dialogVisible = false
    },
    handleDialogClose() {
      this.dialogVisible = false
    },
    handleRowDBClick(data) {
      this.form = data
      this.dialogTitle = '修改'
      this.dialogVisible = true
    },
    handleFocusProject() {
      this.$refs.project.open()
    },
    handleSelectProject(data) {
      this.form.projectId = data.id
      this.form.projectInfo.projectCode = data.projectCode
      this.form.projectInfo.projectName = data.projectName
      this.$refs.form.clearValidate()
    },
    handleFile(file) {
      this.form.accessoryInfo = file
    },
    handleDialogSubmit() {
      this.$refs.form.validate(valid => {
        if (valid && this.form.accessoryInfo.length !== 0) {
          this.form.accessoryInfo.forEach(item => {
            if (item.id) {
              delete item.id
            }
          })
          this.loading = true
          this.$http
            .post(
              '${cqxy}/biz/designEstimate/aquaticProtection/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 if (this.form.accessoryInfo.length === 0) {
          this.$message.error('请至少上传一个附件')
        }
      })
    },
    findinfo() {
      let query = []
      let search = {
        project_id_: this.searchForm.projectId,
        establishment_date_: this.searchForm.establishmentDate
      }
      for (let i in search) {
        if (search[i]) {
          query.push({
            group: 'advance',
            operation: 'LIKE',
            property: i,
            relation: 'AND',
            value: search[i]
          })
        }
      }
      let data = {
        pageBean: {
          page: 1,
          pageSize: 20,
          showTotal: true
        },
        querys: query
      }
      this.$http
        .post('${cqxy}/biz/designEstimate/aquaticProtection/v1/page', data)
        .then(resp => {
          let response = resp.data.value
          this.data = response.rows
          this.pageResult = {
            page: response.page,
            pageSize: response.pageSize,
            total: response.total
          }
        })
    },
    //上传发送事件
    handleUpload() {
      this.$bus.$emit('openUploader')
    },
    loadData(param, cb) {
      param.params = {
        projectId: this.$route.params.id
      }
      this.$http
        .post('${cqxy}/biz/designEstimate/aquaticProtection/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()
          }
        })
    }
  }
}
</script>
<style lang="scss" scoped>
@import '@/assets/css/common.scss';
</style>
