<template lang="pug">
  <div class='m_i_dialog'>
    <el-dialog :title="title" width="450px" :close-on-click-modal='false' :visible.sync='visible' append-to-body @close="closeForm('dataForm')">
         <span slot='title' class="title-bold">{{title}}巡查线路</span>
         <el-form :model='dataForm' ref='dataForm' label-width="120px" :rules="rules">
            <el-form-item label='巡查线路名称:' prop='name'>
              <el-input v-model='dataForm.name' maxlength="100" placeholder="请输入内容"></el-input>
            </el-form-item>
            <el-form-item label='巡查线路描述:' prop='byx1'>
              <el-input v-model='dataForm.byx1' type="textarea" placeholder="请输入内容" maxlength="100" show-word-limit style="width:100%" resize="none"></el-input>
            </el-form-item>
         </el-form>
         <span class="dialog-footer" slot='footer'>
            <el-button type="primary" @click='cancel'>取消</el-button>
            <el-button type="primary" @click='saveClick' v-prevent-re-click>保存</el-button>
         </span>
    </el-dialog>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  computed: {
    ...mapState('d2admin/user', ['info'])
  },
  data() {
    return {
      visible: false,
      dataForm: {
        byx1: '',
        name: ''
      },
      title: '新增',
      sid: '',
      rules: {
          name: [
              { required: true, min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
          ]
      }
    }
  },
  created() {},
  methods: {
    init(sid, res) {
      this.visible = true
      this.sid = sid
      this.dataForm = {}
      if (res) {
        this.title = '编辑'
        this.dataForm = { ...res }
      } else {
        this.title = '新增'
        this.dataForm = {
          sysSign: '',
          name: ''
        }
      }
    },
    saveClick() {
        this.$refs.dataForm.validate((valid) => {
            if (valid) {
                let url = ''
                if (this.title === '编辑') {
                    url = this.$http.adornUrlEq('/epPatrolLine/update')
                } else {
                    url = this.$http.adornUrlEq('/epPatrolLine/save')
                }
                this.$http({
                    url: url,
                    method: 'post',
                    data: {
                        id: this.dataForm.id,
                        stationId: this.sid,
                        name: this.dataForm.name,
                        byx1: this.dataForm.byx1
                    }
                }).then((data) => {
                    if (data && data.code === 0) {
                        this.$message.success('操作成功')
                        this.visible = false
                        this.$emit('closeQ')
                    }
                })
            }
        })
    },
    //取消
    cancel() {
      this.$confirm('确认取消?', this.title + '巡查线路', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.closeForm('dataForm')
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消退出'
          })
        })
    },
    closeForm (formRule) {
      this.dataForm = {}
      this.$refs[formRule].resetFields()
      this.visible = false
    }
  }
}
</script>
<style></style>
