<template>
  <div class="app-container">
    <card-table :modalData="countryList" labelWidth="120px" :searchParams="queryParams" @pagination="getList"
      :loading="loading" :tableSearch="true" @columnSearch="handleQuery" :delProp="['createTime']"
      @resetList="resetQuery" @selection-change="handleSelectionChange" showNo show-selection>
      <template #operations="{ selections }">
        <el-col :span="1.5">
          <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
            v-hasPermi="['user:country:add']">{{ $t('nation.addNation') }}</el-button>
        </el-col>
        <el-col :span="1.5" style="margin-left: 10px;">
          <el-button plain icon="el-icon-download" size="mini" @click="handleExport"
            v-hasPermi="['user:country:export']">{{ $t('common.export') }}</el-button>
        </el-col>
      </template>
      <template #default="{ list }">
        <!--<el-table-column type="selection" width="50" align="center"/>-->
        <!--<el-table-column label="No." type="index" width="50" align="center" />-->
        <!--<Column :label="$t('nation.countryId')" prop="countryId" width="150px" />-->
        <Column :label="$t('nation.countryName')" prop="countryName" :searchParams="queryParams" :showSearch="true"
          width="250px" />
        <Column :label="$t('nation.countryNameUS')" prop="countryNameUS" :searchParams="queryParams" :showSearch="true"
          width="250px" />
        <Column :label="$t('nation.countryNameRU')" prop="countryNameRU" :searchParams="queryParams" :showSearch="true"
          width="250px" />
        <Column :label="$t('nation.countryNameES')" prop="countryNameES" :searchParams="queryParams" :showSearch="true"
          width="250px" />
        <Column :label="$t('nation.countryAbbr')" prop="countryAbbr" :searchParams="queryParams" :showSearch="true"
                width="250px" />
        <Column :label="$t('nation.discount') + '(%)'" prop="discount" width="200px" />
        <Column :label="$t('personal.createDate')" prop="createTime" width="300px" :searchParams="queryParams"
          :showSearch="true" searchType="dateRange">
          <template #default="{ row }">
            <span>{{ parseTime(row.createTime, '{y}-{m}-{d}') }}</span>
          </template>
        </Column>
        <Column :label="$t('common.remark')" prop="remark" width="300px" />
        <Column :label="$t('common.operate')" :min-width="250" fixed="right" :showSearch="true" searchType="operate">
          <template #default="{ row }">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(row)"
              v-hasPermi="['user:country:edit']">{{ $t('common.edit') }}</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(row)"
              v-hasPermi="['user:country:remove']">{{ $t('common.delete') }}</el-button>
          </template>
        </Column>
      </template>
    </card-table>

    <!-- 添加或修改国家对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="750px" append-to-body>
      <el-form class="country-form" ref="form" :model="form" :rules="rules" label-position="top">
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item :label="$t('nation.countryName')" prop="countryName">
              <el-input v-model.trim="form.countryName"
                :placeholder="$t('common.pleaseInput') + $t('nation.countryName')" maxlength="100" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('nation.countryNameUS')" prop="countryNameUS">
              <el-input v-model.trim="form.countryNameUS" maxlength="100"
                :placeholder="$t('common.pleaseInput') + $t('nation.countryNameUS')" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item :label="$t('nation.countryNameRU')" prop="countryNameRU">
              <el-input v-model.trim="form.countryNameRU" maxlength="100"
                :placeholder="$t('common.pleaseInput') + $t('nation.countryNameRU')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('nation.countryNameES')" prop="countryNameES">
              <el-input v-model.trim="form.countryNameES" maxlength="100"
                :placeholder="$t('common.pleaseInput') + $t('nation.countryNameES')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('nation.countryAbbr')" prop="countryAbbr">
              <el-input v-model.trim="form.countryAbbr" maxlength="100"
                        :placeholder="$t('common.pleaseInput') + $t('nation.countryAbbr')" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <InputNumber width="100%" :label="$t('nation.discount') + '(%)'" prop="discount" required :precision=2
              :min="0" />
          </el-col>
        </el-row>
        <!-- <el-form-item :label="$t('nation.discount') + '(%)'" prop="discount">
              <el-input v-model="form.discount" :placeholder="$t('common.pleaseInput') + $t('nation.discount')" />
            </el-form-item> -->
        <el-form-item :label="$t('common.remark')" prop="remark">
          <el-input v-model="form.remark" type="textarea" maxlength="500" show-word-limit
            :placeholder="$t('common.pleaseInput') + $t('common.remark')" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" icon="el-icon-circle-check" @click="submitForm">{{ $t('common.confirm') }}</el-button>
        <el-button @click="cancel" icon="el-icon-remove-outline">{{ $t('common.cancel') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCountry, getCountry, delCountry, addCountry, updateCountry } from "@/api/user/country";
import CardForm from "../../../components/element/form/CardForm";
import DatePickerRange from "../../../components/element/form/items/DatePickerRange";
import Select from "../../../components/element/form/items/Select";
import CardTable from "../../../components/element/table/CardTable";
import Column from "../../../components/element/table/Column";
import { parseTime } from "@/utils/ruoyi";

export default {
  name: "Country",
  components: { Column, CardTable, Select, DatePickerRange, CardForm },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 国家表格数据
      countryList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        countryName: [
          { required: true, message: this.$t('common.pleaseInput') + this.$t('nation.countryName'), trigger: 'blur' },
          // { pattern: /^[^\s]+(\s+[^\s]+)*$/, message: this.$t('common.noSpace'), trigger: 'blur' }
        ],
        countryNameUS: [
          { required: true, message: this.$t('common.pleaseInput') + this.$t('nation.countryNameUS'), trigger: 'blur' },
          // { pattern: /^[^\s]+(\s+[^\s]+)*$/, message: this.$t('common.noSpace'), trigger: 'blur' }
        ],
        countryNameRU: [
          { required: true, message: this.$t('common.pleaseInput') + this.$t('nation.countryNameRU'), trigger: 'blur' },
          // { pattern: /^[^\s]+(\s+[^\s]+)*$/, message: this.$t('common.noSpace'), trigger: 'blur' }
        ],
        countryNameES: [
          { required: true, message: this.$t('common.pleaseInput') + this.$t('nation.countryNameES'), trigger: 'blur' },
          // { pattern: /^[^\s]+(\s+[^\s]+)*$/, message: this.$t('common.noSpace'), trigger: 'blur' }
        ],
      }
    };
  },
  created() {
    this.getList(this.queryParams);
  },
  methods: {
    parseTime,
    /** 查询国家列表 */
    getList(param) {
      this.loading = true;
      listCountry(param).then(response => {
        this.countryList = response.rows;
        this.queryParams.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        countryId: null,
        countryName: null,
        discount: null,
        delFlag: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery(param) {
      param.pageNum = 1;
      this.getList(param);
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
      }
      this.getList(this.queryParams);
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.countryId)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = this.$t('nation.addNation');
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const countryId = row.countryId || this.ids
      getCountry(countryId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = this.$t('nation.editNation');
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.countryId != null) {
            updateCountry(this.form).then(response => {
              if (response.code == 200) {
                this.$modal.msgSuccess(this.$t('common.editSuccess'));
                this.open = false;
                this.getList(this.queryParams);
              } else {
                this.$modal.msgError(response.msg);
              }
            }).catch(() => { });
          } else {
            addCountry(this.form).then(response => {
              if (response.code == 200) {
                this.$modal.msgSuccess(this.$t('common.addSuccess'));
                this.open = false;
                this.getList(this.queryParams);
              } else {
                this.$modal.msgError(response.msg);
              }
            }).catch(() => { });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const countryIds = row.countryId || this.ids;
      this.$modal.confirm(this.$t('common.isDelete')).then(function () {
        return delCountry(countryIds);
      }).then(() => {
        this.getList(this.queryParams);
        this.$modal.msgSuccess(this.$t('common.deleteSuccess'));
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.queryParams.language = this.$i18n.locale;
      // this.queryParams.ids = this.ids;
      let fileName = this.$t('common.countryManage') + parseTime(new Date(), '{y}{m}{d}') + '.xlsx'
      this.download('user/country/export', { ...this.queryParams, ids: this.ids }, fileName)
    }
  }
};
</script>

<style lang="scss" rel="stylesheet/scss" scoped>
.country-form {
  padding: 0 20px;
}

::v-deep .country-form .el-form-item--medium .el-form-item__label {
  line-height: 10px;
}
</style>
