<template>
  <div>
    <Drawer
      :title="formTitle"
      v-model="modalVisible"
      width="720"
      :mask-closable="false"
      :styles="styles"
      @on-close="close"
    >
      <Form
        ref="formValidate"
        :model="formValidate"
        :rules="ruleValidate"
        :label-width="200"
      >
        <FormItem label="VIP购买页说明" prop="title">
          <Input
            v-model="formValidate.title"
            placeholder="Enter something"
          ></Input>
        </FormItem>
        <FormItem label="是否开启VIP会员专属权益">
          <i-switch
            :value="formValidate.exclusive_rights.is_enable === 1 ? true : false"
            @on-change="
              value => {
                change(value, 1);
              }
            "
          />
        </FormItem>
        <FormItem label="标题" prop="exclusive_rights.title">
          <Input
            v-model="formValidate.exclusive_rights.title"
            placeholder="Enter something"
          ></Input>
        </FormItem>
        <FormItem label="特色" prop="title">
          <Input
            style="margin-bottom: 10px;"
            v-for="(item, idx) in formValidate.exclusive_rights.features"
            :key="idx"
            v-model="item.name"
            placeholder="Enter something"
          ></Input>
          <div>
            <Icon
              @click="addFeatures"
              :size="20"
              color="#7744FF"
              style="margin-right: 6px;"
              type="md-add"
            /><Icon
              v-if="formValidate.exclusive_rights.features.length > 1"
              @click="delFeatures"
              color="#7744FF"
              :size="20"
              type="md-remove"
            />
          </div>
        </FormItem>
        <FormItem label="是否开启用户评价板块">
          <i-switch
            :value="formValidate.user_comment_section.is_enable === 1 ? true : false"
            @on-change="
              value => {
                change(value, 2);
              }
            "
          />
        </FormItem>
         <FormItem label="标题" prop="user_comment_section.title">
            <Input
              v-model="formValidate.user_comment_section.title"
              placeholder="Enter something"
            ></Input>
          </FormItem>
        <div v-for="(item, idx) in formValidate.user_comment_section.comments"
            :key="idx">
          <FormItem label="用户头像" prop="title">
            <UploadS
              :ref="gravatar+idx.toString()"
              v-model="item.gravatar"
            />
          </FormItem>
          <FormItem label="用户昵称" prop="user_comment_section.title">
            <Input
              v-model="item.nickname"
              placeholder="Enter something"
            ></Input>
          </FormItem>
           <FormItem label="评论内容"  prop="user_comment_section.title">
            <Input
              v-model="item.comment" type="textarea"
              placeholder="Enter something"
            ></Input>
          </FormItem>
        </div>
        <div style="margin-left: 200px;margin-bottom: 20px;">
            <Icon
              @click="addComments"
              :size="20"
              color="#7744FF"
              style="margin-right: 6px;"
              type="md-add"
            /><Icon
              v-if="formValidate.user_comment_section.comments.length > 1"
              @click="delComments"
              color="#7744FF"
              :size="20"
              type="md-remove"
            />
          </div>
        <FormItem label="是否开启睡眠主题">
          <i-switch
            :value="formValidate.sleep_content_section.is_enable === 1 ? true : false"
            @on-change="
              value => {
                change(value, 3);
              }
            "
          />
        </FormItem>
         <FormItem label="标题" prop="sleep_content_section.title">
            <Input
              v-model="formValidate.sleep_content_section.title"
              placeholder="Enter something"
            ></Input>
          </FormItem>
        <FormItem label="展示图片" prop="sleep_content_section.res_show_pic">
          <UploadS
            ref="sleep_content_section_res_show_pic"
            v-model="formValidate.sleep_content_section.res_show_pic"
          />
        </FormItem>
         <FormItem label="内容图片" prop="title">
          <UploadS maxLength="20"
            ref="res_content_pics"
            v-model="formValidate.sleep_content_section.res_content_pics"
          />
        </FormItem>
          <FormItem label="是否开启冥想主题展示">
          <i-switch
            :value="formValidate.meditation_content_section.is_enable === 1 ? true : false"
            @on-change="
              value => {
                change(value, 4);
              }
            "
          />
        </FormItem>
         <FormItem label="标题" prop="meditation_content_section.title">
            <Input
              v-model="formValidate.meditation_content_section.title"
              placeholder="Enter something"
            ></Input>
          </FormItem>
        <FormItem label="展示图片" prop="meditation_content_section.res_show_pic">
          <UploadS
            ref="meditation_content_section_res_show_pic"
            v-model="formValidate.meditation_content_section.res_show_pic"
          />
        </FormItem>
         <FormItem label="内容图片" prop="title">
          <UploadS maxLength="20"
            ref="meditation_content_section_res_content_pics"
            v-model="formValidate.meditation_content_section.res_content_pics"
          />
        </FormItem>
         <FormItem label="是否开启纯图展示板块">
          <i-switch
            :value="formValidate.show_section.is_enable === 1 ? true : false"
            @on-change="
              value => {
                change(value, 5);
              }
            "
          />
        </FormItem>
         <FormItem label="标题" prop="show_section.title">
            <Input
              v-model="formValidate.show_section.title"
              placeholder="Enter something"
            ></Input>
          </FormItem>
        <FormItem label="展示图片" prop="title">
          <UploadS
            maxLength="20"
            ref="show_section_res_show_pics"
            v-model="formValidate.show_section.res_show_pics"
          />
        </FormItem>
      </Form>
      <div class="demo-drawer-footer">
        <Button style="margin-right: 8px" @click="close">取消</Button>
        <Button type="primary" @click="submit">保存</Button>
      </div>
    </Drawer>
  </div>
