<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="showFormDia"
      width="45%"
      height="600px"
      :before-close="handleClose"
    >
      <el-form
        ref="formInfo"
        :rules="rules"
        :model="params"
        label-width="150px"
      >
        <el-row>
          <el-col :span="18">
            <el-form-item label="巡检结构名称：" prop="structure">
              <el-input
                size="mini"
                placeholder="请输入巡检结构名称"
                v-model="params.structure"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>

            <el-col :span="18">
            <el-form-item label="巡检对象名称：" prop="position">
              <el-input
                size="mini"
                placeholder="请输入巡检对象名称"
                v-model="params.position"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
           <el-col :span="18" >
            <el-form-item label="巡检对象要求描述：" prop="description">
                <el-input
                    type="textarea"
                    placeholder="请输入巡检对象要求描述"
                    :rows = "8"
                    v-model="params.description"
                  >
                  </el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="goInfo">保 存</el-button>
        <el-button @click="handleClose">取 消</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import dsmInsPosition from '@/api/resIns/dsmInsPosition.js'
import req from '@/request.js'
export default {
  name: 'deviceForm-dialog',
  props: {
    showFormDia: {
      type: Boolean,
      default: false,
    },
    title: {
      type: String,
      default: '',
    },
    pId: {
      type: String,
      default: '',
    },
  },
  data() {
    return {
      stcds: [],
      gradeCodes: [],
      params: {
        structure: null, //巡检结构名称
        position: null, //巡检对象名称
        description: null, //巡检对象要求描述
      },
      rules: {
         structure: [
          {
            required: true,
            message: '请输入巡检结构名称',
            trigger: 'blur',
          },
        ],
         position: [
          {
            required: true,
            message: '请输入巡检对象名称',
            trigger: 'blur',
          },
        ],
   
      },
    }
  },
  created() {
    this.getDetailInfo()
    this.getList()
    this.getSysDict()
  },
  methods: {
    getList() {
      dsmInsPosition.getgqList({}, (res) => {
        this.stcds = res.value
        this.params.stcd = this.stcds[0].mpcd
      })
    },
    changeCode(val) {
      this.gradeCodes.forEach((e) => {
        if (e.value === val) {
          this.params.gradeName = e.name
        }
      })
    },
    //获取字典数据
    getSysDict() {
      let portal = window.context.portal
      return new Promise((resolve, reject) => {
        req.post(portal + '/sys/sysDictionary/v1/queryAllDictItems').then(
          (response) => {
          },
          (error) => {
            reject(error)
          }
        )
      })
    },
    // 根据ID查询
    getDetailInfo() {
      if (this.title === '修改') {
        dsmInsPosition.detailId(this.pId, (res) => {
          this.params = res.value;
          this.$set(this.params)
        })
      } else {
        this.params = {
          structure: null, //巡检结构名称
          position: null, //巡检对象名称
          description: null, //巡检对象要求描述
        }
      }
    },
    handleClose() {
      ;(this.params = {
        isNoteMsg: null,
        gradeCode: null,
        gradeName: null,
        stcd: null,
        yjz: null,
        shhjz: null,
        lhjz: null,
        sejz: null,
        esjz: null,
      }),
        this.$emit('closeFormDiao')
    },
    goInfo(e) {
      this.$refs.formInfo.validate((valid) => {
        if (valid) {
          if (this.title === '新增') {
            dsmInsPosition.addSave(this.params, (response) => {
              if (response.state) {
                this.$notify({
                  type: 'success',
                  message: '新增成功',
                  duration: 2000,
                  // offset: 80
                })
                this.$emit('closeFormDiao')
                this.$emit('refList')
              } else {
              }
            })
          }
          if (this.title === '修改') {
            dsmInsPosition.upData(this.params, (response) => {
              if (response.state) {
                this.$notify({
                  type: 'success',
                  message: '修改成功',
                  duration: 2000,
                  // offset: 80
                })
                this.$emit('closeFormDiao')
                this.$emit('refList')
              } else {
              }
            })
          }
        } else {
          return false
        }
      })
    },
  },
}
</script>
<style lang="scss" scoped>
.el-divider--horizontal {
  margin: 24px 0 5px 0;
  height: 7px !important;
  background: #88b4dd !important;
  .el-divider__text {
    font-size: 16px !important;
  }
}
// ::v-deep .el-input__inner {
//   border: 1px solid orange;
// }
// ::v-deep .el-textarea__inner {
//   border: 1px solid orange;
// }
</style>
