<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备类型" prop="devType">
        <el-select v-model="queryParams.devType" placeholder="请选择设备类型" clearable size="small">
          <el-option
            v-for="item in cycleDevType"
            :key="item.dictValue"
            :label="item.dictLabel"
            :value="item.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="出厂编号" prop="devName">
        <el-input
          v-model="queryParams.devName"
          placeholder="请输入出厂编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="规格型号" prop="devNo">
        <el-input
          v-model="queryParams.devNo"
          placeholder="请输入规格型号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="安装位置" prop="devAddr">
        <el-input
          v-model="queryParams.devAddr"
          placeholder="请输入安装位置"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="设备状态" prop="devStatus">
        <el-select v-model="queryParams.devStatus" placeholder="请选择设备状态" clearable size="small">
          <el-option label="正常" value="1" />
          <el-option label="停用" value="2" />
          <el-option label="报废" value="9" />
        </el-select>
      </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"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
		  :loading="exportLoading"
          @click="handleExport"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cycleList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="设备类型" align="center" prop="devTypeName" width="120"/>
      <el-table-column label="设备名称" align="center" prop="devName"/>
      <el-table-column label="出厂编号" align="center" prop="devNo" />
      <el-table-column label="规格型号" align="center" prop="devModel" />
      <el-table-column label="安装位置" align="center" prop="devAddr" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span v-if="scope.row.devAddr==null">-</span>
          <span v-else>{{ scope.row.devAddr }}</span>
        </template>
      </el-table-column>
      <el-table-column label="制造单位" align="center" prop="devCompany">
        <template slot-scope="scope">
          <span v-if="scope.row.devCompany">{{ scope.row.devCompany }}</span>
          <span v-else>-</span>
        </template>
      </el-table-column>

      <el-table-column label="设备状态" align="center" prop="devStatus" >
        <template slot-scope="scope">
          <span v-if="scope.row.devStatus==null">-</span>
          <span v-if="scope.row.devStatus==1" style="color: #1ab394">正常</span>
          <span v-if="scope.row.devStatus==2" style="color: #C03639">停用</span>
          <span v-if="scope.row.devStatus==9" style="color:grey">报废</span>
        </template>
      </el-table-column>
      <el-table-column label="预警天数" align="center" prop="alarmDays"  width="90">
        <template slot-scope="scope">
          <span v-if="scope.row.alarmDays==null">-</span>
          <span v-else>{{ scope.row.alarmDays }}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备投运日期" align="center" prop="useDate" width="100">
        <template slot-scope="scope">
          <span v-if="scope.row.useDate==null">-</span>
          <span v-else>{{ parseTime(scope.row.useDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="最新检验日期" align="center" prop="currOverhaulDate" width="100">-->
<!--        <template slot-scope="scope">-->
<!--          <span v-if="scope.row.currOverhaulDate==null">-</span>-->
<!--          <span v-else>{{ parseTime(scope.row.currOverhaulDate, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="下次检验日期" align="center" prop="nextOverhaulDate" width="100">-->
<!--        <template slot-scope="scope">-->
<!--          <span v-if="scope.row.nextOverhaulDate==null">-</span>-->
<!--          <span v-else>{{ parseTime(scope.row.nextOverhaulDate, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="最新保养日期" align="center" prop="currMaintenanceDate" width="100">-->
<!--        <template slot-scope="scope">-->
<!--          <span v-if="scope.row.currMaintenanceDate==null">-</span>-->
<!--          <span v-else>{{ parseTime(scope.row.currMaintenanceDate, '{y}-{m}-{d}') }}</span>-->
<!--        </template>-->
<!--      </el-table-column>-->
<!--      <el-table-column label="下次保养日期" align="center" prop="nextMaintenanceDate" width="100">-->
<!--        <template slot-scope="scope">-->
<!--          <span v-if="scope.row.nextMaintenanceDate==null">-</span>-->
<!--          <span v-else>{{ parseTime(scope.row.nextMaintenanceDate, '{y}-{m}-{d}') }}</span>-->
<!--        </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"
            icon="el-icon-document"
            @click="handleDetail(scope.row)"
          >详情</el-button>
          <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"
    />

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="设备名称" prop="devName">
              <el-input v-model="form.devName" placeholder="请输入设备名称" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="设备类型" prop="devType">
              <el-select v-model="form.devType" placeholder="请选择设备类型" style="width: 220px">
                <el-option
                  v-for="item in cycleDevType"
                  :key="item.dictValue"
                  :label="item.dictLabel"
                  :value="item.dictValue"
                />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="13">
            <el-form-item label="出厂编号" prop="devName">
              <el-input v-model="form.devNo" placeholder="请输入出厂编号" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="11">
            <el-form-item label="规格型号" prop="devNo">
              <el-input v-model="form.devModel" placeholder="请输入规格型号" style="width: 220px"/>
            </el-form-item>
          </el-col>
          <el-col :span="13">
            <el-form-item label="安装位置" prop="devAddr">
              <el-input v-model="form.devAddr" placeholder="请输入安装位置" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-form-item label="设备描述" prop="devDescribe">
            <el-input type="textarea" v-model="form.devDescribe" placeholder="请输入设备描述" />
          </el-form-item>
        </el-row>




        <!--<el-row v-if="form.devType==1">-->
          <!--<el-col :span="12">-->
            <!--<el-form-item label="计量范围" prop="devFlowRange">-->
              <!--<el-input v-model="form.devFlowRange" placeholder="请输入计量范围" />-->
            <!--</el-form-item>-->
          <!--</el-col>-->

          <!--<el-col :span="12">-->
            <!--<el-form-item label="精度等级" prop="devLevel">-->
              <!--<el-input v-model="form.devLevel" placeholder="请输入精度等级" />-->
            <!--</el-form-item>-->
          <!--</el-col>-->
        <!--</el-row>-->

        <!--<el-row v-if="form.devType==1">-->
          <!--<el-col :span="12">-->
            <!--<el-form-item label="连接口方向" prop="workElectricity">-->
              <!--<el-input v-model="form.workElectricity" placeholder="请输入连接口方向" />-->
            <!--</el-form-item>-->
          <!--</el-col>-->

          <!--<el-col :span="12">-->
            <!--<el-form-item label="内部编号" prop="devNumber">-->
              <!--<el-input v-model="form.devNumber" placeholder="请输入内部编号" />-->
            <!--</el-form-item>-->
          <!--</el-col>-->
        <!--</el-row>-->


        <!--<el-row v-if="form.devType==2">-->
          <!--<el-col :span="12">-->
            <!--<el-form-item label="公称直径" prop="devDiameter">-->
              <!--<el-input v-model="form.devDiameter" placeholder="请输公称直径" />-->
            <!--</el-form-item>-->
          <!--</el-col>-->

          <!--<el-col :span="12">-->
            <!--<el-form-item label="公称压力" prop="devPressure">-->
              <!--<el-input v-model="form.devPressure" placeholder="请输入公称压力" />-->
            <!--</el-form-item>-->
          <!--</el-col>-->
        <!--</el-row>-->

        <!--<el-row v-if="form.devType==2">-->
          <!--<el-col :span="12">-->
            <!--<el-form-item label="压力等级" prop="devLevel">-->
              <!--<el-input v-model="form.devLevel" placeholder="请输入压力等级" />-->
            <!--</el-form-item>-->
          <!--</el-col>-->

          <!--<el-col :span="12">-->
            <!--<el-form-item label="整定压力" prop="workElectricity">-->
              <!--<el-input v-model="form.workElectricity" placeholder="请输入公称压力" />-->
            <!--</el-form-item>-->
          <!--</el-col>-->
        <!--</el-row>-->

        <!--<el-row v-if="form.devType==3">-->
          <!--<el-col :span="12">-->
            <!--<el-form-item label="介质" prop="devDiameter">-->
              <!--<el-input v-model="form.devDiameter" placeholder="请输介质" />-->
            <!--</el-form-item>-->
          <!--</el-col>-->

          <!--<el-col :span="12">-->
            <!--<el-form-item label="精准度" prop="devFlowRange">-->
              <!--<el-input v-model="form.devFlowRange" placeholder="请输入精准度" />-->
            <!--</el-form-item>-->
          <!--</el-col>-->
        <!--</el-row>-->

        <!--<el-row v-if="form.devType==3">-->
          <!--<el-col :span="12">-->
            <!--<el-form-item label="防护等级" prop="devLevel">-->
              <!--<el-input v-model="form.devLevel" placeholder="请输入防护等级" />-->
            <!--</el-form-item>-->
          <!--</el-col>-->

          <!--<el-col :span="12">-->
            <!--<el-form-item label="报警限值" prop="workElectricity">-->
              <!--<el-input v-model="form.workElectricity" placeholder="请输入报警限值" />-->
            <!--</el-form-item>-->
          <!--</el-col>-->
        <!--</el-row>-->

        <!--<el-row v-if="form.devType==3">-->
          <!--<el-col :span="12">-->
            <!--<el-form-item label="生产日期" prop="devDate">-->
              <!--<el-date-picker clearable size="small"-->
                              <!--v-model="form.devDate"-->
                              <!--type="date"-->
                              <!--style="width: 100%"-->
                              <!--value-format="yyyy-MM-dd"-->
                              <!--placeholder="选择启用日期">-->
              <!--</el-date-picker>-->
            <!--</el-form-item>-->
          <!--</el-col>-->
        <!--</el-row>-->


        <!--<el-row v-if="form.devType==4">-->
          <!--<el-col :span="12">-->
            <!--<el-form-item label="设备类别" prop="devDiameter">-->
              <!--<el-input v-model="form.devDiameter" placeholder="请输入设备类别" />-->
            <!--</el-form-item>-->
          <!--</el-col>-->
        <!--</el-row>-->


        <!--<el-row v-if="form.devType==5">-->
          <!--<el-col :span="12">-->
            <!--<el-form-item label="公称通径" prop="devDiameter">-->
              <!--<el-input v-model="form.devDiameter" placeholder="请输入公称通径" />-->
            <!--</el-form-item>-->
          <!--</el-col>-->

          <!--<el-col :span="12">-->
            <!--<el-form-item label="流量范围" prop="devFlowRange">-->
              <!--<el-input v-model="form.devFlowRange" placeholder="请输入流量范围" />-->
            <!--</el-form-item>-->
          <!--</el-col>-->
        <!--</el-row>-->

        <!--<el-row v-if="form.devType==5">-->
          <!--<el-col :span="12">-->
            <!--<el-form-item label="准确度等级" prop="devLevel">-->
              <!--<el-input v-model="form.devLevel" placeholder="请输入准确度等级" />-->
            <!--</el-form-item>-->
          <!--</el-col>-->

          <!--<el-col :span="12">-->
            <!--<el-form-item label="公称压力" prop="devPressure">-->
              <!--<el-input v-model="form.devPressure" placeholder="请输入公称压力" />-->
            <!--</el-form-item>-->
          <!--</el-col>-->
        <!--</el-row>-->

        <!--<el-row v-if="form.devType==5">-->
          <!--<el-col :span="12">-->
            <!--<el-form-item label="工作电源" prop="workElectricity">-->
              <!--<el-input v-model="form.workElectricity" placeholder="请输入工作电源" />-->
            <!--</el-form-item>-->
          <!--</el-col>-->

          <!--<el-col :span="12">-->
            <!--<el-form-item label="生产日期" prop="devDate">-->
              <!--<el-date-picker clearable size="small"-->
                              <!--v-model="form.devDate"-->
                              <!--type="date"-->
                              <!--style="width: 100%"-->
                              <!--value-format="yyyy-MM-dd"-->
                              <!--placeholder="选择启用日期">-->
              <!--</el-date-picker>-->
            <!--</el-form-item>-->
          <!--</el-col>-->
        <!--</el-row>-->




        <el-row>
          <el-col :span="12">
            <el-form-item label="制造单位" prop="devCompany">
              <el-input v-model="form.devCompany" placeholder="请输入制造单位" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="设备状态" prop="devStatus">
              <el-select v-model="form.devStatus" style="width: 100%">
                <el-option label="正常" value="1" />
                <el-option label="停用" value="2" />
                <el-option label="报废" value="9" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="预警天数" prop="alarmDays">
              <el-input v-model="form.alarmDays" placeholder="请输入预警天数" type="number"/>
            </el-form-item>
          </el-col>

          <el-col :span="12">
          <el-form-item label="投运日期" prop="useDate">
            <el-date-picker clearable size="small"
                            v-model="form.useDate"
                            type="date"
                            style="width: 100%"
                            value-format="yyyy-MM-dd"
                            placeholder="选择投运日期">
            </el-date-picker>
          </el-form-item>
        </el-col>
          <el-col :span="12">
            <el-form-item label="生产日期" prop="devDate">
              <el-date-picker clearable size="small"
                              v-model="form.devDate"
                              type="date"
                              style="width: 100%"
                              value-format="yyyy-MM-dd"
                              placeholder="选择生产日期">
              </el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="报废日期" prop="devVoidDate">
              <el-date-picker clearable size="small"
                              v-model="form.devVoidDate"
                              type="date"
                              style="width: 100%"
                              value-format="yyyy-MM-dd"
                              placeholder="选择报废日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

<!--        <el-form-item label="最新检修日期" prop="currOverhaulDate">-->
<!--          <el-date-picker clearable size="small"-->
<!--            v-model="form.currOverhaulDate"-->
<!--            type="date"-->
<!--            value-format="yyyy-MM-dd"-->
<!--            placeholder="选择最新检修日期">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="下次检修日期" prop="nextOverhaulDate">-->
<!--          <el-date-picker clearable size="small"-->
<!--            v-model="form.nextOverhaulDate"-->
<!--            type="date"-->
<!--            value-format="yyyy-MM-dd"-->
<!--            placeholder="选择下次检修日期">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="最新保养日期" prop="currMaintenanceDate">-->
<!--          <el-date-picker clearable size="small"-->
<!--            v-model="form.currMaintenanceDate"-->
<!--            type="date"-->
<!--            value-format="yyyy-MM-dd"-->
<!--            placeholder="选择最新保养日期">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="下次保养日期" prop="nextMaintenanceDate">-->
<!--          <el-date-picker clearable size="small"-->
<!--            v-model="form.nextMaintenanceDate"-->
<!--            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>


    <!-- 详情 -->
    <CycleDetailInfo ref="cycleDetail"/>
  </div>
</template>

<script>
import { listCycle, getCycle, delCycle, addCycle, updateCycle, exportCycle } from "@/api/cycle/cycle";
import CycleDetailInfo from "./components/CycleDetailInfo";
export default {
  name: "Cycle",
  components: {
    CycleDetailInfo
  },
  data() {
    return {
      cycleDevType: [],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 【请填写功能名称】表格数据
      cycleList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        devType: null,
        devName: null,
        devNo: null,
        devAddr: null,
        devDescribe: null,
        devModel: null,
        devDiameter: null,
        devFlowRange: null,
        devLevel: null,
        devPressure: null,
        workElectricity: null,
        devNumber: null,
        devDate: null,
        devStatus: null,
        alarmDays: null,
        useDate: null,
        currOverhaulDate: null,
        nextOverhaulDate: null,
        currMaintenanceDate: null,
        nextMaintenanceDate: null,
      },
      // 表单参数
      form: {
      },
      // 表单校验
      rules: {
        devName: [
          { required: true, message: "设备名称不能为空", trigger: "blur" }
        ],
        devType: [
          { required: true, message: "设备类型不能为空", trigger: "blur" }
        ],
        devName: [
          { required: true, message: "出厂编号不能为空", trigger: "blur" }
        ],
        devStatus: [
          { required: true, message: "设备状态不能为空", trigger: "blur" }
        ],
        devAddr: [
          { required: true, message: "安装位置不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getCycleDevType();
  },
  methods: {
    getCycleDevType(){
      this.getDicts("cytle_dev_type").then((response) => {
        this.cycleDevType = response.data;
      });
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      listCycle(this.queryParams).then(response => {
        this.cycleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        devType: null,
        devName: null,
        devNo: null,
        devAddr: null,
        devDescribe: null,
        devStatus: null,
        alarmDays: null,
        useDate: null,
        currOverhaulDate: null,
        nextOverhaulDate: null,
        currMaintenanceDate: null,
        nextMaintenanceDate: null,
        createTime: 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.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
      getCycle(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) {
            updateCycle(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCycle(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      const name   = row.devName || this.devName;
      this.$confirm('是否确认删除出厂编号为"' + name + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCycle(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(() => {
          this.exportLoading = true;
          return exportCycle(queryParams);
        }).then(response => {
          this.download(response.msg);
          this.exportLoading = false;
        }).catch(() => {});
    },
    //详情
    handleDetail(row){
      this.$refs.cycleDetail.openDetail(row.id);
    }

  }
};
</script>
