<template>
  <div class="aimodeltc-edit">
    <el-dialog
      v-bind="$attrs"
      :title="state.dialogTitle"
      v-model="state.modalVisible"
      :before-close="cancelDialog"
      :append-to-body="true"
      :destroy-on-close="true"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :lock-scroll="false"
      class="aimodel-dialog"
      draggable
    >
      <el-form ref="aimodeltcRef" :model="state.form" :rules="state.rules" label-width="140px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="商品图片" prop="productpic">
              <UploadFile v-model:fileUrl="state.form.productpic" multiple accept="image/*" :oldUrl="state.form.productpic" :limit="1" attmodel="productpic" ref="sfzUploadRef" />
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="产品名称" prop="productname">
              <el-input v-model="state.form.productname" placeholder="产品名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品分类" prop="productclass">
              <el-select v-model="state.form.productclass" clearable>
                <el-option v-for="(item, index) of state.textTypeData" :key="index" :label="item.productclassname" :value="item.productclassid">{{
                  item.productclassname
                }}</el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <!-- 按钮组件 -->
            <all-Select @refreshButton="refreshData" :activeId="state.form.productagentsid" />
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="下行平均速度" prop="productradiusxxpj">
              <el-input v-model="state.form.productradiusxxpj" placeholder="下行平均速度"> </el-input>
            </el-form-item>
          </el-col> -->
          <!-- <el-col :span="12">
            <el-form-item label="下行最大速度" prop="productradiusxxmax">
              <el-input v-model="state.form.productradiusxxmax" placeholder="下行最大速度"> </el-input>
            </el-form-item>
          </el-col> -->
          <!-- <el-col :span="12">
            <el-form-item label="上行平均速度" prop="productradiussxpj">
              <el-input v-model="state.form.productradiussxpj" placeholder="上行平均速度"> </el-input>
            </el-form-item>
          </el-col> -->
          <!-- <el-col :span="12">
            <el-form-item label="上行最大速度" prop="productradiussxmax">
              <el-input v-model="state.form.productradiussxmax" placeholder="上行最大速度"> </el-input>
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="时长" prop="productlimit">
              <el-input v-model="state.form.productlimit" placeholder="产品时长"> </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="时长单位" prop="productlimittype">
              <el-select v-model="state.form.productlimittype" clearable>
                <el-option :label="'天'" :value="'天'">天</el-option>
                <el-option :label="'月'" :value="'月'">月</el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="原价" prop="productprice">
              <el-input v-model="state.form.productprice" placeholder="原价">
                <template #append>元</template>
              </el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="实价" prop="productprice1">
              <el-input v-model="state.form.productprice1" placeholder="实价">
                <template #append>元</template>
              </el-input>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="企业减扣金额" prop="productagentsreduceprice">
              <el-input v-model="state.form.productagentsreduceprice" placeholder="企业减扣金额">
                <template #append>元</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="产品套餐" prop="productradiuscpid">
              <el-select v-model="state.form.productradiuscpid" clearable>
                <el-option v-for="(item, index) of state.taocanTypeData" :key="index" :label="item.radiuscpname" :value="item.radiuscpid">{{ item.radiuscpname }}</el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="赠送时长" prop="productlimit1">
              <el-input v-model="state.form.productlimit1" placeholder="赠送时长"> </el-input>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="12">
            <el-form-item label="带宽" prop="productdkband">
              <el-input v-model="state.form.productdkband" placeholder="带宽">
                <template #append>M</template>
              </el-input>
            </el-form-item>
          </el-col> -->
          <el-col :span="12">
            <el-form-item label="产品是否可用" prop="productsfky">
              <el-radio-group v-model="state.form.productsfky">
                <el-radio :value="1" :label="1">可用</el-radio>
                <el-radio :value="0" :label="0">不可用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="新开可用" prop="productxkky">
              <el-radio-group v-model="state.form.productxkky">
                <el-radio :value="1" :label="1">可用</el-radio>
                <el-radio :value="0" :label="0">不可用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="续费可用" prop="productxfky">
              <el-radio-group v-model="state.form.productxfky">
                <el-radio :value="1" :label="1">可用</el-radio>
                <el-radio :value="0" :label="0">不可用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="升级可用" prop="productsjky">
              <el-radio-group v-model="state.form.productsjky">
                <el-radio :value="1" :label="1">可用</el-radio>
                <el-radio :value="0" :label="0">不可用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备可用" prop="productsbky">
              <el-radio-group v-model="state.form.productsbky">
                <el-radio :value="1" :label="1">可用</el-radio>
                <el-radio :value="0" :label="0">不可用</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>

          <el-col :span="12">
            <el-form-item label="产品排序" prop="productsort">
              <el-input v-model="state.form.productsort" placeholder="产品排序" />
            </el-form-item>
          </el-col>

          <el-col :span="24">
            <el-form-item label="产品介绍" prop="productmemo">
              <el-input type="textarea" :rows="4" placeholder="请输入产品介绍" v-model="state.form.productmemo" />
            </el-form-item>
          </el-col>

          <!-- <el-col :span="12">
            <el-form-item label="产品状态" prop="productstatus">
              <el-input v-model="state.form.productstatus" placeholder="产品状态" />
            </el-form-item>
          </el-col> -->
        </el-row>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitDialog">确 定</el-button>
          <el-button @click="cancelDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import productStoreApi from '@store/modules/productStoreApi'
