<template>
  <div>
    <div class="cardOne">
      <header-box :headerForm="search" @searchClick="getData" :showButtons="showButtons" @openDialog="openDialog"
        @deleteClick="deleteClick">
      </header-box>
    </div>
    <div class="cardTwo">
      <content-box :loading="loading" @openDialog="openDialog" @deleteClick="deleteClick" :showButtons="showButtons"
        :tableData="tableData" :pagination="pagination" :selectList="multipleSelection"
        @handleSelectionChange="handleSelectionChange" @refreshTable="getData" />
    </div>
    <el-dialog v-dialogDrag :title="title" :visible.sync="isDialogVisible" :close-on-click-modal="false" width="45%">
      <el-form ref="formData" :model="formData" label-width="100px" :rules="formDataRules">
        <el-form-item label="全宗代码：" prop="fondscode">
          <el-input clearable size="small" v-model.trim="formData.fondscode" placeholder="请输入全宗代码"></el-input>
        </el-form-item>
        <el-form-item label="全宗名称：" prop="fondsname">
          <el-input clearable size="small" v-model.trim="formData.fondsname" placeholder="请输入全宗名称"></el-input>
        </el-form-item>
        <el-form-item label="单位联系人：">
          <el-input clearable size="small" v-model="formData.unitperson" placeholder="请输入单位联系人"></el-input>
        </el-form-item>
        <el-form-item label="联系方式：">
          <el-input clearable size="small" v-model="formData.unittel" placeholder="请输入联系方式"></el-input>
        </el-form-item>
        <el-form-item label="单位地址：">
          <el-input clearable size="small" v-model="formData.unitaddr" placeholder="请输入单位地址"></el-input>
        </el-form-item>
        <el-form-item label="单位名称：">
          <el-input clearable v-model="formData.unitname" placeholder="请输入单位名称"></el-input>
        </el-form-item>
        <el-form-item label="单位描述：">
          <el-input clearable size="small" v-model="formData.description" placeholder="请输入单位描述"></el-input>
        </el-form-item>
        <el-form-item label=" ">
          <el-button :loading="btnloading" size="mini" type="primary" @click="subClick"><i
              class="iconfont icon-baocun"></i>保存</el-button>
          <el-button size="mini" @click="cancel()"><i class="iconfont icon-error"></i>取消
          </el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
  import {
    message,
    clearObjValue
  } from "@/utils/common";
  import headerBox from "./components/header";
  import contentBox from "./components/content";
  import {
    mapState
  } from "vuex";
  export default {
    data() {
      return {
        btnloading: false,
        formDataRules: {
          fondscode: [{
            required: true,
            message: "请输入全宗代码",
            trigger: "blur"
          }, ],
          fondsname: [{
            required: true,
            message: "请输入全宗名称",
            trigger: "blur"
          }, ],
        },
        search: {
          codeAndFond: ''
        },
        params: {},
        multipleSelection: [],
        title: "",
        isDialogVisible: false,
        formData: {
          fondscode: "",
          fondsname: "",
          unitperson: "",
          unittel: "",
          unitaddr: "",
          unitname: "",
          description: ""
        },
      }
    },
    components: {
      headerBox,
      contentBox
    },
    computed: {
      ...mapState({
        tableData: (state) => state.allAdministration.tableData,
        pagination: (state) => state.allAdministration.pagination,
        showButtons: (state) => state.allAdministration.showButtons,
      }),
      loading: {
        get() {
          return this.$store.state.editingTopics.loading;
        },
        set(val) {
          this.$store.state.editingTopics.loading = val;
        },
      },
    },
    methods: {
      getData() {
        this.params = {
          page: this.pagination.pageNum,
          limit: this.pagination.pageSize,
          codeAndFond: this.search.codeAndFond,
        }
        this.$store.dispatch('allAdministration/getData', this.params).catch(err => {
          this.loading = false
        })
      },
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      deleteClick(val) {
        this.$alert("是否确认删除该数据？", "提示", {
          confirmButtonText: "确定",
          type: "warning",
          callback: async (action) => {
            if (action === "cancel") {
              return false;
            }
            let res;
            if (!val) {
              let arr = [];
              this.multipleSelection.map((item) => {
                arr.push(item.id);
              });
              this.$store.dispatch('allAdministration/deleteFonds', {
                ids: arr.join(",")
              }).then(res => {
                this.getData();
                message('success', '删除成功', 3000)
              })
            } else {
              this.$store.dispatch('allAdministration/deleteFonds', {
                ids: val.id
              }).then(res => {
                this.getData();
                message('success', '删除成功', 3000)
              })
            }
          },
        });
      },
      openDialog(val) {
        clearObjValue(this.formData)
        if (val) {
          this.title = '编辑'
          this.formData = {
            ...val,
          };
          this.isDialogVisible = true
        } else {
          this.title = '新增'
          this.isDialogVisible = true
        }
      },
      subClick() {
        this.$refs["formData"].validate(async (valid) => {
          if (valid) {
            this.btnloading = true;
            if (this.title === '新增') {
              this.$store.dispatch('allAdministration/saveFonds', this.formData).then(res => {
                this.btnloading = false;
                this.isDialogVisible = false;
                message('success', '新增成功', 3000)
                this.getData();
              }).catch(err => {
                this.btnloading = false;
              })
            } else {
              this.$store.dispatch('allAdministration/updateFonds', this.formData).then(res => {
                this.btnloading = false;
                this.isDialogVisible = false;
                message('success', '编辑成功', 3000)
                this.getData();
              }).catch(err => {
                this.btnloading = false;
              })
            }
          } else {
            return false;
          }
        });
      },
      cancel() {
        this.isDialogVisible = false
      }
    },
    created() {
      let btnData = {
        menuid: this.$getMenuId(sessionStorage.getItem("routerIdList"), this.$route.path),
        roleid: sessionStorage.getItem("roleid")
      }
      this.$store.dispatch("allAdministration/getButtonList", btnData);
      this.getData()
    }
  }

</script>
<style lang="scss" scoped>

</style>
