<template>
  <div class="app-container">
    <el-row :gutter="20">
      <el-col :span="4" :xs="24">
        <dept-tree-one :userId="user.userData.dept.parentId" :isFive="true" :isJl="true" :expandOnClickNode="false"
                       :showCheckbox="false"
                       @handleNodeClick="handleNodeClick">
        </dept-tree-one>
      </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="xzqh">-->
<!--        <RegionCascaderSelect :endLevel="2" v-model="xzqh" style="width: 100%" valueProp="code" >-->
<!--        </RegionCascaderSelect>-->
<!--&lt;!&ndash;        <el-input v-model="queryParams.xzqh" placeholder="请输入所属区划" clearable @keyup.enter.native="handleQuery" />&ndash;&gt;-->
<!--      </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="['gljt:qxpz:add']">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="qxpzList">
      <el-table-column label="序号" align="center">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="所属区划" align="center" prop="xzqh" width="200"/>
      <el-table-column label="公众号登录" align="center" prop="gzhdl">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.CZ035" :value="scope.row.gzhdl" />
        </template>
      </el-table-column>
      <el-table-column label="公众号注册" align="center" prop="gzhzc">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.CZ035" :value="scope.row.gzhzc" />
        </template>
      </el-table-column>
      <el-table-column label="公众号申报健康状况" align="center" prop="gzhsb">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.CZ035" :value="scope.row.gzhsb" />
        </template>
      </el-table-column>
      <el-table-column label="公众号申请高龄津贴" align="center" prop="gzhsq">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.CZ035" :value="scope.row.gzhsq" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['gljt:qxpz:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row, scope.$index)"
            v-hasPermi="['gljt:qxpz: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 v-dialog-drag :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="180px">

        <el-form-item label="所属区划" prop="xzqh">
          <RegionCascaderSelect :endLevel="2" v-model="form.xzqh" style="width: 100%" valueProp="code" >
            </RegionCascaderSelect>
        </el-form-item>

        <el-form-item label="公众号登录" prop="gzhdl">
          <el-select v-model="form.gzhdl" placeholder="请选择公众号登录" style="width: 100%">
            <el-option v-for="dict in dict.type.CZ035" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="公众号注册" prop="gzhzc">
          <el-select v-model="form.gzhzc" placeholder="请选择公众号注册" style="width: 100%">
            <el-option v-for="dict in dict.type.CZ035" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="公众号申报健康状况" prop="gzhsb">
          <el-select v-model="form.gzhsb" placeholder="请选择公众号申报健康状况" style="width: 100%">
            <el-option v-for="dict in dict.type.CZ035" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="公众号申请高龄津贴" prop="gzhsq">
          <el-select v-model="form.gzhsq" placeholder="请选择公众号申请高龄津贴" style="width: 100%">
            <el-option v-for="dict in dict.type.CZ035" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="本区划健康状况检测频率" prop="jcpc">
          <el-input v-model="form.jcpc" placeholder="请输入本区划健康状况检测频率" />
        </el-form-item>

        <el-form-item label="高龄津贴申请审核流程" prop="sqshlc">
          <el-select v-model="form.sqshlc" placeholder="请选择高龄津贴申请审核流程" style="width: 100%">
            <el-option v-for="dict in dict.type.GL001" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="高龄津贴停发审核流程" prop="tfshlc">
          <el-select v-model="form.tfshlc" placeholder="请选择高龄津贴停发审核流程" style="width: 100%">
            <el-option v-for="dict in dict.type.GL001" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="高龄津贴补发审核流程" prop="bfshlc">
          <el-select v-model="form.bfshlc" placeholder="请选择高龄津贴补发审核流程" style="width: 100%">
            <el-option v-for="dict in dict.type.GL001" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="高龄津贴追缴审核流程" prop="zjshlc">
          <el-select v-model="form.zjshlc" placeholder="请选择高龄津贴追缴审核流程" style="width: 100%">
            <el-option v-for="dict in dict.type.GL001" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="银行账户变更审核流程" prop="bgshlc">
          <el-select v-model="form.bgshlc" placeholder="请选择银行账户变更审核流程" style="width: 100%">
            <el-option v-for="dict in dict.type.GL001" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="计发时间节点(例：15)" prop="kz1">
          <el-input v-model="form.kz1" placeholder="请输入高龄津贴计发时间节点" />
        </el-form-item>

        <el-form-item label="低保剔除年龄节点" prop="kz2">
          <el-input v-model="form.kz2" placeholder="请输入低保剔除年龄节点" />
        </el-form-item>

        <el-form-item label="补发最早月份" prop="kz3">
          <el-input v-model="form.kz3" placeholder="请输入补发最早月份" />
        </el-form-item>

        <el-form-item label="最多补发月份" prop="bfys">
          <el-input v-model="form.bfys" placeholder="请输入最多补发月份" />
        </el-form-item>

        <el-form-item label="健康检测时间段" prop="kz4">
          <el-input v-model="form.kz4" placeholder="请输入健康检测时间段" />
        </el-form-item>

        <el-form-item label="发证机关" prop="kz5">
          <el-input v-model="form.kz5" placeholder="请输入发证机关" />
        </el-form-item>

        <el-form-item label="appId" prop="kz6">
          <el-input v-model="form.kz6" placeholder="请输入appId" />
        </el-form-item>

        <el-form-item label="secret" prop="kz7">
          <el-input v-model="form.kz7" placeholder="请输入secret" />
        </el-form-item>

        <el-form-item label="计发生成日期(不填默认月末最后一天)" prop="kz8">
          <el-input v-model="form.kz8" placeholder="计发生成日期" />
        </el-form-item>

        <el-form-item label="操作指南" prop="kz9">
          <file-upload v-model="form.kz9" />
        </el-form-item>

        <el-form-item label="是否区分迁入申请" prop="isqr">
          <el-radio-group v-model="form.isqr">
            <el-radio v-for="dict in dict.type.CZ035" :key="dict.value" :label="dict.value">
              {{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="是否区分承诺书" prop="iscns">
          <el-radio-group v-model="form.iscns">
            <el-radio v-for="dict in dict.type.CZ035" :key="dict.value" :label="dict.value">
              {{ dict.label }}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="开户银行" prop="khyh">
          <el-input v-model="form.khyh" placeholder="开户银行代码(逗号分割)" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :loading="submitFormLoading">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { addQxpz, delQxpz, getQxpz, listQxpz, updateQxpz } from "@/api/gljt/qxpz";
import { chineseOne, idCard, Regular } from '@/utils/regular'
import { mapState } from 'vuex'

export default {
  name: "Qxpz",
  dicts: ['CZ035','GL001'],
  computed: {
    ...mapState(['user']),
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      submitFormLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 高龄津贴_区县配置信息表格数据
      xzqh: [],
      qxpzList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        xzqh: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        xzqh: [
          { required: true, message: "所属区划不能为空", trigger: "blur" },
          // { max: 12, message: '所属区划不能超过12个字符', trigger: 'blur' },
        ],
        jcpc: [
          { max: 2, message: '本区划健康状况检测频率不能超过2个字符', trigger: 'blur' },
        ],
        kz1: [
          { max: 20, message: '扩展字段不能超过20个字符', trigger: 'blur' },
        ],
        kz2: [
          { max: 20, message: '扩展字段不能超过20个字符', trigger: 'blur' },
        ],
        kz3: [
          { max: 20, message: '扩展字段不能超过20个字符', trigger: 'blur' },
        ],
        kz4: [
        ],
        kz5: [
          { max: 20, message: '扩展字段不能超过20个字符', trigger: 'blur' },
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 节点单击事件
    handleNodeClick(data) {
      if(data&&data.code){
        this.queryParams.xzqh= data.code;
      }else{
        this.queryParams.xzqh=null;
      }
      this.handleQuery()
    },
    /** 查询高龄津贴_区县配置信息列表 */
    getList() {
      this.loading = true;
      listQxpz(this.queryParams).then(response => {
        this.qxpzList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.submitFormLoading = false;
      this.form = {
        id: null,
        xzqh: null,
        gzhdl: null,
        gzhzc: null,
        gzhsb: null,
        gzhsq: null,
        jcpc: null,
        sqshlc: null,
        tfshlc: null,
        bfshlc: null,
        zjshlc: null,
        bgshlc: null,
        kz1: null,
        kz2: null,
        kz3: null,
        kz4: null,
        kz5: null,
        kz6: null,
        kz7: null,
        kz8: null,
        kz9: null,
        state: null,
        isqr: null,
        iscns: null,
        bfys: null,
        khyh: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      if (this.xzqh) {
        this.queryParams.xzqh = this.xzqh[this.xzqh.length - 1]
      }
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.xzqh = null;
      this.xzqh = [];
      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
      getQxpz(id).then(response => {
        this.form = response.data;
        this.form.xzqh = JSON.stringify(this.form.xzqh.split(','))
        /****** sks 需要改动的地方 start ******/
        // this.copyForm = this.deepCopy(response.data)
        /****** sks 需要改动的地方 end ******/
        this.open = true;
        this.title = "修改高龄津贴_区县配置信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          let xzqh = null;
          if (this.form.xzqh) {
            if (Array.isArray(this.form.xzqh)) {
              xzqh = this.form.xzqh[this.form.xzqh.length - 1]
            }
          }
          this.submitFormLoading = true;
          if (this.form.id != null) {
            /****** sks 需要改动的地方 start ******/
            // let formData = this.comparisonObject(this.form, this.copyForm);
            // if (formData) {
              updateQxpz({...this.form, xzqh:xzqh}).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                // this.qxpzList = this.dataReplacement(this.qxpzList, this.form.id, formData);
                this.getList();
              }).finally(() => this.submitFormLoading = false);
            // } else {
            //   this.$modal.msgSuccess("修改成功");
            //   this.open = false;
            //   this.submitFormLoading = false;
            // }
            /****** sks 需要改动的地方 end ******/
          } else {
            addQxpz({...this.form, xzqh:xzqh}).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
              /****** sks 需要改动的地方 start ******/
              // if (this.queryParams.pageSize === this.qxpzList.length) {
              //   this.qxpzList.pop();
              // }
              // this.qxpzList.unshift({ ...this.form, id: response.data });
              // this.total++;
              /****** sks 需要改动的地方 end ******/
            }).finally(() => this.submitFormLoading = false);
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row, index) {
      const ids = row.id || this.ids;
      const xh = (this.queryParams.pageNum - 1) * this.queryParams.pageSize + index + 1;
      this.$modal.confirm('确认删除' + this.changeDelData(row, 'id', 'ID值', xh) + '的记录？').then(function () {
        return delQxpz(ids);
      }).then(() => {
        this.getList();
        /****** sks 需要改动的地方 ind参数需要传进来 start ******/
        // this.qxpzList.splice(index, 1);
        // if (this.qxpzList.length === 0) {
        //   this.getList();
        // } else {
        //   this.total--;
        // }
        // this.$modal.msgSuccess("删除成功");
        /****** sks 需要改动的地方 end ******/
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('gljt/qxpz/export', {
        ...this.queryParams
      }, `qxpz_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
