<template>
  <div class="app-container">
    <!-- 操作 -->
    <div class="filter-container">
      <el-button
        v-permission="['POST /admin/upload/uploadExcel']"
        class="filter-item"
        type="primary"
        icon="el-icon-upload2"
        @click="importDialog"
      >任务导入</el-button>

       <a href="./static/example.xlsx" rel="external nofollow" download="模板" style="margin-left:20px;"><el-button

        class="filter-item"
        type="primary"
        icon="el-icon-download"
        @click="downloadExcel"
      >下载模板</el-button></a>
    </div>

    <!-- 任务导入 -->
    <el-dialog title="任务导入" :visible.sync="importDialogVisible" :close-on-click-modal="false">
      <el-form
        ref="dataForm"
        :rules="rules"
        status-icon
        label-position="left"
        label-width="150px"
        style="width: 600px; margin-left:50px;"
        :model="importForm"
      >
        <el-form-item label="任务名称" prop="campaignId">
          <el-select
            v-model="importForm.campaignId"
            style="width: 260px"
            class="filter-item"
            placeholder="请选择任务"
            clearable
          >
            <el-option
              v-for="(item, index) in campaignList"
              :key="index"
              :label="item.name"
              :value="item.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="渠道" prop="channelId">
          <el-select
            v-model="importForm.channelId"
            style="width: 260px"
            class="filter-item"
            placeholder="请选择渠道"
            clearable
          >
            <el-option v-for="item in channels" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>

        <el-form-item label="excel导入">
          <input type="file" @change="getFile($event)" ref="file" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="importDialogVisible = false">取消</el-button>
        <el-button type="primary" @click="importConfirm">确定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="导入失败信息" :visible.sync="failDialogVisible" center>

        <!-- <el-form-item v-for="(item,index) in importResult" :key="index">

        </el-form-item> -->
        <el-table :data="importResult"  fit highlight-current-row>
            
            <el-table-column align="center" label="手机号" prop="tel" />
            <el-table-column align="center" label="客户姓名" prop="customerName" />
            <el-table-column align="center" label="失败原因" prop="remark" />
             </el-table>
      
      <div slot="footer" class="dialog-footer">
        <!-- <el-button @click="importDialogVisible = false">取消</el-button> -->
        <el-button type="primary" @click="failDialogVisible = false">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { taskName } from "@/api/report";
import { allChannel } from "@/api/channel";
import { importTask } from "@/api/taskRecycle";

export default {
  name: "taskImport",
  data() {
    return {
      importDialogVisible: false,
      campaignList: [],
      channels: [],
      importForm: {
        campaignId: "",
        channelId: ""
      },
      file: "",
      failDialogVisible:false,
      importResult:[],

      rules: {
        campaignId: [
          { required: true, message: "任务名称不能为空", trigger: "blur" }
        ],
        channelId: [
          { required: true, message: "渠道不能为空", trigger: "blur" }
        ]
      }
    };
  },
  computed: {},
  created() {
    allChannel().then(res => {
      this.channels = res.data.data;
    });
    this.getTaskName();
  },
  methods: {
    getTaskName() {
      let params = {
        page: 1,
        limit: 50,
        sort: "id",
        order: "desc"
      };
      taskName(params)
        .then(response => {
          this.campaignList = response.data.data.list;
        })
        .catch(err => {
          console.log(err);
        });
    },
    importDialog() {
     
      if (this.file != "") {
        this.file = "";
        this.$refs.file.value = "";
      }
       this.importForm={
        campaignId: "",
        channelId: ""
      };
       this.importDialogVisible = true;
    },
    //获取上传文件
    getFile: function(event) {
      this.file = event.target.files[0];
      console.log(this.file);
    },

    importConfirm() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.confUploadBtn();
        }
      });
    },
    confUploadBtn() {
      if (this.file == "") {
        this.$message.warning("请选择文件！");
        return;
      }

      const loading = this.$loading({
        lock: true,
        text: "文件导入中，请耐心等待...",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
      var formData = new window.FormData(); // vue 中使用 window.FormData(),否则会报 'FormData isn't definded'
      formData.append("file", this.file);
      formData.append("channelId", this.importForm.channelId);
      formData.append("campaignId", this.importForm.campaignId);
      //  let formData={};
      // formData.file=this.file;
      // formData.channelId=this.importForm.channelId;
      // formData.campaignId=this.importForm.campaignId;
     
      importTask(formData)
        .then(res => {
          console.log("导入返回数据", res);
          loading.close();
          if (res.data.errno == 0) {
            this.$message.success(res.data.errmsg);
            this.importDialogVisible = false;
             if(res.data.data.length>=1){
               this.failDialogVisible=true;
               this.importResult=res.data.data;
             }
          } else {
            this.$message.warning(res.data.errmsg);
          }
        })
        .catch(err => {
          loading.close();
          console.log(err);
        });
    },
    downloadExcel(){

    }
  }
};
</script>

<style scoped>
.el-form-item {
  margin-bottom: 24px;
}
</style>