</template>
<script>
import UploadS from '@/view/common/upload.vue'
import { addVipPage, editVipPage } from '@/api/vipPage'
const _requireBlur = { required: true, message: ' ', trigger: 'blur' }
const validatorPic = (rule, value, callback) => {
  if (!value) {
    callback(new Error(' '))
  } else {
    callback()
  }
}
const _requirePic = {
  required: true,
  message: ' ',
  trigger: 'blur',
  validator: validatorPic
}
export default {
  props: {
    search: {
      type: Function,
      default: null
    },
    row: {
      type: Object,
      default: () => {}
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  components: {
    UploadS
  },
  data () {
    return {
      gravatar: 'gravatar',
      modalVisible: false,
      editMode: false,
      formTitle: '',
      styles: {
        height: 'calc(100% - 55px)',
        overflow: 'auto',
        paddingBottom: '53px',
        position: 'static'
      },
      formValidate: {},
      ruleValidate: {
        'title': [_requireBlur],
        'exclusive_rights.title': [_requireBlur],
        'user_comment_section.title': [_requireBlur],
        'user_comment_section.nickname': [_requireBlur],
        'user_comment_section.comment': [_requireBlur],
        'sleep_content_section.title': [_requireBlur],
        'show_section.title': [_requireBlur],
        'meditation_content_section.title': [_requireBlur],
        'sleep_content_section.res_show_pic': [_requirePic],
        'sleep_content_section.res_content_pics': [_requirePic],
        'show_section.res_content_pics': [_requirePic],
        'meditation_content_section.res_show_pic': [_requirePic],
        'user_comment_section.gravatar': [_requirePic]
      }
    }
  },
  watch: {
    visible: {
      handler: function (val) {
        this.modalVisible = val
        if (val) {
          this.search()
        }
        this.formValidate = this.row
      },
      immediate: true
    },
    row: {
      handler: function (val) {
        console.log(val)
        if (val.id === 'add') {
          this.editMode = false
          this.formTitle = '新增VIP购买页'
          this.formValidate = {
            title: '',
            exclusive_rights: {
              is_enable: 1,
              title: '',
              features: [{ name: '' }]
            },
            user_comment_section: {
              is_enable: 1,
              title: '',
              comments: [{ comment: '', gravatar: '', nickname: '' }]
            },
            sleep_content_section: {
              is_enable: 1,
              title: '',
              res_content_pics: [],
              res_show_pic: ''
            },
            meditation_content_section: {
              is_enable: 1,
              res_content_pics: [],
              res_show_pic: '',
              title: ''
            },
            show_section: {
              is_enable: 1,
              title: '',
              res_show_pics: []
            }
          }
        } else {
          this.editMode = true
          this.formTitle = '编辑VIP购买页'
          this.formValidate = val
          this.formValidate.exclusive_rights.features = val.exclusive_rights.features !== null && val.exclusive_rights.features.map((p) => {
            let name = {
              'name': p
            }
            return name
          })
        }
      },
      immediate: true
    }
  },
  methods: {
    addFeatures () {
      this.formValidate.exclusive_rights.features.push({ name: '' })
    },
    delFeatures () {
      this.formValidate.exclusive_rights.features.splice(
        this.formValidate.exclusive_rights.features.length - 1,
        1
      )
    },
    addComments () {
      this.formValidate.user_comment_section.comments.push({ comment: '', gravatar: '', nickname: '' })
    },
    delComments () {
      this.formValidate.user_comment_section.comments.splice(
        this.formValidate.user_comment_section.comments.length - 1,
        1
      )
    },
    submit () {
      if (this.$refs['sleep_content_section_res_show_pic'].getData()) {
        this.formValidate.sleep_content_section.res_show_pic = this.$refs['sleep_content_section_res_show_pic'].getData()
      }
      if (this.$refs['meditation_content_section_res_show_pic'].getData()) {
        this.formValidate.meditation_content_section.res_show_pic = this.$refs['meditation_content_section_res_show_pic'].getData()
      }
      let meditation_content_section_res_content_pics = this.$refs['meditation_content_section_res_content_pics'].getData()
      if (meditation_content_section_res_content_pics) {
        this.formValidate.meditation_content_section.res_content_pics = typeof meditation_content_section_res_content_pics === 'string' ? [meditation_content_section_res_content_pics] : meditation_content_section_res_content_pics
      }
      let res_content_pics = this.$refs['res_content_pics'].getData()
      if (res_content_pics) {
        this.formValidate.sleep_content_section.res_content_pics = typeof res_content_pics === 'string' ? [res_content_pics] : res_content_pics
      }
      let show_section_res_show_pics = this.$refs['show_section_res_show_pics'].getData()
      if (show_section_res_show_pics) {
        this.formValidate.show_section.res_show_pics = typeof show_section_res_show_pics === 'string' ? [show_section_res_show_pics] : show_section_res_show_pics
      }
      this.formValidate.user_comment_section.comments.forEach((p, idx) => {
        let refgravatar = this.gravatar + idx.toString()
        let gravatars = this.$refs[refgravatar][0].getData()
        if (gravatars) {
          p.gravatar = gravatars
        }
      })
      console.log(this.formValidate)
      this.$refs['formValidate'].validate(valid => {
        if (valid) {
          this.formValidate.exclusive_rights.features = this.formValidate.exclusive_rights.features.map((p) => {
            return p.name
          })
          delete this.formValidate.created_at
          delete this.formValidate.deleted_at
          delete this.formValidate.updated_at
          let params = [this.formValidate]
          if (this.editMode) {
            editVipPage(params).then(res => {
              if (res.code === 200 && res.data.length > 0) {
                this.$Message.success('修改成功')
                this.close()
                this.search()
              }
            })
          } else {
            addVipPage(params).then(res => {
              if (res.code === 200 && res.data.length > 0) {
                this.$Message.success('新增成功')
                this.close()
                this.search()
              }
            })
          }
        } else {
          this.$Message.error('您还有必填项未填写')
        }
      })
    },
    change (val, type) {
      if (type === 1) {
        this.formValidate.exclusive_rights.is_enable = val ? 1 : 0
      }
      if (type === 2) {
        this.formValidate.user_comment_section.is_enable = val ? 1 : 0
      }
      if (type === 3) {
        this.formValidate.sleep_content_section.is_enable = val ? 1 : 0
      }
      if (type === 4) {
        this.formValidate.meditation_content_section.is_enable = val ? 1 : 0
      }
      if (type === 5) {
        this.formValidate.show_section.is_enable = val ? 1 : 0
      }
    },
    close () {
      this.$emit('update:visible', false)
    }
  }
}
</script>
<style>
.demo-drawer-footer {
  width: 100%;
  position: absolute;
  bottom: 0;
  left: 0;
  border-top: 1px solid #e8e8e8;
  padding: 10px 16px;
  text-align: right;
  background: #fff;
}
</style>
