<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item :label="$t('area.areaName')" prop="areaName">
        <el-input
          v-model="queryParams.areaName"
          placeholder=""
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item :label="$t('area.areaLevel')" prop="areaLevel">
        <el-select v-model="queryParams.areaLevel" placeholder="" clearable>
          <el-option
            v-for="dict in dict.type.area_level"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item :label="$t('area.parentArea')" prop="parentArea">
        <el-input
          v-model="queryParams.parentArea"
          placeholder=""
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">{{ $t('common.search') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('common.reset') }}</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:area:add']"
        >{{ $t('area.addArea') }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:area:export']"
        >{{ $t('common.export') }}</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList" >
      <el-table-column label="No" align="center" width="50px">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('area.areaName')" align="center" prop="areaName" />
      <el-table-column :label="$t('area.areaLevel')" align="center" prop="areaLevel" />
      <el-table-column :label="$t('area.parentArea')" align="center" prop="parentArea" />
      <el-table-column :label="$t('common.createTime')" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('common.remark')" align="center" prop="remark"/>
      <el-table-column :label="$t('common.operate')" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:area:edit']"
          >{{ $t('common.edit') }}</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:area:remove']"
          >{{ $t('common.delete') }}</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"
    />

    <!-- 添加或修改区域对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px" :inline="true">
        <el-row>
          <el-col :span="12">
            <el-form-item :label="$t('area.areaName')" prop="areaName">
              <el-input v-model="form.areaName" placeholder="" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('area.areaLevel')" prop="areaLevel" >
              <el-select v-model="queryParams.areaLevel" placeholder="" clearable>
                <el-option v-for="dict in dict.type.area_level" :key="dict.value" :label="dict.label" :value="dict.value"/>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item :label="$t('area.parentArea')" prop="parentArea">
              <el-input v-model="form.parentArea" placeholder="" :disabled="form.areaLevel == '1'"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <h3>{{ $t('area.containNation') + '：'}}</h3>
          <el-transfer v-model="form.nation" :data="nationList" ></el-transfer>
        </el-row>
      </el-form>

      <el-form label-width="80px" ref="form" :model="form">
        <el-col :span="24">
          <el-form-item :label="$t('common.remark')" prop="remark">
            <el-input v-model="form.remark" type="textarea" placeholder="" style="margin: 5px auto;width:95%" :rows="4"/>
          </el-form-item>
        </el-col>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">{{ $t('common.cancel') }}</el-button>
        <el-button type="primary" @click="submitForm">{{ $t('common.confirm') }}</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPost, getPost, delPost, addPost, updatePost } from "@/api/system/post";
import {parseTime} from "../../../utils/ruoyi";

export default {
  name: "Area",
  dicts: ['area_level'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 岗位表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        areaName: undefined,
        areaLevel: undefined,
        parentArea: undefined
      },
      nationList: [
        {key:'1', label: '国家1'}
      ],
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        countryName: [
          { required: true, message: "国家名称不能为空", trigger: "blur" }
        ],
        discount: [
          { required: true, message: "加价率不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    parseTime,
    /** 查询区域列表 */
    getList() {
      this.loading = true;
      listPost(this.queryParams).then(response => {
        this.postList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        areaName: undefined,
        areaLevel: undefined,
        parentArea: undefined,
        remark: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = this.$t('area.addArea');
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      getPost(row.areaId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = this.$t('area.editArea');
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.areaId != undefined) {
            updatePost(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('common.editSuccess'));
              this.open = false;
              this.getList();
            });
          } else {
            addPost(this.form).then(response => {
              this.$modal.msgSuccess(this.$t('common.addSuccess'));
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const areaIds = row.areaId;
      this.$modal.confirm(this.$t('common.isDelete')).then(function() {
        return delPost(areaIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess(this.$t('common.deleteSuccess'));
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/post/export', {
        ...this.queryParams
      }, `post_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
