<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="所属网格" prop="deptId">
        <treeselect v-model="queryParams.deptId" :options="deptOptions" :disable-branch-nodes="true" style="width:215px;"
                    placeholder="请选择所属网格"/>
      </el-form-item>
      <el-form-item label="居民姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入居民姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="cardId">
        <el-input
          v-model="queryParams.cardId"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="籍贯" prop="jg">
        <el-input
          v-model="queryParams.jg"
          placeholder="请输入籍贯"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="入党时间" prop="joinDate">
        <el-date-picker clearable
          v-model="queryParams.joinDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择入党时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="入党单位" prop="joinUnit">
        <el-input
          v-model="queryParams.joinUnit"
          placeholder="请输入入党单位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="党员分类" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="党员分类"
          clearable
          style="width:100%"
        >
          <el-option
            v-for="dict in dict.type.szxc_dy_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="职务" prop="post">
        <el-input
          v-model="queryParams.post"
          placeholder="请输入职务"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" 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"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['szxc:dymanage: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="['szxc:dymanage: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="['szxc:dymanage: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="['szxc:dymanage:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dymanageList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="45" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="居民姓名" align="center" prop="name" />
      <el-table-column label="身份证号" align="center" prop="cardId" />
      <el-table-column label="手机号" align="center" prop="phone" />
      <el-table-column label="所属网格" align="center" prop="deptName" />
      <el-table-column label="籍贯" align="center" prop="jg" />
      <el-table-column label="入党时间" align="center" prop="joinDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.joinDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="入党单位" align="center" prop="joinUnit" />
      <el-table-column label="职务" align="center" prop="post" />
      <el-table-column label="党员分类" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.szxc_dy_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="remark" />
      <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-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['szxc:dymanage:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['szxc:dymanage: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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="居民姓名" prop="name">
          <el-input readonly v-if="!form.id"
                    placeholder="请选择居民姓名"
                    v-model="form.name">
            <el-button slot="append" icon="el-icon-search" @click="ownerSelect"></el-button>
          </el-input>

          <el-input v-if="form.id" v-model="form.name" disabled placeholder="请输入居民姓名" />

        </el-form-item>
        <el-form-item label="身份证号" prop="cardId">
          <el-input v-model="form.cardId" placeholder="请输入身份证号" :disabled="form.id"/>
        </el-form-item>
        <el-form-item label="所属网格" prop="deptName">
          <el-input v-model="form.deptName" placeholder="请输入所属网格" :disabled="form.id"/>
        </el-form-item>
        <el-form-item label="籍贯" prop="jg">
          <el-input v-model="form.jg" placeholder="请输入籍贯" :disabled="form.id"/>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="入党时间" prop="joinDate">
          <el-date-picker clearable style="width:100%"
            v-model="form.joinDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择入党时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="入党单位" prop="joinUnit">
          <el-input v-model="form.joinUnit" placeholder="请输入入党单位" />
        </el-form-item>
        <el-form-item label="党员分类" prop="type">
          <el-select
            v-model="form.type"
            placeholder="党员分类"
            clearable
            style="width:100%"
          >
            <el-option
              v-for="dict in dict.type.szxc_dy_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="职务" prop="post">
          <el-input v-model="form.post" placeholder="请输入职务" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" 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>

    <el-dialog title="选择居民" :visible.sync="jmin.visible" width="1000px" append-to-body>
      <el-form :model="jmin.queryParams" ref="queryForm" size="small" :inline="true" label-width="70px">
        <el-form-item label="身份证号" prop="cardId">
          <el-input
            v-model="jmin.queryParams.cardId"
            placeholder="请输入身份证号"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input
            v-model="jmin.queryParams.name"
            placeholder="请输入姓名"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="jminQuery">搜索</el-button>
        </el-form-item>
      </el-form>
      <el-alert title="双击选择居民" type="warning" :closable="false"></el-alert>
      <el-table v-loading="jmin.loading" :data="jmin.list" border @cell-dblclick="jminDblclick">
        <el-table-column label="姓名" align="center" prop="name" min-width="80"/>
        <el-table-column label="身份证号" align="center" prop="cardId" min-width="180"/>
        <el-table-column label="所属网格" align="center" prop="deptName" min-width="100"/>
        <el-table-column label="性别" align="center" prop="sex">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
          </template>
        </el-table-column>
        <el-table-column label="籍贯" align="center" prop="jg" min-width="180"/>
        <el-table-column label="手机号" align="center" prop="phone" min-width="120"/>
        <el-table-column label="现居住地" align="center" prop="currentAddress" min-width="180"/>
      </el-table>
      <pagination
        v-show="jmin.total>0"
        :total="jmin.total"
        :page.sync="jmin.queryParams.pageNum"
        :limit.sync="jmin.queryParams.pageSize"
        @pagination="getlistJminfo"
      />
    </el-dialog>

  </div>
</template>

<script>
import { listDymanage, getDymanage, delDymanage, addDymanage, updateDymanage } from "@/api/szxc/dymanage";
import { listJminfo } from "@/api/szxc/jminfo";
import { deptTreeSelect } from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Dymanage",
  dicts: ['szxc_yes_no', 'sys_user_sex', 'szxc_dy_type'],
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 党员管理表格数据
      dymanageList: [],
      // 弹出层标题
      title: "",
      // 组织树选项
      deptOptions: undefined,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        jmId: null,
        name: null,
        cardId: null,
        phone: null,
        deptName: null,
        deptId: null,
        jg: null,
        joinDate: null,
        joinUnit: null,
        post: null,
        type: null,
        userId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "居民姓名不能为空", trigger: "blur" }
        ],
        cardId: [
          { required: true, message: "身份证号不能为空", trigger: "blur" }
        ],
        deptName: [
          { required: true, message: "所属网格不能为空", trigger: "blur" }
        ],
        jg: [
          { required: true, message: "籍贯不能为空", trigger: "blur" }
        ],
        joinDate: [
          { required: true, message: "入党时间不能为空", trigger: "blur" }
        ]
      },
      // 居民信息
      jmin: {
        visible: false,
        // 遮罩层
        loading: false,
        // 总条数
        total: 0,
        // 查询参数
        queryParams:{
          pageNum: 1,
          pageSize: 10,
          off: '0',
          cardId: '',
          name: ''
        },
        // 查询列表
        list: []
      },
    };
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {
    /** 查询组织下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
    /** 查询党员管理列表 */
    getList() {
      this.loading = true;
      listDymanage(this.queryParams).then(response => {
        this.dymanageList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        jmId: null,
        name: null,
        cardId: null,
        phone: null,
        deptName: null,
        deptId: null,
        jg: null,
        joinDate: null,
        joinUnit: null,
        post: null,
        type: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        userId: 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.reset();
      this.open = true;
      this.title = "添加党员";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDymanage(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改党员";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDymanage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDymanage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除党员管理编号为"' + ids + '"的数据项？').then(function() {
        return delDymanage(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('szxc/dymanage/export', {
        ...this.queryParams
      }, `dymanage_${new Date().getTime()}.xlsx`)
    },
    /** 查询居民信息列表 */
    getlistJminfo() {
      this.jmin.loading = true;
      listJminfo(this.jmin.queryParams).then(response => {
        this.jmin.list = response.rows;
        this.jmin.total = response.total;
        this.jmin.loading = false;
      });
    },
    // 点击选择居民
    ownerSelect(){
      this.jmin.visible = true;
      this.getlistJminfo()
    },
    // 居民查询
    jminQuery(){
      this.jmin.queryParams.pageNum = 1;
      this.getlistJminfo();
    },
    // 居民选择 选中数据
    jminDblclick(row) {
      this.form.jmId = row.id;
      this.form.name = row.name;
      this.form.deptId = row.deptId;
      this.form.deptName = row.deptName;
      this.form.phone = row.phone;
      this.form.cardId = row.cardId;
      this.form.jg = row.jg;
      this.jmin.visible = false
    },
  }
};
</script>
