<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-s-data"
                   plain
                   v-if="permission.message_delete"
                   @click="handleInit">生成圖表（部分）
        </el-button>
        <el-button type="primary"
                   size="small"
                   icon="el-icon-s-data"
                   plain
                   v-if="permission.message_delete"
                   @click="handleInitAll">生成圖表（全部）
        </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 {getList, getDetail} from "@/api/bank/checklist";
  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 {
  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,
        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,
            rules: [{
              required: true,
              message: "請输入機構號",
              trigger: "blur"
            }]
          },
          {
            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: "checkDate",
            type: "datetime",
            format: "yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd",
            rules: [{
              required: true,
              message: "請输入核對日期",
              trigger: "blur"
            }]
          },
          {
            label: "核對日期",
            prop: "checkDateRange",
            type: "date",
            format: "yyyy-MM-dd",
            valueFormat: "yyyy-MM-dd",
            searchRange:true,
            hide: true,
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false,
            search: true,
            rules: [{
              required: true,
              message: "請输入交接日期",
              trigger: "blur"
            }]
          },
          {
            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",
            },dicFormatter: (res) => {
              res.data.forEach(item => {item.disabled = item.isSealed == 1;})
              return res.data;
            },
            rules: [{
              required: true,
              message: "請输入核對內容/標準",
              trigger: "blur"
            }]
          },
          {
            label: "已核對",
            prop: "checked",
            type: "checkbox",
            dicData: [{
              value: 1
            }],
            rules: [{
              required: true,
              message: "請勾選已核對",
              trigger: "blur"
            }]
          },
          {
            label: "核對人",
            prop: "checkName",
            disabled: true,
            rules: [{
              required: true,
              message: "請输入核對人姓名",
              trigger: "blur"
            }]
          },
          {
            label: "核對人員工號",
            prop: "checkNo",
            disabled: true,
            rules: [{
              required: true,
              message: "請输入核對人員工號",
              trigger: "blur"
            }]
          },
          {
            label: "備註",
            prop: "remark",
            rules: [{
              required: false,
              message: "請输入備註",
              trigger: "blur"
            }]
          },
          {
            label: "填報時間",
            prop: "fillingDate",
            addDisplay: false,
            editDisplay: false,
            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: {
    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;

    },
    handleInitAll(){
      getHomeCount().then(res => {
        const data = res.data.data;
        if (this.cycle == 'day'){
          this.chartData = {checkedCount: data.dayCheckedCount, noCheckCount: data.dayNocheckCount};

        }else if (this.cycle == 'week'){
          this.chartData = {checkedCount: data.weekCheckedCount, noCheckCount: data.weekNocheckCount};
        }else{
          this.chartData = {checkedCount: data.monthCheckedCount, noCheckCount: data.monthNocheckCount};
        }
        if (this.chartData.checkedCount == 0 && this.chartData.noCheckCount == 0){
          this.$message.success("暫無數據！");
        }else{
          this.chartVisible = true;
        }

      })
    },
    handleClick(tab, event) {
      // console.log(tab, event);
      /*if (tab.name == 'day'){
        this.dataType = 1;
      }else if(tab.name == 'week'){
        this.dataType = 2;
      }else if (tab.name == 'month'){
        this.dataType = 3;
      }*/
      this.cycle = tab.name;
      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.checkDateRange){
        params.checkDate_begin = params.checkDateRange[0], params.checkDate_end = params.checkDateRange[1];
        params.checkDateRange = null;
      }
      if (params.orgNos){
        params.orgNostr = params.orgNos.join();
        params.orgNos = '';
      }
      if (params.type){
        params.type = params.type.join();
      }
      if (params.item){
        params.item = params.item.join();
      }

      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;
      params.cycle = this.cycle;
      params.isChecked = 0; // 未完成
      getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    }
  }
};
</script>

<style>
</style>
