<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               ref="crud"
               v-model="form"
               :permission="permissionList"
               :cell-style="cellStyle"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <el-button type="primary" size="small" icon="el-icon-download"
                   plain @click="handleDownload">下載
        </el-button>
      </template>
    </avue-crud>

  </basic-container>
</template>

<script>
  import {getOrgsSealhandoverCount} from "@/api/bank/sealhandover";
  import {mapGetters} from "vuex";
  import {getDeptTree, getTree} from "@/api/system/dept";
  import CheckChart from "../../components/chart/check-chart";
  import {getHomeCount} from "../../api/bank/home";

export default {
  data() {
    return {
      // dataType: 1,
      cycle: "day",
      chartData: {},
      chartVisible: false,

      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
        height:'auto',
        calcHeight: 30,
        tip: false,
        searchShow: true,
        searchMenuSpan: 6,
        border: true,
        index: false,
        menu: false,
        viewBtn: true,
        addBtn: false,
        editBtn: false,
        delBtn: false,
        selection: false,
        header: true,
        refreshBtn: false,
        align: 'center',
        columnBtn: false,
        dialogClickModal: false,
        column: [
          {
            label: "支行",
            prop: "orgName",
            disabled: true,
            rules: [{
              required: true,
              message: "請輸入機構號",
              trigger: "blur"
            }]
          },

        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    ...mapGetters(["userInfo"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.checklist_add, false),
        viewBtn: this.vaildData(this.permission.checklist_view, false),
        delBtn: this.vaildData(this.permission.checklist_delete, false),
        editBtn: this.vaildData(this.permission.checklist_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  mounted() {
    /*getDeptTree().then(res => {
      const column = this.findObject(this.option.column, "orgNos");
      let treeData = getTree(res.data.data, this.userInfo.dept_id);
      column.dicData = treeData;
    });*/
  },
  methods: {
    cellStyle({row,column,rowIndex,columnIndex}){
      /*if(columnIndex > 0){
        if(row['seal_type' + (columnIndex)] > 0){
          return {
            color:'green',
            fontWeight:'bold',
            fontSize:'20'
          }
        }else{
         /!* return {
            color:'red',
            fontWeight:'bold',
            fontSize:'20'
          }*!/
        }
      }*/
    },
    handleDownload(){
      /*if (this.selectionList.length === 0) {
        this.$message.warning("請選擇至少一條數據");
        return;
      }*/
      let columns = this.deepClone(this.option.column);
      for (let i = 0; i < columns.length; i++) {
        let item = columns[i];
        if (item.hide || item.prop == 'process'){
          columns.splice(i, 1);
          i--;
        }
        if (item.type == 'select' || item.type == 'tree'){
          item.prop = '$' + item.prop;
        }
      }
      this.$Export.excel({
        title: "業務印章持有量匯總表" || new Date().getTime(),
        columns: columns,
        data: this.data
      });
    },
    beforeOpen(done, type) {
      let arr = [];
      arr.push(this.form.isChecked)
      this.form.checked = arr;
      if (["edit", "view"].includes(type)) {
        if (type == "view"){
          this.findObject(this.option.column, "content").tip = this.form.$content;
          console.log(this.form)
        }
        getDetail(this.form.id).then(res => {
          this.form = res.data.data;
          if (type == "view"){
            this.findObject(this.option.column, "content").tip = "this.form.$content";
            console.log(this.form)
          }
        });

      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {

      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage){
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize){
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      getOrgsSealhandoverCount().then(res => {
        const data = res.data.data;
        let column = this.option.column;
        data.sealTypeList.forEach(item => {
          column.push({
            label: item.dictValue,
            prop: "seal_type" + item.dictKey
          })
        })
        this.data = data.sealData;
        this.loading = false;
        this.selectionClear();
      });
    }
  }
};
</script>

<style>
</style>
