<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="nurseStationId">
        <el-select
          v-model="queryParams.nurseStationId"
          placeholder="请选择护理站"
          @keyup.enter.native="handleQuery"
          @change="handleQuery"
        >
          <el-option
            v-for="item in nurseStationlist"
            :key="item.id"
            :label="item.nurseStationName"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="护理人员名称" prop="nursePersonName">
        <el-button type class="stationbtn" @click="nursePersonNameclick(true)">
          {{
          queryParams.nursePersonName
          }}
        </el-button>
        <!-- <el-input
          v-model="queryParams.nursePersonName"
          placeholder="请输入护理人员名称"
          clearable
          @keyup.enter.native="handleQuery"
        />-->
      </el-form-item>
      <el-form-item label="选择月份" prop="monthTime">
        <el-date-picker
          v-model="queryParams.monthTime"
          type="month"
          placeholder="选择月份"
          @change="monthtimechange"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="选择日期区间" prop="monthStartTime">
        <el-date-picker
          v-model="valuetime"
          type="daterange"
          align="right"
          @change="monthStartEndTimechange"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :picker-options="pickerOptions"
        ></el-date-picker>
      </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>
    <!-- <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>-->
    <el-table v-loading="loading" :data="revenueList" @selection-change="handleSelectionChange">
      <el-table-column label="护理站名称" align="center" prop="nurseStationName" />
      <el-table-column label="护理人员名称" align="center" prop="nursePersonName" />
      <el-table-column label="总订单" align="center" prop="orderCount" />
      <el-table-column label="总收益" align="center" prop="orderTotalPrice" />
      <el-table-column label="本月订单" align="center" prop="monthOrderCount" />
      <el-table-column label="本月订单收益" align="center" prop="monthOrderTotalPrice" />
      <el-table-column label="今日订单" align="center" prop="todayOrderCount" />
      <el-table-column label="今日订单收益" align="center" prop="todayOrderPrice" />
      <!-- <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-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:revenue:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:revenue: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="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="护理站人员表id" prop="nurseStationPersonId">
          <el-input v-model="form.nurseStationPersonId" placeholder="请输入护理站人员表id" />
        </el-form-item>
        <el-form-item label="预约订单表id" prop="appointmentOrderId">
          <el-input v-model="form.appointmentOrderId" placeholder="请输入预约订单表id" />
        </el-form-item>
        <el-form-item label="收益金额" prop="revenueAmount">
          <el-input v-model="form.revenueAmount" placeholder="请输入收益金额" />
        </el-form-item>
        <el-form-item label="预约订单完成时间" prop="finishOrderTime">
          <el-date-picker
            clearable
            v-model="form.finishOrderTime"
            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>
    <!-- //选择人员 -->
    <el-dialog
      title="选择护理站"
      :visible.sync="nursePersonshow"
      width="1000px"
      append-to-body
      :before-close="nursePersoncancel"
    >
      <el-form ref="queryForm" :model="nursePersonqueryParams" :inline="true">
        <el-form-item label="护理人员名称" prop="nursePersonName" label-width="120">
          <el-input
            v-model="nursePersonqueryParams.nursePersonName"
            placeholder="请输入护理人员名称"
            clearable
          />
        </el-form-item>
        <!-- <el-form-item label="护理人员编号" prop="nursePersonCode" label-width="120">
          <el-input
            v-model="nursePersonqueryParams.nursePersonCode"
            placeholder="请输入护理人员编号"
            clearable
          />
        </el-form-item>-->
        <el-form-item>
          <el-button
            type="primary"
            icon="el-icon-search"
            size="mini"
            @click="nursePersonhandleQuery"
          >搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="nursePersonreset">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="nursePersonlist" @cell-dblclick="nursePersonclick">
        <el-table-column label="请选择" width="100" align="center">
          <template slot-scope="scope">
            <el-button
              type="primary"
              style="width: 15px; height: 15px"
              v-if="nursePersonid == scope.row.id"
              circle
              @click="nursePersonclick(scope.row)"
            ></el-button>
            <el-button
              v-else
              style="width: 15px; height: 15px"
              circle
              @click="nursePersonclick(scope.row)"
            ></el-button>
          </template>
        </el-table-column>
        <el-table-column property="nurseStationName" label="所属护理站" align="center"></el-table-column>
        <el-table-column property="nursePersonCode" label="护理人员编号" align="center"></el-table-column>
        <el-table-column property="nursePersonName" label="护理人员名称" align="center"></el-table-column>
        <el-table-column property="phone" label="联系电话" align="center"></el-table-column>
        <el-table-column property="address" label="护理站地址" align="center"></el-table-column>
      </el-table>
      <pagination
        v-show="nursePersontotal > 0"
        :total="nursePersontotal"
        :page.sync="nursePersonqueryParams.pageNum"
        :limit.sync="nursePersonqueryParams.pageSize"
        @pagination="nursePersoninfo"
      />
    </el-dialog>
  </div>
</template>

