<template>
  <el-dialog
    :title="classString.title"
    :visible.sync="dialogVisible"
    width="36%"
    :before-close="handleClose"
    :close-on-press-escape="false"
    :close-on-click-modal="false"
    :append-to-body="true"
    v-loading.fullscreen.lock="fullscreenLoading"
  >
    <el-form label-width="120px" :model="form" ref="form" :rules="rules">
      <el-form-item label="分类名称：" prop="name">
        <el-input
          placeholder="请输入名称"
          v-model="form.name"
          maxlength="64"
          show-word-limit
        ></el-input>
      </el-form-item>
      <el-form-item label prop="status">
        <template slot="label">
          <span style="word-spacing: 22px">状&emsp;态</span>：
        </template>
        <el-radio-group v-model="form.status">
          <el-radio :label="1">启用</el-radio>
          <el-radio :label="0">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        style="margin-bottom: 20px"
        label
        v-for="(item, index) in form.role"
        :key="index"
      >
        <template v-if="index == 0" slot="label">
          <span>可访问手机号：</span>
        </template>
        <div class="form_item_content">
          <el-input
            placeholder="请输入手机号"
            v-onlyInt
            clearable
            maxlength="11"
            v-model="item.value"
          ></el-input>
          <i class="iconfont icon-a-zu903" @click="addjurisdiction"></i>
          <i
            class="iconfont icon-a-zu904"
            v-if="form.role.length != 1"
            @click="deletjurisdiction(index)"
          ></i>
        </div>
      </el-form-item>
      <el-form-item label="可访问群组：">
        <el-select
          v-model="form.conversationList"
          multiple
          collapse-tags
          placeholder="请选择"
        >
          <el-option
            v-for="item in conversationList"
            :key="item.openConversationId"
            :label="item.conversationName"
            :value="item.openConversationId"
          >
          </el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button size="small" @click="handleClose">取 消</el-button>
      <el-button
        size="small"
        style="margin-left: 60px"
        type="primary"
        @click="submit('form')"
        >确 定</el-button
      >
    </span>
  </el-dialog>
</template>
<script>
import * as api from '@/api/api'
export default {
  data() {
    const validateUser = (rule, value, callback) => {
      if (!/^1[3456789]\d{9}$/.test(value)) {
        callback(new Error('请输入正确的手机号'))
      } else {
        callback()
      }
    }
    return {
      fullscreenLoading: false,
      dialogVisible: false,
      classString: {},
      form: {
        name: '',
        status: 1,
        role: [{ value: '' }],
        conversationList: []
      },
      conversationList: [],
      rules: {
        name: [{ required: true, trigger: 'blur', message: '请输入分类名称' }]
      }
    }
  },
  methods: {
    // 关闭弹框
    handleClose() {
      this.dialogVisible = false
      this.$parent.search()
      this.form = {
        name: '',
        status: 1,
        role: [{ value: '' }]
      }
      this.$refs['form'].resetFields()
    },
    // 打开弹框
    openClose(val) {
      if (val) {
        this.classString = val
        if (!val.show) {
          this.detailList()
        }
        this.conversationGetDropList()
      }
      this.title = val.title
      this.dialogVisible = true
    },
    /**
     * @method 分类管理详情
     * **/
    detailList() {
      let code = {
        typeId: this.classString.typeId
      }
      api.POST('/doc/type/detail', code).then(data => {
        if (data.code == 0) {
          this.form = data.data
          let role = []
          if (this.form.role && this.form.role.length != 0) {
            this.form.role.forEach(item => {
              role.push({ value: item })
            })
            this.form.role = role
          } else {
            this.form.role = [{ value: '' }]
          }
          let conversationList = []
          if (this.form.conversationList) {
            this.form.conversationList.forEach(item => {
              conversationList.push(item.openConversationId)
            })
          }
          this.form.conversationList = conversationList
        }
      })
    },
    /**
     * @method 增加访问权限
     * **/
    addjurisdiction() {
      this.form.role.push({ value: '' })
    },
    /**
     * @method 删除访问权限
     * **/
    deletjurisdiction(val) {
      this.form.role.splice(val, 1)
    },
    /**
     * @method 确定提交
     **/
    submit(formName) {
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.fullscreenLoading = true
          let role = []
          for (let item of this.form.role) {
            if (item.value && item.value.length != 11) {
              this.fullscreenLoading = false
              return this.$message.warning('请输入正确的手机号')
            }
            if (item.value) {
              role.push(item.value)
            }
          }
          let conversationList = []
          if (this.conversationList) {
            this.conversationList.forEach(item => {
              if (
                this.form.conversationList.find(
                  ltem => item.openConversationId == ltem
                )
              ) {
                conversationList.push({
                  chatId: item.chatId,
                  openConversationId: item.openConversationId
                })
              }
            })
          }
          let code = {
            linkUrl: '',
            name: this.form.name,
            role: role,
            status: this.form.status,
            conversationList: conversationList
          }
          this.classString.show ? this.addclass(code) : this.editclass(code)
        }
      })
    },
    /**
     * @method 新增
     * **/
    addclass(val) {
      api
        .POST('/doc/type/add', val)
        .then(data => {
          if (data.code == 0) {
            this.handleClose()
            this.$message.success(data.message)
            this.fullscreenLoading = false
          }
        })
        .catch(() => {
          this.fullscreenLoading = false
        })
    },
    /**
     * @method 编辑
     * **/
    editclass(val) {
      val.typeId = this.form.typeId
      api
        .POST('/doc/type/update', val)
        .then(data => {
          if (data.code == 0) {
            this.handleClose()
            this.$message.success(data.message)
            this.fullscreenLoading = false
          }
        })
        .catch(() => {
          this.fullscreenLoading = false
        })
    },
    //会话下拉列表
    conversationGetDropList() {
      api.GET('conversation/getDropList').then(res => {
        if (res.code == 0) {
          this.conversationList = res.data
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>
.form_item_content {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}
.icon-a-zu903 {
  color: #f23f3a;
  margin-left: 10px;
  margin-right: 20px;
  cursor: pointer;
  font-size: 22px;
}
.icon-a-zu904 {
  cursor: pointer;
  font-size: 22px;
}
.el-icon-minus {
  margin-right: 16px;
}
</style>