<template>
  <div class="app-container">
    <el-row class="m-t-0">
      <el-col :span="24">
        <el-container>
          <el-header height="42px" class="l-h-42 p-l-10 p-r-10 border border-b-n">
            <div class="l">
              <i v-if="pageState === 'add'" class="el-icon-plus f-s-20"></i>
              <i v-else class="el-icon-edit f-s-20"></i>
              <font class="m-l-10 f-s-18 fb">{{ $route.meta.title }}</font>
            </div>
          </el-header>
          <el-main class="border p-20 m-b-20">
            <el-form ref="form" :model="form" label-width="120px"
              :label-position="$store.state.app.isWideScreen ? 'top' : 'right'">
              <el-form-item v-if="pageState === 'add'" label="所属商铺">
                <SelectionShopId v-model="form.shop_id" />
              </el-form-item>
              <el-form-item label="账号">
                <el-input v-model="form.name" placeholder="账号" :disabled="adminId == undefined ? false : true" />
              </el-form-item>
              <el-form-item label="昵称">
                <el-input v-model="form.uname" placeholder="用户昵称" />
              </el-form-item>
              <el-form-item label="头像">
                <el-upload class="avatar-uploader" drag name="file" :action="action" :headers="myheader"
                  :show-file-list="false" :on-success="handleUpSuccess" :on-error="handleUpErr"
                  :before-upload="beforeAvatarUpload">
                  <img v-if="form.upFile != ''" :src="$store.state.settings.tempPath + form.upFile" class="avatar" />
                  <img v-else-if="form.oldFile != [] && form.upFile == ''" :src="form.oldFile[0]" class="avatar" />
                  <template v-else>
                    <i class="el-icon-plus f-s-30 m-t-70 seatFontColor"></i>
                    <div class="el-upload__text"><em>建议100*100像素 <br> jpg png gif </em></div>
                  </template>
                </el-upload>
              </el-form-item>
              <el-form-item v-if="$store.state.user.power !== 'editor'" label="权限设置" >
                <el-radio-group v-model="form.role">
                  <el-radio label="admin">管理员</el-radio>
                  <el-radio label="editor">编辑</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item :label="adminId == undefined ? '初始密码' : '新密码'">
                <el-input show-password v-model="form.pwd" :placeholder="adminId == undefined ? '密码' : '空置则为保持原密码'" />
              </el-form-item>
              <el-form-item v-if="pageState == 'add' ? true : false">
                <el-button type="primary" icon="el-icon-plus" @click="addAdmin">创建</el-button>
                <el-button @click="setForm({ shop_id: form.shop_id, oldFile: '' }); form.upFile = ''"
                  class="iconfont icon-qingchu">
                  <font class="m-l-5">重填</font>
                </el-button>
              </el-form-item>
              <el-form-item v-else>
                <el-button type="primary" icon="el-icon-edit" @click="saveAdmin">更新</el-button>
              </el-form-item>
            </el-form>
          </el-main>
        </el-container>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import SelectionShopId from '@/components/SelectionShopId'

export default {
  name: 'AdminAdd',
  data () {
    return {
      action: this.$store.state.settings.baseURL + this.$store.state.settings.apiAdminPath + 'upImgFile',
      myheader: { token: this.$store.state.user.token },
      form: {
        shop_id: '',
        name: '',
        uname: '',
        upFile: '',
        oldFile: '',
        pwd: '',
        role: ''
      },
      adminId: this.$route.params.id, // get参数 获取管理员id 没有为添加页面
      pageState: 'add', // 页面状态
      admin: null
    }
  },
  components: {
    SelectionShopId
  },
  computed: {
    /**
     * @description: 获取管理员列表
     */
    adminList () {
      return this.$store.state.adminList
    }
  },
  methods: {
    /* 文件上传表单 */
    handleUpSuccess (res) {
      if (res.status === 0) {
        this.$message.error(res.msg)
      } else {
        this.form.upFile = res.data
        this.$message.success(res.msg)
      }
    },
    beforeAvatarUpload (file) {
      const isJPG = file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/gif'
      if (!isJPG) {
        this.$message.error('上传图片只能是JPG|PNG|GIF格式!')
      }
      return isJPG
    },
    handleUpErr () {
      this.$message.error('接口访问失败')
    },
    // 设置表单
    setForm (data) {
      this.form.shop_id = data.shop_id
      this.form.name = data.name
      this.form.uname = data.uname
      this.form.oldFile = data.oldFile
      this.form.role = data.role
      if (Object.keys(data).length === 0) {
        this.$message.warning('清空表单')
      }
    },
    // 初始化页面 添加or编辑
    initPage () {
      if (this.adminId === undefined) {
        this.pageState = 'add'
      } else {
        this.pageState = 'edit'
        this.admin = this.adminList.find((item) => item.id === this.adminId)
        if (this.admin) {
          const data = {
            name: this.admin.name,
            uname: this.admin.uname,
            role: (Number(this.admin.role) === 5 || Number(this.admin.role) === 4) ? 'admin' : 'editor',
            oldFile: this.admin.photo,
            shop_id: this.admin.shop_id
          }
          this.setForm(data)
        }
      }
    },
    /**
     * @description: 创建新账号
     */
    async addAdmin () {
      const res = await this.$store.dispatch('fetchAddAdmin', this.form)
      if (res.data.status === 1) {
        this.$router.push('/admin/index')
      }
    },
    /**
     * @description: 更新账号
     */
    async saveAdmin () {
      this.form.id = this.adminId
      const res = await this.$store.dispatch('fetchSaveAdmin', this.form)
      if (res.data.status === 1) {
        this.$router.push('/admin/index')
      }
    }
  },
  watch: {
    adminList () {
      this.initPage()// 初始化页面
    }
  },
  created () {
    if (this.adminList.length > 0) {
      this.initPage()// 初始化页面
    }
  }
}
</script>

<style scoped>
.line {
  text-align: center;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.el-upload__text {
  line-height: 1.2em !important;
}
</style>
