<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="项目名称" prop="proName">
        <el-input
          v-model="queryParams.proName"
          placeholder="请输入项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="堪查人" prop="surveyUser">
        <el-input
          v-model="queryParams.surveyUser"
          placeholder="请输入堪查人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="勘查时间" prop="surveyTime">
        <el-date-picker v-model="queryParams.surveyTime"
                        format="yyyy-MM-dd"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择日期选择" clearable>
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['basic:survey:add']"
        >新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="proList" @selection-change="handleSelectionChange">
      <el-table-column label="序号" align="center" type="index" />
      <el-table-column label="所属项目" align="center" prop="proName" />
      <el-table-column label="堪查人" align="center" prop="surveyUser" />
      <el-table-column label="勘查附件" align="center" prop="surveyAttach" />
      <el-table-column label="勘查内容" align="center" prop="surveyContent" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['basic:survey:edit']"
          >编辑</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['basic:survey:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改岗位对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="项目名称" prop="proId">
          <el-select v-model="form.proId" placeholder="请选择项目">
            <el-option
              v-for="item in projectOptions"
              :key="item.proId"
              :label="item.proName"
              :value="item.proId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="勘查时间" prop="surveyTime">
          <el-date-picker v-model="form.surveyTime"
                          format="yyyy-MM-dd"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择日期选择" clearable>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="勘查人" prop="surveyUser">
          <el-input v-model="form.surveyUser" placeholder="请输入勘查人" />
        </el-form-item>
        <el-form-item label="勘查内容" prop="surveyContent">
          <el-input v-model="form.surveyContent" placeholder="请输入勘查内容" />
        </el-form-item>
        <el-form-item label="勘查附件" prop="surveyAttach">
            <el-upload
            action=""
            :auto-upload="false"
            :multiple="true"
            :limit="3"
            :on-change="handleFileChange"
            :http-request="customUpload"
            :file-list="fileList">
            <el-button size="small">上传附件</el-button>
            <div slot="tip">支持扩展名：.doc/.pdf/.xlsx</div>
            </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listSurvey, getSurvey, delSurvey, addSurvey, updateSurvey, projectSelect } from "@/api/basic/survey"

export default {
  name: "Post",
  dicts: ['sys_normal_disable'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 项目选择
      projectOptions: [],
      // 岗位表格数据
      proList: [],
      // 弹出层标题
      title: "",
      // 日期范围
      dateRange: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        proName: undefined,
        unit: undefined,
        chargePerson: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        proId: [
          { required: true, message: "项目名称不能为空", trigger: "blur" }
        ],
        surveyTime: [
          { required: true, message: "勘查时间不能为空", trigger: "blur" }
        ],
        surveyUser: [
          { required: true, message: "勘查人不能为空", trigger: "blur" }
        ],
        surveyContent: [
          { required: true, message: "勘查内同不能为空", trigger: "blur" }
        ]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询岗位列表 */
    getList() {
      this.loading = true
      listSurvey(this.queryParams).then(response => {
        this.proList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        proId: undefined,
        proName: undefined,
        proType: undefined,
        unit: undefined,
        chargePerson: undefined,
        location: undefined,
        remark: undefined,
        dataRange: undefined
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      projectSelect().then(response => {
        this.open = true
        this.projectOptions = response.rows
        this.title = "添加勘查日志"
      })
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      projectSelect().then(response => {
        this.projectOptions = response.rows
      })
      getSurvey(id).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改勘查日志"
      })
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateSurvey(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addSurvey(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除项目编号为"' + ids + '"的数据项？').then(function() {
        return delSurvey(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },

  }
}
</script>
