<!--组织列表-->
<template>
  <div class="organization-list">
    <div v-loading.fullscreen.lock="loadingFull" />
    <div class="search">
      <el-form ref="searchParams" :model="searchParams" label-width="80px">
        <el-input
          v-model="searchParams.name"
          placeholder="支部名称"
          clearable
        ></el-input>
        <el-input
          v-model="searchParams.zb_sj"
          placeholder="支部书记"
          clearable
        ></el-input>
        <el-button type="primary" @click="searchOrganize" icon="el-icon-search"
          >搜索</el-button
        >
        <el-button
          type="success"
          @click="creatZhibu"
          icon="el-icon-edit"
          v-if="roles == 'superAdmin'"
          >创建支部</el-button
        >
      </el-form>
    </div>
    <!--组织列表-->
    <el-table :data="organizeList" border style="width: 100%">
      <el-table-column label="序号" type="index" width="50" align="center">
      </el-table-column>
      <el-table-column prop="name" label="支部名称" align="center" width="300">
      </el-table-column>
      <el-table-column prop="set_up_date" label="成立时间" align="center">
      </el-table-column>
      <el-table-column prop="zb_sj" label="支部书记" align="center">
      </el-table-column>
      <el-table-column
        prop="activists_member_num"
        label="积极分子数"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="development_member_num"
        label="发展对象数"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="prepare_party_member_num"
        label="预备党员数"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="party_member_num"
        label="正式党员数"
        align="center"
      >
      </el-table-column>

      <el-table-column
        prop="graduate_party_num"
        label="归档人数"
        align="center"
      >
      </el-table-column>
      <el-table-column prop="status" label="状态" align="center">
        <template slot-scope="{ row }">
          <el-tag size="mini" type="success" v-if="row.status == 1"
            >正常</el-tag
          >
          <el-tag size="mini" type="danger" v-else>暂停</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="操作" width="250" align="center">
        <template slot-scope="{ row }">
          <el-button
            type="primary"
            plain
            size="mini"
            @click="organizeDetail(row)"
            >详情</el-button
          >
          <el-button type="success" plain size="mini" @click="edit(row)"
            >编辑</el-button
          >
          <el-button
            v-if="roles == 'superAdmin'"
            type="warning"
            plain
            size="mini"
            @click="deleteList(row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!--分页-->
    <div class="paging">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        background
        :page-sizes="[15, 20, 30, 50, 100, 200]"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalPage"
      >
      </el-pagination>
    </div>
    <!--创建组织-->
    <el-dialog
      title="创建组织"
      :visible.sync="dialogVisible"
      width="60%"
      center
    >
      <div class="creatform">
        <el-form
          :model="createForm"
          :rules="creatRules"
          ref="createForm"
          label-width="130px"
          class="demo-ruleForm"
          label-position="left"
        >
          <el-form-item label="组织名称" prop="name">
            <el-input v-model="createForm.name"></el-input>
          </el-form-item>
          <el-form-item label="组织简称" prop="name">
            <el-input v-model="createForm.short_name"></el-input>
          </el-form-item>
          <el-form-item label="组织类型" prop="type">
            <el-select v-model="createForm.type" placeholder="请选择">
              <el-option
                v-for="item in organizeType"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="第几任支部" prop="record">
            <el-input-number
              v-model="createForm.record.zb_rq"
            ></el-input-number>
          </el-form-item>
          <!-- <el-form-item label="上级组织" prop="pid">
            <el-select v-model="createForm.pid" filterable placeholder="请选择">
              <el-option
                v-for="item in fatherList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item> -->

          <el-form-item label="成立日期" prop="set_up_date">
            <el-date-picker
              v-model="createForm.set_up_date"
              type="date"
              placeholder="选择日期时间"
              value-format="yyyy-MM-dd"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="支部状态" prop="status">
            <el-select
              v-model="createForm.status"
              filterable
              placeholder="请选择"
            >
              <el-option
                v-for="item in status"
                :key="item.value"
                :label="item.name"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="支部书记" prop="record">
            <el-input v-model="createForm.record.zb_sj"></el-input>
          </el-form-item>
          <el-form-item label="支部组织委员" prop="record">
            <el-input v-model="createForm.record.zb_wy"></el-input>
          </el-form-item>
          <el-form-item label="支部宣传委员" prop="record">
            <el-input v-model="createForm.record.zb_xcwy"></el-input>
          </el-form-item>
          <el-form-item label="支部其他人职务" prop="record">
            <div
              class="other_personnel"
              v-for="(item, index) in createForm.record.zb_qtry"
              :key="index"
            >
              <el-input v-model="item.name" placeholder="请输入姓名"></el-input>
              <el-input v-model="item.job" placeholder="请输入职位"></el-input>
            </div>
            <svg-icon
              icon-class="add"
              style="color:#409EFF;font-size:24px;"
              @click="addOtherPerson"
            />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm('createForm')"
              >立即创建</el-button
            >
            <el-button @click="resetForm('createForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
    <!--编辑组织-->
    <el-dialog
      title="编辑组织信息"
      :visible.sync="editOrganize"
      width="60%"
      center
    >
      <div class="creatform">
        <el-form
          :model="editForm"
          :rules="creatRules"
          ref="editForm"
          label-width="130px"
          class="demo-ruleForm"
        >
          <el-form-item label="组织名称" prop="name">
            <el-input v-model="editForm.name"></el-input>
          </el-form-item>
          <el-form-item label="组织简称" prop="name">
            <el-input v-model="editForm.short_name"></el-input>
          </el-form-item>
          <el-form-item label="组织类型" prop="type">
            <el-select
              v-model="editForm.type"
              placeholder="请选择"
              @change="changeOrgain"
            >
              <el-option
                v-for="item in organizeType"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item>

          <!-- <el-form-item label="上级组织" prop="pid">
            <el-select v-model="editForm.pid" filterable placeholder="请选择">
              <el-option
                v-for="item in fatherList"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              >
              </el-option>
            </el-select>
          </el-form-item> -->

          <el-form-item label="成立日期" prop="set_up_date">
            <el-date-picker
              v-model="editForm.set_up_date"
              type="date"
              placeholder="选择日期时间"
              value-format="yyyy-MM-dd"
            >
            </el-date-picker>
          </el-form-item>
          <el-form-item label="支部状态" prop="status">
            <el-select
              v-model="editForm.status"
              filterable
              placeholder="请选择"
            >
              <el-option
                v-for="item in status"
                :key="item.value"
                :label="item.name"
                :value="item.value"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="editForms('editForm')"
              >编辑</el-button
            >
            <el-button @click="resetForm('editForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  organizationsList,
  fatherLists,
  editOrganization,
  deleteOrganization,
  buildOrganizations
} from "@/api/organizationList/index";
import { async } from "q";
import { runInThisContext } from "vm";
import store from "@/store";
export default {
  data() {
    return {
      loadingFull: false,
      editOrganize: false,
      dialogVisible: false,
      searchParams: {
        name: "", //支部名称
        zb_sj: "" //支部书记
      }, //搜索框数据
      limit: 15, //每页显示条数
      page: 1, //当前页码
      totalPage: 0, //总页码
      organizeList: [], //组织列表
      fatherList: [
        {
          id: 0,
          name: "未选择"
        }
      ], //父级党组织列表
      organizeType: [
        {
          id: 0,
          name: "未选择"
        }
      ], //组织类型
      orgainIndex: "", //组织索引
      status: [
        {
          value: 0,
          name: "暂停"
        },
        {
          value: 1,
          name: "正常"
        }
      ],
      editForm: {
        name: "",
        short_name: "",
        type: "",
        pid: "",
        set_up_date: "",
        status: "",
        id: ""
      },
      createForm: {
        name: "",
        short_name: "",
        type: 5,
        pid: 0,
        set_up_date: "",
        status: 1,
        record: {
          zb_sj: "",
          zb_wy: "",
          zb_xcwy: "",
          zb_qtry: [
            {
              name: "",
              job: ""
            }
          ],
          zb_rq: 1,
          zb_hj_date: ""
        }
      }, //创建组织表单

      otherPerson: [
        {
          index: "",
          name: "",
          job: ""
        }
      ],
      editId: "", //编辑列表id
      otherIndex: 0,
      ediPersonValue: "1",
      //创建表单验证规则
      creatRules: {
        name: [{ required: true, message: "请输入组织名称", trigger: "blur" }],
        short_name: [
          { required: true, message: "请输入组织简称", trigger: "blur" }
        ],
        type: [
          { required: true, message: "请选择组织类型", trigger: "change" }
        ],
        pid: [{ required: true, message: "请选择父级组织", trigger: "change" }],
        set_up_date: [
          { required: true, message: "请选择组织成立时间", trigger: "change" }
        ]
      },
      roles: ""
    };
  },
  created() {
    let role = store.getters.roles;
    if (role.includes("Super admin")) {
      this.roles = "superAdmin";
    }
    this.organizationsList();
    this.fatherOrganiza();
    let allStatus = JSON.parse(sessionStorage.getItem("status"));
    let arr = [];
    let organize = allStatus["App\\Repositories\\Enums\\OrganizationTypeEnum"];
    for (let i in organize) {
      arr.push({
        id: parseInt(i),
        name: organize[i]
      });
    }
    this.organizeType = arr;
  },
  methods: {
    creatZhibu: function() {
      this.dialogVisible = true;
      this.createForm = {
        type: 5,
        pid: 0,
        status: 1,
        record: {
          zb_rq: 1
        }
      };
    },
    //组织索引
    changeOrgain(e) {
      this.orgainIndex = e;
      this.editForm.type = e;
    },
    //获取父级党组织名称
    async fatherOrganiza(name) {
      await fatherLists(name).then(res => {
        console.log(res);
        this.fatherList = this.fatherList.concat(res.data);

        console.log(this.fatherList, "this.fatherList");
      });
    },
    //获取党组织列表
    async organizationsList(data) {
      this.loadingFull = true;
      const params = data;
      console.log(data, "data");
      await organizationsList(params).then(res => {
        console.log(res, "909090");
        this.totalPage = res.data.meta.pagination.total;
        this.organizeList = res.data.data;
        this.loadingFull = false;
      });
    },
    //列表搜索
    searchOrganize() {
      let data = this.searchParams;
      this.organizationsList(data);
    },
    //添加组织内部其他任职人员
    addOtherPerson(date) {
      this.index = this.index++;
      this.otherPerson.push({
        name: "",
        index: this.index,
        job: ""
      });
      console.log(this.otherPerson);
      this.createForm.record.zb_qtry = this.otherPerson;
    },
    //组织详情
    organizeDetail(row) {
      this.$router.push({ name: "detail", query: { id: row.id } });
    },
    //编辑组织
    edit(row) {
      this.editOrganize = true;
      this.editForm = {
        name: row.name,
        short_name: row.short_name,
        type: row.type,
        pid: row.pid,
        set_up_date: row.set_up_date,
        status: row.status,
        id: row.id
      };
      this.orgainIndex = row.type;
      this.editId = row.id;
    },
    editForms(formName) {
      this.editOrganize = true;
      this.$refs[formName].validate(valid => {
        if (valid) {
          console.log(this.createForm, "crea6");
          this.editOrganization();
          this.editOrganize = false;
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    async editOrganization() {
      const data = this.editForm;
      await editOrganization(data)
        .then(res => {
          console.log(res, "编辑列表");
          if (res.code === 200) {
            this.$message({
              message: "编辑成功",
              type: "success"
            });
            this.organizationsList();
          } else {
            this.$message.error("编辑失败请重试");
          }
        })
        .catch(err => {
          console.log(err, "");
          this.loadingFull = false;
        });
    },
    //删除列表
    deleteList(data) {
      this.$confirm("此操作将删除此项模板, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.deleteOrganize(data);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    async deleteOrganize(data) {
      console.log(data, "daatata");
      await deleteOrganization(data).then(res => {
        if (!res.code) {
          this.$message({
            type: "success",
            message: "删除成功!"
          });
          this.organizationsList();
        } else {
          this.$message({
            type: "error",
            message: "删除失败!"
          });
        }
      });
    },
    submitForm(formName) {
      console.log(this.createForm.type, "创建测试");
      this.$refs[formName].validate(valid => {
        if (valid) {
          this.buildOrganize();
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    async buildOrganize() {
      this.loadingFull = true;
      const data = this.createForm;
      await buildOrganizations(data)
        .then(res => {
          console.log(res, "chuangjianceshi");
          this.dialogVisible = false;
          this.organizationsList();
          this.loadingFull = false;
          this.$message({
            type: "success",
            message: "创建成功!"
          });

          // this.$store.dispatch("tagsView/delView", this.$route);
          // 返回上一步路由
          //this.$router.go(-1);
          //this.$refs[formName].resetFields();
        })
        .catch(err => {
          console.log(err, "");
          this.loadingFull = false;
        });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    //切换页码
    handleSizeChange(e) {
      console.log(e, "切换页码");
      this.limit = e;
      this.searchParams.limit = e;
      const data = {
        name: this.searchParams.name, //支部名称
        zb_sj: this.searchParams.zb_sj, //支部书记
        limit: e,
        page: this.page
      };
      this.organizationsList(data);
    },
    //切换每页显示数量
    handleCurrentChange(e) {
      console.log(e, "当前页码");
      this.searchParams.page = e;
      const data = {
        name: this.searchParams.name, //支部名称
        zb_sj: this.searchParams.zb_sj, //支部书记
        page: e,
        limit: this.limit
      };
      this.organizationsList(data);
    }
  }
};
</script>
<style lang="scss" scoped>
@import "@/assets/styles/content.scss";
.organization-list {
  .creatform {
    width: 60%;
    margin: 0 auto;
    .other_personnel {
      display: inline-block;
      vertical-align: middle;
      margin-bottom: 10px;
      width: 90%;
      .svg-icon {
        vertical-align: middle;
        display: inline-block;
      }
      .el-input {
        width: 45% !important;
        display: inline-block;
        vertical-align: middle;
        margin-right: 20px;
      }
    }
  }
  .search {
    .el-input {
      width: 300px !important;
    }
  }
}
</style>
