<template>
  <view class="month-container">
    <u-navbar
      class="u-navbar"
      :title="monthPlanTitle"
      placeholder
      @leftClick="leftClick"
      leftIconColor="#fff"
      bgColor="#00337A"
      :titleStyle="{ color: '#FFF', fontSize: '32rpx' }"
    />

    <view class="month-content">
      <view class="btn-header" id="btnHeaderRef">
        <button
          :key="index"
          @tap="onTapCheck(index)"
          v-for="(item, index) in btnList"
          :class="{ active: index === activeIndex }"
        >
          {{ item.btn_title }}
        </button>
      </view>

      <!-- 发起提交 -->
      <view class="content-2" v-show="activeIndex === 1">
        <view class="pages-add" id="pagesAddRef">
          <view style="text-align: right; width: 30%">
            <image
              style="width: 50rpx; height: 50rpx; margin: 13rpx 20rpx 13rpx 0"
              src="../../../static/images/workPlan/delete.png"
              @tap="onReducePages"
            ></image>
          </view>
          <view class="num-content">
            <!-- <uni-pagination :show-icon="true" :total="pagesTotal" :pageSize="1" /> -->

            <scroll-view scroll-x="true" class="scroll-x" scroll-with-animation="true">
              <view
                :class="{ 'active-page': item == currentSwiper + 1 }"
                v-for="item in pagesTotal"
                :key="item"
                @tap="onChangePages(item)"
              >
                {{ item }}
              </view>
            </scroll-view>
          </view>
          <view style="text-align: left; width: 30%">
            <image
              style="width: 50rpx; height: 50rpx; margin: 13rpx 0 13rpx 20rpx"
              src="../../../static/images/workPlan/add.png"
              @tap="onAddPages"
            ></image>
          </view>
        </view>
        <view>
          <uni-forms
            class="form-container"
            ref="form"
            :modelValue="formData"
            :rules="rules"
            label-position="top"
            label-width="200"
          >
            <uni-forms-item label="工程名称" require style="margin-bottom: 2rpx">
              <view class="time-container" @tap="onSelectProject">
                <view>请选择</view>
                <text>&gt;</text>
              </view>
            </uni-forms-item>
            <!-- <uni-forms-item label="选择时间" required style="margin-bottom: 2rpx">
              <view>
              </view>
            </uni-forms-item> -->
          </uni-forms>
        </view>
        <view class="week-container">
          <uni-icons
            type="left"
            size="20"
            @click="changeWeek(-1)"
            style="margin-left: 40rpx; font-weight: bold"
          ></uni-icons>
          <text class="week-container">{{ weekStart }}~{{ weekEnd }}</text>
          <uni-icons
            type="right"
            size="20"
            @click="changeWeek(1)"
            style="margin-right: 40rpx; font-weight: bold"
          ></uni-icons>
        </view>

        <!-- <scroll-view :scroll-y="true" style="height: 340px">
         
        </scroll-view> -->

        <view style="background-color: #fff">
          <swiper circular :autoplay="false" :current="currentSwiper" style="height: 700rpx" @change="onSwiperChange">
            <swiper-item v-for="item in pagesTotal" :key="item">
              <scroll-view scroll-y="true" class="scroll-Y" style="height: 100%">
                <uni-forms
                  class="form-container"
                  ref="form"
                  :modelValue="formData"
                  :rules="rules"
                  label-position="top"
                  label-width="200"
                >
                  <uni-forms-item
                    v-for="(item, index) in monthPlanTitle === '基建周计划'
                      ? infrastructureFormLabel
                      : productionFormLabel"
                    :key="index"
                    :label="item.form_label"
                    name="name"
                    required
                  >
                    <uni-easyinput
                      v-if="item.items_type === 'ipt'"
                      type="text"
                      v-model="formData.name"
                      placeholder="请填写"
                    />
                    <uni-data-select
                      v-model="formData.nr"
                      :localdata="item.options"
                      v-if="item.items_type === 'sel'"
                    ></uni-data-select>
                  </uni-forms-item>
                </uni-forms>
              </scroll-view>
            </swiper-item>
          </swiper>

          <!-- <view class="submit-btn">
            <button>导入计划</button>
            <button @tap="onSaveSubmit">保存提交</button>
          </view> -->
        </view>

        <view class="bottom-btn">
          <view>
            <uni-icons type="cloud-upload" size="20"></uni-icons>
            导入计划
          </view>
          <view>保存提交</view>
        </view>
      </view>

      <!-- 查看报表 -->
      <view v-show="activeIndex === 2" class="common-style">
        <uni-forms ref="form-b" label-position="top" label-width="200">
          <uni-forms-item label="工程名称" required>
            <view class="time-container" @tap="onSelectProject">
              <view>请选择</view>
              <text>&gt;</text>
            </view>
          </uni-forms-item>
        </uni-forms>

        <view class="icon-search-time">
          <uni-icons
            type="left"
            size="20"
            @click="changeWeek(-1)"
            style="margin-right: 16rpx; font-weight: bold"
          ></uni-icons>
          <text class="week-container">{{ weekStart }}~{{ weekEnd }}</text>
          <uni-icons
            type="right"
            size="20"
            @click="changeWeek(1)"
            style="margin-left: 16rpx; font-weight: bold"
          ></uni-icons>

          <button @tap="onBackToday">返回本周</button>
        </view>
      </view>

      <!-- 查看数据 -->
      <view v-show="activeIndex === 3" class="common-style">
        <uni-easyinput
          prefixIcon="search"
          v-model="searchValue"
          placeholder="搜索"
          @iconClick="iconClick"
        ></uni-easyinput>

        <uni-forms ref="form-b" label-position="top" label-width="200">
          <uni-forms-item label="工程名称" required>
            <view class="time-container" @tap="onViewSelectProject">
              <view>请选择</view>
              <text>&gt;</text>
            </view>
          </uni-forms-item>
          <uni-forms-item label="全部状态" required>
            <view class="time-container" @tap="onSelectAllType">
              <view>请选择</view>
              <text>&gt;</text>
            </view>
          </uni-forms-item>
        </uni-forms>
      </view>
    </view>

    <!-- 弹框 -->
    <uni-popup ref="popup" background-color="#fff">
      <view class="popup-content" v-if="popupType === 'center'">
        <view class="flex-style">
          <text style="font-size: 16px">选择工程</text>
          <uni-icons type="closeempty" size="30" @tap="onClosePopup"></uni-icons>
        </view>
        <view>{{ projectQueryParams.planTime }}</view>
        <view class="search">
          <view class="select">
            <uni-data-select
              v-model="projectQueryParams.state"
              @change="onProjectChange"
              :localdata="projectRange"
            ></uni-data-select>
          </view>
          <button @tap="getProjectListData()">搜索</button>
        </view>
        <view class="project-content">暂无数据</view>
      </view>

      <view v-else class="popup-bottom-content">
        <view class="bottom-title">
          <text>请选择</text>
          <text>请选择</text>
          <uni-icons type="closeempty" size="20" @tap="onClosePopup"></uni-icons>
        </view>

        <view v-if="isType">
          <view class="select-item" v-for="item in isAUditingList" :key="item.title" @tap="onCheckAuditing(item.value)">
            {{ item.title }}
          </view>
        </view>
      </view>
    </uni-popup>
  </view>
