<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 class="ai-form" :model="form" label-width="120px" ref="form">
            <el-form-item label="主题" style="width: 100%;" prop="title" :rules="[{required: true, message: '请输入主题', trigger: 'blur'}]">
              <el-input type="textarea" :rows="5" v-model="form.title" clearable placeholder="请输入主题..." maxlength="30" show-word-limit></el-input>
            </el-form-item>
            <el-form-item prop="areaId" style="width: 100%;" label="发布地区" :rules="[{required: true, message: '请选择地区', trigger: 'change'}]">
              <ai-area-select clearable always-show :instance="instance" v-model="form.areaId" :disabled-level="disabledLevel"></ai-area-select>
            </el-form-item>
            <el-form-item label="议事截止时间" prop="date" :rules="[{required: true, message: '请选择议事截止时间', trigger: 'change'}]">
              <el-date-picker
                v-model="form.date"
                type="datetime"
                style="width: 100%;"
                size="small"
                placeholder="选择议事截止时间">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="公示截止时间" prop="time" :rules="[{required: true, message: '请选择公示截止时间', trigger: 'change'}]">
              <el-date-picker
                v-model="form.time"
                size="small"
                style="width: 100%;"
                type="datetime"
                placeholder="选择公示截止时间">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="议事类型" prop="type" :rules="[{required: true, message: '请选择议事类型', trigger: 'change'}]">
              <el-radio-group v-model="form.type">
                <el-radio label="0">意见征集</el-radio>
                <el-radio label="1">投票表决</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item class="vite-form__item" style="width: 100%;" label="投票选项" prop="type" :rules="[{required: true, message: '请添加投票选项', trigger: 'change'}]">
              <el-form-item label-width="80px" :label="'选项' + index" v-for="(item, index) in form.voteList" :key="index + item.name">
                <div class="form-flex">
                  <el-input v-model="item.name" size="small" clearable placeholder="请输入选项"></el-input>
                  <el-button type="danger" size="small" @click="removeVote(index)">删除</el-button>
                </div>
              </el-form-item>
              <el-button type="primary" size="small" @click="addVote">添加选项</el-button>
            </el-form-item>
            <el-form-item label="是否匿名投票" prop="anonymity" :rules="[{required: true, message: '请选择是否匿名投票', trigger: 'change'}]">
              <el-switch
                v-model="form.anonymity"
                active-value="1"
                inactive-value="0">
            </el-switch>
            </el-form-item>
            <el-form-item label="投票方式" prop="type" :rules="[{required: true, message: '请选择投票方式', trigger: 'change'}]">
              <el-radio-group v-model="form.type">
                <el-radio label="0">单选</el-radio>
                <el-radio label="1">多选</el-radio>
              </el-radio-group>
            </el-form-item>
            <el-form-item label="图片" style="width: 100%;" prop="thumbUrl">
              <ai-uploader
                :instance="instance"
                isShowTip
                v-model="form.thumbUrl"
                :limit="9">
              </ai-uploader>
            </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: {},
        form: {
          title: '',
          content: '',
          areaId: '',
          date: '',
          type: '',
          voteList: [],
          time: '',
          anonymity: '1',
          createUnitName: '',
          createUserName: '',
          status: '',
          thumbUrl: []
        },
        id: ''
      }
    },

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

    created () {
      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/appcountrysidetourism/queryDetailById?id=${id}`).then(res => {
          if (res.code === 0) {
            this.form = res.data
            this.form.thumbUrl = res.data.thumbUrl ? JSON.parse(res.data.thumbUrl) : []
          }
        })
      },

      addVote () {
        this.form.voteList.push({
          name: ''
        })
      },

      removeVote (index) {
        this.form.voteList.splice(index, 1)
      },

      confirm () {
        this.$refs.form.validate((valid) => {
          if (valid) {
            this.instance.post(`/app/appcountrysidetourism/addOrUpdate`, {
              ...this.form,
              type: 0,
              createUserName: this.user.info.name,
              thumbUrl: this.form.thumbUrl.length ? JSON.stringify([{
                url: this.form.thumbUrl[0].url
              }]) : ''
            }).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">
    .vite-form__item {
      .form-flex {
        display: flex;
        align-items: center;

        .el-button {
          margin-left: 20px;
        }

        ::v-deep .el-form-item__content {
          margin-left: 0!important;
        }
      }
    }
</style>
