<template>
  <div class="page-body">
    <div class="page-content">
      <q-table :pagination="msg" :loading="loading" no-data-label="暂无相关数据" flat class="sticky-column-table"
        separator="none" :data="dataList" :columns="columns" row-key="name">
        <template v-slot:top>
          <div class="col-2 q-table__title">目标管理</div>
          <q-space />
          <div class="page-option">
            <el-date-picker v-model="dateTime" size="small" style="margin-right:10px;" type="month"
              value-format="yyyy-MM" @change="dateChange" placeholder="选择月">
            </el-date-picker>
            <q-btn color="accent" size="sm" class="q-mr-md" icon="add" label="新增目标" @click="addObj(null)" />
          </div>
        </template>
        <template v-slot:body-cell-Id="props">
          <q-td :props="props">
            <div>
              <q-btn flat size="xs" icon="edit" color="accent" style="font-weight:400" label="编辑"
                @click="addObj(props.row)" />
              <q-btn flat size="xs" icon="delete" color="negative" class="q-mr-xs" label="删除"
                @click="deletTarget(props.row.Id)" />
            </div>
          </q-td>
        </template>
        <template v-slot:bottom>
          <q-pagination class="full-width justify-end" v-model="msg.pageIndex" color="primary" :max="pageCount"
            :input="true" @input="changePage" />
        </template>
      </q-table>
    </div>
    <target-form v-if="isShowTargetForm" :save-obj="customObj" @close="closestageForm" @success="refreshPage">
    </target-form>
  </div>
</template>

<script>
  import {
    GetConsultantGoalPage,
    RemoveConsultantGoal
  } from '../../api/system/index'
  import targetForm from '../../components/system/target-form'
  export default {
    meta: {
      title: "目标管理"
    },
    components: {
      targetForm
    },
    data() {
      return {
        columns: [{
            name: 'DeptName',
            label: '部门',
            field: 'DeptName',
            align: 'left'
          },
          {
            name: 'YearStr',
            label: '年份',
            field: 'YearStr',
            align: 'left'
          },
          {
            name: 'MonthStr',
            label: '月份',
            field: 'MonthStr',
            align: 'left'
          },
          {
            name: 'GoalMoney',
            label: '金额',
            field: 'GoalMoney',
            align: 'left'
          }, {
            name: 'CreateByName',
            label: '创建人',
            align: 'left',
            field: 'CreateByName'
          },
          {
            name: 'CreateTimeStr',
            label: '创建时间',
            align: 'left',
            field: 'CreateTimeStr'
          },
          {
            name: 'Id',
            label: '操作',
            field: 'Id'
          }
        ],
        msg: {
          pageIndex: 1,
          pageSize: 12,
          YearStr: '',
          MonthStr: '',
          rowsPerPage: 12
        },
        dataList: [],
        pageCount: 0,
        loading: true,
        isShowTargetForm: false,
        customObj: {},
        dateTime: ''
      }
    },
    mounted() {
      this.getList()
    },
    methods: {
      //选择日期
      dateChange(val) {
        if (val) {
          let myDate = val.split('-');
          this.msg.YearStr = myDate[0];
          this.msg.MonthStr = myDate[1];
        } else {
          this.msg.YearStr = '';
          this.msg.MonthStr = '';
        }
        this.getList();
      },
      //翻页
      changePage(val) {
        this.msg.pageIndex = val;
        this.getList()
      },
      deletTarget(Id) {
        let delMsg = {
          Id: Id
        };
        this.$q.dialog({
          title: '提示信息',
          message: '是否确定该目标？',
          cancel: true,
          persistent: true,
          ok: "确定",
          cancel: "取消",
        }).onOk(() => {
          RemoveConsultantGoal(delMsg).then(res => {
            if (res.Code == 1) {
              this.$q.notify({
                icon: 'iconfont icon-chenggong',
                color: 'accent',
                timeout: 2000,
                message: '删除成功！',
                position: 'top'
              })
              this.getList();
            }
          })
        }).onCancel(() => {

        });
      },
      getList() {
        this.loading = true;
        GetConsultantGoalPage(this.msg).then(res => {
          this.loading = false;
          if (res.Code == 1) {
            this.dataList = res.Data.PageData;
            this.pageCount = res.Data.PageCount;
          }
        })
      },
      closestageForm() {
        this.isShowTargetForm = false
      },
      refreshPage() {
        this.getList();
      },
      addObj(obj) {
        if (obj) {
          this.customObj = obj
        } else {
          this.customObj = null
        }
        this.isShowTargetForm = true;
      }
    },
  }
</script>

<style lang="sass">
  @import url('~assets/css/table.sass')
</style>