</template>

<script>
import TimeSelect from './components/time-select'
import { getProjectListApi } from '../../../api/workPlan/monthPlan'
export default {
  components: {
    TimeSelect
  },
  data() {
    return {
      monthPlanTitle: '基建周计划',
      activeIndex: 0,
      pagesTotal: 1,
      currentPage: 0,
      popupType: '',
      scrollHeight: 500,
      formData: {},
      isType: false,
      currentTime: this.$moment().format('YYYY-MM'),
      btnList: [
        { btn_title: '工作待办' },
        { btn_title: '发起提交' },
        { btn_title: '查看报表' },
        { btn_title: '查看数据' }
      ],
      rules: {
        name: {
          rules: [{ required: true, errorMessage: '请填写' }]
        },
        gk: {
          rules: [{ required: true, errorMessage: '请填写' }]
        },
        jd: {
          rules: [{ required: true, errorMessage: '请填写' }]
        },
        nr: {
          rules: [{ required: true, errorMessage: '请填写' }]
        },
        dj: {
          rules: [{ required: true, errorMessage: '请填写' }]
        }
      },
      infrastructureFormLabel: [
        { form_label: '作业内容', items_type: 'sel' },
        { form_label: '风险等级', items_type: 'sel' },
        { form_label: '是否存在危大工程', items_type: 'sel' },
        { form_label: '是否有涉网计划', items_type: 'sel' },
        {
          form_label: '施工方案是否已审批',
          items_type: 'sel',
          options: [
            { value: 0, text: '是否需要办理工作票' },
            { value: 1, text: '甲、乙供物资到货是否满足作业条件' },
            { value: 2, text: '施工机具是否满足作业条件' },
            { value: 2, text: '视频监控是否满足作业条件' },
            { value: 2, text: '投入监控设备数(台)' }
          ]
        },
        {
          form_label: '是否需要办理工作票',
          items_type: 'sel',
          options: [
            { value: 0, text: '项目部' },
            { value: 1, text: '分公司' },
            { value: 2, text: '公司职能部门' },
            { value: 2, text: '公司领导' },
            { value: 2, text: '运检站' },
            { value: 2, text: '各单位级' }
          ]
        },
        {
          form_label: '甲、乙供物资到货是否满足作业条件',
          items_type: 'sel',
          options: [
            { value: 0, text: '是' },
            { value: 1, text: '否' }
          ]
        },
        {
          form_label: '施工机具是否满足作业条件',
          items_type: 'sel',
          options: [
            { value: 0, text: '是' },
            { value: 1, text: '否' }
          ]
        },
        {
          form_label: '视频监控是否满足作业条件',
          items_type: 'sel',
          options: [
            { value: 0, text: '是' },
            { value: 1, text: '否' }
          ]
        },
        {
          form_label: '投入监控设备数(台)',
          items_type: 'ipt'
        },
        { form_label: '公司职工人数', items_type: 'ipt' },
        { form_label: '分包人员人数', items_type: 'ipt' },
        { form_label: '项目总人数', items_type: 'ipt' },
        { form_label: '投入车辆(辆)', items_type: 'ipt' },
        { form_label: '现场负责人(电话)', items_type: 'ipt' },
        { form_label: '分包商名称', items_type: 'ipt' },
        { form_label: '备注', items_type: 'ipt' }
      ],
      productionFormLabel: [
        { form_label: '业主单位', items_type: 'ipt' },
        { form_label: '实施单位', items_type: 'ipt' },
        { form_label: '实施部门', items_type: 'ipt' },
        { form_label: '作业内容', items_type: 'sel' },
        {
          form_label: '风险等级',
          items_type: 'sel',
          options: [
            { value: 0, text: '检修' },
            { value: 1, text: '维护' }
          ]
        },
        { form_label: '计划编码', items_type: 'ipt' },

        {
          form_label: '是否开展现场勘探',
          items_type: 'sel',
          options: [
            { value: 0, text: '是' },
            { value: 1, text: '否' }
          ]
        },
        { form_label: '施工方案是否已审批', items_type: 'sel' },
        { form_label: '是否需要办理工作票', items_type: 'sel' },
        { form_label: '是否属于危大工程', items_type: 'sel' },
        {
          form_label: '停电需求',
          items_type: 'sel',
          options: [
            { value: 0, text: '项目部' },
            { value: 1, text: '分公司' },
            { value: 2, text: '公司职能部门' },
            { value: 2, text: '公司领导' },
            { value: 2, text: '运检站' },
            { value: 2, text: '各单位级' }
          ]
        },
        {
          form_label: '作业地点',
          items_type: 'ipt',
          options: [
            { value: 0, text: '可接受风险' },
            { value: 1, text: '低风险' },
            { value: 2, text: '中风险' },
            { value: 2, text: '高风险' },
            { value: 2, text: '特高风险' }
          ]
        },
        {
          form_label: '物资准备',
          items_type: 'sel',
          options: [
            { value: 0, text: '过程管控' },
            { value: 1, text: '巡视管控' },
            { value: 2, text: '驻点管控' },
            { value: 2, text: '节点管控' }
          ]
        },
        {
          form_label: '机具装备',
          items_type: 'sel',
          options: [
            { value: 0, text: '是' },
            { value: 1, text: '否' }
          ]
        },
        {
          form_label: '施工人数',
          items_type: 'ipt'
        },
        {
          form_label: '现场施工负责人',
          items_type: 'ipt'
        },
        {
          form_label: '现场施工安全员',
          items_type: 'ipt'
        },
        { form_label: '公司职工人数', items_type: 'ipt' },
        { form_label: '分包人员人数', items_type: 'ipt' },
        { form_label: '分包商', items_type: 'ipt' },
        { form_label: '投入车辆(辆)', items_type: 'ipt' },
        { form_label: '开始时间', items_type: 'sel' },
        { form_label: '结束时间', items_type: 'sel' },
        { form_label: '典型中高风险分部分项目工程', items_type: 'ipt' },
        { form_label: '开始时间', items_type: 'sel' },
        { form_label: '结束时间', items_type: 'sel' },
        { form_label: '作业地点', items_type: 'ipt' }
      ],
      currentSwiper: 0,
      projectQueryParams: {
        planTime: this.$moment().format('YYYY-MM-DD'),
        type: 3,
        state: ''
      },
      projectRange: [
        { value: '', text: '全部' },
        { value: '0', text: '未填报' },
        { value: '1', text: '已填报' }
      ],
      isAUditingList: [
        { title: '未审核', value: 0 },
        { title: '已审核', value: 1 }
      ],
      searchValue: '',
      weekStart: this.$moment().week(this.$moment().week()).startOf('week').format('YYYY-MM-DD'),
      weekEnd: this.$moment().week(this.$moment().week()).endOf('week').format('YYYY-MM-DD')
    }
  },
  mounted() {
    // console.log('this.$refs.btnHeaderRef', this.$refs.btnHeaderRef, this.$refs.pagesAddRef)
    const query = uni.createSelectorQuery()
    query
      .select('#btnHeaderRef')
      .boundingClientRect(data => {
        this.scrollHeight += data.height
      })
      .exec()
    query
      .select('#pagesAddRef')
      .boundingClientRect(data => {
        this.scrollHeight += data.height
      })
      .exec()

    // console.log('this.scrollHeight', this.scrollHeight)
    this.getProjectListData()
  },
  methods: {
    leftClick() {
      uni.navigateBack()
    },
    /* 获取工程数据 */
    async getProjectListData() {
      const res = await getProjectListApi(this.projectQueryParams)
      console.log('工程数据res', res)
    },
    onTapCheck(i) {
      this.activeIndex = i
    },
    onReducePages() {
      if (this.pagesTotal > 1) {
        this.pagesTotal -= 1
      }
    },
    onAddPages() {
      this.pagesTotal += 1
    },
    onSelectProject() {
      this.$refs.popup.open('center')
      this.popupType = 'center'
    },
    onClosePopup() {
      this.$refs.popup.close()
      if (this.isType) {
        this.isType = !this.isType
      }
    },
    onSwiperChange(e) {
      // this.currentPage = e.detail.current + 1

      this.currentSwiper = e.detail.current
    },
    onCheckAuditing(val) {
      console.log(val, '----')
    },
    /* 保存提交 */
    onSaveSubmit() {
      console.log('保存提交')
    },
    onChangePages(num) {
      this.currentSwiper = num - 1
      // if (num > 1) {
      //   console.log('居中')
      // }
    },
    onReduceMonth() {
      this.currentTime = this.$moment(this.currentTime).subtract(1, 'year').format('YYYY-MM')
    },
    onReduceDay() {
      this.currentTime = this.$moment(this.currentTime).subtract(1, 'months').format('YYYY-MM')
    },
    onAddMonth() {
      this.currentTime = this.$moment(this.currentTime).add(1, 'year').format('YYYY-MM')
    },
    onAddDay() {
      this.currentTime = this.$moment(this.currentTime).add(1, 'months').format('YYYY-MM')
    },
    onBackToday() {
      //   this.currentTime = this.$moment().format('YYYY-MM')

      this.weekStart = this.$moment().week(this.$moment().week()).startOf('week').format('YYYY-MM-DD')
      this.weekEnd = this.$moment().week(this.$moment().week()).endOf('week').format('YYYY-MM-DD')
    },
    iconClick() {
      console.log('搜索')
    },
    onChangeCurrentData(e) {
      this.currentTime = e.detail.value
    },
    onViewSelectProject() {
      this.$refs.popup.open('bottom')
      this.popupType = 'bottom'
    },
    onSelectAllType() {
      this.$refs.popup.open('bottom')
      this.popupType = 'bottom'
      this.isType = true
    },
    changeWeek(delta) {
      if (delta > 0) {
        this.weekStart = this.$moment(this.weekStart).add(7, 'days').format('YYYY-MM-DD')
        this.weekEnd = this.$moment(this.weekEnd).add(7, 'days').format('YYYY-MM-DD')
      } else {
        this.weekStart = this.$moment(this.weekStart).subtract(7, 'days').format('YYYY-MM-DD')
        this.weekEnd = this.$moment(this.weekEnd).subtract(7, 'days').format('YYYY-MM-DD')
      }
    }
  },
  onLoad(options) {
    if (options.type == 1) {
      this.monthPlanTitle = '基建周计划'
    } else {
      this.monthPlanTitle = '生产周计划'
    }
  }
}
</script>

