<!--
 * @Author: liuzhenlin 461480418@qq.ocm
 * @Date: 2023-02-15 10:34:49
 * @LastEditors: liuzhenlin
 * @LastEditTime: 2023-03-27 16:58:46
 * @Description: file content
 * @FilePath: \oms\components\SelectProduct.vue
-->
<template>
  <view>
    <u-popup :show="selectVisible" @close="close">
      <view class="select-header">
        <view class="tit">选择产品</view>
        <view class="save-btn" @click="close()">保存</view>
      </view>
      <view class="search-container">
        <view class="search-input">
          <u-input
            clearable
            placeholderStyle="font-size:26rpx"
            :customStyle="{ height: '66rpx' }"
            v-model="queryForm.prodName"
            prefixIcon="search"
            prefixIconStyle="font-size: 22px;color: #909399"
            placeholder="请输入产品名称"
            shape="circle"
            @input="getProjectList()"
            border="surround"></u-input>
        </view>
      </view>
      <view class="tit">产品名称/产品类别/产品型号</view>

      <view class="flex_l allCheck">
        <u-checkbox-group placement="column">
          <u-checkbox label="全部" :checked="isallcheck" @change="checkboxChange"></u-checkbox>
        </u-checkbox-group>
      </view>
      <u-empty mode="list" v-if="checkboxList.length == 0"></u-empty>
      <view class="concat-list" v-else>
        <u-checkbox-group v-model="userValue" placement="column">
          <view class="radio-item" v-for="item in checkboxList" :key="item.id">
            <u-checkbox
              :label="item.label"
              :name="item.id"
              :checked="item.ischeck"
              @change="radioChange(item)"></u-checkbox>
          </view>
        </u-checkbox-group>
      </view>
    </u-popup>
  </view>
</template>

<script>
  import productApi from '@/api/base/product'
  import to from 'await-to-js'
  export default {
    name: 'OmsCustomerContact',
    props: {
      selectedRows: {
        default: () => [],
      },
    },
    data() {
      return {
        selectVisible: false,
        checkboxList: [],
        queryForm: {
          prodName: '',
          pageNum: 1,
          pageSize: 999,
          stateType: '启用',
          type: '全部客户',
        },
        // u-radio-group的v-model绑定的值如果设置为某个radio的name，就会被默认选中
        userValue: null,
        isallcheck: false, //全选
        checkedId: [],
      }
    },

    methods: {
      async getProjectList() {
        let params = Object.assign(this.queryForm, this.queryParams)
        const [err, res] = await to(productApi.getList(params))
        if (err) return
        if (res.code == 200) {
          if (res.data.list && res.data.list.length > 0) {
            this.checkboxList = res.data.list.map((itemA) => {
              const itemB = this.selectedRows.find((itemB) => itemB.id === itemA.id)
              return {
                ...itemA,
                label: `${itemA.prodName}/${itemA.prodClass}/${itemA.prodCode}`,
                prodNum: '1',
                ischeck: Boolean(itemB),
              }
            })
          } else {
            this.checkboxList = []
          }
          let allChecked = this.checkboxList.length == 0 ? false : this.checkboxList.every((el) => el.ischeck === true)
          if (allChecked) {
            this.isallcheck = true //都有 全选
          } else {
            this.isallcheck = false // 反选
          }
        }
      },
      open() {
        this.selectVisible = true
        this.getProjectList()
      },
      close() {
        this.selectVisible = false
        let selected = this.checkboxList.filter((item) => item.ischeck)
        this.$emit('close', selected)
      },
      radioChange(item) {
        item.ischeck = !item.ischeck
        let allChecked = this.checkboxList.length == 0 ? false : this.checkboxList.every((el) => el.ischeck === true)
        if (allChecked) {
          this.isallcheck = true //都有 全选
        } else {
          this.isallcheck = false // 反选
        }
      },
      // 全部
      checkboxChange() {
        this.isallcheck = !this.isallcheck
        this.checkboxList.forEach((el) => {
          el.ischeck = this.isallcheck
        })
      },
    },
  }
</script>

<style lang="scss" scoped>
  .tit {
    font-size: 26rpx;
    font-weight: bold;
    padding: 30rpx 0 0 30rpx;
  }
  .search-container {
    padding: 30rpx 30rpx 0 30rpx;
    display: flex;
    align-items: center;

    .search-input {
      flex: 1;
    }
    .search-btn {
      text-align: center;
      line-height: 60rpx;
      border-radius: 12rpx;
      width: 100rpx;
      height: 60rpx;
      font-size: 26rpx;
      margin: 0 0 0 12rpx;
      background: $u-primary;
      color: #ffffff;
    }
  }
  .allCheck {
    padding: 30rpx 0 0 30rpx;
  }
  .concat-list {
    padding: 0 0 20rpx 0;
    width: 100%;
    height: 900rpx;
    overflow: auto;
    margin-top: 20rpx;
    .radio-item {
      padding: 20rpx 30rpx;
      border-bottom: 1px solid #ccc;
    }
  }
</style>
