<template>
  <div class="mod-config">
    <el-form
      :inline="true"
      :model="dataForm"
      @keyup.enter.native="getDataList()"
    >
      <el-form-item>
        <el-button
          v-if="isAuth('generator:volumes:save')"
          type="primary"
          @click="addOrUpdateHandle()"
          >新增存储卷</el-button
        >
        <el-form-item>
          <el-input
            v-model="dataForm.volumesId"
            placeholder="通过存储卷id查询"
            clearable
          ></el-input>
        </el-form-item>
        <el-button @click="getDataListById()">查询</el-button>
      </el-form-item>
      <el-form-item>
        <el-select
          clearable
          label="挂载状态"
          v-model="dataForm.volumesMount"
          @change="getDataListByMount(dataForm.volumesMount)"
        >
          <el-option
            v-for="item in mountOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          ></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"
      @selection-change="selectionChangeHandle"
      :default-sort="{ prop: 'volumesName', order: 'descending' }"
      style="width: 100%"
    >
      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50"
      >
      </el-table-column>
      <el-table-column
        prop="volumesName"
        header-align="center"
        align="center"
        label="存储卷名称"
      >
      </el-table-column>
      <el-table-column
        prop="volumesAlias"
        header-align="center"
        align="center"
        label="存储卷别名"
      >
      </el-table-column>
      <el-table-column
        prop="volumesId"
        header-align="center"
        align="center"
        label="存储卷ID"
      >
      </el-table-column>
      <el-table-column
        prop="accessMode"
        header-align="center"
        align="center"
        label="访问模式"
      >
      </el-table-column>
      <el-table-column
        prop="volumesMount"
        header-align="center"
        align="center"
        label="挂载状态"
      >
      </el-table-column>
      <el-table-column
        prop="createTime"
        header-align="center"
        align="center"
        label="创建时的时间"
        sortable
      >
      </el-table-column>
      <el-table-column
        prop="parentProjectName"
        header-align="center"
        align="center"
        label="项目名称"
      >
      </el-table-column>
      <el-table-column
        prop="volumesSize"
        header-align="center"
        align="center"
        label="存储卷容量"
        sortable
      >
      </el-table-column>
      <el-table-column
        prop="volumesDescription"
        header-align="center"
        align="center"
        label="存储卷描述信息"
      >
      </el-table-column>
      <el-table-column
        prop="creatorName"
        header-align="center"
        align="center"
        label="创建者"
      >
      </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            v-if="isAuth('generator:volumes:delete')"
            type="text"
            size="small"
            @click="deleteHandle(scope.row.volumesId)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="this.$store.state.settings.pageSizeList"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update
      v-if="addOrUpdateVisible"
      ref="addOrUpdate"
      @refreshDataList="getDataList"
    ></add-or-update>
  </div>
</template>

<script>
import AddOrUpdate from "./volumes-add-or-update";
export default {
  data() {
    return {
      dataForm: {
        volumesId: "",
        volumesMount: "",
      },
      dataList: [],
      pageIndex: 1,
      pageSize: this.$store.state.settings.pageSize,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      filterVal: "",
      mountOptions: [
           {
          value: "",
          label: "全部",
        },
        {
          value: "已挂载",
          label: "已挂载",
        },
        {
          value: "未挂载",
          label: "未挂载",
        },
      ],
    };
  },
  components: {
    AddOrUpdate,
  },
  activated() {
    this.getDataList();
  },
  methods: {
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/generator/volumes/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          volumesId: this.dataForm.volumesId,
          volumesState: this.dataForm.volumesState,
           volumesMount:this.dataForm.volumesMount
        }),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list;
          this.totalPage = data.page.totalCount;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },

    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val;
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 通过id获取name
    getNameById(id) {
      var item = this.dataList.filter((item) => item.volumesId === id);
      return item[0].volumesName;
    },
    // 通过id判断是否删除
    isDelete(id) {
      var item = this.dataList.filter((item) => item.volumesId === id);
      if (item[0].volumesMount === '未挂载') return true;
      return false;
    },
    // 删除
    deleteHandle(id) {
      var ids = id
        ? [id]
        : this.dataListSelections.map((item) => {
            return item.volumesId;
          });
      this.$confirm(
        `确定对[id=${ids.join(",")}]进行[${id ? "删除" : "批量删除"}]操作?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).then(() => {
        // 如果该存储卷挂载则不删除
        if (!this.isDelete(id)) {
          this.$message({
            type: "error",
            message: "请先删除挂载的应用",
            duration: 3000,
          });
          return;
        }
        this.$http({
          url: `/kubesphereApi/api/v1/namespaces/mkcloud/persistentvolumeclaims/${this.getNameById(
            id
          )}`,
          method: "delete",
// 
        })
          .then((res) => {
            this.$http({
              url: this.$http.adornUrl("/generator/volumes/delete"),
              method: "post",
              data: this.$http.adornData(ids, false),
            }).then(({ data }) => {
              if (data && data.code === 0) {
                this.$message({
                  message: "操作成功",
                  type: "success",
                  duration: 1500,
                  onClose: () => {
                    this.getDataList();
                  },
                });
              } else {
                this.$message.error(data.msg);
              }
            });
          })
          .catch((error) => {
            console.log("删除存储卷错误", error);
            this.$message.warning("删除存储卷错误");
          });
      });
    },

    //查询搜索框
    getDataListById() {
    
      console.log(
        "getDataListById  dataForm  " +
          this.dataForm.volumesId +
          "|" +
          this.dataForm.volumesState +
          "|" +
          this.dataForm.volumesMount
      );
      this.getDataList();
    },

    //标签搜索选择
    getDataListByMount(value) {
      console.log(
        "getDataListByMount  dataForm  " +
          +this.dataForm.volumesId +
          "|" +
          this.dataForm.volumesState +
          "|" +
          this.dataForm.volumesMount
      );
      this.getDataList();
    },
  },
};
</script>
