<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="所属网格" prop="deptName">
        <el-input
          v-model="queryParams.deptName"
          placeholder="请输入所属网格"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="资产设备" prop="zcName">
        <el-input
          v-model="queryParams.zcName"
          placeholder="请输入资产设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="巡检人" prop="xjName">
        <el-input
          v-model="queryParams.xjName"
          placeholder="请输入巡检人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="巡检人电话" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入巡检人电话"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="巡检日期" prop="xjDate">
        <el-date-picker clearable
          v-model="queryParams.xjDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择巡检日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="巡检标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入巡检标题"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="巡检结果" prop="xjResult">
        <el-input
          v-model="queryParams.xjResult"
          placeholder="请输入巡检结果"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间" prop="xjStart">
        <el-date-picker clearable
          v-model="queryParams.xjStart"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="xjEnd">
        <el-date-picker clearable
          v-model="queryParams.xjEnd"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择结束时间">
        </el-date-picker>
      </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="['szxc:zcssxj: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="['szxc:zcssxj: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="['szxc:zcssxj: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="['szxc:zcssxj:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="45" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="所属网格" align="center" prop="deptName" />
      <el-table-column label="资产设备名称" align="center" prop="zcName" />
      <el-table-column label="巡检人" align="center" prop="xjName" />
      <el-table-column label="巡检人电话" align="center" prop="phone" />
      <el-table-column label="巡检日期" align="center" prop="xjDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.xjDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="巡检标题" align="center" prop="title" />
      <el-table-column label="开始时间" align="center" prop="xjStart">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.xjStart, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="xjEnd">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.xjEnd, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="巡检结果" align="center" prop="xjResult" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.szxc_xj_result" :value="scope.row.xjResult"/>
        </template>
      </el-table-column>
      <el-table-column label="巡检说明" align="center" prop="xjExplain" />
      <el-table-column label="巡检照片" align="center" prop="picture" >
        <template slot-scope="scope">
          <image-preview :src="scope.row.picture" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="巡检状态" align="center" prop="xjStatus" >
        <template slot-scope="scope">
          <dict-tag :options="dict.type.szxc_zcssxj_staus" :value="scope.row.xjStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="处理说明" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" min-width="150" fixed="right">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.xjStatus==='0'"
            style="padding: 5px 10px"
            size="mini"
            type="primary"
            plain
            @click="handleUpdate(scope.row)"
            v-hasPermi="['szxc:zcssxj:edit']"
          >巡检登记</el-button>

          <el-button
            v-if="scope.row.xjStatus==='2'"
            style="padding: 5px 10px"
            size="mini"
            type="primary"
            plain
            @click="handled(scope.row)"
            v-hasPermi="['szxc:zcssxj:edit']"
          >完成</el-button>

          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['szxc:zcssxj: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="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">

        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="资产设备" prop="zcName">
              <el-input readonly v-if="!form.id"
                        placeholder="请选择资产设备"
                        v-model="form.zcName">
                <el-button slot="append" icon="el-icon-search" @click="zcQuery"></el-button>
              </el-input>

              <el-input v-if="form.id" v-model="form.zcName" placeholder="请选择资产设备" disabled/>

            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属网格" prop="deptName">
              <el-input v-model="form.deptName" placeholder="请输入所属网格" disabled/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10">
          <el-col :span="24">
            <el-form-item label="巡检标题" prop="title">
              <el-input v-model="form.title" placeholder="请输入巡检标题" :disabled="form.id ? true : false"/>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10">
          <el-col :span="12">
            <el-form-item label="开始时间" prop="xjStart">
              <el-date-picker clearable style="width: 100%" :disabled="form.id ? true : false"
                              v-model="form.xjStart"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择开始时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结束时间" prop="xjEnd">
              <el-date-picker clearable style="width: 100%" :disabled="form.id ? true : false"
                              v-model="form.xjEnd"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择结束时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :gutter="10" v-if="form.id">
          <el-col :span="12">
            <el-form-item label="巡检人" prop="xjName">
              <el-input v-model="form.xjName" placeholder="请输入巡检人"/>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="巡检人电话" prop="phone">
              <el-input v-model="form.phone" placeholder="请输入巡检人电话"/>
            </el-form-item>
          </el-col>

        </el-row>

        <el-row :gutter="10" v-if="form.id">
          <el-col :span="12">
            <el-form-item label="巡检日期" prop="xjDate">
              <el-date-picker clearable style="width: 100%;"
                              v-model="form.xjDate"
                              type="date"
                              value-format="yyyy-MM-dd"
                              placeholder="请选择巡检日期">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="巡检结果" prop="xjResult">
              <el-radio-group v-model="form.xjResult">
                <el-radio
                  v-for="dict in dict.type.szxc_xj_result"
                  :key="dict.value"
                  :label="dict.value"
                >{{dict.label}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>

        <el-form-item label="巡检说明" prop="xjExplain" v-if="form.id">
          <el-input v-model="form.xjExplain" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="巡检照片" prop="picture" v-if="form.id">
          <image-upload v-model="form.picture"/>
        </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="选择资产设施" :visible.sync="zcss.visible" width="900px" append-to-body>
      <el-form :model="zcss.queryParams" ref="queryForm" size="small" :inline="true" label-width="70px">
        <el-form-item label="姓名" prop="name">
          <el-input
            v-model="zcss.queryParams.name"
            placeholder="请输入姓名"
            clearable
            @keyup.enter.native="zcssQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="zcssQuery">搜索</el-button>
        </el-form-item>
      </el-form>
      <el-alert title="双击选择资产设施" type="warning" :closable="false"></el-alert>
      <el-table v-loading="zcss.loading" :data="zcss.list" border @cell-dblclick="zcssDblclick">
        <el-table-column label="所属网格" align="center" prop="deptName" />
        <el-table-column label="资产名称" align="center" prop="zcName" />
        <el-table-column label="资产类型" align="center" prop="zcType" >
          <template slot-scope="scope">
            <dict-tag :options="dict.type.szxc_zc_type" :value="scope.row.zcType"/>
          </template>
        </el-table-column>
        <el-table-column label="安装时间" align="center" prop="installDate">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.installDate, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="负责人" align="center" prop="fzr" />
        <el-table-column label="联系电话" align="center" prop="phone" />
        <el-table-column label="详细地址" align="center" prop="address" />
        <el-table-column label="资产照片" align="center" prop="picture" >
          <template slot-scope="scope">
            <image-preview :src="scope.row.picture" :width="50" :height="50"/>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="zcss.total>0"
        :total="zcss.total"
        :page.sync="zcss.queryParams.pageNum"
        :limit.sync="zcss.queryParams.pageSize"
        @pagination="getlistZcss"
      />
    </el-dialog>

    <!-- 资产设施巡检完成对话框 -->
    <el-dialog title="完成" :visible.sync="wc_open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">

        <el-form-item label="处理说明" prop="remark">
          <el-input v-model="form.remark" type="textarea" 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 { listZcssxj, getZcssxj, delZcssxj, addZcssxj, updateZcssxj } from "@/api/szxc/zcssxj";
import { listZcss } from "@/api/szxc/zcss";
import { deptTreeSelect } from "@/api/system/user";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "Zcssxj",
  components: { Treeselect },
  dicts: ['szxc_zc_type', 'szxc_zcssxj_staus', 'szxc_xj_result'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 资产设施巡检表格数据
      list: [],
      // 组织树选项
      deptOptions: undefined,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 完成
      wc_open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        zcssId: null,
        zcName: null,
        xjName: null,
        phone: null,
        xjDate: null,
        title: null,
        xjResult: null,
        xjExplain: null,
        picture: null,
        xjStatus: null,
        xjStart: null,
        xjEnd: null,
        deptId: null,
        deptName: null,
        userId: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        zcName: [
          { required: true, message: "资产设备不能为空", trigger: "blur" }
        ],
        deptName: [
          { required: true, message: "所属网格不能为空", trigger: "blur" }
        ],
        title: [
          { required: true, message: "巡检标题不能为空", trigger: "blur" }
        ],
        xjName: [
          { required: true, message: "巡检人不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "巡检人电话不能为空", trigger: "blur" }
        ],
        xjDate: [
          { required: true, message: "巡检日期不能为空", trigger: "blur" }
        ],
        xjStart: [
          { required: true, message: "开始时间不能为空", trigger: "blur" }
        ],
        xjEnd: [
          { required: true, message: "结束时间不能为空", trigger: "blur" }
        ],
        xjResult: [
          { required: true, message: "巡检结果不能为空", trigger: "blur" }
        ],
        xjExplain: [
          { required: true, message: "巡检说明不能为空", trigger: "blur" }
        ],
        picture: [
          { required: true, message: "巡检照片不能为空", trigger: "blur" }
        ],
        remark: [
          { required: true, message: "处理说明不能为空", trigger: "blur" }
        ]
      },
      // 资产设施信息
      zcss: {
        visible: false,
        // 遮罩层
        loading: false,
        // 总条数
        total: 0,
        // 查询参数
        queryParams:{
          pageNum: 1,
          pageSize: 10
        },
        // 查询列表
        list: [],
        // 选中数据
        selectRow: null
      },
    };
  },
  created() {
    this.getList();
    this.getDeptTree();
  },
  methods: {
    /** 查询组织下拉树结构 */
    getDeptTree() {
      deptTreeSelect().then(response => {
        this.deptOptions = response.data;
      });
    },
    /** 查询资产设施巡检列表 */
    getList() {
      this.loading = true;
      listZcssxj(this.queryParams).then(response => {
        this.list = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.wc_open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        zcssId: null,
        zcName: null,
        xjName: null,
        phone: null,
        xjDate: null,
        title: null,
        xjResult: null,
        xjExplain: null,
        picture: null,
        xjStatus: null,
        xjStart: null,
        xjEnd: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        deptId: null,
        deptName: null,
        userId: 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
      getZcssxj(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) {
            if (this.form.xjResult==='0'){
              this.form.xjStatus = '1'
            }
            if (this.form.xjResult==='1'){
              this.form.xjStatus = '2'
            }
            if(this.form.xjStatus === '2' && this.wc_open){
              this.form.xjStatus = '3'
            }
            updateZcssxj(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.wc_open = false;
              this.getList();
            });
          } else {
            this.form.xjStatus = '0';
            addZcssxj(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除资产设施巡检编号为"' + ids + '"的数据项？').then(function() {
        return delZcssxj(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('szxc/zcssxj/export', {
        ...this.queryParams
      }, `zcssxj_${new Date().getTime()}.xlsx`)
    },
    // 点击选择资产设施
    zcQuery(){
      this.zcss.visible = true;
      this.getlistZcss()
    },
    /** 查询资产设施信息列表 */
    getlistZcss() {
      this.zcss.loading = true;
      listZcss(this.zcss.queryParams).then(response => {
        this.zcss.list = response.rows;
        this.zcss.total = response.total;
        this.zcss.loading = false;
      });
    },
    // 资产设施信息查询
    zcssQuery(){
      this.zcss.queryParams.pageNum = 1;
      this.getlistZcss();
    },
    // 资产设施选择 选中数据
    zcssDblclick(row) {
      this.zcss.selectRow = row;
      this.form.zcssId = row.id;
      this.form.zcName = row.zcName;
      this.form.deptId = row.deptId;
      this.form.deptName = row.deptName;
      this.zcss.visible = false
    },
    // 去处理
    handled(row){
      this.reset();
      this.wc_open = true;
      this.form = row;
    }
  }
};
</script>
