<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="areaName"
            placeholder="请输入区域名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container" style="height: 580px; overflow-y: auto">
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            @node-click="handleNodeClick"
            @node-expand="handleNodeClick"
          ></el-tree>
        </div>
      </el-col>
      <!--用户数据-->
      <el-col :span="20" :xs="24">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
          v-show="showSearch"
          label-width="68px"
        >
          <el-form-item label="区域名称" prop="areaName">
            <el-input
              v-model="queryParams.areaName"
              placeholder="请输入区域名称"
              clearable
              @keyup.enter.native="resethandleQuery"
            />
          </el-form-item>
          <el-form-item label="区域编码" prop="areaCode">
            <el-input
              v-model="queryParams.areaCode"
              placeholder="请输入区域编码"
              clearable
              @keyup.enter.native="resethandleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="resethandleQuery"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
              >重置</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']"
              >新增</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              :disabled="single"
              @click="handleUpdate"
              v-hasPermi="['system:area:edit']"
              >修改</el-button
            >
          </el-col>
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              :disabled="multiple"
              @click="handleDelete"
              v-hasPermi="['system:area:remove']"
              >删除</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']"
              >导出</el-button
            >
          </el-col>
          <right-toolbar
            :showSearch.sync="showSearch"
            @queryTable="getList"
          ></right-toolbar>
        </el-row> -->
        <el-table
          v-loading="loading"
          :data="areaList"
          @selection-change="handleSelectionChange"
        >
          <el-table-column type="selection" width="55" align="center" />
          <!-- <el-table-column label="父级id" align="center" prop="parentId" /> -->
          <el-table-column label="区域名称" align="center" prop="areaName" />
          <el-table-column label="区域编码" align="center" prop="areaCode" />
          <el-table-column label="偏远地区" align="center" prop="remoteSigns">
            <template slot-scope="scope">
              <el-switch
                v-hasPermi="['system:area:updateRemoteSigns']"
                v-model="scope.row.remoteSigns"
                active-color="#13ce66"
                inactive-color="#ff4949"
                @change="updateRemoteSigns(scope.row)"
              >
              </el-switch>

              <!-- {{ scope.row.remoteSigns == 0 ? "否" : "是" }} -->
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-circle-plus-outline"
                @click="handleadd(scope.row)"
                v-hasPermi="['system:area:add']"
                >新增</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:area:edit']"
                >修改</el-button
              >
              <!-- <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                v-hasPermi="['system:area:remove']"
                >删除</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-col>
    </el-row>

    <!-- 添加或修改区域对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="130px">
        <el-form-item
          label="父级区域名称"
          prop="parentName"
          :required="true"
          v-if="form.parentName"
        >
          <el-input
            v-model="form.parentName"
            placeholder="请输入父级区域名称"
            disabled
          />
        </el-form-item>
        <el-form-item
          label="父级区域编码"
          prop="parentCode"
          :required="true"
          v-if="form.parentCode"
        >
          <el-input
            v-model="form.parentCode"
            placeholder="请输入父级区域编码"
            disabled
          />
        </el-form-item>
        <el-form-item label="区域名称" prop="areaName">
          <el-input v-model="form.areaName" placeholder="请输入区域名称" />
        </el-form-item>
        <el-form-item label="区域编码" prop="areaCode">
          <el-input v-model="form.areaCode" placeholder="请输入区域编码" />
        </el-form-item>
        <el-form-item label="区域级别" prop="areaLevel">
          <el-input v-model="form.areaLevel" placeholder="请输入区域级别" />
        </el-form-item>
        <el-form-item label="偏远地区" prop="remoteSigns">
          <el-radio v-model="form.remoteSigns" label="0">否</el-radio>
          <el-radio v-model="form.remoteSigns" label="1">是</el-radio>
        </el-form-item>
        <el-form-item label="排序" prop="">
          <el-input v-model.number="form.sort" placeholder="请输入排序" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listArea,
  getArea,
  delArea,
  addArea,
  updateArea,
  selectAreaList,
  selectAreaListinfo,
  selectAreaListquery,
  updateRemoteSigns,
} from "@/api/system/area";

