<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="变化原因" prop="reason">
        <el-input
          v-model="queryParams.reason"
          placeholder="请输入变化原因"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="操作用户" prop="operatorName">
        <el-input
          v-model="queryParams.operatorName"
          placeholder="请输入操作用户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户类型" prop="operatorType">
        <el-select v-model="queryParams.operatorType" placeholder="请选择用户类型" @change="handleQuery">
          <el-option
            v-for="dict in dict.type.ss_record_time_operator_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          ></el-option>
        </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="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ss:time:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="timeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="记录id" align="center" prop="id" width="100"/>-->
      <el-table-column label="操作时间" align="center" prop="operatorTime" width="180"/>
      <el-table-column label="变化类型" align="center" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.record_time_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="变化值" align="center" prop="amount" >
        <template slot-scope="d">
          <span v-if="d.row.amount >= 0" class="plus">+{{descTime(d.row).text}}</span>
          <span v-else class="subtract">{{descTime(d.row).text}}</span>
        </template>
      </el-table-column>
      <el-table-column label="变化原因" align="center" prop="reason" />
      <el-table-column label="操作人" align="center" prop="operatorName">
        <template slot-scope="d">
          <span v-if="d.row.operatorType === RecordTimeOperatorType.USER">
            <user-link :id="d.row.operatorId" :name="d.row.operatorName"/>
          </span>
          <span v-else>{{d.row.operatorName}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作人类型" align="center" prop="operatorType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ss_record_time_operator_type" :value="scope.row.operatorType"/>
        </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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备id" prop="deviceId">
          <el-input v-model="form.deviceId" placeholder="请输入设备id" />
        </el-form-item>
        <el-form-item label="时长变化量" prop="amount">
          <el-input v-model="form.amount" placeholder="请输入时长变化量" />
        </el-form-item>
        <el-form-item label="变化原因" prop="reason">
          <el-input v-model="form.reason" placeholder="请输入变化原因" />
        </el-form-item>
        <el-form-item label="用户名称" prop="operatorName">
          <el-input v-model="form.operatorName" placeholder="请输入操作人名称" />
        </el-form-item>
        <el-form-item label="操作人类型" prop="operatorType" label-width="6em">
          <el-select v-model="form.operatorType" placeholder="请选择操作人类型">
            <el-option
              v-for="dict in dict.type.ss_record_time_operator_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="操作时间" prop="operatorTime">
          <el-date-picker clearable
            v-model="form.operatorTime"
            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>
  </div>
</template>

<script>
import { listTime, getTime, delTime, addTime, updateTime } from "@/api/ss/time";
import { $view } from '@/utils/mixins'
import { toDescriptionFromSecond } from '@/utils/date'
import { RecordTimeOperatorType, RecordTimeType } from '@/utils/constants'
import UserLink from '@/components/Business/SmUser/UserLink.vue'

export default {
  name: "RecordTime",
  components: { UserLink },
  mixins: [$view],
  dicts: ['ss_record_time_operator_type', 'record_time_type'],
  props: {
    query: {
      type: Object,
      default: () => {}
    }
  },
  computed: {
    RecordTimeOperatorType() {
      return RecordTimeOperatorType
    },
    descTime() {
      return (row) => {
        if (RecordTimeType.TIME === row.type) {
          return toDescriptionFromSecond(row.amount);
        } else {
          return {
            text: row.amount + '度'
          }
        }
      }
    }
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备时长变化记录表格数据
      timeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        reason: null,
        operatorName: null,
        operatorType: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deviceId: [
          { required: true, message: "设备id不能为空", trigger: "blur" }
        ],
        amount: [
          { required: true, message: "时长变化量不能为空", trigger: "blur" }
        ],
        operatorTime: [
          { required: true, message: "操作时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.queryParams = {
      ...this.queryParams,
      ...this.query
    }
    this.getList();
  },
  methods: {
    /** 查询设备时长变化记录列表 */
    getList() {
      this.loading = true;
      listTime(this.queryParams).then(response => {
        this.timeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        deviceId: null,
        amount: null,
        reason: null,
        operatorId: null,
        operatorName: null,
        operatorType: null,
        operatorTime: 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
      getTime(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) {
            updateTime(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTime(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 delTime(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ss/time/export', {
        ...this.queryParams
      }, `time_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
