<template>
  <ai-detail>
    <template slot="title">
      <ai-title :title="params.id ? '编辑审批负责人' : '添加审批负责人'" isShowBack isShowBottomBorder @onBackClick="cancel(false)">
      </ai-title>
    </template>
    <template slot="content">
      <ai-card title="基本信息">
        <template #content>
          <el-form ref="form" class="ai-form" :model="form" label-width="110px" label-position="right">
            <el-form-item label="角色说明" style="width: 100%;" prop="">
              <p style="color: red">网格员上报疑似风险/建议解除风险对象信息后，需管理员确认纳入监测对象或解除风险。</p>
            </el-form-item>
            <el-form-item label="姓名" prop="name" :rules="[{ required: true, message: '请选择人员', trigger: 'blur' }]">
              <el-input disabled :value="form.name" size="small" placeholder="请选择人员">
                <template slot="append">
                  <ai-wechat-selecter refs="addTags" :instance="instance" v-model="users" @change="onChooseUser">
                    <el-button size="small">选择人员</el-button>
                  </ai-wechat-selecter>
                </template>
              </el-input>
            </el-form-item>
            <el-form-item style="width: 100%!important;" label="所在地区" prop="areaId" :rules="[{ required: true, message: '请选择所在地区', trigger: 'change' }]">
              <ai-area-select
                v-model="form.areaId"
                always-show
                :instance="instance"
                clearable
                @fullname="v=>form.areaName = v"
                :disabledLevel="disabledLevel"/>
            </el-form-item>
          </el-form>
        </template>
      </ai-card>
    </template>
    <template #footer>
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="confirm">提交</el-button>
    </template>
  </ai-detail>
</template>

<script>
  import { mapState } from 'vuex'
  export default {
    name: 'Add',

    props: {
      instance: Function,
      dict: Object,
      params: Object
    },

    data () {
      return {
        info: {},
        users: [],
        form: {
          name: '',
          areaName: '',
          areaId: ''
        },
        id: '',
        disabledLevel: 3
      }
    },

    computed: {
      ...mapState(['user'])
    },

    created () {
      this.form.areaName = this.user.info.areaName
      this.form.areaId = this.user.info.areaId
      this.disabledLevel = this.user.info.areaList.length
      if (this.params && this.params.id) {
        this.id = this.params.id
        this.getInfo(this.params.id)
      }
    },

    methods: {
      getInfo (id) {
        this.instance.post(`/app/apppreventionreturntopovertyriskperson/queryDetailById?id=${id}`).then(res => {
          if (res.code === 0) {
            this.form = {
              ...res.data
            }
          }
        })
      },

      onChooseUser (v) {

      },

      confirm () {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.instance.post(`/app/apppreventionreturntopovertyriskperson/addOrUpdate`, {
              ...this.form
            }).then(res => {
              if (res.code == 0) {
                this.$message.success('提交成功')
                setTimeout(() => {
                  this.cancel(true)
                }, 600)
              }
            })
          }
        })
      },

      cancel (isRefresh) {
        this.$emit('change', {
          type: 'List',
          isRefresh: !!isRefresh
        })
      }
    }
  }
</script>

<style scoped lang="scss">
</style>
