<!-- 新增、编辑井（数据预录入使用） -->
<template>
  <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible" :close-on-click-modal="false" append-to-body>
    <el-form ref="dataForm" :rules="rules" :model="form" label-well-code="right" label-width="120px">
      <el-form-item label="所属项目" prop="projectId">
        <el-select v-model="form.projectId" filterable placeholder="请选择" @change="changeSelectProject">
          <el-option
            v-for="item in projectList"
            :key="item.id"
            :label="item.projectName"
            :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item class="selectForm-container-item" label="井编号" prop="wellCode">
        <el-input v-model.trim="form.wellCode" placeholder="井编号" clearable/>
      </el-form-item>
      <el-form-item class="selectForm-container-item" label="井名称" prop="wellName">
        <el-input v-model.trim="form.wellName" placeholder="井名称" clearable/>
      </el-form-item>
      <el-form-item class="selectForm-container-item" label="井类型" prop="wellType">
        <el-select v-model="form.wellType" filterable placeholder="井类型" clearable value="" >
          <el-option
            v-for="item in wellTypeList"
            :key="item.id"
            :label="item.name"
            :value="item.id"/>
        </el-select>
      </el-form-item>
      <el-form-item class="selectForm-container-item" label="位置" prop="position">
        <el-input v-model.trim="form.position" placeholder="位置" clearable/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="saveData">提交</el-button>
      <el-button @click="cancel">取消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getProjectListUnpageSegment } from '@/api/factoryManager/project'
import { addFactoryManagerList, editFactoryManagerList } from '@/api/factoryManager/dataPreentry'
export default {
  name: 'AddUserDialog',
  data() {
    return {
      dialogFormVisible: false, // 对话框是否显示
      dialogStatus: '', // 对话框类型：create,update
      form: {
        wellCode: '', // 井编号
        wellName: '', // 名称
        projectId: '', // 所属项目
        wellType: '', // 井类型
        id: '' // 主键
      }, // 表单
      textMap: {
        update: '编辑',
        create: '新增',
        detail: '详情'
      }, // 表头显示标题
      rules: {
        wellCode: [{ required: true, message: '井编号不能为空', trigger: ['blur', 'change'] }],
        wellType: [{ required: true, message: '井类型不能为空', trigger: ['blur', 'change'] }],
        projectId: [{ required: true, message: '所属项目不能为空', trigger: ['blur', 'change'] }],
        wellName: [{ required: true, message: '井名称不能为空', trigger: ['blur', 'change'] }]
      }, // 前端校验规则
      projectList: [],
      wellTypeList: [
        {
          id: '1',
          name: '液位井'
        },
        {
          id: '2',
          name: '燃气井'
        }
      ]
    }
  },
  created() {
    this.fetchProjectListUnpageSegment() // 获取项目列表
  },
  methods: {
    // 初始化对话框
    initDialog: function(dialogStatus, dialogFormVisible, row = null) {
      this.dialogStatus = dialogStatus // 类型 新增create 编辑update 详情detail
      this.dialogFormVisible = dialogFormVisible // 对话框显隐
      if (dialogStatus === 'create') { // 如果是新增，清除验证
        this.resetForm()
        this.$nextTick(() => {
          this.$refs['dataForm'].clearValidate()
        })
      } else if (dialogStatus === 'update' || dialogStatus === 'detail') { // 如果是修改，将row中数据填写到输入框中
        this.form = {
          wellCode: row.wellCode, // 井编号
          wellName: row.wellName, // 井名称
          wellType: row.wellType, // 井类型
          position: row.position, // 位置
          projectId: row.projectId, // 所在项目
          id: row.id
        }
        console.log(this.form)
      }
    },
    // 重置表单
    resetForm() {
      this.form = {
        wellCode: '', // 井编号
        wellName: '', // 井名称
        wellType: '', // 井类型
        position: '', // 位置
        projectId: '', // 所在项目
        id: '' // 主键
      }
    },
    // 获取项目列表
    fetchProjectListUnpageSegment() {
      getProjectListUnpageSegment().then(response => {
        this.projectList = response.data
      })
    },
    // 点击提交
    saveData: function() {
      if (this.dialogStatus === 'update') {
        this.updateData()
      } else if (this.dialogStatus === 'create') {
        this.createData()
      }
    },
    // 新增数据
    createData: function() {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          addFactoryManagerList(this.form).then(response => {
            if (response.code === 200) {
              this.$confirm('新增成功，是否继续新增?', '提示', {
                confirmButtonText: '是',
                cancelButtonText: '否',
                type: 'info'
              }).then(() => {
                this.resetForm()
                this.$nextTick(() => {
                  this.$refs['dataForm'].clearValidate()
                })
              }).catch(() => {
                this.$emit('watchChild')
                this.dialogFormVisible = false
              })
            }
          })
        }
      })
    },
    // 修改数据
    updateData: function() {
      this.$refs['dataForm'].validate((valid) => {
        // 在这里注意如果密码是空的话，就默认111111
        if (valid) {
          editFactoryManagerList(this.form).then(response => {
            if (response.code === 200) {
              this.$message.success('修改成功')
              this.$emit('watchChild')
              this.dialogFormVisible = false
            }
          })
        }
      })
    },
    // 点击取消
    cancel: function() {
      this.dialogFormVisible = false
      this.$emit('watchChild')
    },
    // 所在项目选中值发生变化
    changeSelectProject(val) {
      console.log(val)
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  .el-select{
    width: 100%;
  }
  .el-date-editor{
    width: 100%;
  }
</style>
