<template>
  <div class="page-body">
    <div class="page-search row items-center">
      <div class="col row wrap q-mr-lg q-col-gutter-md">
        <div class="col-3">
          <q-input @input="resetSearch" clearable filled dense v-model="msg.Name" label="名称" @clear="resetSearch"
            maxlength="20" />
        </div>
        <div class="col-3">
          <q-select class="col-12 q-pb-lg" @input="resetSearch" filled stack-label dense clearable option-value="ID"
            option-label="Name" v-model="msg.CountryId" :options="CountryList" label="国家/地区" emit-value map-options />
        </div>
      </div>
    </div>
    <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">
            <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="deleteType(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>
    <type-form v-if="isShowType" :save-obj="typeObj" @close="closeTypeForm" @success="refreshPage">
    </type-form>
  </div>
</template>

<script>
  import {
    queryCountryList
  } from '../../../api/common/common'
  import {
    GetStudyabroadTypePage,
    RemoveStudyabroadType
  } from '../../../api/system/index'
  import typeForm from '../../../components/system/type-form'
  export default {
    meta: {
      title: "留学产品分类"
    },
    components: {
      typeForm
    },
    data() {
      return {
        columns: [{
            name: 'Name',
            label: '名称',
            field: 'Name',
            align: 'left',
            required: true,
          }, {
            name: 'CountryName',
            label: '国家/地区',
            align: 'left',
            field: 'CountryName'
          }, , {
            name: 'CreateByName',
            label: '创建人',
            align: 'left',
            field: 'CreateByName'
          }, , {
            name: 'CreateTimeStr',
            label: '创建时间',
            align: 'left',
            field: 'CreateTimeStr'
          },
          {
            name: 'Id',
            label: '操作',
            field: 'Id'
          }
        ],
        msg: {
          pageIndex: 1,
          pageSize: 10,
          Name: "",
          Type: 1,
          CountryId: '',
          rowsPerPage: 10
        },
        dataList: [],
        loading: true,
        isShowType: false,
        typeObj: {},
        PageCount: 0,
        CountryList: []
      }
    },
    mounted() {
      this.getCountryList();
      this.getList()
    },
    methods: {
      //获取国家列表
      getCountryList() {
        queryCountryList({}).then(res => {
          if (res.Code == 1) {
            this.CountryList = res.Data;
          }
        });
      },
      changePage(val) {
        this.msg.pageIndex = val;
        this.getList();
      },
      deleteType(Id) {
        let delMsg = {
          Id: Id
        };
        this.$q.dialog({
          title: '提示信息',
          message: '是否确定该类型？',
          cancel: true,
          persistent: true,
          ok: "确定",
          cancel: "取消",
        }).onOk(() => {
          RemoveStudyabroadType(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;
        GetStudyabroadTypePage(this.msg).then(res => {
          this.loading = false;
          if (res.Code == 1) {
            this.dataList = res.Data.PageData;
            this.PageCount = res.Data.PageCount;
          }
        })
      },
      closeTypeForm() {
        this.isShowType = false
      },
      refreshPage() {
        this.getList();
      },
      //重新查询
      resetSearch() {
        this.msg.pageIndex = 1;
        this.getList();
      },

      addObj(obj) {
        if (obj) {
          this.typeObj = obj
        } else {
          this.typeObj = null
        }
        this.isShowType = true;
      }
    },
  }
</script>

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