<template>
  <view>
    <Navbar :title="navbarTitle" />
    <div class="tabs">
      <div class="item" :class="{ active: active == 1 }" @click="handleTab(1)">计划详情</div>
      <div class="item" :class="{ active: active == 2 }" @click="handleTab(2)">派车详情</div>
      <div class="item" :class="{ active: active == 3 }" @click="handleTab(3)">审核记录</div>
    </div>

    <!-- 列表 -->
    <div class="list-cont" v-for="(item, index) in tableList" :key="index">
      <u-row justify="space-between" customStyle="margin-bottom: 10px; padding: 0 15px;">
        <u-col span="6">
          <div>{{ item.code }}</div>
        </u-col>
        <u-col span="6" v-if="active == 1">
          <div style="display: flex; justify-content: flex-end">
            <u-tag text="已派车" type="success" v-if="item.isSend"></u-tag>
            <u-tag text="待派车" type="warning" v-else></u-tag>
          </div>
        </u-col>
      </u-row>
      <u-line style="margin-bottom: 5px"></u-line>
      <div class="list-item">
        <span>工程名称：</span>
        <span>{{ item.proName }}</span>
      </div>
      <div class="list-item df-item">
        <div>
          <span>计划类型：</span>
          <span>{{ item.plan }}</span>
        </div>
        <div>
          <u-button v-if="active == 1 && !item.isSend" type="primary" size="small" @click="openModal">
            派车分配
          </u-button>
          <u-button v-else-if="active == 2 && item.status == 0" type="warning" size="small" @click="openModal">
            审核
          </u-button>
          <u-tag text="已通过" type="success" v-else-if="active == 2 && item.status == 1"></u-tag>
        </div>
      </div>
      <div class="list-item">
        <span>供 应 商：</span>
        <span>{{ item.sub }}</span>
      </div>
    </div>

    <!-- 弹框 -->
    <u-modal :show="showModal" :title="modalTitle" showCancelButton @cancel="showModal = false" @confirm="submit">
      <view class="slot-content">
        <div v-if="active == 1">
          <u-radio-group v-model="radioValue" placement="column" @change="groupChange">
            <u-radio
              :customStyle="{ marginBottom: '8px' }"
              v-for="(item, index) in radioList"
              :key="index"
              :label="item.name"
              :name="item.value"
            ></u-radio>
          </u-radio-group>
        </div>
        <div v-else>
          <div class="item-wrap">
            <div class="item" :class="{ active: isPass }" @click="isPass = true">通过</div>
            <div class="item" :class="{ active: !isPass }" @click="isPass = false">不通过</div>
          </div>
          <u-textarea
            v-model="opinion"
            placeholder="不通过必须填写审核不通过意见"
            :rows="5"
            maxlength="200"
            count
          ></u-textarea>
        </div>
      </view>
    </u-modal>

    <!-- 选择器 -->
    <u-picker
      v-if="showPicker"
      :show="showPicker"
      :columns="[columns]"
      keyName="label"
      @cancel="showPicker = false"
      @confirm="confirm"
    ></u-picker>

    <u-loadmore :status="status" line v-if="tableList.length > 10" />

    <!-- 空状态 -->
    <u-empty v-if="tableList.length == 0" mode="data"></u-empty>
  </view>
</template>

<script>
import { getInventoryList } from '@/api/aqSafety'
import AuditRecord from './components/AuditRecord'
import PlanDetails from './components/PlanDetails'
import SendCarDetails from './components/SendCarDetails'