<style lang="scss" scoped>
.month-container {
  height: 100vh;

  .month-content {
    position: relative;

    .bottom-btn {
      width: 100%;
      height: 80rpx;
      position: fixed;
      bottom: 12rpx;
      left: 0;
      display: flex;
      align-items: center;
      justify-content: space-between;
      background-color: #fff;
    }
    .bottom-btn view:first-child {
      display: flex;
      flex-direction: column;
      margin-left: 30rpx;
      // justify-content: center;
      align-items: center;
      font-size: 14px;
    }
    .bottom-btn view:last-child {
      width: 75%;
      height: 60rpx;
      margin-right: 40rpx;
      line-height: 60rpx;
      text-align: center;
      color: #fff;
      background-color: #003778;
      border-radius: 14rpx;
    }
    .btn-header {
      display: flex;
      width: 100%;
      button {
        flex: 1;
        height: 120rpx;
        color: #2c2b2b;
        font-size: 26rpx;
        line-height: 120rpx;
        text-align: center;
        border-radius: 0;
        background-color: #fff;
      }

      button::after {
        border: none;
      }
      .active {
        border-bottom: 2px solid #00337a;
      }
    }

    .content-2 {
      width: 100%;
      .pages-add {
        width: 100%;
        height: 76rpx;
        display: flex;
        align-items: center;
        background-color: #ccc;

        view {
          height: 76rpx;
        }

        .num-content {
          flex: 1;
          background-color: #fff;

          .scroll-x {
            width: 100%;
            white-space: nowrap;

            view {
              width: 50%;
              display: inline-block;
              height: 76rpx;
              text-align: center;
              line-height: 76rpx;
              color: #003778;
            }
            .active-page {
              border-bottom: 1px solid #003778;
            }
          }
        }
      }

      .time-container {
        display: flex;
        align-items: center;
        justify-content: space-between;
        padding: 0 38rpx;
      }
      .form-container {
        padding: 10rpx 20rpx;
      }
      .submit-btn {
        margin: 15rpx 0;
      }
    }
  }

  .common-style {
    padding: 15rpx;
  }
  .time-container {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10rpx 38rpx;
  }

  // 弹框样式
  .popup-content {
    width: 90vw;
    padding: 20rpx;
    background-color: #eeeeee;

    .search {
      width: 100%;
      height: 90rpx;
      margin-bottom: 30rpx;
      display: flex;
      align-items: center;
      justify-content: space-between;
      background-color: #fff;
      .select {
        height: 68rpx;
        width: 65%;
        margin-left: 10rpx;
      }

      button {
        height: 60rpx;
        width: 25%;
        line-height: 60rpx;
        font-size: 28rpx;
        background-color: #003777;
        color: #fff;
        border-radius: 12rpx;
      }
    }

    .project-content {
      height: 460rpx;
      width: 97%;
      margin: 0 auto;
      background-color: #fff;
      text-align: center;
    }
  }
}

