<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!--<el-form-item label="巡检项ID" prop="busItemsId">-->
      <!--<el-input-->
      <!--v-model="queryParams.busItemsId"-->
      <!--placeholder="请输入巡检项ID"-->
      <!--clearable-->
      <!--@keyup.enter.native="handleQuery"-->
      <!--/>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="上级ID" prop="parentId">-->
      <!--<el-input-->
      <!--v-model="queryParams.parentId"-->
      <!--placeholder="请输入上级ID"-->
      <!--clearable-->
      <!--@keyup.enter.native="handleQuery"-->
      <!--/>-->
      <!--</el-form-item>-->
      <el-form-item label="名称" prop="busItemsName">
        <el-input
          v-model="queryParams.busItemsName"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <!--<el-form-item label="整改期限" prop="busTimeLimit">-->
      <!--<el-input-->
      <!--v-model="queryParams.busTimeLimit"-->
      <!--placeholder="请输入整改期限"-->
      <!--clearable-->
      <!--@keyup.enter.native="handleQuery"-->
      <!--/>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="置顶" prop="isTop">-->
      <!--<el-input-->
      <!--v-model="queryParams.isTop"-->
      <!--placeholder="请输入置顶"-->
      <!--clearable-->
      <!--@keyup.enter.native="handleQuery"-->
      <!--/>-->
      <!--</el-form-item>-->
      <!--<el-form-item label="排序号" prop="other">-->
      <!--<el-input-->
      <!--v-model="queryParams.other"-->
      <!--placeholder="请输入排序号"-->
      <!--clearable-->
      <!--@keyup.enter.native="handleQuery"-->
      <!--/>-->
      <!--</el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['system:items:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:items:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:items:remove']"
        >删除
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:items:export']"
        >导出
        </el-button>
      </el-col>

      <el-col :span="1.5" v-if="items_ceng >0 ">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="back"
        >返回
        </el-button>
      </el-col>


      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="itemsList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <!--<el-table-column label="巡检项ID" align="center" prop="busItemsId" />-->
      <!--<el-table-column label="上级ID" align="center" prop="parentId" />-->
      <el-table-column label="名称" align="center">
        <template slot-scope="scope">
          <label style="cursor: pointer;color:#0000ff" v-show="items_ceng <=1" @click="toNext(scope.row.busItemsId)">{{scope.row.busItemsName}}</label>
          <label v-show="items_ceng ==2">{{scope.row.busItemsName}}</label>
        </template>
      </el-table-column>
      <el-table-column label="整改期限" align="center" prop="busTimeLimit" v-if="items_ceng == 2">
      </el-table-column>
      <el-table-column label="置顶" align="center" prop="isTop">
        <template slot-scope="scope">
          <label v-show="scope.row.isTop == 'N'">否</label>
          <label v-show="scope.row.isTop == 'Y'">是</label>
        </template>
      </el-table-column>
      <el-table-column label="排序号" align="center" prop="other"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:items:edit']"
          >修改
          </el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:items:remove']"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

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

    <!-- 添加或修改巡检项对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="items_ceng == 2 ? rulesItem: rules " label-width="80px">
        <!--<el-form-item label="巡检项ID" prop="busItemsId">-->
        <!--<el-input v-model="form.busItemsId" placeholder="请输入巡检项ID" />-->
        <!--</el-form-item>-->
        <!--<el-form-item label="上级ID" prop="parentId">-->
        <!--<el-input v-model="form.parentId" placeholder="请输入上级ID" />-->
        <!--</el-form-item>-->
        <el-form-item label="名称" prop="busItemsName">
          <el-input v-model="form.busItemsName" placeholder="请输入名称"/>
        </el-form-item>
        <el-form-item label="整改期限" prop="busTimeLimit" v-show="items_ceng == 2">
          <el-input v-model="form.busTimeLimit" placeholder="请输入整改期限"/>
        </el-form-item>
        <el-form-item label="置顶" prop="isTop">
          <el-select v-model="form.isTop">
            <el-option label="否" value="N" sel></el-option>
            <el-option label="是" value="Y"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="排序号" prop="other">
          <el-input v-model="form.other" 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 {listItems, getItems, delItems, addItems, updateItems} from "@/api/centerdata_emergency1/items";

  export default {
    name: "Items",
    data() {
      return {
        items_ceng: 0,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        items_names: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 巡检项表格数据
        itemsList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          busItemsId: null,
          parentId: null,
          busItemsName: null,
          busTimeLimit: null,
          isTop: null,
          other: null
        },
        // 表单参数
        form: {
          isTop: 'N'
        },
        rulesItem:{
          other:[
            {
              pattern: /^\d*$/,
              message: "序号必须为数字",
              trigger: "blur"
            }
          ],
          busItemsName: [
            {
              required: true, message: "名称不能为空", trigger: "blur"
            },
            {min: 1, max: 20, message: '巡检项目名长度必须介于 1 和 20 之间', trigger: 'blur'}
          ],
          busTimeLimit: [
            {
              required: true, message: "整改期限不能为空", trigger: "blur"
            },
            {
              pattern: /^\d*$/,
              message: "整改期限必须为数字",
              trigger: "blur"
            }
          ]
        },
        // 表单校验
        rules: {
          other:[
            {
              pattern: /^\d*$/,
              message: "序号必须为数字",
              trigger: "blur"
            }
          ],
          busItemsName: [
            {
              required: true, message: "名称不能为空", trigger: "blur"
            },
            {min: 1, max: 20, message: '巡检项目名长度必须介于 1 和 20 之间', trigger: 'blur'}
          ],
        }
      };
    },
    created() {
      this.queryParams.parentId = this.$route.query.parent_id
      this.items_ceng = this.$route.query.items_ceng
      this.getList();
    },

    watch: {
      $route() {
        this.queryParams.parentId = this.$route.query.parent_id
        this.items_ceng = this.$route.query.items_ceng
        this.getList();
      }
    },
    methods: {
      back() {
        this.$router.go(-1)
      },

      toNext(busItemsId) {
        this.items_ceng++;
        this.$router.push({
          path: '',
          query: {"parent_id": busItemsId, "items_ceng": this.items_ceng}
        })
      },


      /** 查询巡检项列表 */
      getList() {
        this.loading = true;
        listItems(this.queryParams).then(response => {
          this.itemsList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          busItemsId: null,
          parentId: this.queryParams.parentId,
          busItemsName: null,
          busTimeLimit: null,
          isTop: 'N',
          other: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.busItemsId)
        this.items_names = selection.map(item => item.busItemsName)
        this.single = selection.length !== 1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加巡检项";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const busItemsId = row.busItemsId || this.ids
        getItems(busItemsId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改巡检项";
        });
      },
      /** 提交按钮 */
      submitForm() {
        console.log("kkkkkkk")
        this.$refs["form"].validate(valid => {
          console.log("1111111", valid)
          if (valid) {
            if (this.form.busItemsId != null) {
              updateItems(this.form).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addItems(this.form).then(response => {
                this.$modal.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const busItemsIds = row.busItemsId || this.ids;
        const busItemsName = row.busItemsName || this.items_names
        this.$modal.confirm('是否确认删除巡检项为"' + busItemsName + '"的数据项？').then(function () {
          return delItems(busItemsIds);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      },
      /** 导出按钮操作 */
      handleExport() {
        let path;
        if (this.items_ceng == 2) {
          path = 'system/items/export'
        } else {
          path = 'system/items/export12'
        }
        this.download(path, {
          ...this.queryParams
        }, `items_${new Date().getTime()}.xlsx`)
      }
    }
  };
</script>
