<!-- 用户编辑弹窗 -->
<template>
  <ele-modal
    width="800px"
    :visible="visible"
    :append-to-body="true"
    :close-on-click-modal="true"
    custom-class="ele-dialog-form"
    :title="isUpdate ? '修改优惠券' : '添加优惠券'"
    @update:visible="updateVisible"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="优惠券标题:" prop="title">
        <el-input
          clearable
          :maxlength="60"
          v-model="form.title"
          placeholder="请输入优惠券标题"
        />
      </el-form-item>
      <el-form-item label="优惠券金额:" prop="price">
        <el-input
          clearable
          type="number"
          :maxlength="60"
          placeholder=""
          v-model="form.price"
        />
      </el-form-item>
      <el-form-item label="有效期:" prop="dateRange">
        <el-date-picker
          v-model="form.dateRange"
          type="datetimerange"
          unlink-panels
          range-separator="~"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
          class="ele-fluid"
        />
      </el-form-item>
      <el-form-item label="使用规则:">
        <div v-if="editVersion">
          <tinymce-editor
            :init="editoption"
            v-model="form.rule"
            placeholder="请输入使用规则"
          />
        </div>
      </el-form-item>
      <el-form-item label="可用门店:">
        <el-radio-group v-model="form.userType">
          <el-radio :label="0">指定门店</el-radio>
          <el-radio :label="1">所有门店</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="指定使用门店:" v-if="form.userType === 0">
        <el-row>
          <el-button @click="showSetBiz = true">选择门店</el-button
          ><span style="margin-left: 10px"
            >已选择
            <span class="ele-text-danger">{{ form.bizs.length }}</span></span
          >个门店
        </el-row>
        <el-row>
          <el-tag
            type="info"
            closable
            @close="removeBiz(index)"
            v-for="(item, index) in form.selectBiz"
            :key="index"
            >{{ item }}</el-tag
          >
        </el-row>
        <el-row></el-row>
      </el-form-item>
    </el-form>
    <!-- 选择门店 -->
    <select-biz
      :visible.sync="showSetBiz"
      v-model="form.bizs"
      :show-biz.sync="form.selectBiz"
      :product-id="productId"
    />
    <template v-slot:footer>
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" :loading="loading" @click="save">
        保存
      </el-button>
    </template>
  </ele-modal>
</template>

<script>
  import TinymceEditor from '@/components/TinymceEditor/index.vue';
  import SelectBiz from '@/components/SelectBiz/index.vue';
  import { addProductCoupon, updateProductCoupon } from '@/api/car/coupon';

  export default {
    components: {
      TinymceEditor,
      SelectBiz
    },
    props: {
      // 弹窗是否打开
      visible: Boolean,
      productId: String,
      // 修改回显的数据
      data: Object
    },
    data() {
      const defaultForm = {
        id: null,
        title: '',
        dateRange: '',
        price: '',
        rule: '',
        userType: 0,
        bizs: [],
        selectBiz: {},
        productId: 0
      };
      return {
        showSetBiz: false,
        editVersion: false,
        defaultForm,
        // 表单数据
        form: { ...defaultForm },
        // 表单验证规则
        rules: {
          title: [
            {
              required: true,
              message: '请输入标题',
              trigger: 'blur'
            }
          ],
          price: [
            {
              required: true,
              message: '请输入金额',
              trigger: 'blur'
            }
          ],
          dateRange: [
            {
              required: true,
              message: '请选择有效时间',
              trigger: 'blur'
            }
          ]
        },
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false,
        //用户画像
        crowdProfiling: [],
        //产品标签
        carProductLabel: '',
        editoption: {
          height: 300,
          toolbar: [
            'code | forecolor backcolor bold italic underline strikethrough link anchor | alignleft aligncenter alignright alignjustify outdent indent | fontsizeselect | bullist numlist | \
    table emoticons hr preview | fullscreen | bdmap indent2em lineheight formatpainter axupimgs| image emoticons hr '
          ]
        }
      };
    },
    computed: {
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },
    created() {},
    methods: {
      /* 保存编辑 */
      save() {
        this.$refs.form.validate((valid) => {
          if (!valid) {
            return false;
          }
          this.loading = true;
          const data = {
            ...this.form
          };
          data.productId = this.productId;
          const saveOrUpdate = this.isUpdate
            ? updateProductCoupon
            : addProductCoupon;
          saveOrUpdate(data)
            .then((msg) => {
              this.loading = false;
              this.$message.success(msg);
              this.updateVisible(false);
              this.$emit('done');
            })
            .catch((e) => {
              this.loading = false;
              this.$message.error(e.message);
            });
        });
      },
      handleCarChange(carInfo) {
        this.form.selectedCar = carInfo;
      },
      /* 更新visible */
      updateVisible(value) {
        this.$emit('update:visible', value);
      },
      /* 添加海报描述 */
      addShareTitle() {
        this.form.promotion_text.push('');
      },
      removeBiz(bizId) {
        this.form.bizs.splice(this.form.bizs.indexOf(bizId), 1);
        this.$delete(this.form.selectBiz, bizId);
      }
    },
    watch: {
      visible(visible) {
        this.editVersion = false;
        if (visible) {
          this.editVersion = true;
          if (this.data) {
            this.$util.assignObject(this.form, {
              ...this.data
            });
            this.isUpdate = true;
          } else {
            this.form.imgs = [];
            this.isUpdate = false;
          }
        } else {
          this.$refs.form.clearValidate();
          this.form = { ...this.defaultForm };
        }
      }
    }
  };
</script>
<style lang="scss" scoped>
  .ele-image-upload-list .ele-image-upload-item {
    margin-top: 2px;
    margin-bottom: 2px;
  }
</style>
