<template>
  <el-dialog
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="60%"
    @close="close"
  >
    <el-form
      ref="form"
      label-position="left"
      label-width="100px"
      :model="form"
      :rules="rules"
      size="medium"
    >
      <el-form-item
        :label="__('分站名称')"
        label-width="130px"
        prop="subsite_name"
      >
        <el-input
          v-model="form.subsite_name"
          clearable
          :maxlength="8"
          :placeholder="__('请输入分站名称')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item
        label="LOGO"
        label-width="130px"
        prop="subsite_logo"
        required
      >
        <upload
          height="100px"
          :image="form.subsite_logo"
          width="100px"
          @upImage="upVoucherImage"
        />
      </el-form-item>
      <el-form-item label="SEO标题" label-width="130px" prop="subsite_title">
        <el-input
          v-model="form.subsite_title"
          clearable
          :placeholder="__('请输入SEO标题')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item
        label="SEO关键字"
        label-width="130px"
        prop="subsite_keywords"
      >
        <el-input
          v-model="form.subsite_keywords"
          clearable
          :placeholder="__('请输入SEO关键字')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item
        label="SEO描述"
        label-width="130px"
        prop="subsite_description"
      >
        <el-input
          v-model="form.subsite_description"
          clearable
          :placeholder="__('请输入SEO描述')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item :label="__('描述')" label-width="130px" prop="subsite_des">
        <el-input
          v-model="form.subsite_des"
          clearable
          :placeholder="__('请输入描述')"
          :style="{ width: '100%' }"
        />
      </el-form-item>
      <el-form-item
        :label="__('是否启用')"
        label-width="130px"
        prop="subsite_enable"
        required
      >
        <el-switch
          v-model="form.subsite_enable"

          :active-value="1"

          :inactive-value="0"
        />
      </el-form-item>
      <el-form-item
        :label="__('是否推荐')"
        label-width="130px"
        prop="subsite_hot"
        required
      >
        <el-switch
          v-model="form.subsite_hot"

          :active-value="1"

          :inactive-value="0"
        />
      </el-form-item>
      <el-form-item
        :label="__('分站管理员昵称')"
        label-width="130px"
        prop="user_id"
      >
        <el-select
          v-model.trim="form.user_id"
          clearable
          filterable
          :loading="loading"
          :placeholder="__('请输入用户昵称')"
          remote
          :remote-method="remoteMethod"
          reserve-keyword
          :style="{ width: '100%' }"
        >
          <el-option
            v-for="item in accountOptions"
            :key="item.user_id"
            :label="item.user_nickname"
            :value="item.user_id"
          />
        </el-select>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">{{ __('取消') }}</el-button>
      <el-button type="primary" @click="save">{{ __('确定') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'

  import { doEdit } from '@/api/plantform/subsite'
  import upload from '@/components/upload'
  import { getAccounts } from '@/api/user/base'
  export default {
    name: 'PlantformSubsiteEdit',
    components: { upload },
    data() {
      return {
        loading: false,
        accountOptions: [],
        form: {
          subsite_logo: '',
          subsite_enable: 1,
          subsite_hot: 0,
        },
        rules: {
          subsite_name: [
            {
              required: true,
              message: '请输入分站名称',
              trigger: 'blur',
            },
          ],
          subsite_title: [
            {
              required: true,
              message: '请输入SEO标题',
              trigger: 'blur',
            },
          ],
          subsite_logo: [
            {
              required: true,
              message: '请输入LOGO',
              trigger: 'blur',
            },
          ],
          subsite_keywords: [
            {
              required: true,
              message: '请输入SEO关键字',
              trigger: 'blur',
            },
          ],
          subsite_description: [
            {
              required: true,
              message: '请输入SEO描述',
              trigger: 'blur',
            },
          ],
          subsite_des: [
            {
              required: true,
              message: '请输入描述',
              trigger: 'blur',
            },
          ],
          user_id: [
            {
              required: true,
              message: '请输入分站管理员昵称',
              trigger: 'blur',
            },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')
          this.form = Object.assign({}, row)
          let obj = {
            user_nickname: row.user_nickname,
            user_account: row.user_account,
            user_id: row.user_id,
          }
          this.accountOptions.push(obj)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = {
          subsite_logo: '',
          subsite_enable: 1,
          subsite_hot: 0,
        }
        this.accountOptions = []
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await doEdit(this.form)
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
      upVoucherImage(image) {
        this.form.subsite_logo = image
      },
      async remoteMethod(user_nickname) {
        if (user_nickname.trim() !== '') {
          this.loading = true
          const { data } = await getAccounts({ user_nickname })
          this.accountOptions = data
          this.loading = false
        } else {
          this.accountOptions = []
        }
      },
    },
  }
</script>
