<template>
  <ai-detail class="AppAnnounceDetail">
    <template slot="title">
      <ai-title :title="id ? '编辑居民群发' : '添加居民群发'" isShowBack isShowBottomBorder @onBackClick="cancel(false)">
      </ai-title>
    </template>
    <template slot="content">
      <div class="AppAnnounceDetail-container">
        <el-form ref="form" class="left" :model="form" label-width="110px" label-position="right">
          <ai-card title="基本信息">
            <template #content>
              <div class="ai-form">
                <el-form-item label="任务名称" prop="name" style="width: 100%;" :rules="[{ required: true, message: '请输入任务名称', trigger: 'blur' }]">
                  <el-input size="small" placeholder="请输入任务名称" v-model="form.name"></el-input>
                </el-form-item>
                <el-form-item label="发送范围" style="width: 100%;" prop="gender">
                  <el-radio-group v-model="form.gender">
                    <el-radio label="1">全部居民群</el-radio>
                    <el-radio label="2">按部门选择</el-radio>
                    <el-radio label="3">按网格选择</el-radio>
                  </el-radio-group>
                </el-form-item>
                <el-form-item label="消息提醒" prop="name" style="width: 100%;" :rules="[{ required: true, message: '请输入任务名称', trigger: 'blur' }]">
                  <el-switch
                    v-model="form.status"
                    active-text="开启后，创建的群发任务需要审批人进行审批">
                  </el-switch>
                </el-form-item>
              </div>
            </template>
          </ai-card>
        </el-form>
        <div class="right"></div>
      </div>
    </template>
    <template #footer>
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="confirm">提交</el-button>
    </template>
  </ai-detail>
</template>

<script>
  export default {
    name: 'Add',

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

    data () {
      const validatorPhone = function (rule, value, callback) {
        if (value === '') {
          callback(new Error('请输入手机号'))
        } else if (!/^1\d{10}$/.test(value)) {
          callback(new Error('手机号格式错误'))
        } else {
          callback()
        }
      }
      return {
        info: {},
        department: [],
        validatorPhone: validatorPhone,
        form: {
          position: '',
          name: '',
          email: '',
          telephone: '',
          gender: '',
          mobile: '',
          departmentName: '',
          departmentIds: [],
          tagIds: [],
          id: '',
          status: false
        },
        id: '',
        tagsList: []
      }
    },

    created () {
      this.getTags()

      if (this.params && this.params.departmentId && !this.params.id) {
        this.department = [{
          id: String(this.params.departmentId),
          name: this.params.departmentName
        }]
        this.form.departmentIds = [this.params.departmentId]
        this.form.departmentName = this.params.departmentName
      }

      if (this.params && this.params.id) {
        this.id = this.params.id
        this.getInfo(this.params.id)
      }
    },

    methods: {
      getInfo (id) {
        this.instance.post(`/app/wxcp/wxuser/queryDetailById?id=${id}`).then(res => {
          if (res.code === 0) {
            this.form = {
              ...res.data,
              departmentName: res.data.departmentNames,
              tagIds: res.data.tags.map(v => v.id),
              departmentIds: res.data.departmentIdsStr.split(',')
            }
          }
        })
      },

      onChange (e) {
        if (e.length) {
          this.form.departmentIds = e.map(v => v.id)
          this.form.departmentName = e.map(v => v.name).join(',')
        } else {
          this.form.departmentIds = ''
          this.form.departmentName = ''
        }
      },

      getTags () {
        this.instance.post(`/app/wxcp/wxtag/listAll`).then(res => {
          if (res.code == 0) {
            this.tagsList = res.data
          }
        })
      },

      onClose () {
        this.form.explain = ''
      },

      confirm () {
        this.$refs.form.validate((valid) => {
          if (valid) {
            const api = this.id ? '/app/wxcp/wxuser/update' : '/app/wxcp/wxuser/add'
            this.instance.post(api, {
              ...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">
  .AppAnnounceDetail {
    ::v-deep  .ai-detail__content {
      .ai-detail__content--wrapper {
        position: relative;
        max-width: 100%;
        margin: 0;
        height: 100%;
        overflow: hidden;
      }
    }

    .AppAnnounceDetail-container {
      display: flex;
      position: relative;
      height: 100%;
      padding: 0 20px;
      overflow-y: overlay;

      .left {
        flex: 1;
        margin-right: 20px;
      }

      .right {
        position: sticky;
        top: 0;
        width: 400px;
        height: 90%;
        background: red;
      }
    }
  }
</style>
