<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="jianxiu">
        <el-select v-model="queryParams.jianxiu" placeholder="请选择设备状态" clearable size="small">
          <el-option label="未检验" value="-1" />
          <el-option label="已检验" value="0" />
          <el-option label="预警中" value="1" />
          <el-option label="已过期" value="2" />
        </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="handleBatchAdd()"
        >批量新增检验</el-button>
      </el-col>

    </el-row>


    <el-table v-loading="loading" :data="cycleList" @selection-change="handleSelectionChange">
      <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="devAddr" >
        <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="devDescribe" />-->
      <el-table-column label="检验状态" align="center" prop="jianxiu" >
        <template slot-scope="scope">
          <span v-if="scope.row.jianxiu==-1">未检验</span>
          <span v-if="scope.row.jianxiu==0" style="color: #1ab394">已检验</span>
          <span v-if="scope.row.jianxiu==1" style="color: #fb8c00">预警中</span>
          <span v-if="scope.row.jianxiu==2" style="color: #C03639">已过期</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="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="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-if="scope.row.jianxiu==1" style="color: red">{{ parseTime(scope.row.nextOverhaulDate, '{y}-{m}-{d}') }}</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" width="220">
        <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-document-add"
            @click="handleAddInspect(scope.row)" v-if="scope.row.devStatus<9"
          >新增检验</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-takeaway-box"
            @click="handleInspectFei(scope.row)" v-if="scope.row.devStatus<9"
          >报废</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="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="设备类型">
              <span>{{ devCycleRow.devTypeName }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出厂编号">
              <span>{{ devCycleRow.devName }}</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="检验人" prop="insUname">
              <el-input v-model="form.insUname" placeholder="请输入检验人姓名" style="width: 270px"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号" prop="insUphone">
              <el-input v-model="form.insUphone" placeholder="请输入手机号" oninput="if(value.length>11)value=value.slice(0,11)" type="number" style="width: 270px"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="内容描述" prop="insContext">
            <el-input v-model="form.insContext" type="textarea" placeholder="请输入内容" style="width: 630px"/>
          </el-form-item>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="本次日期" prop="currDate">
              <el-date-picker clearable size="small"
                              v-model="form.currDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择本次日期" style="width: 270px">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="下次日期" prop="nextDate" v-if="showNextTime">
              <el-date-picker clearable size="small"
                              v-model="form.nextDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择下次日期" style="width: 270px">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>


<!--          <el-input v-model="form.fileUrl" placeholder="请输入附件" />-->
        <el-form-item label="附件" prop="fileUrl">
          <MyFileUpload
            listType="picture"
            @resFun="getFileInfo"
            @remove="listRemove"
            :fileArr="fileList"
            :fileSize = "200"
          />
          <el-input v-show="false" disabled v-model="form.fileUrl"></el-input>
        </el-form-item>
<!--        <el-form-item label="备注" prop="remark">-->
<!--          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" style="width: 630px"/>-->
<!--        </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>


    <!-- 设备检验记录（生命全周期）列表对话框 -->
    <el-dialog :title="title" :visible.sync="openList" width="950px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="设备类型">
              <span>{{ devCycleRow.devTypeName }}</span>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="出厂编号">
              <span>{{ devCycleRow.devName }}</span>
            </el-form-item>
          </el-col>
        </el-row>

      </el-form>
<!--      <el-row>检验列表 </el-row>-->
      <el-table v-loading="loading" :data="devInspectList">
        <el-table-column label="操作类型" align="center" prop="insType" width="85">
          <template slot-scope="scope">
            <span v-if="scope.row.insType==null">-</span>
            <span v-if="scope.row.insType==1" >检修</span>
            <span v-if="scope.row.insType==2" >保养</span>
            <span v-if="scope.row.insType==9" style="color:red">报废</span>
          </template>
        </el-table-column>
        <el-table-column label="检验人" align="center" prop="insUname" width="85"/>
        <el-table-column label="手机号" align="center" prop="insUphone" width="120"/>
        <el-table-column label="内容描述" align="center" prop="insContext" />
        <el-table-column label="本次日期" align="center" prop="currDate" width="95">
        </el-table-column>
        <el-table-column label="下次日期" align="center" prop="nextDate" width="95">
          <template slot-scope="scope">
            <span v-if="scope.row.nextDate==null">-</span>
            <span v-else>{{scope.row.nextDate}}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作时间" align="center" prop="createTime" width="150"/>
        <el-table-column label="附件" align="center" prop="fileUrl" width="70">
          <template slot-scope="scope">
            <span v-if="scope.row.fileUrl==null">-</span>
            <span v-else><a style="color: #1c84c6" :href="scope.row.fileUrl" target="_blank">查看</a></span>
          </template>
        </el-table-column>


      </el-table>
      <pagination
        v-show="true"
        :total="devInspectTotal"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="handlelistInspet"
      />
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancelInspect">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 批量新增安检 -->
    <el-dialog title="批量新增设备检验记录" :visible.sync="batchOpen" width="800px" append-to-body :close-on-click-modal="false">
      <el-form ref="batchForm" :model="form" :rules="rules" label-width="90px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="设备类型">
              <el-select
                v-model="form.devType"
                placeholder="请选择设备类型"
                style="width: 100%"
              >
                <el-option
                  v-for="type in deviceOptions"
                  :key="type.dictValue"
                  :label="type.dictLabel + '(' + type.num + ')'"
                  :value="type.dictValue"
                />

              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="检验人" prop="insUname">
              <el-input v-model="form.insUname" placeholder="请输入检验人姓名"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="手机号" prop="insUphone">
              <el-input v-model="form.insUphone" placeholder="请输入手机号" oninput="if(value.length>11)value=value.slice(0,11)" type="number"/>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="内容描述" prop="insContext">
            <el-input v-model="form.insContext" type="textarea" placeholder="请输入内容"/>
          </el-form-item>
        </el-row>

        <el-row>
          <el-col :span="12">
            <el-form-item label="本次日期" prop="currDate">
              <el-date-picker clearable size="small"
                              v-model="form.currDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择本次日期"
                              style="width: 100%"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="下次日期" prop="nextDate" v-if="showNextTime">
              <el-date-picker clearable size="small"
                              v-model="form.nextDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="选择下次日期"
                              style="width: 100%"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="batchSubmit">确 定</el-button>
        <el-button @click="batchCancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listCycleByParam, listInspect, delCycle, addInspect, updateCycle, exportCycle, devLifeStatistic, batchAddInspect } from "@/api/cycle/cycle";
import MyFileUpload from '@/components/MyFileUpload';
let uploadfile = require("@/assets/uploadfile.png");
export default {
  name: "Cycle",
  components: {
    MyFileUpload,
  },
  data() {
    return {
      showNextTime:true,
      fileList:[],
      cycleDevType: [],
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      devInspectTotal:0,
      // 【请填写功能名称】表格数据
      cycleList: [],
      devInspectList: [],
      // 弹出层标题
      title: "",
      devCycleRow: {},
      // 是否显示弹出层
      open: false,
      openList: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        devType: null,
        devName: null,
        devNo: null,
        devAddr: null,
        devDescribe: null,
        devStatus: null,
        alarmDays: null,
        useDate: null,
        currOverhaulDate: null,
        nextOverhaulDate: null,
        currMaintenanceDate: null,
        nextMaintenanceDate: null,
        devLifeId:null
      },
      // 表单参数
      form: {
      },
      // 表单校验
      rules: {
        insUname: [
          { required: true, message: "检验人不能为空", trigger: "blur" }
        ],
        currDate: [
          { required: true, message: "本次日期不能为空", trigger: "blur" }
        ],
        nextDate: [
          { required: true, message: "下次日期不能为空", trigger: "blur" }
        ],
        insContext: [
          { required: true, message: "内容描述不能为空", trigger: "blur" }
        ]

      },
      deviceOptions: [],
      batchOpen: false
    };
  },
  created() {
    this.getList();
    this.getCycleDevType();
  },
  methods: {
    getFileInfo(res,file){
      this.form.fileUrl = res.url;
      this.fileList.push({
      name: file.name,
      url: uploadfile,
      });
    },

    listRemove(res) {
      this.form.fileUrl = "";
      this.fileList=[];
    },
    getCycleDevType(){
      this.getDicts("cytle_dev_type").then((response) => {
        this.cycleDevType = response.data;
      });
    },
    /** 查询列表 */
    getList() {
      this.loading = true;
      listCycleByParam(this.queryParams).then(response => {
        this.cycleList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    cancelInspect() {
      this.openList = false;
      this.resetList();
    },
    resetList() {
      this.devInspectList = [];
      this.devInspectTotal = 0;
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        devLifeId: null,
        devStatus:null,
        insType: null,
        insUname: null,
        insUphone: null,
        insContext: null,
        currDate: null,
        nextDate: null,
        fileUrl: null,
        remark: null,
        adminId: null,
        createTime: null
      };
      this.fileList=[];
      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
    },
    /** 新增按钮操作 */
    handleAddInspect(row){

      this.reset();
      this.open = true;
      this.title = "添加设备检验记录";
      this.form.devLifeId=row.id;
      this.form.insType=1;//检修、保养类型，1为检修
      this.devCycleRow=row;
      this.showNextTime=true;
    },
    /** 报废 */
    handleInspectFei(row){
      this.reset();
      this.open = true;
      this.title = "报废设备";
      this.form.devLifeId=row.id;
      this.form.devStatus=9;//报废设备
      this.form.insType=9;//检修、保养类型，9为报废
      this.showNextTime=false;
      this.devCycleRow=row;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.openList = true;
      this.title = "设备检验记录";
      this.form.devLifeId=row.id;
      this.devCycleRow=row;
      this.queryParams.devLifeId=row.id;
      this.handlelistInspet();
    },
    handlelistInspet(){
      listInspect(this.queryParams).then(response => {
        this.devInspectList = response.rows;
        this.devInspectTotal = response.total;
        this.loading = false;
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateInspect(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addInspect(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除设备id为"' + ids + '"的数据项?', "警告", {
          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(() => {});
    },
    //设备类型统计
    deviceTypeOptions(){
      devLifeStatistic().then(res =>{
        if(res.code == 200 && res.data){
          this.deviceOptions = res.data;
        }
      })
    },
    //批量新增安检
    handleBatchAdd(){
      //设备类型查询
      this.deviceTypeOptions();
      this.reset();
      this.form.insType=1;
      this.batchOpen = true;
    },
    // 取消按钮
    batchCancel() {
      this.batchOpen = false;
      this.reset();
    },
    batchSubmit(){
      this.$refs["batchForm"].validate(valid => {
        if (valid) {
          batchAddInspect(this.form).then(res =>{
              if(res.code == 200){
              this.msgSuccess("批量新增检验记录成功");
              this.batchOpen = false;
              this.getList();
            }
          })
        }
      });
    }
  }
};
</script>
