<template>
  <!-- 项目模板库 -->
  <div class="xmmbkContent">
    <div class="d-flex flex-between">
      <div>
        <el-button type="success" size="medium" @click="createNew">
          <span class="d-in-block v-mid el-icon-plus font-size-16"></span>
          <span class="d-in-block v-mid font-size-16">上传模板</span>
        </el-button>
      </div>
      <el-form label-width="100px" :inline="true">
        <el-form-item prop="create_dateline">
          <el-date-picker v-model="searchForm.create_dateline" type="date" placeholder="选择日期" value-format="yyyy-MM-dd" @change="getListData"></el-date-picker>
        </el-form-item>
        <el-form-item prop="keyword">
          <el-input placeholder="请输入搜索关键字" v-model="searchForm.keyword" clearable @clear="getListData"></el-input>
        </el-form-item>
        <el-button type="primary" class="ml5" @click="getListData">搜索</el-button>
      </el-form>
    </div>

    <el-table :data="tableData" tooltip-effect="dark" v-loading="loading" style="width: 100%">
      <el-table-column label="序号" align="center" type="index" width="50">
      </el-table-column>
      <el-table-column v-for="(item, index) in columnData" :key="index" :label="item.name" show-overflow-tooltip :prop="item.key" width="auto"></el-table-column>
      <el-table-column label="操作" width="150">
        <template slot-scope="scope">
          <el-button type="text" @click="editData(scope.row, 1)">详情 </el-button>
          <el-button type="text" @click="editData(scope.row, 2)">编辑 </el-button>
          <el-button type="text" @click="delData(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <footer class="flex-item-none" style="display: flex; justify-content: flex-end; margin-top: 30px">
      <el-pagination background layout="total,prev, pager, next" :page-size="limit" :total="total" :current-page.sync="cur_page" @current-change="handleCurrentChange"></el-pagination>
    </footer>

    <el-dialog :title="dialogTitle" :visible.sync="dialogFormVisible" :width="dialogType==1 ? '80%' : '600px'" @close="handleClose">
      <preview-attach v-if="dialogType==1" :commonUrl="commonUrl" :filename="filename" style="width: 100%; height: 300px;"></preview-attach>
      <add-and-edit v-if="dialogType==2" ref="addAndEdit" :currentId="currentId" @close="handleClose"></add-and-edit>
      <div slot="footer" v-if="dialogType==2">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="formSubmit">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mbkList, mbkDelete } from "./api";
import AddAndEdit from "./components/AddAndEdit.vue";
import PreviewAttach from "@/components/PreviewAttach/index.vue";
export default {
  data() {
    return {
      searchForm: {
        xm_start_time: "",
        keyword: "",
      },
      date: "",
      loading: false,
      columnData: [
        {
          name: "模板名称",
          key: "mb_name"
        },
        {
          name: "上传人",
          key: "mb_create_username"
        },
        {
          name: "上传时间",
          key: "create_dateline"
        }
      ],
      tableData: [{}],
      limit: 10,
      total: 12,
      cur_page: 1,
      dialogTitle: "",
      dialogType: "",
      dialogFormVisible: false,
      currentId: "",
      commonUrl: "",
      filename: "",
    }
  },
  components: { AddAndEdit, PreviewAttach },
  methods: {
    async getListData() {
      this.loading = true;
      let obj = {
        page: this.cur_page,
        limit: this.limit,
        create_dateline: this.searchForm.create_dateline,
        keyword: this.searchForm.keyword,
      }
      let data = await mbkList(obj);
      if (data) {
        this.tableData = data.page_data;
        this.cur_page = Number(data.page_now);
        this.total = Number(data.total_rows);
        this.loading = false;
      }
    },
    handleCurrentChange(val) {
      this.cur_page = val;
      this.getListData();
    },
    createNew() {
      this.dialogFormVisible = true;
      this.dialogTitle = "新建模板";
      this.dialogType = 2;
    },
    editData(data, type) {
      this.dialogFormVisible = true;
      this.currentId = data.mb_id;
      this.dialogType = type;
      if (type == 1) {
        this.dialogTitle = "详情";
        let arr = data.mb_file.split("|");
        if (arr[0] && arr[1]) {
          this.commonUrl = arr[0];
          this.filename = arr[1];
        } else {
          this.commonUrl = data.mb_file;
          this.filename = "附件";
        }
      } else {
        this.dialogTitle = "编辑模板";
      }
    },
    delData(data) {
      this.$confirm("是否确定删除当前数据？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let res = await mbkDelete({ mb_id: data.mb_id });
          if (res && res != "") {
            this.$message.success("删除成功！");
            this.getListData();
          }
        })
        .catch(() => { });
    },
    handleClose(type) {
      this.dialogFormVisible = false;
      this.dialogType = "";
      this.$refs.addAndEdit && this.$refs.addAndEdit.resetForm();
      this.currentId = "";
      if (type == 1) {
        this.getListData();
      }
    },
    formSubmit() {
      this.$refs.addAndEdit.commitData();
    }
  },
  created() {
    this.getListData();
  }
}
</script>

<style>
</style>