<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="家族ID" prop="code">
        <el-input
          v-model="queryParams.code"
          placeholder="请输入家族ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="家族名称" prop="fullName">
        <el-input
          v-model="queryParams.fullName"
          placeholder="请输入家族名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓氏" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓氏"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="家族地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入家族地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="cyan"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</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"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:family:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:family:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:family:remove']"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="familyList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="主键" align="center" prop="id" v-if="false" />
      <el-table-column
        label="家族ID"
        align="center"
        prop="code"
        width="120px"
      />
      <el-table-column label="姓氏" align="center" prop="name" width="80px" />
      <el-table-column
        label="家族全称"
        align="center"
        prop="fullName"
        width="150px"
      />
      <el-table-column
        label="创建者"
        align="center"
        prop="createName"
        width="100px"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.createName">{{ scope.row.createName }}</span>
          <span v-if="scope.row.createName == null">管理员</span>
        </template>
      </el-table-column>
      <el-table-column
        label="家族地址"
        align="center"
        prop="address"
        width="350px"
      />
      <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-share"
            @click="familyTree(scope.row)"
            v-hasPermi="['system:family:list']"
            >家族树</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="familyMember(scope.row)"
            v-hasPermi="['system:family:edit']"
            >成员</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-menu"
            @click="familyAlbum(scope.row)"
            v-hasPermi="['system:album:list']"
            >相册</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="familyEvents(scope.row)"
            v-hasPermi="['system:events:list']"
            >大事件</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-menu"
            @click="familyGarden(scope.row)"
            v-hasPermi="['system:garden:list']"
            >家族园地</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:family: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-dialog
      :title="title"
      :visible.sync="open"
      width="750px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="0" type="flex" justify="start">
          <el-col :span="16">
            <el-form-item label="家族名称" prop="fullName">
              <el-input v-model="form.fullName" placeholder="请输入家族名称" />
            </el-form-item>
            <el-form-item label="姓氏" prop="name">
              <el-input v-model="form.name" placeholder="请输入姓氏" />
            </el-form-item>
            <el-form-item label="家族地址" prop="address">
              <area-cascader
                v-if="pshow"
                :level="1"
                type="all"
                :data="pcaa"
                v-model="addressCode"
                placeholder="请选择地址"
                @change="getChangeVal"
              ></area-cascader>
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label-width="0px" prop="avatar">
              <div class="text-center">
                <familyAvatar :family="form" :key="reFresh" />
                ID: {{ form.code }}
              </div>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="家族描述" prop="contents">
          <!-- <editor v-model="form.contents" :min-height="292" /> -->
          <el-input
            :rows="10"
            v-model="form.contents"
            type="textarea"
            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 {
  listFamily,
  getFamily,
  delFamily,
  addFamily,
  updateFamily,
  exportFamily
} from "@/api/system/family";
import { pca, pcaa } from "area-data";
import Editor from "@/components/Editor";
import familyAvatar from "./detail/familyAvatar";

export default {
  name: "Family",
  components: {
    Editor,
    familyAvatar
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 家族表格数据
      familyList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        name: null,
        fullName: null,
        hometown: null,
        address: null,
        contents: null,
        avatar: null
      },
      reFresh: 1,
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        fullName: [
          { required: true, message: "家族名称不能为空", trigger: "blur" }
        ],
        name: [{ required: true, message: "姓氏不能为空", trigger: "blur" }],
        contents: [{ required: true, message: "描述不能为空", trigger: "blur" }]
      },

      addressCode: [],
      pcaa: pcaa,
      pshow: true
    };
  },
  watch: {
    // 监听form值，只要有改变，则key值reFresh变更。
    // 然后自定义上传图片组件familyAvatar重新渲染
    form() {
      ++this.reFresh;
    }
  },
  created() {
    this.addressShow();
    this.getList();
  },
  methods: {
    /** 清空地址选择器 */
    addressShow() {
      this.pshow = false;
      this.addressCode = [];
      var t = setTimeout(() => {
        this.pshow = true;
      }, 0);
    },
    /** 选择家族地址赋值 */
    getChangeVal(data) {
      var val = "";
      var kk = [];
      for (var key in data) {
        for (var k in data[key]) {
          val += data[key][k];
          kk.push(k);
        }
      }
      console.log(kk.join(",") + " | " + val);

      this.form.addressCode = kk.join(",");
      this.form.address = val;
    },
    /** 查询家族列表 */
    getList() {
      this.loading = true;
      listFamily(this.queryParams).then(response => {
        this.familyList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        name: null,
        fullName: null,
        hometown: null,
        addressCode: null,
        address: null,
        contents: null,
        avatar: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      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.addressShow();
      this.reset();
      this.open = true;
      this.title = "添加家族";
      // this.addressCode = [pcaa[86]["110000"], pcaa["110000"]["110100"], pcaa["110100"]["110105"]]
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getFamily(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改家族";
        var code = this.form.addressCode.split(",");
        this.addressCode = [
          pcaa[86][code[0]],
          pcaa[code[0]][code[1]],
          pcaa[code[1]][code[2]]
        ];
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateFamily(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addFamily(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除家族编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delFamily(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有家族数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return exportFamily(queryParams);
        })
        .then(response => {
          this.download(response.msg);
        })
        .catch(function() {});
    },
    familyMember(row) {
      this.$router.push({
        path: "/family/member",
        query: {
          fid: row.id
        }
      });
    },
    familyTree(row) {
      this.$router.push({
        path: "/family/tree",
        query: {
          fid: row.id
        }
      });
    },
    familyAlbum(_row) {
      this.$router.push({
        path: "/family/album",
        query: {
          fid: _row.id
        }
      });
    },
    familyEvents(_row) {
      this.$router.push({
        path: "/family/events",
        query: {
          fid: _row.id
        }
      });
    },
    familyGarden(_row) {
      this.$router.push({
        path: "/family/garden",
        query: {
          fid: _row.id
        }
      });
    },
  }
};
</script>

<style>
.area-select {
  position: relative;
  display: inline-block;
  vertical-align: top;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  user-select: none;
  height: 36px;
  line-height: 36px;
  cursor: pointer;
  background: #fff;
  border-radius: 4px;
  border: 1px solid #e1e2e6;
}
.area-select .area-selected-trigger {
  padding: 0px 13px;
}
.area-select.large {
  width: 100%;
}
/* .el-form-item {
  margin-bottom: 8px;
} */
</style>