import {filterObject} from '@/utils/index'
import {nextTick} from 'vue'
import UploadFile from '@/components/UploadFile/index.vue'
import useAppStore from '@store/modules/app'

// 接口封装文件
const productManageStore = productStoreApi()
// 全局变量
const {proxy} = getCurrentInstance()
// 验证form
const aimodeltcRef = ref(null)

const state = reactive({
  // 弹窗开关
  modalVisible: false,
  // 弹窗标题
  dialogTitle: '',
  // 详情
  editView: {},
  // 表单
  form: {
    // 企业id 企业码
    productagentsid: '',
    productagentsqym: '',
    productname: '',
    productclass: '',
    productdkband: '',
    productlimit: '',
    productprice: '',
    productprice1: '',
    productlimit1: '',
    productlimittype: '',
    productmemo: '',
    productsfky: '',
    productsort: '',
    productpic: '',
    productxkky: '',
    productxfky: '',
    productsjky: '',
    productsbky: '',
    productradiusxxpj: '',
    productradiusxxmax: '',
    productradiussxpj: '',
    productradiussxmax: '',
    productagentsreduceprice: '',
    productradiuscpid: '',
    productstatus: ''
  },
  // 验证表单
  rules: {
    productradiusxxpj: [{required: true, message: '此项不能为空', trigger: 'blur'}],
    productradiusxxmax: [{required: true, message: '此项不能为空', trigger: 'blur'}],
    productradiussxpj: [{required: true, message: '此项不能为空', trigger: 'blur'}],
    productradiussxmax: [{required: true, message: '此项不能为空', trigger: 'blur'}]
    // username: [{ required: true, message: '姓名/昵称不能为空', trigger: 'blur' }],
    // userworkgroup1: [{ required: true, message: '关联公司不能为空', trigger: 'blur' }],
    // userworkgroup2: [{ required: true, message: '关联部门不能为空', trigger: 'blur' }],
  },
  textTypeData: [],
  taocanTypeData: []
})

const emits = defineEmits(['refresh'])

// 确定弹窗回调
const submitDialog = () => {
  console.log(state.form, ';state.form')
  aimodeltcRef.value.validate(async (valid) => {
    if (valid) {
      if (Object.keys(state.editView).length) {
        let params = JSON.parse(JSON.stringify(state.form))
        params['productid.value'] = state.editView.productid
        params['productid'] = state.editView.productid
        await productManageStore.up(params)
        proxy.$modal.msgSuccess('修改成功！')
      } else {
        let params = JSON.parse(JSON.stringify(state.form))
        await productManageStore.add(params)
        proxy.$modal.msgSuccess('新增成功！')
      }
      state.editView = {}
      aimodeltcRef.value.resetFields()
      emits('refresh')
      state.modalVisible = false
    }
  })
}

// 关闭弹窗
const cancelDialog = () => {
  state.editView = {}
  aimodeltcRef.value.resetFields()
  state.modalVisible = false
}
const refreshData = (data) => {
  state.form.productagentsid = data.map((value) => value.agentsid).join(',')
  state.form.productagentsqym = data.map((value) => value.agentsqym).join(',')
}

// 打开弹窗
const openDialog = async (data = {}) => {
  // 选择信息
  await getDataType()
  if (Object.keys(data).length) {
    await getDataView(data)
    // console.log(state.form.productpic, 'state.form.productpic')
  } else {
    state.form = {
      productagentsid: '',
      productagentsqym: '',
      productname: '',
      productclass: '',
      productdkband: '',
      productlimit: '',
      productprice: '',
      productprice1: '',
      productlimit1: '',
      productlimittype: '',
      productmemo: '',
      productsfky: '',
      productsort: '',
      productpic: '',
      productxkky: '',
      productxfky: '',
      productsjky: '',
      productsbky: '',
      productradiusxxpj: '',
      productradiusxxmax: '',
      productradiussxpj: '',
      productradiussxmax: '',
      productagentsreduceprice: '',
      productradiuscpid: '',
      productstatus: ''
    }
    state.modalVisible = true
  }
  state.dialogTitle = Object.keys(data).length ? `编辑【${data.productname}】` : '新增'
}

// 申请详情
const getDataView = (data) => {
  nextTick(async () => {
    const result = await productManageStore.view({'productid.value': data.productid})
    state.editView = result
    state.form = filterObject(result, Object.keys(state.form))
    state.modalVisible = true
  })
}

import sortStoreApi from '@store/modules/sortStoreApi'
const sortStoreApiStore = sortStoreApi()
import allStoreApi from '@store/modules/allStoreApi'
const alleApi = allStoreApi()

const getDataType = () => {
  nextTick(async () => {
    const {Total, Data} = await sortStoreApiStore.list({pageindex: 1, rows: 99})
    state.textTypeData = Data

    const Data2 = await alleApi.list({pagevalue: 349, pageindex: 1, rows: 99})
    state.taocanTypeData = Data2.Data
    console.log(state.taocanTypeData, 'taocanTypeData')
  })
}

defineExpose({
  openDialog
})
</script>