export default {
  name: "Area",
  data() {
    return {
      // 省级树选项
      deptOptions: undefined,
      defaultProps: {
        children: "children",
        label: "areaName",
      },
      areaName: "",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 区域表格数据
      areaList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parentId: null,
        areaCode: null,
        areaName: null,
        areaLevel: null,
        sort: null,
        areaType: null,
        parentCode: null,
        remoteSigns: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        areaCode: [
          { required: true, message: "请输入区域编码", trigger: "blur" },
        ],
        areaName: [
          { required: true, message: "请输入区域名称", trigger: "blur" },
        ],
        areaLevel: [
          { required: true, message: "请输入区域级别", trigger: "blur" },
        ],
        remoteSigns: [
          { required: true, message: "请选择是否是偏远地区", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.selectAreaListin();
  },
  watch: {
    // 根据名称筛选部门树
    areaName(val) {
      if (val) {
        if (val == " ") {
          this.areaName = "";
          return;
        } else {
          selectAreaListquery(val).then((res) => {
            this.deptOptions = res.data;
          });
        }
      } else {
        if (val == "") {
        } else {
          this.selectAreaListin();
        }
      }
    },
  },
  methods: {
    //修改是否为边远地区
    updateRemoteSigns(row) {
      if (row.remoteSigns) {
        this.$confirm('确定修改"' + row.areaName + '"为偏远地区吗?', {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            var remoteSigns = null;
            if (row.remoteSigns) {
              remoteSigns = 1;
            } else {
              remoteSigns = 0;
            }
            updateRemoteSigns(row.id, remoteSigns).then((res) => {
              if (res.code == 200) {
                this.$message({
                  type: "success",
                  message: "修改为偏远地区!",
                });
              }
            });
          })
          .catch(() => {
            row.remoteSigns = false;
            this.$message({
              type: "info",
              message: "取消修改",
            });
          });
      } else {
        this.$confirm('确定修改"' + row.areaName + '"为非偏远地区吗吗?', {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(() => {
            var remoteSigns = null;
            if (row.remoteSigns) {
              remoteSigns = 1;
            } else {
              remoteSigns = 0;
            }
            updateRemoteSigns(row.id, remoteSigns).then((res) => {
              if (res.code == 200) {
                this.$message({
                  type: "success",
                  message: "修改为非偏远地区!",
                });
              }
            });
          })
          .catch(() => {
            row.remoteSigns = true;
            this.$message({
              type: "info",
              message: "取消修改",
            });
          });
      }
    },
    // 节点单击事件
    handleNodeClick(data) {
      this.queryParams.areaLevel = null;
      this.loading = true;
      this.queryParams.parentCode = data.areaCode;
      if (data.areaCode != 1) {
        this.handleQuery();
        selectAreaList("", data.areaCode).then((res) => {
          if (res.data.length > 0) {
            res.data.forEach((e) => {
              e.children = [
                {
                  areaName: "",
                },
              ];
            });
          } else {
            this.queryParams.parentCode = null;
          }
          data.children = res.data;
          this.loading = false;
        });
      } else {
        this.queryParams.parentCode = null;
        this.queryParams.areaLevel = 1;
        this.handleQuery();
        selectAreaListinfo(1).then((res) => {
          if (res.data.length > 0) {
            res.data.forEach((e) => {
              e.children = [
                {
                  areaName: "",
                },
              ];
            });
          } else {
            this.queryParams.parentCode = null;
          }
          this.loading = false;
          data.children = res.data;
        });
      }
    },
    selectAreaListin() {
      this.loading = true;
      selectAreaListinfo(1).then((res) => {
        res.data.forEach((e) => {
          e.children = [
            {
              areaName: "",
            },
          ];
        });
        var obj = [
          {
            areaName: "区域列表",
            children: res.data,
            areaCode: 1,
          },
        ];
        this.deptOptions = obj;
        this.loading = false;
      });
    },
    info() {
      this.loading = true;
      selectAreaList(this.areaName).then((res) => {
        this.loading = false;
        var obj = {
          areaName: "区域列表",
          children: res.data,
        };
        this.deptOptions = obj;
      });
    },
    /** 查询区域列表 */
    getList() {
      this.loading = true;
      listArea(this.queryParams).then((response) => {
        response.rows.forEach((e) => {
          if (e.remoteSigns == 1) {
            e.remoteSigns = true;
          } else {
            e.remoteSigns = false;
          }
        });
        this.areaList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        parentName: null,
        parentId: null,
        areaCode: null,
        areaName: null,
        areaLevel: null,
        sort: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        areaType: null,
        parentCode: null,
        remoteSigns: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    resethandleQuery() {
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      this.queryParams.areaLevel = null;
      this.queryParams.parentName = null;
      this.getList();
    },
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.parentCode = null;
      this.queryParams.areaLevel = 1;
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleadd(row) {
      this.reset();
      this.form.parentId = row.id;
      this.form.parentCode = row.areaCode;
      this.form.parentName = row.areaName;
      this.title = "添加区域";
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getArea(id).then((response) => {
        response.data.remoteSigns = String(response.data.remoteSigns);
        this.form = response.data;
        this.open = true;
        this.title = "修改区域";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateArea(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addArea(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除区域名称为"' + row.areaName + '"的数据项吗？')
        .then(function () {
          return delArea(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/area/export",
        {
          ...this.queryParams,
        },
        `area_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
