<template>
  <basic-container>
    <el-dialog :modal-append-to-body="false" :visible.sync="doorRecordVisible" title="开门记录" width="80%">
      <open-door-record :openDoorRecordList="openDoorRecordList"></open-door-record>
    </el-dialog>
    <el-dialog :modal-append-to-body="false" :visible.sync="solveDetailVisible" title="处理工单" width="50%">
      <device-job ref="deviceJob"  :jobSolve="solveDetail" :visible="solveDetailVisible"
                  @close-dialog="closeDialog"></device-job>
    </el-dialog>
    <avue-data-icons :option="countOption" style="margin-left: 250px"></avue-data-icons>
    <avue-crud :option="option" :search.sync="searchForm" :table-loading="loading" :data="data" :page="page" :permission="permissionList"
               :before-open="beforeOpen" v-model="form" ref="crud" @row-update="rowUpdate" @row-save="rowSave" @row-del="rowDel"
               @search-change="searchChange" @search-reset="searchReset" @selection-change="selectionChange" @current-change="currentChange"
               @size-change="sizeChange" @refresh-change="refreshChange">

      <template slot="menu" slot-scope="scope">
        <el-button type="text" size="mini" icon="el-icon-s-order" plain v-if="permission.deviceorder_edit &&scope.row.jobStatus ==1" @click.native="handleSolve(scope.row)">处理工单
        </el-button>
        <el-button type="text" size="mini" icon="el-icon-s-order" plain v-if="permission.deviceorder_edit &&scope.row.jobStatus ==2" @click.native="handleSolve(scope.row)">处理结果
        </el-button>
        <el-button type="text" size="mini" icon="el-icon-info" plain v-if="scope.row.deviceType == 9" @click="openDoorRecord(scope.row)">开门记录
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {
    getList,
    getDetail,
    add,
    update,
    remove,
    getDeviceJobStaticVo
  } from "../../api/order/deviceorder";
  import {
    mapGetters
  } from "vuex";
  import {iconsOption} from "../../api/dataview/dataviewoption";

  export default {
    data() {
      return {
        searchForm:{},
        deviceJobStaticVo:{},
        openDoorRecordList: [],
        doorRecordVisible: false,
        solveDetailVisible: false,
        solveDetail: {},
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        countOption: {},
        option: {
          height: 'auto',
          calcHeight: 30,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: false,
          addBtn: false,
          editBtn: false,
          delBtn: false,
          selection: true,
          dialogClickModal: false,
          column: [{
            label: "设备编号",
            prop: "deviceId",
            search: true,
            width: 150,
            rules: [{
              required: true,
              message: "请输入设备编号",
              trigger: "blur"
            }]
          },
            {
              label: "设备名称",
              prop: "deviceName",
            },
            {
              label: "相关人",
              prop: "person",
              hide: true,
              rules: [{
                required: true,
                message: "请输入相关人",
                trigger: "blur"
              }]
            },
            {
              label: "告警时间",
              prop: "alarmTime",
              type: "datetime",
              width: 150,
              valueFormat: "yyyy-MM-dd HH:mm:ss",
              rules: [{
                required: true,
                message: "请输入告警时间",
                trigger: "blur"
              }]
            },
            {
              label: "设备类型",
              prop: "deviceType",
              search: true,
              type: "select",
              dataType: "number",
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=device_type",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: true,
                message: "请输入设备类型",
                trigger: "blur"
              }]
            },
            {
              label: "告警地点",
              prop: "alarmPosition",
              width: 200,
              rules: [{
                required: true,
                message: "请输入告警地点",
                trigger: "blur"
              }]
            },
            {
              label: "处理人",
              prop: "handler",
              rules: [{
                required: true,
                message: "请输入处理人",
                trigger: "blur"
              }]
            },
            {
              label: "电话",
              prop: "phone",
              width: 120,
              rules: [{
                required: true,
                message: "请输入电话",
                trigger: "blur"
              }]
            },
            {
              label: "工单状态",
              prop: "jobStatus",
              type: "select",
              dataType: "number",
              search: true,
              dicUrl: "/api/blade-system/dict-biz/dictionary?code=job_status",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: true,
                message: "请输入工单状态",
                trigger: "blur"
              }]
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.deviceorder_add, false),
          viewBtn: this.vaildData(this.permission.deviceorder_view, false),
          delBtn: this.vaildData(this.permission.deviceorder_delete, false),
          editBtn: this.vaildData(this.permission.deviceorder_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    mounted() {
      this.$nextTick(()=>{
        if (this.$route.params.todo){
          this.$refs.crud.searchForm.jobStatus = 1;
          this.query.jobStatus = 1;
        }
        this.onLoad(this.page,this.query);
      })
    },
    watch:{
      solveDetailVisible:function(value){

        this.onLoad(this.page,this.query)

      },
    },
    methods: {
      openDoorRecord(row) {
        this.openDoorRecordList = [{
          openTime: "2020-05-14 12:55:45",
          openStatus: "开"
        },
          {
            openTime: "2020-05-14 12:55:45",
            openStatus: "开"
          },
          {
            openTime: "2020-05-14 12:55:45",
            openStatus: "关"
          },
          {
            openTime: "2020-05-14 12:55:45",
            openStatus: "开"
          },
          {
            openTime: "2020-05-14 12:55:45",
            openStatus: "关"
          },
          {
            openTime: "2020-05-14 12:55:45",
            openStatus: "开"
          },
          {
            openTime: "2020-05-14 12:55:45",
            openStatus: "开"
          },
          {
            openTime: "2020-05-14 12:55:45",
            openStatus: "开"
          },
          {
            openTime: "2020-05-14 12:55:45",
            openStatus: "开"
          },
          {
            openTime: "2020-05-14 12:55:45",
            openStatus: "开"
          },
          {
            openTime: "2020-05-14 12:55:45",
            openStatus: "开"
          },
          {
            openTime: "2020-05-14 12:55:45",
            openStatus: "开"
          },
        ];
        this.doorRecordVisible = true;
      },
      closeDialog() {
        this.solveDetail = {};
        this.solveDetailVisible = false;
        this.onLoad(this.page,this.query);
      },
      select(deviceType,params={}) {
        this.$refs.crud.searchForm.deviceType = deviceType;
        this.query.deviceType = deviceType;
        this.onLoad(this.page,this.query);
      },
      handleSolve(row) {
        this.solveDetail = row;
        this.solveDetailVisible = true;
        this.$nextTick(()=>{
          this.$refs.deviceJob.init()
        })
      },
      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {

        if (params.jobStatus == 0) {
          params.jobStatus = null;
        }
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage) {
        this.page.currentPage = currentPage;
        this.onLoad(this.page,this.query);
      },
      sizeChange(pageSize) {
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        this.countOption = iconsOption(this)
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
        // debugger
        getDeviceJobStaticVo().then(res => {
          this.deviceJobStaticVo = res.data.data;
          this.countOption.data[0].count = this.deviceJobStaticVo.smokeCount;
          this.countOption.data[1].count = this.deviceJobStaticVo.gasCount;
          this.countOption.data[2].count = this.deviceJobStaticVo.coverCount;
          this.countOption.data[3].count = this.deviceJobStaticVo.alarmCount;
          this.countOption.data[4].count = this.deviceJobStaticVo.lampCount;
          this.countOption.data[5].count = this.deviceJobStaticVo.xfCount;
          this.countOption.data[6].count = this.deviceJobStaticVo.trashbinCount;
          this.countOption.data[7].count = this.deviceJobStaticVo.parkCount;
          this.countOption.data[8].count = this.deviceJobStaticVo.gateCount;
          // console.log(this.deviceJobStaticVo.alarmCount)
        });
      }
    }
  };
</script>

<style>
</style>
