<!-- 用户编辑弹窗 -->
<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="banner">
        <upload-img
          v-model="form.banner"
          :images="form.banner"
          :limit="1"
          :multiple="true"
        />
      </el-form-item>
      <el-form-item label="轮播图:" prop="imgs">
        <upload-img
          v-model="form.imgs"
          :images="form.imgs"
          :limit="10"
          :multiple="true"
        />
        <!--
        <div class="ele-text-secondary">
          尺寸宽度750
        </div>
        -->
      </el-form-item>
      <el-form-item label="海报背景图:" prop="posterBg">
        <upload-img
          v-model="form.posterBg"
          :images="form.posterBg"
          :limit="10"
          :multiple="true"
        />
      </el-form-item>
      <el-form-item label="指导价:" prop="guide_price">
        <el-input
          clearable
          :maxlength="60"
          placeholder=""
          v-model="form.guide_price"
        />
      </el-form-item>
      <el-form-item label="优惠信息1:">
        <el-input
          clearable
          :maxlength="60"
          placeholder=""
          v-model="form.discounts"
        />
      </el-form-item>
      <el-form-item label="优惠信息2:">
        <el-input
          clearable
          :maxlength="60"
          placeholder=""
          v-model="form.discounts2"
        />
      </el-form-item>
      <el-form-item label="优惠信息3:">
        <el-input
          clearable
          :maxlength="60"
          placeholder=""
          v-model="form.discounts3"
        />
      </el-form-item>
      <el-form-item label="推广范围:" prop="">
        <regions-select
          v-model="form.provinceCity"
          placeholder="全国"
          type="provinceCity"
          :props="{ multiple: true }"
          class="ele-fluid"
        />
      </el-form-item>
      <car-model-selector
        type="brandSeries"
        :model-value="form.selectedCar"
        v-model="form.selectedCar"
        @update:modelValue="handleCarChange"
      />
      <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="价格区间:">
        <el-checkbox-group v-model="form.carProductLabel">
          <el-checkbox
            v-for="value in priceRange"
            :label="value.dictDataId"
            :key="value.dictDataId"
            >{{ value.dictDataName }}</el-checkbox
          >
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="级别:">
        <el-checkbox-group v-model="form.carProductLabel">
          <el-checkbox
            v-for="value in productLevel"
            :label="value.dictDataId"
            :key="value.dictDataId"
            >{{ value.dictDataName }}</el-checkbox
          >
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="能源类型:">
        <el-checkbox-group v-model="form.carProductLabel">
          <el-checkbox
            v-for="value in carProductLabel"
            :label="value.dictDataId"
            :key="value.dictDataId"
            >{{ value.dictDataName }}</el-checkbox
          >
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="人群画像:">
        <el-checkbox-group v-model="form.crowdProfiling">
          <el-checkbox
            v-for="value in crowdProfiling"
            :label="value.dictDataId"
            :key="value.dictDataId"
            >{{ value.dictDataName }}</el-checkbox
          >
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="核心卖点:">
        <el-checkbox-group v-model="form.carProductLabel">
          <el-checkbox
            v-for="value in couponSelling"
            :label="value.dictDataId"
            :key="value.dictDataId"
            >{{ value.dictDataName }}</el-checkbox
          >
        </el-checkbox-group>
      </el-form-item>
      <el-form-item label="推广文案:">
        <div
          style="margin: 8px 0"
          v-for="(item, index) in form.promotion_text"
          :key="index"
        >
          <el-input
            clearable
            :maxlength="60"
            v-model="form.promotion_text[index]"
            placeholder="请输入推广文案"
          />
        </div>
        <el-button
          type="primary"
          plain
          size="small"
          icon="el-icon-plus"
          @click="addShareTitle"
        >
          新增推广文案
        </el-button>
      </el-form-item>
    </el-form>
    <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 { addProduct, updateProduct } from '@/api/car/product';
  import { listDictionaryData } from '@/api/system/dictionary-data';
  import UploadImg from '@/components/UploadImg/index.vue';
  import RegionsSelect from '@/components/RegionsSelect/index.vue';
  import CarModelSelector from '@/components/CarSelector/index.vue';

  export default {
    components: {
      UploadImg,
      RegionsSelect,
      CarModelSelector
    },
    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 修改回显的数据
      data: Object
    },
    data() {
      const defaultForm = {
        id: null,
        title: '',
        dateRange: '',
        banner: [],
        imgs: [],
        provinceCity: [],
        crowdProfiling: [],
        selectedCar: {
          brandId: null,
          seriesId: null,
          modelId: null
        },
        guide_price: '',
        discounts: '',
        discounts2: '',
        discounts3: '',
        promotion_text: [],
        carProductLabel: [],
        posterBg: []
      };
      return {
        editVersion: false,
        defaultForm,
        // 表单数据
        form: { ...defaultForm },
        // 表单验证规则
        rules: {
          title: [
            {
              required: true,
              message: '请输入活动标题',
              trigger: 'blur'
            }
          ],
          banner: [
            {
              required: true,
              message: '请上传略缩图',
              trigger: 'blur'
            }
          ],
          imgs: [
            {
              required: true,
              message: '请上传轮播图',
              trigger: 'blur'
            }
          ],
          guide_price: [
            {
              required: true,
              message: '请填写指导价',
              trigger: 'blur'
            }
          ],
          dateRange: [
            {
              required: true,
              message: '请选择有效时间',
              trigger: 'blur'
            }
          ]
        },
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false,
        //用户画像
        crowdProfiling: [],
        //价格区间
        priceRange: '',
        //能源类型
        productLevel: '',
        //产品标签
        carProductLabel: '',
        couponSelling: []
      };
    },
    computed: {
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },
    created() {
      listDictionaryData({ dictCode: 'crowdProfiling' }).then((data) => {
        this.crowdProfiling = data;
      });
      listDictionaryData({ dictCode: 'priceRange' }).then((data) => {
        this.priceRange = data;
      });
      listDictionaryData({ dictCode: 'productLevel' }).then((data) => {
        this.productLevel = data;
      });
      listDictionaryData({ dictCode: 'carProductLabel' }).then((data) => {
        this.carProductLabel = data;
      });
      listDictionaryData({ dictCode: 'couponSelling' }).then((data) => {
        this.couponSelling = data;
      });
    },
    methods: {
      /* 保存编辑 */
      save() {
        this.$refs.form.validate((valid) => {
          if (!valid) {
            return false;
          }
          this.loading = true;
          const data = {
            ...this.form
          };
          const saveOrUpdate = this.isUpdate ? updateProduct : addProduct;
          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('');
      }
    },
    watch: {
      visible(visible) {
        this.editVersion = false;
        if (visible) {
          this.editVersion = true;
          if (this.data) {
            this.$util.assignObject(this.form, {
              ...this.data
            });
            this.isUpdate = true;
            if (this.form.promotion_text.length <= 0) {
              this.addShareTitle();
            }
          } 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>
