<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <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="xmTitle">
        <el-input
          v-model="queryParams.xmTitle"
          placeholder="请输入补贴项目名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="项目编号" prop="xmId">
        <el-input
          v-model="queryParams.xmId"
          placeholder="请输入项目编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="年度" prop="year">
        <el-input
          v-model="queryParams.year"
          placeholder="请输入年度"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="标准金额" prop="money">
        <el-input
          v-model="queryParams.money"
          placeholder="请输入标准金额"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </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>
        <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:butie: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:butie: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:butie: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:butie:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="45" align="center" />
      <el-table-column label="ID" align="center" prop="id" min-width="60" show-overflow-tooltip/>
      <el-table-column label="补贴组织" align="center" prop="deptName" min-width="120" show-overflow-tooltip/>
<!--      <el-table-column label="补贴类别" align="center" prop="type" min-width="120" show-overflow-tooltip/>-->
      <el-table-column label="补贴项目名称" align="center" prop="xmTitle" min-width="180" show-overflow-tooltip/>
      <el-table-column label="项目编号" align="center" prop="xmId" min-width="120" show-overflow-tooltip/>
      <el-table-column label="年度" align="center" prop="year" min-width="60" show-overflow-tooltip/>
      <el-table-column label="标准金额" align="center" prop="money" min-width="80" show-overflow-tooltip/>
      <el-table-column label="补贴人" align="center" prop="name" min-width="120" show-overflow-tooltip/>
      <el-table-column label="内容描述" align="center" prop="content" min-width="180" show-overflow-tooltip/>
      <el-table-column label="审核状态" align="center" prop="zfStatus" min-width="100" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.szxc_audit_status" :value="scope.row.zfStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="审核部门" align="center" prop="auditDept" min-width="180" show-overflow-tooltip/>
      <el-table-column label="审核结果" align="center" prop="auditResult" min-width="180" show-overflow-tooltip>
        <template slot-scope="scope">
          <dict-tag :options="dict.type.szxc_audit_status" :value="scope.row.auditResult"/>
        </template>
      </el-table-column>
      <el-table-column label="审核人" align="center" prop="auditName" min-width="180" show-overflow-tooltip/>
      <el-table-column label="审核原因" align="center" prop="auditReason" min-width="180" show-overflow-tooltip/>
      <el-table-column label="审核备注" align="center" prop="shRemark" min-width="180" show-overflow-tooltip/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="220">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.zfStatus==='0'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['szxc:butie:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['szxc:butie:remove']"
          >删除</el-button>
          <el-button
            v-if="scope.row.zfStatus==='0'&& ($store.state.user.deptId===scope.row.auditDeptid || $store.state.user.admin)"
            size="mini"
            type="text"
            @click="handleShenHe(scope.row)"
            v-hasPermi="['szxc:wish:edit']"
          >审核</el-button>
          <el-button
            v-if="scope.row.zfStatus==='2'"
            size="mini"
            type="text"
            @click="handleUpdate1(scope.row)"
            v-hasPermi="['szxc:wish:edit']"
          >重新提交</el-button>
          <el-button
            size="mini"
            type="text"
            @click="handleSHJL(scope.row)"
            v-hasPermi="['szxc:wish:edit']"
          >审核记录</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="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="补贴项目名称" prop="xmTitle">
          <el-input v-model="form.xmTitle" placeholder="请输入补贴项目名称" />
        </el-form-item>
        <el-form-item label="项目编号" prop="xmId">
          <el-input v-model="form.xmId" placeholder="请输入项目编号" />
        </el-form-item>
        <el-form-item label="补贴居民" prop="name">
          <el-input readonly
                    placeholder="请选择补贴居民"
                    v-model="form.name">
            <el-button slot="append" icon="el-icon-search" @click="jminSelect"></el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="补贴组织" prop="deptName">
          <el-input v-model="form.deptName" placeholder="请输入补贴组织" disabled/>
        </el-form-item>
        <el-form-item label="年度" prop="year">
          <el-input v-model="form.year" placeholder="请输入年度" />
        </el-form-item>
        <el-form-item label="标准金额" prop="money">
          <el-input v-model="form.money" placeholder="请输入标准金额" />
        </el-form-item>
        <el-form-item label="内容描述" prop="content">
          <editor v-model="form.content" :min-height="150"/>
        </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>

    <!-- 审核对话框 -->
    <el-dialog title="审核" :visible.sync="shenHe.open" width="500px" append-to-body>
      <el-form ref="shenHeForm" :model="shenHe.form" :rules="shenHe.rules" label-width="90px">
        <el-form-item label="审核结果" prop="auditResult">
          <el-select
            v-model="shenHe.form.auditResult"
            placeholder="请选择审核结果"
            clearable
            style="width:100%"
          >
            <el-option label="通过" value="1"/>
            <el-option label="不通过" value="2"/>
          </el-select>
        </el-form-item>
        <el-form-item label="审核原因" prop="auditReason">
          <el-input v-model="shenHe.form.auditReason" type="textarea" placeholder="请输入审核原因" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="shenHe.form.remark" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="shenHeSubmit">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="审核记录" :visible.sync="shjl.visible" width="1000px" append-to-body>
      <el-table v-loading="shjl.loading" :data="shjl.list" border>
        <el-table-column label="审核部门" align="center" prop="auditDept" />
        <el-table-column label="审核人" align="center" prop="auditName" />
        <el-table-column label="审核结果" align="center" prop="auditResult" >
          <template slot-scope="scope">
            <dict-tag :options="dict.type.szxc_audit_status" :value="scope.row.auditResult"/>
          </template>
        </el-table-column>
        <el-table-column label="审核原因" align="center" prop="auditReason" />
        <el-table-column label="备注" align="center" prop="remark" />
      </el-table>
    </el-dialog>

  </div>
</template>

<script>
import { listButie, getButie, delButie, addButie, updateButie } from "@/api/szxc/butie";
import { listXyrecard, addXyrecard } from "@/api/szxc/xyrecard";
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: "Butie",
  components: { Treeselect },
  dicts: ['szxc_yes_no', 'szxc_audit_status', 'sys_user_sex'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 补贴管理表格数据
      list: [],
      // 组织树选项
      deptOptions: undefined,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        type: null,
        xmTitle: null,
        xmId: null,
        year: null,
        money: null,
        jmId: null,
        name: null,
        content: null,
        zfStatus: null,
        shRemark: null,
        deptId: null,
        deptName: null,
        userId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        xmTitle: [
          { required: true, message: "补贴项目名称不能为空", trigger: "blur" }
        ],
        xmId: [
          { required: true, message: "项目编号不能为空", trigger: "blur" }
        ],
        name: [
          { required: true, message: "补贴居民不能为空", trigger: "blur" }
        ],
        deptName: [
          { required: true, message: "补贴组织不能为空", trigger: "blur" }
        ],
        year: [
          { required: true, message: "年度不能为空", trigger: "blur" }
        ],
        money: [
          { required: true, message: "标准金额不能为空", trigger: "blur" }
        ],
        content: [
          { required: true, message: "内容描述不能为空", trigger: "blur" }
        ]
      },
      // 居民信息
      jmin: {
        visible: false,
        // 遮罩层
        loading: false,
        // 总条数
        total: 0,
        // 查询参数
        queryParams:{
          pageNum: 1,
          pageSize: 10,
          off: '0',
          cardId: '',
          name: ''
        },
        // 查询列表
        list: []
      },
      // 审核
      shenHe:{
        row: null,
        // 是否显示审核弹出层
        open: false,
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          auditResult: [
            { required: true, message: "审核结果不能为空", trigger: "blur" }
          ],
          auditReason: [
            { required: true, message: "审核原因不能为空", trigger: "blur" }
          ],
        }
      },
      // 审核记录信息
      shjl: {
        visible: false,
        // 遮罩层
        loading: false,
        // 查询列表
        list: [],
        // 选中信息
        selectedRow: null,
      },
    };
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {
    /** 查询组织下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
    /** 查询补贴管理列表 */
    getList() {
      this.loading = true;
      listButie(this.queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        type: null,
        xmTitle: null,
        xmId: null,
        year: null,
        money: null,
        jmId: null,
        name: null,
        content: null,
        zfStatus: null,
        shRemark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        deptId: null,
        deptName: 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
      getButie(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) {
            updateButie(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.zfStatus = '0'
            addButie(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 delButie(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('szxc/butie/export', {
        ...this.queryParams
      }, `butie_${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;
      });
    },
    // 点击选择居民
    jminSelect(){
      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.jmin.visible = false
    },
    /** 审核按钮操作 */
    handleShenHe(row) {
      this.shenHe.row = row;
      this.resetForm("shenHeForm");
      this.shenHe.form = {
        id: null,
        shRecardType: '4',
        jlId: row.id,
        auditDept: row.auditDept,
        auditName: this.$store.state.user.nickName,
        auditResult: null,
        auditReason: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.shenHe.open = true
    },
    // 审核提交
    shenHeSubmit(){
      this.$refs["shenHeForm"].validate(valid => {
        if (valid) {
          this.shenHe.row.auditName = this.shenHe.form.auditName;
          this.shenHe.row.auditResult =  this.shenHe.form.auditResult;
          this.shenHe.row.auditReason =  this.shenHe.form.auditReason;
          this.shenHe.row.shRemark =  this.shenHe.form.remark;
          if(this.shenHe.form.auditResult==='1'){
            this.shenHe.row.zfStatus = '1'
          }
          if(this.shenHe.form.auditResult==='2'){
            this.shenHe.row.zfStatus = '2'
          }
          updateButie(this.shenHe.row).then(response1 => {
            addXyrecard(this.shenHe.form).then(response => {
              this.$modal.msgSuccess("操作成功");
              this.shenHe.open = false;
              this.getList();
            });
          });
        }
      });
    },
    // 审核记录信息
    handleSHJL(row){
      this.shjl.selectedRow = row;
      this.shjl.visible = true;
      this.getlistXyrecard();
    },
    /** 查询审核记录信息列表 */
    getlistXyrecard() {
      listXyrecard({
        pageNum: 1,
        pageSize: 100000000,
        shRecardType: '4',
        jlId: this.shjl.selectedRow.id
      }).then(response => {
        this.shjl.list = response.rows
      });
    },
    /** 重新提交按钮操作 */
    handleUpdate1(row) {
      this.reset();
      const id = row.id
      getButie(id).then(response => {
        this.form = response.data;
        this.form.zfStatus = '0';
        this.form.auditName = '';
        this.form.auditResult = '';
        this.form.auditReason = '';
        this.form.shRemark = '';
        this.open = true;
        this.title = "修改补贴管理";
      });
    },
  }
};
</script>
