<template>
  <div  class="app-container">
    <!-- 列表 -->
    <el-card class="box-card">
      <div class="search">
        <el-button-group style="margin-right: 15px">
          <el-button  type="primary" @click="dialogVisible = true" class="addbtn">新增排班</el-button>
        </el-button-group>
        <el-button-group>
          <el-button
            type="success"
            icon="el-icon-upload2"
            @click="handleImport"
          >导入排班</el-button>
        </el-button-group>
      </div>
      <!-- 信息 -->
      <el-tabs type="border-card" v-model="activeName2" @tab-click="handleClick" style="margin-top: 20px;">
        <el-tab-pane label="本周值班" name="first">
          <el-col :span="8" v-for="(item,index) in dutyList">
            <el-card class="box-card">
              <div slot="header" class="clearfix">
                <span>{{item.week}}值班</span>
              </div>
              <el-table :data="[item]" border>
                <el-table-column prop="time" label="日期">
                </el-table-column>
                <el-table-column
                  prop="leader"
                  label="带班领导"
                >
                </el-table-column>
                <el-table-column
                  prop="am"
                  label="上午"
                >
                </el-table-column>
                <el-table-column
                  prop="noon"
                  label="下午">
                </el-table-column>
                <el-table-column
                  prop="pm"
                  label="晚上">
                </el-table-column>
              </el-table>
            </el-card>
          </el-col>
        </el-tab-pane>
        <el-tab-pane label="往期值班" name="second">
          <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-form :model="queryParams" ref="queryForm" :inline="true">
                <el-form-item label="选择日期" prop="time">
                  <el-date-picker clearable size="small" style="width: 200px"
                                  v-model="queryParams.beginTime"
                                  type="date"
                                  value-format="yyyy-MM-dd"
                                  placeholder="选择开始时间">
                  </el-date-picker>
                  至
                  <el-date-picker clearable size="small" style="width: 200px"
                                  v-model="queryParams.endTime"
                                  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-item  style="margin-right: 0px;"  v-if="queryParams.id == null">
                  <el-button type="success" icon="el-icon-s-claim" size="mini" @click="handleQuery1">上月值班</el-button>
                </el-form-item>
                <el-form-item  v-if="queryParams.id == '1'">
                  <el-button type="success" icon="el-icon-s-claim" size="mini" @click="resetQuery">本月值班</el-button>
                </el-form-item>
              </el-form>
            </el-col>
            <el-col :span="24">
              <el-card class="box-card">
                <div slot="header" class="clearfix">
                  <span>{{queryParams.id == '1' ? '上月' : '本月'}}值班</span>
                </div>
                <el-table  :data="dqselMonth" border>
                  <el-table-column
                    type="index"
                    align="center"
                    label="序号" width="50"
                  >
                  </el-table-column>
                  <el-table-column
                    prop="time"
                    label="日期"
                  >
                  </el-table-column>
                  <el-table-column
                    prop="week"
                    label="星期"
                  >
                  </el-table-column>
                  <el-table-column
                    prop="leader"
                    label="带班领导"
                  >
                  </el-table-column>
                  <el-table-column
                    prop="am"
                    label="上午值班"
                  >
                  </el-table-column>
                  <el-table-column
                    prop="noon"
                    label="下午值班">
                  </el-table-column>
                  <el-table-column
                    prop="pm"
                    label="晚上值班">
                  </el-table-column>
                </el-table>
                <pagination
                  v-show="total>0"
                  :total="total"
                  :page.sync="queryParams.pageNum"
                  :limit.sync="queryParams.pageSize"
                  @pagination="getselMonth"
                />
<!--                <pagination-->
<!--                  v-show="total>0" getlistTime-->
<!--                  :total="total"-->
<!--                  :page.sync="queryParams.pageNum"-->
<!--                  :limit.sync="queryParams.pageSize"-->
<!--                  v-if="flag = true"-->
<!--                  @pagination="getselMonth"-->
<!--                />-->
              </el-card>
            </el-col>
          </el-row>
        </el-tab-pane>
      </el-tabs>

    </el-card>
    <!-- 添加信息 -->
    <el-dialog title="添加排班" :visible.sync="dialogVisible" width="55%" @close="dialogClosed">
      <el-form label-width="120px" :model="form"  ref="formRef">
        <el-form-item label="值班日期" prop="time">
          <el-date-picker
            v-model="form.time"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
                <el-form-item label="值班领导" prop="leader">
                  <el-input v-model="form.leader" placeholder="请选择带班领导" clearable="true" readonly>
                    <template slot="append">
                      <el-button
                        type="primary"
                        icon="el-icon-plus"
                        size="mini"
                        @click="handleSelect" >
                        选择人员</el-button>
                    </template>
                  </el-input>
                </el-form-item>
        <el-form-item label="上午值班人员" prop="am">
          <el-input v-model="form.am" placeholder="请选择上午值班人员" clearable="true" readonly>
            <template slot="append">
              <el-button
                type="primary"
                icon="el-icon-plus"
                size="mini"
                @click="handleSelect1" >
                选择人员</el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="下午值班人员" prop="noon">
          <el-input v-model="form.noon" placeholder="请选择下午值班人员" clearable="true" readonly>
            <template slot="append">
              <el-button
                type="primary"
                icon="el-icon-plus"
                size="mini"
                @click="handleSelect2" >
                选择人员</el-button>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item label="晚上值班人员" prop="pm">
          <el-input v-model="form.pm" placeholder="请选择晚上值班人员" clearable="true" readonly>
            <template slot="append">
              <el-button
                type="primary"
                icon="el-icon-plus"
                size="mini"
                @click="handleSelect3" >
                选择人员</el-button>
            </template>
          </el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 选择人员对话框 -->
    <select-user-by-dept ref="selectUserByDept" :title="title" :open="open" :inputName="receiverName" :inputAccount="receiverAccount" append-to-body @ok="selectOK"/>
    <select-user-by-dept ref="selectUserByDept1" :title="title" :open="open1" :inputName="receiverName" :inputAccount="receiverAccount" append-to-body @ok="selectOK1"/>
    <select-user-by-dept ref="selectUserByDept2" :title="title" :open="open2" :inputName="receiverName" :inputAccount="receiverAccount" append-to-body @ok="selectOK2"/>
    <select-user-by-dept ref="selectUserByDept3" :title="title" :open="open3" :inputName="receiverName" :inputAccount="receiverAccount" append-to-body @ok="selectOK3"/>

    <!-- 用户导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
