<template>
  <div class="app-container">

    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" >
      <el-form-item :label="$t('fenlei.活动名称')" prop="salesName">
        <el-input
          v-model="queryParams.salesName"
          :placeholder="$t('fenlei.请输入活动名称')"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('fenlei.减免方式')" prop="salesType">
        <el-select v-model="queryParams.salesType" :placeholder="$t('fenlei.请选择减免方式')" clearable>
          <el-option
            v-for="dict in dict.type.sys_sales_type"
            :key="dict.value"
            :label="generateTitle(dict.label)"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
<!--      <el-form-item :label="$t('fenlei.活动商家')" prop="shId">-->
<!--        <el-input-->
<!--          v-model="queryParams.shId"-->
<!--          :placeholder="$t('fenlei.请输入活动商家')"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item :label="$t('fenlei.活动商品')" prop="spId">-->
<!--        <el-input-->
<!--          v-model="queryParams.spId"-->
<!--          :placeholder="$t('fenlei.请输入活动商品')"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item :label="$t('fenlei.结束日期')" prop="endTime">
        <el-date-picker clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          :placeholder="$t('fenlei.请选择活动结束日期')">
        </el-date-picker>
      </el-form-item>
      <el-form-item :label="$t('fenlei.活动状态')" prop="salesState">
        <el-select v-model="queryParams.salesState" :placeholder="$t('fenlei.请选择活动状态')" clearable>
          <el-option
            v-for="dict in dict.type.sys_sales_state"
            :key="dict.value"
            :label="generateTitle(dict.label)"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{$t('fenlei.搜索')}}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{$t('fenlei.重置')}}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:promotion:add']"
        >{{$t('fenlei.新增')}}</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="promotionList" @selection-change="handleSelectionChange">
      <el-table-column label="id"  width="100" align="center" prop="id" />
      <el-table-column :label="$t('fenlei.活动名称')" align="center" prop="salesName" />
      <el-table-column :label="$t('fenlei.减免方式')" align="center" prop="salesType">
        <template slot-scope="scope">
          <div v-if="scope.row.salesType==1">{{generateTitle('运费')}}</div>
          <div v-if="scope.row.salesType==2">{{generateTitle('订单')}}</div>
          <div v-if="scope.row.salesType==3">{{generateTitle('商品')}}</div>
        </template>
      </el-table-column>
      <el-table-column :label="$t('fenlei.结束日期')" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('fenlei.活动状态')" align="center" prop="salesState">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.salesState==0" type="primary">{{generateTitle('启用')}}</el-tag>
          <el-tag v-if="scope.row.salesState==1" type="danger">{{generateTitle('停用')}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column :label="$t('fenlei.操作')" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.shId==null"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:promotion:edit']"
          >{{$t('fenlei.修改')}}</el-button>
          <el-button
             v-if="scope.row.shId==null"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:promotion:remove']"
          >{{$t('fenlei.删除')}}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改SalesPromotion对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="650px" append-to-body>
      <!-- 红色提示框 -->
      <el-alert
        v-if="!form.edit"
        :title="$t('youhuijuanquanyi.不能修改提示')"
        type="error"
        show-icon
        :closable="false"
        style="margin-bottom: 16px;"
      />
      <el-form ref="form" :model="form" :rules="rules" label-width="180px">
        <el-form-item :label="$t('fenlei.活动名称')" prop="salesName">
          <el-input v-model="form.salesName" :placeholder="$t('fenlei.请输入促销活动名称')" :readonly="!form.edit" />
        </el-form-item>
        <el-form-item :label="$t('fenlei.活动海报')" prop="poster">
          <image-upload v-model="form.poster"/>
        </el-form-item>
        <el-form-item :label="$t('fenlei.减免方式')" prop="salesType">
          <el-radio-group v-model="form.salesType" :disabled="!form.edit">
            <el-radio
              v-for="dict in dict.type.sys_sales_type"
              :key="dict.value"
              :label="dict.value"
            >{{generateTitle(dict.label)}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="$t('fenlei.减免值')" prop="salesReduction">
          <el-input type="number" v-model="form.salesReduction" :placeholder="$t('fenlei.减免值说明')"  :readonly="!form.edit" />
        </el-form-item>
        <el-form-item :label="$t('fenlei.使用条件')" prop="salesCondition">
          <el-input type="number" v-model="form.salesCondition" :placeholder="$t('fenlei.使用条件说明')"  :readonly="!form.edit" />
        </el-form-item>
        <el-form-item :label="$t('fenlei.可用次数')" prop="salesFund">
          <el-input type="number" v-model="form.salesFund" :placeholder="$t('fenlei.可用次数说明')"  :readonly="!form.edit"/>
        </el-form-item>
        <el-form-item :label="$t('fenlei.结束日期')" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            :placeholder="$t('fenlei.请选择活动结束日期')"  :readonly="!form.edit">
          </el-date-picker>
        </el-form-item>
        <el-form-item :label="$t('fenlei.活动状态')" prop="salesState">
          <el-radio-group v-model="form.salesState">
            <el-radio
              v-for="dict in dict.type.sys_sales_state"
              :key="dict.value"
              :label="dict.value"
            >{{generateTitle(dict.label)}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item :label="$t('fenlei.活动说明')" prop="illustrate">
          <el-input v-model="form.illustrate" type="textarea" :placeholder="$t('fenlei.请输入内容')" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">{{$t('fenlei.确定')}}</el-button>
        <el-button @click="cancel">{{$t('fenlei.取消')}}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPromotion, getPromotion, delPromotion, addPromotion, updatePromotion } from "@/api/system/promotion";
import {generateTitle} from '../../../utils/i18.js'
import Bus from "@/utils/Bus";
export default {
  name: "Promotion",
  dicts: ['sys_sales_type', 'sys_sales_state'],
  data() {
    return {
      language:null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // SalesPromotion表格数据
      promotionList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        salesName: null,
        salesType: null,
        shId: null,
        spId: null,
        endTime: null,
        salesState: null,
        language:null,
      },
      // 表单参数
      form: {edit:true},
      // 表单校验
      rules: {
        salesName: [
          { required: true, message: this.$t('fenlei.活动名不能空'), trigger: "blur" }
        ],
        salesType: [
          { required: true, message: this.$t('fenlei.减免方式为必选'), trigger: "blur" }
        ],
        salesReduction: [
          { required: true, message: this.$t('fenlei.减免值不能为空'), trigger: "blur" },
          {
            validator: (rule, value, callback) => {
              // 当不是运费优惠(yhfangs≠1)时检查是否>0
              if (this.form.salesType != 1 && value <= 0) {
                callback(new Error(this.$t('fenlei.值必须大于0')));
              } else {
                callback();
              }
            },
            trigger: 'blur'
          }
        ],
        endTime: [
          { required: true, message: this.$t('fenlei.结束日期为必选'), trigger: "blur" }
        ],
        salesState: [
          { required: true, message: this.$t('fenlei.活动状态为必选'), trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.language =localStorage.getItem('lang')
    this.getList();
  },
  mounted() {
    // 确保Bus监听已建立
    Bus.$on('l18zh', (res) => {
      this.language = res;
      this.getList();
    })
  },
  methods: {
    generateTitle,
    /** 查询SalesPromotion列表 */
    getList() {
      this.loading = true;
      this.queryParams.language= this.language=='zh_CN'?'2':this.language=='zh_TW'?'3':this.language=='en_US'?'1':'0';
      listPromotion(this.queryParams).then(response => {
        this.promotionList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        salesName: null,
        salesType: null,
        salesReduction: null,
        salesCondition: null,
        salesFund: null,
        endTime: null,
        poster: null,
        salesState: null,
        illustrate: null,
        language:null,
        edit: true
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = this.$t('fenlei.新增');
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPromotion(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = this.$t('fenlei.修改');
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePromotion(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('fenlei.修改成功'));
              this.open = false;
              this.getList();
            });
          } else {
            this.form.language= this.language=='zh_CN'?'2':this.language=='zh_TW'?'3':this.language=='en_US'?'1':'0';
            addPromotion(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('fenlei.新增成功'));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm(this.$t('fenlei.是否确认删除门店分类编号为') + ids + this.$t('fenlei.的数据项')).then(function() {
        return delPromotion(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(this.$t('fenlei.删除成功'));
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/promotion/export', {
        ...this.queryParams
      }, `promotion_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
