<template>
  <div class="cmain">
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="轮播图片" prop="pic"><cropper v-model="form.pic" :fixed_number="[5, 2.3]"></cropper></el-form-item>
      <el-form-item label="点击跳转到" prop="contentTitle">
        <el-input placeholder="请选择跳转链接,留空则点击图片不跳转" v-model="form.contentTitle" :disabled="true">
          <el-button slot="append" icon="el-icon-search" @click="add()"></el-button>
        </el-input>
      </el-form-item>
      <el-form-item label="展示位置" prop="type">
        <el-select v-model="form.type" placeholder="请选择">
          <el-option label="首页" :value="0"></el-option>
          <el-option label="更多服务" :value="1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="显示顺序" prop="orderNum"><el-input type="number" :min="0" v-model="form.orderNum" placeholder="请输入显示顺序" /></el-form-item>
      <el-form-item label="状态" prop="state">
        <el-select v-model="form.state" placeholder="请选择">
          <el-option label="正常" :value="0"></el-option>
          <el-option label="停用" :value="1"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <div class="mfooter">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="$layer.close(layerid)">取 消</el-button>
    </div>
  </div>
</template>

<script>
import selectUrl from './select_url';
export default {
  data() {
    return {
      form: { contentTitle: '', contentId: '', orderNum: 0, state: 0, contentType: '' },
      rules: {
        pic: [{ required: true, message: '请上传图片', trigger: 'blur' }],
        contentTitle: [{ required: true, message: '请选择跳转链接', trigger: 'blur' }],
        orderNum: [{ required: true, message: '请输入显示顺序', trigger: 'blur' }],
        state: [{ required: true, message: '状态不能为空', trigger: 'blur' }]
      }
    };
  },
  props: {
    param: {
      type: Object,
      default: () => {
        return {};
      }
    },
    layerid: {
      type: String
    }
  },
  mounted() {
    if (this.param.id) {
      this.ajax({ url: '/work/banner/detail/' + this.param.id }).then((response) => {
        this.form = response.data;
      });
    }
  },
  methods: {
    //添加跳转链接
    add() {
      this.iframe({ obj: selectUrl, param: { types: this.form.types }, title: '添加跳转链接', width: '60%', height: '65%' });
    },
    change(e) {
      this.form.contentTitle = '';
    },
    submitForm() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.id) {
            this.ajax({ method: 'post', url: '/work/banner/edit', data: this.form }).then((response) => {
              this.$modal.msgSuccess('修改成功');
              this.$layer.close(this.layerid);
              this.$parent.getList();
            });
          } else {
            this.ajax({ method: 'post', url: '/work/banner/add', data: this.form }).then((response) => {
              this.$modal.msgSuccess('新增成功');
              this.$layer.close(this.layerid);
              this.$parent.getList();
            });
          }
        }
      });
    }
  }
};
</script>
