<script>
import { dicts } from "./dicts";
import useColumns from "./columns";
import {
  listGather,
  delGather,
  effective,
  loseEffective,
} from "@/api/business/spd/bo/gather";
export default {
  name: "Gather",
  dicts: [...dicts],
  components: {
    GatherDetails: () => import("./details/index.vue"),
    ElSuperSearch: () => import("@/components/super-search/index.vue"),
    ElSuperUxTable: () => import("@/components/super-ux-table/index.vue"),
  },
  data() {
    const { SearchColumns, TableColumns } = useColumns();
    const params = this.$init.params(SearchColumns);
    const page = this.$init.page();
    return {
      page,
      params,
      TableColumns,
      SearchColumns,
      size: "mini",
      tableData: [],
      loading: false,
      addType: "add",
      rowDetails: {},
      selectData: []
    };
  },
  created() {
    this.getList(this.params, this.page);
  },
  methods: {
    /** 查询竞争产品信息收集列表 */
    async getList(params, page) {
      try {
        this.loading = true;
        let { code, rows, total } = await listGather({
          ...params,
          ...page,
        });
        if (code == 200) {
          this.tableData = rows;
          this.page.total = total;
        }
      } catch (error) {
      } finally {
        this.loading = false;
      }
    },
    // 重置
    useReset() {
      this.params = this.$init.params(this.SearchColumns);
      this.page = this.$init.page();
      this.getList(this.params, this.page);
    },
    // 查看
    handleSee(row) {
      this.addType = "see";
      this.rowDetails = row;
      const { setVisible } = this.$refs.GatherDetails;
      setVisible(true);
    },
    // 有效/失效
    async effectiveBtn(row, type) {
      this.$modal
        .confirm("是否确认进行有效/失效操作？")
        .then(async () => {
          try {
            const { effectiveState, id } = row;
            let { code, msg } =
              type == "lose" ? await loseEffective(id) : await effective(id);
            if (code == 200) {
              this.$modal.notifySuccess(msg);
              this.getList(this.params, this.page);
            }
          } catch (error) {}
        })
        .catch(() => {});
    },
    // 删除
    handleDelete(row) {
      this.$modal
        .confirm("是否确认删除？")
        .then(async () => {
          try {
            const { id } = row;
            let { code, msg } = await delGather(id);
            if (code == 200) {
              this.$modal.notifySuccess(msg);
              this.getList(this.params, this.page);
            }
          } catch (error) {}
        })
        .catch(() => {});
    },
    //条件导出
    exportBtn() {
      this.download(
        "mk/bo/gathercg/export",
        {
          ...this.params
        },
        `项目信息收集_${new Date().getTime()}.xlsx`
      );
    },
    //选择导出
    chooseExportBtn(){
      if(this.selectData.length < 1){
        this.$modal.msgWarning("请选择数据！");
        return;
      }
      let ids = this.selectData.map((item)=> item.id);
      this.download(
        "mk/bo/gathercg/chooseExport/" + ids,
        {},
        `项目信息收集_${new Date().getTime()}.xlsx`
      );
    },
    useSelect(prop) {
      this.selectData = prop;
    },
  },
};
</script>

<template>
  <el-card
    v-loading="loading"
    :body-style="{
      height: '100%',
      padding: 0,
      display: 'flex',
      'flex-direction': 'column',
    }"
  >
    <el-super-search
      v-model="params"
      :size="size"
      :dict="dict"
      :columns="SearchColumns"
      @reset="useReset"
      @submit="getList(params, page)"
    ></el-super-search>

    <el-row
      :gutter="10"
      class="mb10"
      type="flex"
      justify="end"
      style="margin-top: 20px"
    >
      <el-col :span="1.5">
        <el-button :size="size" @click="exportBtn">条件导出</el-button>
        <el-button :size="size" @click="chooseExportBtn">选择导出</el-button>
      </el-col>
    </el-row>

    <el-super-ux-table
      checkbox
      @row-select="useSelect"
      v-model="tableData"
      :size="size"
      :dict="dict"
      :page="page"
      :columns="TableColumns"
      index
      pagination
      convenitentOperation
      highlight-current-row
      storage-key="GatherSuperTable"
      @pagination="getList(params, page)"
      style="margin: 16px 0 0"
    >
      <ux-table-column fixed="right" title="操作" align="center" width="160">
        <template slot-scope="scope">
          <el-button :size="size" type="text" @click="handleSee(scope.row)"
            >查看</el-button
          >
          <el-button
            v-if="scope.row.effectiveState != '1'"
            :size="size"
            type="text"
            @click="effectiveBtn(scope.row)"
            >有效</el-button
          >
          <el-button
            v-if="scope.row.effectiveState != '2'"
            :size="size"
            type="text"
            @click="effectiveBtn(scope.row, 'lose')"
            >失效</el-button
          >
          <el-button :size="size" type="text" @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </ux-table-column>
    </el-super-ux-table>

    <gather-details
      ref="GatherDetails"
      :dict="dict"
      :size="size"
      :addType="addType"
      :data="rowDetails"
      @refresh="getList(params, page)"
    ></gather-details>
  </el-card>
</template>

<style scoped lang="scss">
.el-card {
  width: calc(100% - 32px);
  height: calc(100vh - 32px);
  margin: 16px;
  padding: 16px;
  border-radius: 8px;
  overflow-y: auto;
  overflow-x: hidden;
}
</style>
