<!-- 用户编辑弹窗 -->
<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="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="">
        <regions-select
          v-model="form.provinceCity"
          placeholder="全国"
          type="provinceCity"
          class="ele-fluid"
        />
      </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="一级线索佣金:">
        <el-input
          clearable
          :maxlength="60"
          placeholder=""
          v-model="form.firstLevelClues"
        />
      </el-form-item>
      <el-form-item label="一级成交佣金:">
        <el-input
          clearable
          :maxlength="60"
          placeholder=""
          v-model="form.firstLevelDeal"
        />
      </el-form-item>
      <el-form-item label="二级线索佣金:">
        <el-input
          clearable
          :maxlength="60"
          placeholder=""
          v-model="form.secondLevelClues"
        />
      </el-form-item>
      <el-form-item label="二级成交佣金:">
        <el-input
          clearable
          :maxlength="60"
          placeholder=""
          v-model="form.secondLevelDeal"
        />
      </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>
    <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';

  export default {
    components: {
      UploadImg,
      RegionsSelect
    },
    props: {
      // 弹窗是否打开
      visible: Boolean,
      // 全部机构
      organizationList: Array,
      // 修改回显的数据
      data: Object
    },
    data() {
      const defaultForm = {
        id: null,
        title: '',
        dateRange: '',
        imgs: [],
        provinceCity: [],
        firstLevelClues: '',
        secondLevelClues: '',
        firstLevelDeal: '',
        secondLevelDeal: '',
        crowdProfiling: []
      };
      return {
        editVersion: false,
        defaultForm,
        // 表单数据
        form: { ...defaultForm },
        // 表单验证规则
        rules: {
          title: [
            {
              required: true,
              message: '请输入活动标题',
              trigger: 'blur'
            }
          ],
          imgs: [
            {
              required: true,
              message: '请上传推广图',
              trigger: 'blur'
            }
          ],
          dateRange: [
            {
              required: true,
              message: '请选择有效时间',
              trigger: 'blur'
            }
          ]
        },
        // 提交状态
        loading: false,
        // 是否是修改
        isUpdate: false,
        crowdProfiling: []
      };
    },
    computed: {
      // 是否开启响应式布局
      styleResponsive() {
        return this.$store.state.theme.styleResponsive;
      }
    },
    created() {
      listDictionaryData({ dictCode: 'crowdProfiling' }).then((data) => {
        this.crowdProfiling = data;
      });
    },
    methods: {
      /* 添加海报描述 */
      addShareTitle() {
        this.form.shareTitle.push('');
      },
      /* 保存编辑 */
      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);
            });
        });
      },
      /* 更新visible */
      updateVisible(value) {
        this.$emit('update:visible', value);
      }
    },
    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>
  .ele-image-upload-list .ele-image-upload-item {
    margin-top: 2px;
    margin-bottom: 2px;
  }
</style>
