<template>
  <div>
    <div class="i-layout-page-header">
      <PageHeader :title="$route.meta.title" :content="$route.meta.description" hidden-breadcrumb />
    </div>
    <Card :bordered="false" dis-hover class="ivu-mt">
      <view-table
        ref="table"
        view-code="MailTemplate"
        :select-enable="export"
        :create-enable="allow('create')"
        :remove-enable="allow('remove')"
        @create="$refs.form.open()"
        @edit="({ row, index }) => $refs.form.open(row)"
      >
        <template #filterCommand>
          <Button v-if="allow('export')" size="small" type="info" custom-icon="fa fa-cloud-download-alt" @click="exportFile()">{{ $t('Front_Btn_Export') }}</Button>
          <file-upload v-if="allow('import')" url="api/MailTemplate/importFile" accept=".mt" @on-success="importFileSuccess">
            <template #>
              <Button type="primary" custom-icon="fa fa-cloud-upload-alt" size="small">{{ $t('Front_Btn_Import') }}</Button>
            </template>
          </file-upload>
        </template>
      </view-table>
    </Card>
    <modal-form ref="form" view-code="MailTemplateEdit" :create-enable="allow('create')" :edit-enable="allow('edit')" @on-after-save="$refs.table.loadData()" />
  </div>
</template>
<script>
  import mixinPage from '../../mixins/page';
  import mailTemplateApi from '../../api/mailTemplate';

  export default {
    name: 'system-mailTemplate',
    mixins: [mixinPage],
    components: {},
    data() {
      return {};
    },
    computed: {},
    created() {},
    methods: {
      // 导出文件
      exportFile() {
        let ids = this.$refs.table.getSelected().map(x => {
          return x.id;
        });
        if (ids.length > 0) {
          mailTemplateApi.exportFile(ids);
        } else {
          this.error('Front_Msg_Please_Select_Export_Item');
        }
      },
      // 导入成功
      importFileSuccess(res) {
        if (res.code == 0) {
          this.success('Front_Msg_Success', () => {
            this.$refs.table.loadData();
          });
        } else {
          this.error(res.message);
        }
      }
    }
  };
</script>