.flex-style {
  display: flex;
  align-content: center;
  justify-content: space-between;
}

.icon-search-time {
  width: 100%;
  height: 68rpx;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-weight: bold;

  button {
    margin-right: 60rpx;
    height: 65rpx;
    background-color: #003777;
    color: #fff;
    line-height: 65rpx;
    font-weight: normal;
    font-size: 30rpx;
  }

  .view-time {
    margin: 0 35rpx;
  }
}
.popup-content {
  width: 90vw;
  padding: 20rpx;
  background-color: #eeeeee;

  .search {
    width: 100%;
    height: 70rpx;
    display: flex;
    align-items: center;
    justify-content: space-between;
    background-color: #eeeeee;
    .select {
      height: 68rpx;
      width: 65%;
      margin: 0;
    }

    button {
      height: 68rpx;
      width: 30%;
      line-height: 68rpx;
      font-size: 32rpx;
      background-color: #003777;
      color: #fff;
      border-radius: 12rpx;
    }
  }

  .project-content {
    height: 460rpx;
    width: 97%;
    margin: 0 auto;
    background-color: #fff;
  }
}
.select-item {
  height: 90rpx;
  line-height: 90rpx;
  text-align: center;
  border-bottom: 1px solid #ccc;
}
.select-item:hover {
  background-color: rgb(224, 239, 249);
}
.popup-bottom-content {
  height: 50vh;

  .bottom-title {
    height: 90rpx;
    padding-right: 30rpx;
    display: flex;
    align-items: center;
    justify-content: space-between;
    border-bottom: 1px solid #ccc;
  }
  .bottom-title text:first-child {
    color: transparent;
  }

  .select-item {
    height: 90rpx;
    line-height: 90rpx;
    text-align: center;
    border-bottom: 1px solid #ccc;
  }
  .select-item:hover {
    background-color: rgb(224, 239, 249);
  }
}

.week-container {
  padding: 10rpx 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-weight: bold;
  font-size: 30rpx;
}
</style>
