<template>
  <el-card shadow="never" class="aui-card--fill">
    <div class="mod-user__icPointVaccinesInoculation}">
      <el-form
        :inline="true"
        :model="dataForm"
        @keyup.enter.native="getDataList()"
      >
        <el-form-item>
          <el-input v-model="dataForm.id" placeholder="id" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">{{ $t("query") }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addOrUpdateHandle()">{{
            $t("add")
          }}</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="danger" @click="deleteHandle()">{{
            $t("deleteBatch")
          }}</el-button>
        </el-form-item>
      </el-form>
      <div class="exportBtn">
        <el-button style=";margin-left:10px"
                   class="diy-button--export"
                   size="small"
                   @click="handleExportModule()">下载疫苗接种点模板</el-button>

        <el-upload style=""
                   ref="upload_room"
                   :multiple='false'
                   :show-file-list='false'
                   :before-upload="((file)=>{beforeUpload(file)})"
                   action=""
                   accept=".xls,.xlsx"
                   :http-request="(()=>{uploadFile()})">
          <el-button style="margin-left:10px"
                     size="small"
                     class="diy-button--delete">导入疫苗接种点数据</el-button>
        </el-upload>
      </div>
      <el-table
        v-loading="dataListLoading"
        :data="dataList"
        border
        @selection-change="dataListSelectionChangeHandle"
        style="width: 100%"
      >
        <el-table-column
          type="selection"
          header-align="center"
          align="center"
          width="50"
        ></el-table-column>
        <el-table-column
          prop="orgName"
          label="所属组织名称"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="name"
          label="疫苗接种点名称"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="inoculationDate"
          label="接种日期"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="moStartTime"
          label="接种上午开始时间"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="moEndTime"
          label="接种上午结束时间"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="afStartTime"
          label="接种下午开始时间"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="afEndTime"
          label="接种下午结束时间"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="noAvailableVaccines"
          label="暂无疫苗（1：是；2：否）"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="mobile"
          label="咨询电话"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="address"
          label="接种点地址"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="longitude"
          label="经度"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          prop="latitude"
          label="纬度"
          header-align="center"
          align="center"
        ></el-table-column>
        <el-table-column
          :label="$t('handle')"
          fixed="right"
          header-align="center"
          align="center"
          width="150"
        >
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              @click="addOrUpdateHandle(scope.row.id)"
              >{{ $t("update") }}</el-button
            >
            <el-button
              type="text"
              size="small"
              @click="deleteHandle(scope.row.id)"
              >{{ $t("delete") }}</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page="page"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="limit"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @size-change="pageSizeChangeHandle"
        @current-change="pageCurrentChangeHandle"
      >
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <el-dialog
        :visible.sync="addOrUpdateVisible"
        :title="!dataForm.id ? $t('add') : $t('update')"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :destroy-on-close="true"
      >
        <add-or-update
          ref="addOrUpdate"
          @refreshDataList="getDataList"
        ></add-or-update>
      </el-dialog>
    </div>
  </el-card>
</template>

<script>
import mixinViewModule from "@/mixins/view-module";
import AddOrUpdate from "./icpointvaccinesinoculation-add-or-update";

export default {
  mixins: [mixinViewModule],
  data() {
    return {
      mixinViewModuleOptions: {
        getDataListURL: "/epmetuser/icPointVaccinesInoculation/page",
        getDataListIsPage: true,
        deleteURL: "/epmetuser/icPointVaccinesInoculation",
        deleteIsBatch: true,
      },
      dataForm: {
        id: "",
      },
    };
  },
  components: {
    AddOrUpdate,
  },
  methods:{
    handleExportModule () {
      let title = '疫苗接种点模板'
      let url = "/epmetuser/icPointNucleicMonitoring/exporttemplate"

      let params = {}

      app.ajax.exportFilePost(
        url,
        params,
        (data, rspMsg) => {

          this.download(data, title + '.xls')
        },
        (rspMsg, data) => {
          this.$message.error(rspMsg);
        }
      );
    },
    // 上传文件之前的钩子
    beforeUpload (file) {
      this.files = file;

      const isText = file.type === 'application/vnd.ms-excel'
      const isTextComputer = file.type === 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'

      if (!isText && !isTextComputer) {
        this.$message.error('请选择正确格式的文件')
        this.files = null
        this.fileName = ''
        return false
      } else {
        this.fileName = file.name;
        return true
      }

    },
    async uploadFile () {

      if (this.fileName == "") {
        this.$message.warning('请选择要上传的文件！')
        return false
      }

      this.$message({
        showClose: true,
        message: '导入中，请到系统管理-导入记录中查看进度',
        duration: 0
      })


      //清空上传列表
      let url = ''

      let fileFormData = new FormData();
      fileFormData.append('file', this.files);//filename是键，file是值，就是要传的文件，test.zip是要传的文件名
      fileFormData.append('orgId', this.agencyObj.id);//filename是键，file是值，就是要传的文件，test.zip是要传的文件名
      fileFormData.append('orgType', this.agencyObj.level === 'grid' ? 'grid' : 'agency');//filename是键，file是值，就是要传的文件，test.zip是要传的文件名

      this.$refs['upload_room'].clearFiles()
      url = '/epmetuser/icPointNucleicMonitoring/pointNucleicMonitoringImport'
      this.importRoomLoading = true

      window.app.ajax.post2(url, fileFormData,
        (data, rspMsg) => {

          if (data.code === 0 && data.msg == 'success') {
            // this.$message.success('导入成功')
          } else {
            // this.$message({
            //   showClose: true,
            //   message: rspMsg,
            //   duration: 0,
            //   type: "error"
            // })
            // this.$message.error(rspMsg)
          }
          this.$emit('refreshTree')
          this.loadTable()
          // this.showMessage(rspMsg)
          
          this.importRoomLoading = false
        },
        (rspMsg, data) => {
          this.importRoomLoading = false
          // this.$message.error(rspMsg)
        }, { headers: { 'Content-Type': 'multipart/form-data' } })


    },
  }
};
</script>
<style lang="scss" scoped>
  .exportBtn{
    display: flex;
    align-items: center;
    margin-bottom: 20px;
  }
</style>