<!--          <el-checkbox v-model="upload.updateSupport" />是否更新已经存在的用户数据-->
          <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link>
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>

  import { listDuty,getDuty,addDuty,updateDuty,selMonth,listTime,exporDuty,importTemplate} from "@/api/duty/duty";
  import SelectUserByDept from "@/components/SelectRen/selectUserByDept";
  import { getToken } from "@/utils/auth";

  export default {
    components: {SelectUserByDept},
    data() {
      return {
        flag:false,
        // 用户导入参数
        upload: {
          // 是否显示弹出层（用户导入）
          open: false,
          // 弹出层标题（用户导入）
          title: "",
          // 是否禁用上传
          isUploading: false,
          // 是否更新已经存在的用户数据
          updateSupport: 0,
          // 设置上传的请求头部
          headers: { Authorization: "Bearer " + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/system/duty/importData"
        },
        leader: "",
        am:'',
        noon:'',
        pm:'',
        noull:'1',
        receiverAccount: "",
        // 遮罩层
        loading: true,
        // 总条数
        total: 0,
        totaldq: 0,
        // 【请填写功能名称】表格数据
        dutyList: [],
        dqselMonth:[],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        open1: false,
        open2: false,
        open3: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          time: undefined,
          leader: undefined,
          am: undefined,
          noon: undefined,
          pm: undefined,
          applyUser: undefined,
          id:undefined,
          beginTime:undefined,
          endTime:undefined
        },
        activeName2: 'first',
        base: "",
        monday: "",
        today: "",
        dateFormat2: "",
        dateFormat8: "",
        nextMonday: "",
        lastsunday: "",
        queryInfo: {
          query: "",
          pagenum: 1,
          pagesize: 10,
          beginTime:undefined,
          endTime:undefined
        },
        role: false,
        homes: [],
        workers: [],
        dialogVisible: false,
        form: {},
        formRules: {
          time: [
            { required: true, message: "日期不能为空", trigger: "blur" }
          ],
          leader:[{
            required:true,
            message:'请选择值班领导',
            trigger:'blur'
          }],
          am:[{
            required:true,
            message:'请选择上午值班人员',
            trigger:'blur'
          }],
          noon:[{
            required:true,
            message:'请选择下午值班人员',
            trigger:'blur'
          }],
          pm:[{
            required:true,
            message:'请选择晚上值班人员',
            trigger:'blur'
          }],
        }
      };
    },
    created() {
      this.getList();
      this.getselMonth();
      // this.getlistTime();
    },
    watch: {},
    mounted() {
    },
    methods: {
      /** 导入按钮操作 */
      handleImport() {
        this.upload.title = "用户导入";
        this.upload.open = true;
      },
      /** 下载模板操作 */
      importTemplate() {
        importTemplate().then(response => {
          this.download(response.msg);
        });
      },
      // 文件上传中处理
      handleFileUploadProgress(event, file, fileList) {
        this.upload.isUploading = true;
      },
      // 文件上传成功处理
      handleFileSuccess(response, file, fileList) {
        this.upload.open = false;
        this.upload.isUploading = false;
        this.$refs.upload.clearFiles();
        // this.msgSuccess("导入成功");
        this.$alert(response.msg, "导入结果", { dangerouslyUseHTMLString: true });
        this.dutyList = [];
        this.getList();
      },
      // 提交上传文件
      submitFileForm() {
        this.$refs.upload.submit();
      },

      handleSelect() {
        this.$refs.selectUserByDept.childOpen=true;
        this.open = true;
        this.title = "选择值班领导";


        this.$refs.selectUserByDept1.childOpen=false;
        this.$refs.selectUserByDept2.childOpen=false;
        this.$refs.selectUserByDept3.childOpen=false;
        this.open1 = false;
        this.open2 = false;
        this.open3 = false;
      },
      handleSelect1() {
        this.$refs.selectUserByDept1.childOpen=true;
        this.open1 = true;
        this.title = "选择上午值班人员";


        this.$refs.selectUserByDept.childOpen=false;
        this.$refs.selectUserByDept2.childOpen=false;
        this.$refs.selectUserByDept3.childOpen=false;
        this.open = false;
        this.open2 = false;
        this.open3 = false;
      },
      handleSelect2() {
        this.$refs.selectUserByDept2.childOpen=true;
        this.open2 = true;
        this.title = "选择下午值班人员";


        this.$refs.selectUserByDept1.childOpen=false;
        this.$refs.selectUserByDept.childOpen=false;
        this.$refs.selectUserByDept3.childOpen=false;
        this.open1 = false;
        this.open = false;
        this.open3 = false;
      },
      handleSelect3() {
        this.$refs.selectUserByDept3.childOpen=true;
        this.open3 = true;
        this.title = "选择晚上值班人员";


        this.$refs.selectUserByDept1.childOpen=false;
        this.$refs.selectUserByDept2.childOpen=false;
        this.$refs.selectUserByDept.childOpen=false;
        this.open1 = false;
        this.open2 = false;
        this.open = false;
      },
      selectOK(zh, xm) {
        this.leader=xm;
        // this.form.leader = this.leader;
        this.form.leader = xm
        this.open=false;
      },
      selectOK1(zh, xm) {
        this.am=xm;
        // this.form.am = this.am;
        this.form.am = xm;
        this.open1=false;
      },
      selectOK2(zh, xm) {
        this.noon=xm;
        // this.form.noon = this.noon;
        this.form.noon = xm;
        this.open2=false;
      },
      selectOK3(zh, xm) {
        this.pm=xm;
        // this.form.pm = this.pm;
        this.form.pm = xm;
        this.open3=false;
      },
      cancel() {
        this.form = {};
        this.dialogVisible = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          time: undefined,
          leader: undefined,
          am: undefined,
          noon: undefined,
          pm: undefined,
          createTime: undefined,
          applyUser: undefined
        };
        this.resetForm("form");
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["formRef"].validate(valid => {
          if (valid) {
              addDuty(this.form).then(response => {
                if (response.code === 200) {
                  this.msgSuccess("新增成功");
                  console.log(this.form)
                  this.dialogVisible = false;
                  this.form = {};
                  this.dutyList = [];
                  this.getList();
                  this.getselMonth();
                }else{
                  this.msgError("添加失败");
                }
              });
            }
        });
      },
      getList(){
        this.loading = true;
        listDuty(this.queryParams).then(response => {
          let arr = response.rows;
          for(var i =0; i < arr.length;i++){
            this.dutyList.push(
              arr[i]
            )
          };

          // this.total = response.total;
          this.loading = false;
          console.log(this.dutyList)
        })

      },
      // getlistTime(){
      //   this.loading = true;
      //   listTime(this.queryParams).then(response => {
      //     this.dqselMonth = response.rows
      //     this.total = response.total;
      //     // this.total = response.rows.length;
      //     this.loading = false;
      //   })
      // },
      getselMonth() {
        this.loading = true;
        selMonth(this.queryParams).then(response => {
          console.log(this.queryParams)
          this.dqselMonth = response.rows
          console.log(response)

          this.total = response.total;
          // this.total = response.rows.length;
          this.loading = false;
        })
      },

      handleQuery(){
        this.flag = true;
        this.queryParams.pageNum = 1;
        this.getselMonth();
      },
      handleQuery1(){
        this.queryParams.id = 1
        this.loading = true;
        selMonth(this.queryParams).then(response => {
          console.log(this.queryParams)
          this.dqselMonth = response.rows
          console.log(response.rows);
          this.total = response.total;
          this.loading = false;
        })
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.flag = false;
        this.queryParams.pageNum = 1;
        this.queryParams.id = undefined;
        this.queryParams.beginTime = undefined;
        this.queryParams.endTime = undefined;
        this.getselMonth();
      },
      /** 导出按钮操作 */
      handleExport() {
        const queryParams = this.queryParams;
        this.$confirm('是否确认导出所有用户数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportUser(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
      },
      /** 导入按钮操作 */
      handleImport() {
        this.upload.title = "用户导入";
        this.upload.open = true;
      },



    }
  };
</script>
<style>
  .el-card__header{background: #cae6ff;}
  .el-row {
    margin-top: 20px;
    margin-bottom: 20px;
  &:last-child {
     margin-bottom: 0;
   }
  }
  .el-col {
    border-radius: 4px;
    padding:10px;
  }
  .bg-purple-dark {
    background: #99a9bf;
  }
  .bg-purple {
    background: #d3dce6;
  }
  .bg-purple-light {
    background: #e5e9f2;
  }
  .grid-content {
    border-radius: 4px;
    min-height: 36px;
  }
  .row-bg {
    padding: 10px 0;
    background-color: #f9fafc;
  }
</style>
