<template>
  <basic-container>
    <el-tabs type="card" v-model="cycle" @tab-click="handleClick">
      <el-tab-pane label="每日報表" name="day">
      </el-tab-pane>
      <el-tab-pane label="每週報表" name="week">
      </el-tab-pane>
      <el-tab-pane label="每月報表" name="month">
      </el-tab-pane>
    </el-tabs>

    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               ref="crud"
               v-model="form"
               :page.sync="page"
               :permission="permissionList"
               @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>

    <el-dialog :append-to-body="true" :modal-append-to-body="false" :visible.sync="chartVisible" title="圖表">
      <div v-if="chartVisible">
        <check-chart :cycle="cycle" :data="chartData"></check-chart>
      </div>
    </el-dialog>

  </basic-container>
</template>

<script>
  import {mapGetters} from "vuex";
  import {getDeptTree, getTree} from "@/api/system/dept";
  import CheckChart from "../../components/chart/check-chart";
  import {getReport} from "../../api/bank/home";
  import {dateFormat} from "../../util/date";

export default {
  components: {CheckChart},
  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: true,
        viewBtn: true,
        addBtn: false,
        editBtn: false,
        delBtn: false,
        menu: false,
        selection: true,
        dialogClickModal: false,
        column: [
          /*{
            label: "銀行號",
            prop: "bankNo",
            disabled: true,
            search: true,
            searchSpan: 4,
            rules: [{
              required: true,
              message: "請输入銀行號",
              trigger: "blur"
            }]
          },*/
          {
            label: "機構號",
            prop: "orgNo",
            disabled: true,
          },
          {
            label: "支行",
            prop: "deptName",
          },
          /*{
            label: "区域/支行",
            prop: "orgNos",
            hide: true,
            display: false,
            searchMultiple: true,
            search: true,
            type: "tree",
            dicData: [],
            props: {
              label: "title",
              value: "key"
            },
            checkStrictly: true,
            rules: [{
              required: true,
              message: "請输入機構號",
              trigger: "blur"
            }]
          },*/
          {
            label: "需核對日期",
            prop: "needCheckDate",
          },
          {
            label: "核對日期",
            prop: "checkDate",
          },
          {
            label: "日期",
            prop: "dateRange",
            type: "date",
            format: "yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd",
            searchRange:true,
            hide: true,
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false,
            search: true,
          },
          {
            label: "週",
            prop: "week",
            type: "week",
            format: "yyyy 第 WW 週",
            hide: true,
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false,
            search: false,
          },
          {
            label: "月",
            prop: "month",
            type: "month",
            hide: true,
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false,
            search: false,
          },
          {
            label: "分類",
            prop: "type",
            type: "select",
            // search: true,
            // searchSpan: 4,
            searchMultiple: true,
            dicUrl: "/api/blade-system/dict/dictionary?code=check_category",
            props:{
              label: "dictValue",
              value: "dictKey",
            },dicFormatter: (res) => {
              res.data.forEach(item => {item.disabled = item.isSealed == 1;})
              return res.data;
            },
            rules: [{
              required: true,
              message: "請输入分類",
              trigger: "blur"
            }]
          },
          {
            label: "核對項目",
            prop: "item",
            type: "select",
            // search: true,
            // searchSpan: 4,
            searchMultiple: true,
            dicUrl: "/api/blade-system/dict/dictionary?code=check_item",
            props:{
              label: "dictValue",
              value: "dictKey",
            },dicFormatter: (res) => {
              res.data.forEach(item => {item.disabled = item.isSealed == 1;})
              return res.data;
            },
            rules: [{
              required: true,
              message: "請输入核對項目",
              trigger: "blur"
            }]
          },
          /*{
            label: "核對內容/標準",
            prop: "content",
            type: "select",
            dicUrl: "/api/blade-system/dict/dictionary?code=check_content_normal",
            props:{
              label: "dictValue",
              value: "dictKey",
            },
            rules: [{
              required: true,
              message: "請输入核對內容/標準",
              trigger: "blur"
            }]
          },*/
          {
            label: "已核對",
            prop: "checked",
          },
          {
            label: "未核對",
            prop: "nocheck",
          },
          {
            label: "後補核對",
            prop: "over",
          },
        ]
      },
      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() {
  },
  methods: {
    handleInit(){
      if (this.selectionList.length === 0) {
        this.$message.warning("請選擇至少一条数据");
        return;
      }
      let checkedCount = 0, noCheckCount = 0;
      for (let i = 0; i < this.selectionList.length; i++) {
        let item = this.selectionList[i];
        if (item.checked == 1){
          checkedCount++;
        }else{
          noCheckCount++;
        }
      }
      this.chartData = {checkedCount: checkedCount, noCheckCount: noCheckCount};
      this.chartVisible = true;

    },
    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;
        }
      }

      let title = '';
      if (this.cycle == "day"){
        title = "每日核對記錄匯總";
      }
      if (this.cycle == "week"){
        title = "每週核對記錄匯總";
      }
      if (this.cycle == "month"){
        title = "每月核對記錄匯總";
      }
      this.$Export.excel({
        title: title || new Date().getTime(),
        columns: columns,
        data: this.selectionList
      });
    },
    handleInitAll(){
    },
    handleClick(tab, event) {

      //搜索条件重置
      this.query = {};
      this.cycle = tab.name;
      this.findObject(this.option.column, 'dateRange').search = tab.name == 'day';
      this.findObject(this.option.column, 'week').search = tab.name == 'week';
      this.findObject(this.option.column, 'month').search = tab.name == 'month';
      this.findObject(this.option.column, "needCheckDate").hide = tab.name != 'day';
      this.onLoad(this.page);
    },
    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) {
      if (params.dateRange){
        params.date_begin = params.dateRange[0], params.date_end = params.dateRange[1];
        params.dateRange = null;
      }
      if (params.week){
        params.date_begin = dateFormat(params.week, "yyyy-MM-dd");
        params.date_end = dateFormat(params.week, "yyyy-MM-dd");
      }
      if (params.month){
        params.date_begin = dateFormat(params.month, "yyyy-MM-dd");
        params.date_end = dateFormat(params.month, "yyyy-MM-dd");
      }

      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;
      delete this.query.cycle;
      params.cycle = this.cycle;
      getReport(Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.data = data;
        this.loading = false;
        this.selectionClear();
      });
    }
  }
};
</script>

<style>
</style>
