<template>
  <div class="app-container">
    <div style="background-color: #fff;padding: 20px;border-radius: 10px;">
      <p style="margin: 0; font-weight: 800;font-size: 16px;color: #343434;display: flex;align-items: center;margin-bottom: 20px;">
        <img src="../../../assets/images/icon_htgl_btzs.png" alt="" style="width: 16px;height: 20px;">
       <span style="margin-left: 10px;">社区资产类型</span>
      </p>
      <div style="background: #F3F3F3;padding: 20px 15px; padding-bottom: 0;margin-bottom: 20px;">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-row>

        <el-col :span="24">
          <el-col :span="8">
            <el-form-item label="资产类型名称" prop="postCode">
              <el-input
                v-model="queryParams.postCode"
                placeholder="请输入资产类型名称"
                clearable
                @keyup.enter.native="handleQuery"
                style="width: 210px;"
              />
            </el-form-item>
          </el-col>

          <el-col :span="8">
            <el-form-item label="是否停用" prop="status">
              <el-select style="width: 210px;" v-model="queryParams.status" placeholder="是否停用" clearable>
                <el-option
                  v-for="dict in dict.type.zinxdunt"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                  @keyup.enter.native="handleQuery"

                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="上级资产名称" prop="parentId">
             <treeselect style="width: 210px;"  v-model="queryParams.parentId" :options="deptOptions" :normalizer="normalizer" placeholder="选择资产类型" />
            </el-form-item>
          </el-col>
        </el-col>
      </el-row>
      <el-form-item class="butt">
        <el-button size="mini" type="primary"   @click="handleQuery">搜索</el-button>
        <el-button size="mini"   @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    </div>

     <div class="casr">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wuYe:assetType:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wuYe:assetType:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wuYe:assetType:remove']"
        >删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          size="mini"
          @click="handleExport"
          v-hasPermi="['wuYe:assetType:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-if="refreshTable"
      v-loading="loading"
      :data="assetTypeList"
      row-key="assetId"
      :default-expand-all="isExpandAll"
      :tree-props="{children: 'children', hasChildren: 'hasChildren'}"
    >
      <el-table-column label="资产类型名称" align="center" prop="assetName" />
      <!-- <el-table-column label="显示顺序" align="center" prop="orderNum" /> -->
      <el-table-column label="资产类型状态" align="center" key="status" >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            active-value="1"
            inactive-value="2"
            @change="handleStatusChange(scope.row)"
          ></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleAdd(scope.row)"
            v-hasPermi="['wuYe:assetType:add']"
          >新增</el-button>
          <el-button
            size="mini"
            type="text"
            style="color: #28C529;"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wuYe:assetType:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            style="color: red;"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wuYe:assetType:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    </div>

    <!-- <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    /> -->
    </div>

    <!-- 添加或修改资产类型对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="上级资产名称" prop="parentId">
             <treeselect v-model="form.parentId" :options="deptOptions" :normalizer="normalizer" placeholder="选择资产类型" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="资产类型名称" prop="assetName">
              <el-input v-model="form.assetName" placeholder="请输入资产类型名称" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="显示顺序" prop="orderNum">
              <el-input v-model="form.orderNum" type="Number" placeholder="请输入显示顺序" />
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="是否停用" prop="status">
              <el-select style="width: 100%;" v-model="form.status" placeholder="是否停用" clearable>
                <el-option
                  v-for="dict in dict.type.zinxdunt"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>



        <!-- <el-form-item label="删除标志" prop="delFlag">
          <el-input v-model="form.delFlag" placeholder="请输入删除标志" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listAssetType, getAssetType, delAssetType, addAssetType, updateAssetType } from "@/api/wuguan/assetType";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "AssetType",
  dicts: ['sys_normal_disable','zinxdunt'],
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 是否展开，默认全部展开
      isExpandAll: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 部门树选项
      deptOptions: [],
      // 资产类型表格数据
      assetTypeList: [],
      // 弹出层标题
      title: "",
      // 重新渲染表格状态
      refreshTable: true,
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        parentId: null,
        ancestors: null,
        assetName: null,
        orderNum: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        parentId: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        assetName: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
        orderNum: [
          { required: true, message: "不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询资产类型列表 */
    getList() {
      this.loading = true;
      listAssetType(this.queryParams).then(response => {
        this.assetTypeList =  this.handleTree(response.data, "assetId", "parentId");
         this.deptOptions = this.handleTree(response.data, "assetId");
        // this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        assetId: null,
        parentId: 0,
        ancestors: null,
        assetName: null,
        orderNum: null,
        status: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 转换部门数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.assetId,
        label: node.assetName,
        children: node.children
      };
    },
    // 用户状态修改
    handleStatusChange(row) {
      let text = row.status === "1" ? "启用" : "停用";
      var that = this
      this.$modal.confirm('确认要"' + text + '""' + row.assetName + '"').then(function() {
         that.gwths(row);
      }).then(() => {
        this.$modal.msgSuccess(text + "成功");
      }).catch(function() {
        row.status = row.status === "1" ? "2" : "1";
      });
    },
    gwths(row){
      updateAssetType(row).then(response => {

      });
    },
    /** 展开/折叠操作 */
    toggleExpandAll() {
      this.refreshTable = false;
      this.isExpandAll = !this.isExpandAll;
      this.$nextTick(() => {
        this.refreshTable = true;
      });
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.assetId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.reset();
      if (row != null && row.assetId) {
        this.form.parentId = row.assetId;
      } else {
        this.form.parentId = 0;
      }
      this.open = true;
      this.title = "添加资产类型";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const assetId = row.assetId || this.ids
      getAssetType(assetId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改资产类型";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.assetId != null) {
            updateAssetType(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addAssetType(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const assetIds = row.assetId || this.ids;
      this.$modal.confirm('是否确认删除资产类型编号为"' + assetIds + '"的数据项？').then(function() {
        return delAssetType(assetIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wuYe/assetType/export', {
        ...this.queryParams
      }, `assetType_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style  lang="scss">
  .butt{
    .el-button--small{
      padding: 9px 23px !important;
      font-size: 14px;
    }
  }
  .casr{
    .el-tabs__item.is-active{
      color: #196CFF;
    }
    .el-tabs--card > .el-tabs__header .el-tabs__item{
      padding: 0  30px !important;
    }
  }
  .el-table .el-table__header-wrapper th{
    background-color:#F5F6F8;
    color:#343434;
  }
  .el-table{
    color:#343434;
  }
</style>
<style scoped lang="scss">
  .goe{
    p{
      margin: 0;
      font-weight: 800;
      font-size: 16px;
      color: #161616;
      margin-top: 10px;
      margin-bottom: 3px;
      padding: 0 10px;
      overflow: hidden;
      text-overflow: ellipsis;
      display:-webkit-box; //作为弹性伸缩盒子模型显示。
      -webkit-box-orient:vertical; //设置伸缩盒子的子元素排列方式--从上到下垂直排列
      -webkit-line-clamp:1; //显示的行
    }
    p:nth-child(2){
      // margin-bottom: 10px;
      margin-top: 0;
     font-weight: 400;
     font-size: 14px;
     color: #AAAAAA;
     display: flex;
     justify-content: space-between;
     align-items: center;
     padding-bottom: 10px;
     span:nth-child(2){
       padding: 3px 5px;
       background: #EDEDED;
       border-radius: 10px;
     }
    }
  }
  .ingwe{
    border: 2px solid #196CFF;
  }
  </style>