export default {
  components: { AuditRecord, PlanDetails, SendCarDetails },
  data() {
    return {
      opt: {},
      navbarTitle: '详情',
      active: 1,
      showModal: false,
      modalTitle: '',
      readonly: false,
      keyWord: '',
      status: 'loadmore',
      pageNum: 1,
      pageSize: 10,
      total: 0,
      tableList: [],
      checkStatus: '', // 审核状态
      checkStatusId: '', // 审核状态id
      showPicker: false,
      columns: [
        {
          label: '已审核',
          value: 1
        },
        {
          label: '待审核',
          value: 0
        }
      ],
      // 单选
      radioList: [
        {
          name: '苹果',
          value: '1',
          disabled: false
        },
        {
          name: '香蕉',
          value: '2',
          disabled: false
        },
        {
          name: '橙子',
          value: '3',
          disabled: false
        },
        {
          name: '榴莲',
          value: '4',
          disabled: false
        }
      ],
      radioValue: '', // 选中的值
      opinion: '', // 审批意见
      isPass: true // 是否通过
    }
  },
  computed: {
    justifyContent() {
      return this.active == 1 ? 'flex-start' : 'center'
    }
  },
  onLoad(opt) {
    this.opt = JSON.parse(opt.params)
    console.log('🚀 ~ onLoad ~ this.opt', this.opt)
    // this.getList()
  },
  methods: {
    handleTab(index) {
      this.active = index
      if (index == 1) {
        this.tableList = [
          {
            code: '20210801',
            proName: '项目名称',
            plan: '计划1',
            sub: '供应商1',
            // 已派车
            isSend: true
          },
          {
            code: '20210802',
            proName: '项目名称',
            plan: '计划2',
            sub: '供应商2',
            // 待派车
            isSend: false
          }
        ]
      } else {
        this.tableList = [
          {
            code: '20210803',
            proName: '项目名称',
            plan: '计划1',
            sub: '供应商1',
            status: 1
          },
          {
            code: '20210804',
            proName: '项目名称',
            plan: '计划2',
            sub: '供应商2',
            status: 0
          }
        ]
      }
    },
    handleSearch() {
      this.pageSize = 10
      this.getList()
    },
    async getList() {
      try {
        const params = {
          encryptedData: JSON.stringify({ keyWord: this.keyWord }),
          pageNum: this.pageNum,
          pageSize: this.pageSize
        }
        console.log('🚀 ~ getList ~ params', params)
        const res = await getInventoryList(params)
        console.log('🚀 ~ getList ~ res', res)
        this.tableList = res.data.list
        this.total = res.data.total
      } catch (error) {
        console.log('🚀 ~ getList ~ error', error)
      }
    },
    onReachBottom() {
      console.log('🚀 ~ onReachBottom ~ ')
      if (this.total < 10 || this.total == this.tableList.length) {
        this.status = 'nomore'
        return
      }
      this.status = 'loading'
      setTimeout(() => {
        this.pageSize += 10
        this.getList()
        if (this.tableList.length != this.total) this.status = 'loadmore'
        else this.status = 'nomore'
      }, 500)
      console.log('加载..', this.pageSize)
    },
    // 打开弹窗
    openModal() {
      this.showModal = true
      if (this.active == 1) {
        this.modalTitle = '指定派车供应商'
        this.radioValue = ''
      } else {
        this.modalTitle = '派车审核'
        this.isPass = true
        this.opinion = ''
      }
    },
    confirm(e) {
      console.log('🚀 ~ confirm ~ e', e)
      this.checkStatus = e.value[0].label
      this.checkStatusId = e.value[0].value
      this.showPicker = false
    },
    groupChange() {
      console.log('🚀 ~ radioValue', this.radioValue)
    },
    submit() {
      console.log('提交')
      if (this.active == 1) {
        console.log('派车分配', this.radioValue)
        if (!this.radioValue) {
          uni.showToast({
            title: '请选择派车供应商',
            icon: 'none'
          })
          return
        }
      } else {
        console.log('派车审核', this.isPass, this.opinion)
        if (!this.isPass && !this.opinion) {
          uni.showToast({
            title: '请填写审核不通过意见',
            icon: 'none'
          })
          return
        }
      }
      this.showModal = false
    }
  }
}
</script>

<style lang="scss" scoped>
page {
  background-color: #f8f8f8;
}
.search {
  margin: 15px;
  display: flex;
}
.list-cont {
  margin: 15px;
  padding: 15px 0;
  margin-bottom: 15px;
  background-color: #fff;
  border-radius: 8px;
  box-shadow: 0 2px 4px 0 rgba(0, 0, 0, 0.1);

  .list-item {
    padding: 5px 15px;
  }

  .df-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}
.tabs {
  display: flex;
  justify-content: space-between;
  background-color: #fff;
  .item {
    flex: 1;
    text-align: center;
    padding: 10px 0;
    color: #666;
    font-size: 16px;
    font-weight: 500;
    border-bottom: 2px solid transparent; // 添加透明边框
    transition: color 0.3s ease, border-bottom 0.3s ease, background-color 0.3s ease, box-shadow 0.3s ease;

    // 激活状态
    &.active {
      color: $u-primary;
      border-bottom: 2px solid $u-primary;
      background-color: #f8f8f8;
    }
  }
}
.slot-content {
  width: 100%;
  height: 100%;
  .item-wrap {
    margin-bottom: 15px;
    width: 100%;
    height: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .item {
      width: 45%;
      height: 30px;
      display: flex;
      justify-content: center;
      align-items: center;
      border: 1px solid #e8e8e8;
      border-radius: 30px;
      background-color: #f8f8f8;
      color: #666;
      font-size: 16px;
      font-weight: 500;
      transition: color 0.3s ease, background-color 0.3s ease, border 0.3s ease;

      &.active {
        color: $u-primary;
        background-color: #f8f8f8;
        border: 1px solid $u-primary;
      }
    }
  }
}
</style>
