<template>
  <div class="name">
    <el-card class="mt10 box-card">
      <div class="lcl-title">巡检计划</div>
      <div class="mt10" style="overflow: hidden">
        <el-button
          type="primary"
          icon="el-icon-plus"
          @click="addPlan"
          style="float: left"
          >新增</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-download"
          @click="exportExcl"
          style="float: left"
          >下载模板</el-button
        >
        <el-upload
          class="upload"
          ref="upload"
          action="#"
          :multiple="false"
          :on-change="onChange"
          :on-remove="onRemove"
          :on-success="successFun"
          :http-request="planExcle"
          :show-file-list="false"
          accept=".xlsx,.xls"
        >
          <el-button
            type="primary"
            icon="el-icon-upload2"
            style="margin-left: 10px"
            >导入巡检计划</el-button
          >
          <div slot="tip" class="el-upload__tip" style="color: #fff">
            提示:仅允许上传excel文件!
          </div>
        </el-upload>
      </div>
      <div class="mt20" style="clear: both; margin-top: 20px">
        <el-form
          :model="queryParams"
          ref="queryForm"
          size="small"
          :inline="true"
        >
          <el-form-item label="日期">
            <el-date-picker
              v-model="queryParams.time"
              style="width: 240px"
              value-format="yyyy-MM-dd"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :clearable="false"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="类型" prop="planType">
            <el-select
              v-model="queryParams.planType"
              placeholder="巡检类型"
              :clearable="false"
              style="width: 240px"
            >
              <el-option
                v-for="dict in dict.type.slope_ins_plan"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="责任人" prop="planName">
            <el-input
              v-model="queryParams.planName"
              placeholder="请输入责任人"
              :clearable="false"
              style="width: 240px"
            />
          </el-form-item>

          <el-form-item>
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="search"
              >搜索</el-button
            >
          </el-form-item>
        </el-form>
      </div>

      <div class="mt10" id=" radar-table">
        <el-table
          :data="dataList"
          border
          stripe
          style="width: 100%"
          :header-cell-style="{
            textAlign: 'center',
            height: '20px',
          }"
          :row-style="{ height: '20px' }"
        >
          <el-table-column
            prop="orderNum"
            label="排序"
            type="index"
            width="50"
          ></el-table-column>
          <el-table-column align="center" prop="planDate" label="日期">
          </el-table-column>
          <el-table-column align="center" prop="planType" label="巡检类型">
            <template slot-scope="scope">
              <span v-if="scope.row.planType == 1">巡检记录</span>
              <span v-if="scope.row.planType == 2">人工观测</span>
              <span v-if="scope.row.planType == 3">设备校准</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="planName" label="责任人">
          </el-table-column>
          <el-table-column align="center" prop="planStatus" label="状态">
            <template slot-scope="scope">
              <el-tag
                class="subject-btn"
                type="warning"
                v-if="scope.row.planStatus == 0"
                >未开始</el-tag
              >
              <el-tag
                class="subject-btn"
                type="success"
                v-if="scope.row.planStatus == 1"
                >完成</el-tag
              >
              <el-tag
                class="subject-btn"
                type="error"
                v-if="scope.row.planStatus == 2"
                >未完成</el-tag
              >
            </template>
          </el-table-column>

          <el-table-column
            align="center"
            class-name="small-padding fixed-width"
            label="状态"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                >修改</el-button
              >
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
                >删除</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"
        />
      </div>
    </el-card>
    <!-- 新增修改 -->
    <el-dialog
      width="30%"
      :inline="true"
      title="巡检计划"
      :visible.sync="watchDialog"
    >
      <el-form ref="form" :model="form">
        <el-form-item label="巡检计划日期" label-width="120">
          <el-col :span="11">
            <el-date-picker
              v-model="form.planDate"
              type="date"
              placeholder="选择日期"
              value-format="yyyy-MM-dd"
            >
            </el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="巡检计划类型" label-width="120">
          <el-col :span="11">
            <el-select
              v-model="form.planType"
              placeholder="巡检类型"
              clearable
              style="width: 240px"
            >
              <el-option
                v-for="dict in dict.type.slope_ins_plan"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-col>
        </el-form-item>
        <el-form-item label="负责人" label-width="120">
          <el-col :span="11">
            <el-input
              v-model="form.planName"
              placeholder="请输入负责人"
              clearable
              style="width: 240px"
            />
          </el-col>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="watchDialog = false">取 消</el-button>
        <el-button type="primary" @click="submitForm">提 交</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  exportModel,
  planAdd,
  planHistory,
  planEdit,
  planRemove,
} from "@/api/lclApi/lclapi";
import { planImport } from "@/api/lclApi/formData";
import ChartsForLine from "@/components/lclEcharts/ChartsForLine";
export default {
  name: "name",
  dicts: ["slope_ins_plan"],
  components: { ChartsForLine },
  props: [],
  data() {
    return {
      watchDialogTitle: "1#边坡沉降观测点",
      watchDialog: false,
      dialogView: false,
      dataList: [
        {
          id: 6,
          planDate: "2023-03-01",
          planType: "1",
          planName: "1",
          planStatus: "0",
        },
      ],
      form: {
        watchSub: null,
        val: null,
      },

      queryParams: {
        pageNum: 1,
        pageSize: 10,
        time: "",
        planName: "",
        planStatus: "",
        planType: "",
      },
      total: 0,
      baseURL: process.env.VUE_APP_BASE_API,
      fileList: [],
      formData: null,
    };
  },
  watch: {},
  created() {
    this.$nextTick(() => {});
  },
  mounted() {
    this.$nextTick(() => {
      this.getList();
    });
  },
  methods: {
    formRest() {
      this.form = {
        id: null,
        planDate: null,
        planName: null,
        planStatus: null,
        planType: null,
      };
    },
    handleAdd(row) {
      console.log(row);
    },
    handleView(row) {
      this.watchDialogTitle = row.ren;
      this.getWatchList();
      this.dialogView = true;
    },
    handleChange(e) {
      console.log(e);
    },
    search() {
      this.getList();
    },
    addPlan() {
      this.formRest();
      this.watchDialog = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.formRest();
      this.form = row;
      this.watchDialog = true;
    },
    getList() {
      this.loading = true;
      planHistory(this.queryParams).then((res) => {
        if (res && res.code === 200 && res.rows) {
          this.dataList = res.rows;
          this.total = res.total;
        }
      });
    },
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != undefined) {
            planEdit(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.watchDialog = false;
              this.getList();
            });
          } else {
            planAdd(this.form).then((response) => {
              if (response.code === 200) {
                this.$modal.msgSuccess("新增成功");
                this.watchDialog = false;
                this.getList();
              }
            });
          }
        }
      });
    },
    handleDelete(row) {
      const id = row.id || this.ids;
      this.$modal
        .confirm("是否确认删除?")
        .then(function () {
          return planRemove({ id }).then();
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },

    exportExcl() {
      exportModel().then((res) => {
        this.$modal
          .confirm("是否确认导出?")
          .then(function () {
            return window.open(
              `${this.$store.getters.appPicApi}/excel/${res.msg}`,
              "_blank"
            );
          })
          .then(() => {
            this.$modal.msgSuccess("导出成功");
          })
          .catch(() => {});
      });
    },
    onChange(e) {
      this.formData = new FormData();
      this.formData.append("file", e.raw);
    },
    onRemove(file, fileList) {
      console.log(file, fileList, "onRemove");
    },
    successFun(response, file, fileList) {
      console.log(response, file, fileList, "successFun");
    },
    planExcle() {
      planImport(this.formData)
        .then((res) => {
          if (res && res.code === 200) {
            this.$modal.msgSuccess(res.msg);
            // this.$refs.upload.onSuccess(res);
            this.getList();
          }
          // this.$refs.upload.onSuccess(res);
        })
        .catch((err) => {
          this.$refs.upload.onError(err);
        });
    },
  },
};
</script>
<style lang="scss" scoped>
.name {
  width: 100%;
  min-height: 100%;
  .box-card {
    width: 100%;
    .lcl-title {
      font-size: 22px;
      font-family: Source Han Sans CN;
      font-weight: 400;
      color: #333333;
      line-height: 30px;
      position: relative;
      padding-left: 10px;
      &:before {
        content: "";
        position: absolute;
        left: 0px;
        top: 4px;
        width: 6px;
        height: 22px;
        background: #0d85f4;
      }
    }
  }
}
.el-input-number--medium {
  width: 100%;
}
.watchTable {
  width: 50%;
  min-height: 500px;
  float: left;
}

.watch-view-dialog {
  width: 100%;
  overflow: hidden;
  #charta {
    width: 50%;
    height: 500px;
    float: left;
    .nbnb {
      width: 100%;
      height: 500px !important;
    }
  }
}
.cb {
  clear: both;
}
</style>