<template>
  <!-- <div class="app-container"> -->
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator="/" style="margin-bottom: 20px">
      <el-breadcrumb-item
        v-for="(item, index) in routeList"
        :key="item + index"
        :to="{ path: item.path }"
      >
        {{ item.routeName }}
      </el-breadcrumb-item>
    </el-breadcrumb>

    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="任务名称" prop="taskName">
        <el-input
          v-model="queryParams.data.taskName"
          placeholder="请输入任务名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        >
          <el-button
            type="primary"
            slot="append"
            icon="el-icon-search"
            size="small"
            @click="handleQuery"
          ></el-button>
        </el-input>
      </el-form-item>
      <!-- <el-form-item label="任务状态" prop="taskStatus">
        <el-select
          v-model="queryParams.data.taskStatus"
          placeholder="请选择任务状态"
          clearable
          size="small"
        >
          <el-option
            v-for="dict in taskStatusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item> -->

      <el-form-item>
        <!-- <el-button
          type="cyan"
          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"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['df:aqrw:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['df:aqrw:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['df:aqrw:remove']"
          >删除</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="aqrwList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" fixed />
      <el-table-column
        label="序号"
        type="index"
        width="50"
        align="center"
        fixed
      />

      <el-table-column
        label="任务名称"
        align="center"
        prop="taskName"
        min-width="120"
      />
      <!-- <el-table-column
        label="任务堤防id"
        align="center"
        prop="dikeCodes"
        min-width="120"
      /> -->
      <el-table-column
        label="计划完成时间"
        align="center"
        prop="planCompletionTime"
        min-width="120"
      >
        <template slot-scope="scope">
          <span>{{
            parseTime(scope.row.planCompletionTime, "{y}-{m}-{d}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="任务堤防数量"
        align="center"
        prop="taskSluiceNumber"
        min-width="120"
      />
      <el-table-column
        label="完成堤防数量"
        align="center"
        prop="completionNumber"
        min-width="120"
      />
      <el-table-column
        label="未完成堤防数量"
        align="center"
        prop="incompletionNumber"
        min-width="120"
      />
      <el-table-column
        label="任务状态"
        align="center"
        prop="taskStatus"
        :formatter="taskStatusFormat"
        min-width="120"
      />
      <el-table-column
        label="下发时间"
        align="center"
        prop="distributeTime"
        min-width="120"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.distributeTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="完成时间"
        align="center"
        prop="completionTime"
        min-width="120"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.completionTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
        width="180"
        fixed="right"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['df:aqrw:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['df:aqrw: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="1200px"
      append-to-body
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="任务名称" prop="taskName">
          <el-input v-model="form.taskName" placeholder="请输入任务名称" />
        </el-form-item>
        <el-form-item label="任务堤防" prop="dikeCodes">
          <linkDike :form="form" :isMultiple="true" v-if="open" />
          <!-- <el-select
            v-model="form.dikeCodes"
            placeholder="请选择堤防名称"
            style="width: 100%"
            multiple
          >
            <el-option
              v-for="dict in dikeNameOptions"
              :key="dict.id"
              :label="dict.dikeName"
              :value="dict.dikeCode"
            ></el-option>
          </el-select> -->
        </el-form-item>
        <el-form-item label="计划完成时间" prop="planCompletionTime">
          <el-date-picker
            clearable
            size="small"
            style="width: 100%"
            v-model="form.planCompletionTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择计划完成时间"
          >
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="任务堤防数量" prop="taskSluiceNumber">
          <el-input
            v-model="form.taskSluiceNumber"
            placeholder="请输入任务堤防数量"
          />
        </el-form-item>
        <el-form-item label="完成堤防数量" prop="completionNumber">
          <el-input
            v-model="form.completionNumber"
            placeholder="请输入完成堤防数量"
          />
        </el-form-item>
        <el-form-item label="未完成堤防数量" prop="incompletionNumber">
          <el-input
            v-model="form.incompletionNumber"
            placeholder="请输入未完成堤防数量"
          />
        </el-form-item>
        <el-form-item label="任务状态" prop="taskStatus">
          <el-select
            v-model="form.taskStatus"
            placeholder="请选择任务状态"
            style="width: 100%"
          >
            <el-option
              v-for="dict in taskStatusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            ></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item label="下发时间" prop="distributeTime">
          <el-date-picker
            clearable
            size="small"
            style="width: 100%"
            v-model="form.distributeTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择下发时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="完成时间" prop="completionTime">
          <el-date-picker
            clearable
            size="small"
            style="width: 100%"
            v-model="form.completionTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择完成时间"
          >
          </el-date-picker>
        </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 {
  listAqrw,
  getAqrw,
  delAqrw,
  addAqrw,
  updateAqrw,
  exportAqrw,
} from "@/api/yg/dike/aqrw";
import { listJbxx } from "@/api/yg/dike/jbxx";
import linkDike from "@/components/linkNameSelect/linkDike.vue";

export default {
  name: "Aqrw",
  components: { linkDike },
  data() {
    return {
      // 面包屑，路由信息
      routeList: [
        {
          path: "/dike/safetyManage/safetyAppraisal/task",
          routeName: "安全鉴定任务",
        },
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 堤防安全鉴定任务信息表格数据
      aqrwList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 任务状态字典
      taskStatusOptions: [],
      // 堤防项目
      dikeNameOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ids: null,
        data: {
          dikeCodes: null,
          planCompletionTime: null,
          taskSluiceNumber: null,
          completionNumber: null,
          incompletionNumber: null,
          taskStatus: null,
          distributeTime: null,
          completionTime: null,
          createUid: null,
          updateUid: null,
          owerDept: null,
          relation: null,
        },
        // 排序方式
        params: {
          // 按哪个字段排序
          orderBy: "create_time",
          // desc降序，升序asc
          sortBy: "desc",
        },
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        taskName: [
          { required: true, message: "任务名称不能为空", trigger: "blur" },
        ],
        dikeCodes: [
          { required: true, message: "请选择任务堤防", trigger: "change" },
        ],
      },
    };
  },
  created() {
    this.getList();
    listJbxx({ data: {} }).then((res) => {
      // console.log(111, res);
      this.dikeNameOptions = res.records;
    });
    this.getDicts("sys_job_status").then((response) => {
      this.taskStatusOptions = response.data;
    });
  },
  methods: {
    /** 查询堤防安全鉴定任务信息列表 */
    getList() {
      this.loading = true;
      listAqrw(this.queryParams).then((response) => {
        this.aqrwList = response.records;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 任务状态字典翻译
    taskStatusFormat(row, column) {
      return this.selectDictLabel(this.taskStatusOptions, row.taskStatus);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        dikeCodes: null,
        planCompletionTime: null,
        taskSluiceNumber: null,
        completionNumber: null,
        incompletionNumber: null,
        taskStatus: null,
        distributeTime: null,
        completionTime: null,
        createUid: null,
        updateUid: null,
        createTime: null,
        updateTime: null,
        owerDept: null,
        remark: null,
        relation: null,
      };
      this.resetForm("form");
    },
    // 查询表单重置
    resetQueryForm() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        data: {
          id: null,
          dikeCodes: null,
          planCompletionTime: null,
          taskSluiceNumber: null,
          completionNumber: null,
          incompletionNumber: null,
          taskStatus: null,
          distributeTime: null,
          completionTime: null,
          createUid: null,
          updateUid: null,
          createTime: null,
          updateTime: null,
          owerDept: null,
          remark: null,
          relation: null,
        },
        params: {
          // 按哪个字段排序
          orderBy: "create_time",
          // desc降序，升序asc
          sortBy: "desc",
        },
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetQueryForm();
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加堤防安全鉴定任务信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getAqrw(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改堤防安全鉴定任务信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateAqrw(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              }
            });
          } else {
            addAqrw(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      if (ids) {
        this.$confirm("是否删除选中的数据？", "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
          .then(function () {
            return delAqrw(ids);
          })
          .then(() => {
            this.getList();
            this.msgSuccess("删除成功");
          })
          .catch(function () {});
      } else {
        this.$message.warning("请选择要删除的数据！！");
      }
    },
  },
};
</script>
<style lang="scss" scoped>
@import "@/assets/css/dialog.scss";
// ::v-deep {
//   .el-dialog {
//     margin-top: 2vh !important;
//   }
// }
</style>