<script>
import {
  listRevenue,
  getRevenue,
  delRevenue,
  addRevenue,
  updateRevenue
} from "@/api/system/revenue.js";
import { getListByUser } from "@/api/system/userlist.js";
import { listPerson } from "@/api/system/person";
export default {
  name: "Revenue",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 护理员订单佣金收益信息表格数据
      revenueList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        nurseStationPersonId: null,
        appointmentOrderId: null,
        revenueAmount: null,
        finishOrderTime: null,
        monthTime: null,
        monthStartTime: null, //开始时间
        monthEndTime: null //结束时间
      },
      //护理站请求参数
      nurseStationqueryParams: {
        pageNum: 1,
        pageSize: 10
      },
      //人员请求
      nursePersonqueryParams: {
        pageNum: 1,
        pageSize: 10
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      //选择人员
      nursePersonshow: false,
      //护理站list
      nurseStationlist: [],
      //选择人员
      nursePersonshow: false,
      //人员list
      nursePersonlist: [],
      nursePersontotal: 0,
      nursePersonid: null,
      pickerOptions: {
        shortcuts: [
          {
            text: "最近一周",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近一个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
              picker.$emit("pick", [start, end]);
            }
          },
          {
            text: "最近三个月",
            onClick(picker) {
              const end = new Date();
              const start = new Date();
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
              picker.$emit("pick", [start, end]);
            }
          }
        ]
      },
      valuetime: ""
    };
  },
  created() {
    this.nurseStationinfo();
    this.nursePersoninfo();
  },
  methods: {
    //选择时期区间
    monthStartEndTimechange(e) {
      if (!this.queryParams.monthTime) {
        this.queryParams.monthStartTime = this.formatDate(
          new Date(e[0]).getTime()
        );
        this.queryParams.monthEndTime = this.formatDate(
          new Date(e[1]).getTime()
        );
      } else {
        this.queryParams.monthStartTime = null;
        this.queryParams.monthEndTime = null;
        this.valuetime = "";
      }
    },
    //选择时期
    monthtimechange(e) {
      if (!this.queryParams.monthStartTime) {
        this.queryParams.monthTime = this.formatDate(new Date(e).getTime());
      } else {
        this.queryParams.monthTime = "";
      }
    },
    formatDate(time, timetype) {
      let date = new Date(time); //13位时间戳
      //let date = new Date(parseInt(time) * 1000); //10位时间戳
      let y = date.getFullYear();
      let MM = date.getMonth() + 1;
      MM = MM < 10 ? "0" + MM : MM;
      let d = date.getDate();
      d = d < 10 ? "0" + d : d;
      return y + "-" + MM + "-" + d;
    },
    /** 查询护理员订单佣金收益信息列表 */
    getList() {
      this.loading = true;
      listRevenue(this.queryParams).then(response => {
        this.revenueList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        nurseStationPersonId: null,
        appointmentOrderId: null,
        revenueAmount: null,
        finishOrderTime: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.pageNum = 1;
      this.queryParams.pageSize = 10;
      this.queryParams.nursePersonName = null;
      this.queryParams.nurseStationPersonId = null;
      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;
      getRevenue(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) {
            updateRevenue(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRevenue(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 delRevenue(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "system/revenue/export",
        {
          ...this.queryParams
        },
        `revenue_${new Date().getTime()}.xlsx`
      );
    },
    //护理站请求
    nurseStationinfo() {
      getListByUser(this.nurseStationqueryParams).then(res => {
        this.nurseStationlist = res.rows;
        this.nurseStationtotal = res.total;
        this.queryParams.nurseStationId = res.rows[0].id;
        this.queryParams.nurseStationName = res.rows[0].nurseStationName;
        this.getList();
      });
    },
    /** 护理人员重置按钮操作 */
    nursePersoncancel() {
      this.nursePersonshow = false;
      this.nursePersonreset();
    },
    //护理人员重置
    nursePersonreset() {
      this.nursePersonqueryParams = {
        pageNum: 1,
        pageSize: 10
      };
      this.nursePersoninfo();
    },
    //护理人员请求
    nursePersoninfo() {
      listPerson(this.nursePersonqueryParams).then(res => {
        res.rows.forEach(e => {
          if (e.modifyCheckStatus == "CHECKED") {
            e.modifyCheckStatus = true;
          } else {
            e.modifyCheckStatus = false;
          }
        });
        this.nursePersonlist = res.rows;
        this.nursePersontotal = res.total;
      });
    },
    //护理人员搜索
    nursePersonhandleQuery() {
      this.nursePersonqueryParams.pageNum = 1;
      this.nursePersoninfo();
    },
    //护理人员click
    nursePersonclick(row) {
      this.nursePersonid = row.id;
      this.queryParams.nursePersonName = row.nursePersonName;
      this.queryParams.nurseStationPersonId = row.id;
      this.nursePersonshow = false;
    },
    //form点击护理人员
    nursePersonNameclick() {
      this.nursePersonid = this.queryParams.nurseStationPersonId;
      this.nursePersonshow = true;
    }
  }
};
</script>
<style scoped>
.stationbtn {
  width: 208px;
  text-align: left;
  height: 32px;
  overflow: hidden;
  font-size: 14px;
}
</style>
