<template>
  <div class="g-main">
    <div  v-show="pageType == 'list'">
      <div class="m-search">
        <el-form  :inline="true" :model="formData"  :label-width="'100px'">
        <el-form-item label="任务类型">
            <el-select v-model="formData.importCategory" placeholder="请输入" size="small" class="u-item-width-normal" clearable>
              <el-option v-for="item in dicts.resident_category_import" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="状态">
            <el-select v-model="formData.processStatus" placeholder="请输入" size="small" class="u-item-width-normal" clearable>
              <el-option v-for="item in dicts.import_status" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>

          <el-form-item label="任务名称">
            <el-input v-model="formData.taskName" placeholder="请输入" size="small" class="u-item-width-normal"
              clearable></el-input>
          </el-form-item>
          <el-row type="flex">
            <el-col :span="24" align="right">
              <el-button style="margin-left: 30px" size="small" type="primary " @click="handleSearch">查询</el-button>
              <el-button style="margin-left: 10px" is-plain class="diy-button--white el-button--default" size="small"
                @click="resetSearch">重置</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>

      <div class="m-table">
        <div class="div_btn">
          <el-button size="small" @click="handleAdd" type="primary" icon="el-icon-plus">新建导入任务</el-button>
        </div>

        <el-table :data="tableData" border v-loading="tableLoading" class="m-table-item" style="width: 100%"
          :height="maxTableHeight">
          <el-table-column type="selection" fixed="left" align="center" width="50" />
          <el-table-column label="序号" fixed="left" type="index" align="center" width="50" />

          <el-table-column prop="taskName" align="center"  label="任务名称" :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column prop="importCategory" align="center" width="100" label="任务类型" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <span>{{
                scope.row.importCategory
                ? scope.row.importCategory == "1"
                  ? "导入房屋信息"
                  : scope.row.importCategory == "2"
                    ? "导入居民信息"
                    : "--"
                : "--"
              }}</span>
            </template>
          </el-table-column>

          <el-table-column prop="processStatus" align="center" width="200" label="状态" :show-overflow-tooltip="true">
            <template slot-scope="scope">
              <span>{{ binaryText(scope.row.processStatus) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="importSuccessNum" align="center" width="80" label="成功数目" :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column prop="importFailNum" align="center" width="80" label="失败数目" :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column prop="operatorName" align="center" width="100" label="操作人" :show-overflow-tooltip="true"> 
          </el-table-column>
          <el-table-column prop="completedTime" align="center" width="180" label="操作完成时间"
            :show-overflow-tooltip="true">
          </el-table-column>
          <el-table-column fixed="right" label="操作" align="center" width="220">
            <template slot-scope="scope">
              <el-button @click="handleCloseImport(scope.row)" v-if="scope.row.importCategory === 'processing'" type="text" size="small">取消导入</el-button>
              <!-- <el-button @click="handleCloseCheck(scope.row)"  v-if="scope.row.importCategory === 'processing'" type="text" size="small">确认完成</el-button> -->
              <el-button @click="handleDetail(scope.row)" type="text" size="small">查看</el-button>
            </template>
          </el-table-column>
        </el-table>
        <div>
          <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page.sync="pageNo"
            :page-sizes="[20, 50, 100, 200]" :page-size="parseInt(pageSize)" layout="sizes, prev, pager, next, total"
            :total="total">
          </el-pagination>
        </div>
      </div>
    </div>
    <div v-if="pageType == 'add'">
      <!-- :eventDetailData="eventDetailData" -->
            <smart-import-info ref="eleEditForm" :pageType="pageType"  
                @handleClose="handleClose" @handleOk="handleOk" :fileCodeRow="fileCode" :taskId="taskId" :processStatus="processStatus"/>
        </div>
  </div>
</template>
  
<script>
import { requestPost } from "@/js/dai/request";
import { genderList, binaryOptionList } from "@/js/columns/constants";
import nextTick from "dai-js/tools/nextTick";
import { mapGetters } from "vuex";
import axios from "axios";
import smartImportInfo from "./cpts/smartImportInfo";
export default {
  components: {smartImportInfo},

  data() {
    return {
      tableLoading: false,
      searchH: 170,
      tableData: [],
      genderList,
      binaryOptionList,
      formData: {},
      pageNo: 1,
      pageSize: window.localStorage.getItem("pageSize") || 20,
      total: 1,
      pageType: "list", // 列表list 新增add  详情detail

      dicts: {
        import_status:[],
        resident_category_import:[],
      }, //字典对象

      fileCode:null,
      taskId:null,
      processStatus:null,
    };
  },
  computed: {
    maxTableHeight() {
      const h = this.clientHeight - this.searchH - 330 + this.iframeHeight;
      const _h = this.clientHeight - 330 - this.searchH;
      return this.$store.state.inIframe ? h : _h;
    },
    ...mapGetters(["clientHeight", "iframeHeight"]),
  },
  watch: {},
  async created() {
    await this.getDicts();
  },
  mounted() {
    this.getTableData();
  },
  methods: {
    // --------------------------------------字典------------------------------------------
    async getDicts() {
      try {
        const dictTypes = ["import_status", 'resident_category_import']; // 把所有dictType放在这个数组里
        const requests = dictTypes.map((dictType) => {
          return this.$http.post("sys/dict/data/dictlist", {
            dictType: dictType,
          });
        });
        const results = await Promise.all(requests);
        results.forEach((result, index) => {
          if (result.data.code === 0) {
            this.dicts[dictTypes[index]].push(...result.data.data);
          } else {
            console.log(`获取${dictTypes[index]}字典失败: ${result.msg}`);
          }
        });
      } catch (error) {
        console.log("获取字典失败: ", error);
      }
    },
    binaryText(value) {
      let text = "";
      let foundItem = this.dicts.import_status.filter(
        (item) => item.value == value
      )[0];
      text = foundItem ? foundItem.label : "--";
      return text;
    },
    // ------------------------------------事件------------------------------------------
    // 搜索事件
    handleSearch(val) {
      console.log(this.formData);
      this.pageNo = 1;
      this.getTableData();
    },

    //   调出新增组件
    async handleAdd() {
      this.pageType = "add";
      this.fileCode = null;
    },
    //   查看详情触发事件
    async handleDetail(row) {
      this.pageType = "add";
      const {taskId,fileCode,processStatus} = row;
      this.taskId = taskId;
      this.fileCode = fileCode;
      this.processStatus = processStatus;
    },
   
    //  新增弹出框组件取消事件
    handleClose() {
      this.pageType = "list";
      this.processStatus = null;
      this.pageNo = 1;
      this.getTableData();
    },
    //  新增弹出框组件确定事件
    handleOk(type) {
     
    },
    //   删除触发事件
    async handleDel(row) {
      this.$confirm("确认删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.cancelFuwu([row.categorizedResiId]);
        })
        .catch((err) => {
          if (err == "cancel") {
            // this.$message({
            //   type: "info",
            //   message: "已取消删除"
            // });
          }
        });
    },
    //   删除处理
    async cancelFuwu(id) {
      const url = "/actual/base/resiCategorized/disability/deleteBatch";
      const { data, code, msg } = await requestPost(url, id);
      if (code === 0) {
        this.$message.success("删除成功！");
        this.getTableData();
      } else {
        this.$message.error("操作失败！");
      }
    },
    //   获取列表
    async getTableData() {
      this.tableLoading = true;
      const url = "/commonservice/import-task/pageList";
      const { pageSize, pageNo, formData } = this;
      const { data, code, msg } = await requestPost(url, {
        pageSize,
        pageNo,
        ...formData,
      });
      this.tableLoading = false;
      if (code === 0) {
        this.total = data.total || 0;
        this.tableData = data.list;
      } else {
        this.$message.error(msg);
      }
    },

    //   分页
    handleSizeChange(val) {
      this.pageSize = val;
      window.localStorage.setItem("pageSize", val);
      this.getTableData();
    },

    //   分页
    handleCurrentChange(val) {
      this.pageNo = val;
      this.getTableData();
    },
    //   重置
    resetSearch() {
      this.formData = {};

      this.pageNo = 1;
      this.getTableData();
    },
  },

  props: {},
};
</script>
<style lang="scss" scoped>
@import "@/assets/scss/buttonstyle.scss";
@import "@/assets/scss/modules/management/list-main.scss";
</style>
  