<template>
  <el-dialog
    title="批量更新机器"
    :visible.sync="upshow"
    width="1200px"
    top="2vh"
    append-to-body
    class="dialogclass"
    :close-on-click-modal="false"
    :before-close="handleClose"
  >
    <span>更新信息时，仅对<span style="color:#ff6b81">非采购单</span>的机器生效并且<span style="color:#ff6b81">按照列表顺序</span>进行更新</span>
    <div class="main" style="display:flex">
      <div class="topclass">
        <div>
          <template>
            <el-select v-model="imei_type" placeholder="请选择" size="small" style="margin-bottom:5px">
              <el-option
                v-for="item in [{label:'IMEI/IMEI2/SN',value:1},{label:'第三方条码',value:2},]"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
          </template>
          <el-input v-model="imei" type="textarea" :rows="8" :placeholder="imei_type ==1 ? '请输入IME/IME2/SN,换行表示：一行一台机器865928061032740 865928061032740 865928061032740':'请输入第三方条码 一行一台机器'" />
        </div>
        <div>
          <span>备注</span>
          <el-input v-model="remark" size="small" placeholder="请输入内容" clearable @input="editchange(remark,'remarkall')" />
          <el-input v-model="remarkall" type="textarea" :rows="6" placeholder="请输入内容" style="margin-top:10px" @input="editchange(remarkall,'remark')" />
        </div>
        <div>
          <div>机器等级(成色)</div>
          <el-select v-model="grade" style="width:100%" size="small" placeholder="请选择机器等级(成色)" clearable @input="editchange(grade,'gradeall')">
            <el-option
              v-for="item in grade_options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
          <el-input v-model="gradeall" type="textarea" :rows="6" :placeholder="(grade_options && grade_options.length>0) ? '请输入机器等级(成色) 一行一台机器' : '暂无机器等级(成色)无法批量添加'" style="margin-top:10px" @input="editchange(gradeall,'grade')" />
        </div>
      </div>
      <div style="margin-left: 20px;">
        <div class="flex-b">
          <div v-if="(formLabelAlign && formLabelAlign['warehousing_type'] == 1) || $route.name=='Createpurchaseorder'">
            <span>价格</span>
            <el-input v-model="price" size="small" placeholder="请输入内容" clearable @input=" price = AmountInput(price,2,'exceed');priceall=''" />
            <el-input v-model="priceall" type="textarea" :rows="6" placeholder="请输入内容" style="margin-top:10px" @input="priceall = ssaa(priceall,2);price=''" />
          </div>
          <div v-if="formLabelAlign && formLabelAlign['warehousing_type'] == 2">
            <span>参考价</span>
            <el-input v-model="reference_price" size="small" placeholder="请输入参考价" clearable @input=" reference_price = AmountInput(reference_price,2,'exceed');reference_priceall=''" />
            <el-input v-model="reference_priceall" type="textarea" :rows="6" placeholder="请输入价格 一行一台机器" style="margin-top:10px" @input="reference_priceall = ssaa(reference_priceall,2);reference_price=''" />
          </div>
          <div style="margin-left:10px">
            <div>账号锁/密码</div>
            <el-select v-model="idlock" style="width:100%" size="small" placeholder="请选择ID锁" clearable @input="editchange(idlock,'idlockall')">
              <el-option
                v-for="item in id_options"
                :key="item.value"
                :label="item.label"
                :value="item.value"
              />
            </el-select>
            <el-input v-model="idlockall" type="textarea" :rows="6" placeholder="请输入内容" style="margin-top:10px" @input="editchange(idlockall,'idlock')" />
          </div>
        </div>
        <h3 style="font-weight: bold;">机器信息</h3>
        <div v-if="bandshow==false">
          <el-row :gutter="10">
            <el-col :span="8">
              <span>分类:</span>
              <el-select v-model="form.cate_id" size="small" placeholder="请选择分类" filterable clearable @change="change(form.cate_id,'cate_id')">
                <el-option v-for="(e,i) in cate_options" :key="i" :label="e.label" :value="e.value" />
              </el-select>
            </el-col>
            <el-col :span="8">
              <span>品牌:</span>
              <el-select v-model="form.brand_id" size="small" placeholder="请选择品牌" filterable clearable @change="change(form.brand_id,'brand_id')">
                <el-option v-for="(e,i) in brand_options" :key="i" :label="e.label" :value="e.value" />
              </el-select>
            </el-col>
            <el-col :span="8">
              <span>型号:</span>
              <el-select v-model="form.model_id" size="small" placeholder="请选择型号" filterable clearable @change="change(form.model_id,'model_id')">
                <el-option v-for="(e,i) in model_options" :key="i" :label="e.label" :value="e.value" />
              </el-select>
            </el-col>
          </el-row>
          <div v-if="!!form.model_id">
            <div>
              <h3 style="font-weight: bold;">容量</h3>
              <div class="btnclass">
                <el-button v-for="(v,index) in rom_options" :key="index" class="buttonDiv" plain :type="form.rom_id==v.value ? 'primary' :''" @click="romclick($event,v)">{{ v.label }}</el-button>
              </div>
            </div>
            <h3 style="font-weight: bold;">颜色</h3>
            <div style="display:flex;">
              <div v-for="(v,index) in color_options" :key="index" style="display:flex;flex-direction:column;margin:0 5px;align-items: center;">
                <el-popover
                  placement="right"
                  width="350"
                  trigger="click"
                >
                  <el-image
                    v-if="v.img"
                    style="height: 350px"
                    :src="v.img"
                  >
                    <div slot="error" class="image-slot">
                      <i class="el-icon-picture-outline" />
                    </div>
                  </el-image>
                  <el-image
                    slot="reference"
                    style="height: 70px;width:70px"
                    :src="v.img"
                  >
                    <div slot="error" class="image-slot">
                      <i class="el-icon-picture-outline" />
                    </div>
                  </el-image>
                </el-popover>
                <div class="btnclass">
                  <el-button plain class="buttonDiv" :type="form.color_id==v.value ? 'primary' :''" @click="colorclick($event,v)">{{ v.label }}</el-button>
                </div>
              </div>
            </div>
          </div>
        </div>
        <div v-else>
          <span style="color:red">当前入库品牌存在2种及以上，暂不支持批量更改机器信息</span>
        </div>
        <!-- 属性 -->
        <div style="margin:10px 0">
          <h3 style="font-weight: bold;">属性</h3>
          <el-form :inline="true">
            <el-form-item label="客户">
              <el-select v-model="form.customer_id" size="small" placeholder="请选择客户" filterable clearable>
                <el-option v-for="(e,i) in customer_options" :key="i" :label="e.label" :value="e.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="报价师">
              <el-select v-model="form.quote_user_id" size="small" placeholder="请选择报价师" filterable clearable>
                <el-option v-for="(e,i) in quote_user_options" :key="i" :label="e.label" :value="e.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="国别">
              <el-select v-model="form.countries" size="small" placeholder="请选择国别" filterable clearable>
                <el-option v-for="(e,i) in countries_options" :key="i" :label="e.label" :value="e.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="网络制式">
              <el-select v-model="form.network" size="small" placeholder="请选择网络制式" filterable clearable>
                <el-option v-for="(e,i) in network_options" :key="i" :label="e.label" :value="e.value" />
              </el-select>
            </el-form-item>
            <el-form-item label="质检员">
              <el-select v-model="form.op_user_id" size="small" placeholder="请选择质检员" filterable clearable>
                <el-option v-for="(e,i) in op_user_options" :key="i" :label="e.label" :value="e.value" />
              </el-select>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
    <!-- <span style="display: flex;justify-content: flex-end;align-items: center;margin-top: 50px;" >
      <span style="margin-right:5px">机器是否开启查询功能 </span>
      <el-switch v-model="warranty_query" active-color="#3498db" @change="warrantychange" :active-value="1" :inactive-value="2" ></el-switch>
    </span> -->
    <span slot="footer" class="dialog-footer">
      <el-button @click="handleClose()">取 消</el-button>
      <el-button type="primary" @click="addphone">确认</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { ai_model_update_v2, cate_brand_list_all, user_warranty_query } from '@/api/Warehousing' // 获取客户信息接口
import { res_advanced_search } from '@/api/machineList' // 获取机器列表信息接口
import { pre_print } from '@/api/machineList' // 获取列表信息接口
import { op_order_details_code, cate_brand_list, edit_drafts_box, purchase_order_edit_phone, batch_save_drafts_box, batch_edit_drafts_box } from '@/api/Warehousing' // 获取二维码信息接口
import { mapGetters } from 'vuex'
import { uuid } from 'vue-uuid'
export default {
  name: 'EditBrand',
  data() {
    return {
      imei_type: 1,
      number: '',
      quote_user_id: null,
      customer_id: null,
      countries: null,
      network: null,
      imei: '',
      price: '',
      reference_price: '',
      reference_priceall: '',
      priceall: '',
      idlock: '',
      idlockall: '',
      remark: '',
      remarkall: '',
      grade: '',
      gradeall: '',
      keywords: '',
      warranty_query: 2,
      form: { cate_id: null, brand_id: null, model_id: null, rom_id: null, color_id: null, op_user_id: '' },
      grade_options: [],
      cate_options: [],
      brand_options: [],
      model_options: [],
      rom_options: [],
      color_options: [],
      customer_options: [],
      op_user_options: [],
      quote_user_options: [],
      network_options: [],
      countries_options: [// 国别
        { label: '中国', value: '中国' },
        { label: '国行运营商', value: '国行运营商' },
        { label: '国行全网通', value: '国行全网通' },
        { label: '海外', value: '海外' },
        { label: '美版', value: '美版' },
        { label: '日本', value: '日本' },
        { label: '韩版', value: '韩版' },
        { label: '港澳台', value: '港澳台' },
        { label: '新加坡', value: '新加坡' },
        { label: '欧盟', value: '欧盟' },
        { label: '国际版', value: '国际版' }],
      id_options: [
        { label: '有', value: '有' },
        { label: '无', value: '无' },
        { label: '未知', value: '未知' }],
      current: [],
      brandtotal: null,
      modeltotal: null,
      romtotal: null,
      default_brand_id: null,
      default_model_id: null
    }
  },
  computed: {
    ...mapGetters([
      'is_warranty_query', // 1查询，2不查询
      'user_id'
    ]),
    bandshow() {
      let flag = true
      if (this.$parent.tableData.length > 0) {
        const f = this.$parent.tableData[0].brand_name
        flag = this.$parent.tableData.every(e => { return e == f })
      }
      return flag
    }

  },
  watch: {
    'is_warranty_query': {
      handler(val) {
        this.warranty_query = val
      }, deep: true, immediate: true
    }
  },
  created() {
    this.getcate()
  },
  mounted() {

  },
  props: {
    upshow: {
      type: Boolean,
      default: () => {
        return false
      }
    },
    is_purchase: {
      type: Number,
      default: () => {
        return ''
      }
    },
    tableData: {
      type: Array,
      default: () => {
        return []
      }
    },
    brandform: {
      type: Object,
      default: () => {
        return {}
      }
    },
    id: {
      type: String,
      default: () => {
        return ''
      }
    },
    input: {
      type: Array,
      default: () => {
        return []
      }
    },
    formLabelAlign: {
      type: Object,
      default: () => {
        return {}
      }
    },
    jiqidata: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  methods: {
    ssaa(val, limit = 0, exceed) { // 金额输入框限制方法
      if (this._.isNumber(val)) { // 判断是否数字类型
        val = '' + val
      }
      // val = val.replace(/[^\d.]/g, ""); //保留数字
      val = val.replace(/[^\r\n\d.]/g, '') // 保留数字换行
      val = val.replace(/^00/, '0.') // 开头不能有两个0
      val = val.replace(/^\./g, '0.') // 开头为小数点转换为0.
      val = val.replace(/\.{2,}/g, '.') // 两个以上的小数点转换成一个
      val = val.replace(/(\d\.[\d]{0,2}).*([\s]*)/g, '$1$2'); // 只保留一个小数点
      /^0\d+/.test(val) ? val = val.slice(1) : '' // 两位以上数字开头不能为0
      const str = '^(\\d+)\\.(\\d{' + limit + '}).*$'
      const reg = new RegExp(str)
      if (limit === 0) {
        // 不需要小数点
        val = val.replace(reg, '$1')
      } else {
        // 通过正则保留小数点后指定的位数
        val = val.replace(reg, '$1.$2')
      }
      if (!!exceed && Number(val) > 30000) {
        Message({ duration: 4000, message: '输入价格已超过30000', type: 'warning', showClose: true })
      }
      return val
    },
    inputchange(queryString, callback) {
      if (!!queryString && queryString.length >= 2) {
        // 调用的后台接口
        cate_brand_list({ keywords: queryString }).then((res) => {
          callback(res.datas)
        }).catch((error) => {
        })
      } else {
        callback([])
      }
    },
    // 是否查询状态改变
    warrantychange(e) {
      user_warranty_query({ user_id: this.user_id, is_warranty_query: e }).then(res => {
        this.$message({ type: 'success', message: e == 1 ? '查询已开启' : '查询已关闭' })
        this.$store.dispatch('user/getInfo')
      })
    },
    // 添加机器
    addphone() {
      batch_edit_drafts_box({ imei_type: this.imei_type, imei_text: this.imei, pay_amount: this.priceall, all_pay_amount: this.price, id_lock: this.idlockall, all_id_lock: this.idlock,
        remark: this.remarkall, all_remark: this.remark, level_name: this.gradeall, all_level_name: this.grade, reference_price: this.reference_price, reference_priceall: this.reference_priceall,
        client_type: 2, is_purchase: this.is_purchase, ...this.form, drafts_box_id_arr: this._.uniq(this.$parent.tableData.map(e => { return e.id })) }).then(res => {
        this.handleClose()
        this.form = { cate_id: null, brand_id: null, model_id: null, rom_id: null, color_id: null, op_user_id: '' }
        this.$parent.getlist()
      })
    },
    editchange(change, name) {
      if (change) {
        this[name] = ''
      }
    },
    getcate() {
      cate_brand_list().then(res => {
        this.current = [this.brandform.cate_id]
        this.cate_options = res.datas.datas
      })
      res_advanced_search({ p_user_id: this.$parent.formLabelAlign.subsidiary }).then(e => {
        this.quote_user_options = e.datas['quote_user_id']
        this.customer_options = e.datas['supplier_info']
        this.grade_options = e.datas['rk_grade_list']
        this.network_options = e.datas['network']
        this.op_user_options = e.datas['op_user_id']
      })
    },
    getbrand(page) {
      cate_brand_list({ cate_id: this.brandform.cate_id, list_row: 15, page: page, default_brand_id: this.default_brand_id }).then(res => {
        this.brand_options = res.datas.datas
        this.brandtotal = res.datas.total
      })
    },
    getmodel(page) {
      cate_brand_list({ cate_id: this.brandform.cate_id, list_row: 15, page: page, brand_id: this.brandform.brand_id, default_model_id: this.default_model_id, default_brand_id: this.default_brand_id }).then(res => {
        this.model_options = res.datas.datas
        this.modeltotal = res.datas.total
      })
    },
    async handleSelect(data) {
      this.brandform.cate_id = data.cate_id
      this.brandform.brand_id = data.brand_id
      this.default_brand_id = data.brand_id
      this.default_model_id = data.model_id
      this.brandform.model_id = data.model_id
      this.brandform.rom_id = null
      this.brandform.color_id = null
      await this.getbrand()
      await this.getmodel()
      await this.getromcolor()
    },
    getromcolor(page) {
      cate_brand_list({ cate_id: this.brandform.cate_id, list_row: 15, page: page, brand_id: this.brandform.brand_id, model_id: this.brandform.model_id, default_model_id: this.default_model_id, default_brand_id: this.default_brand_id }).then(res => {
        this.rom_options = res.datas.datas['rom_list']
        this.color_options = res.datas.datas['color_list']
        this.romtotal = res.datas.total
      })
    },
    editinfo() {
      if (this.brandform.res_id) { // 采购单
        purchase_order_edit_phone(this.brandform).then(res => {
          this.$parent.getlist()
          this.handleClose()
        })
      } else {
        edit_drafts_box({ ...this.brandform, client_type: 2 }).then(res => {
          this.$parent.getlist()
          this.handleClose()
        })
      }
    },
    change(val, name) {
      if (name == 'cate_id') {
        this.form.brand_id = null
        this.form.model_id = null
        this.form.rom_id = null
        this.form.color_id = null
        if (val) {
          cate_brand_list_all({ cate_id: val }).then(res => {
            this.brand_options = res.datas
          })
        } else {
          this.brand_options = []
          this.rom_options = []
          this.color_options = []
        }
      } else if (name == 'brand_id') {
        this.form.model_id = null
        this.form.rom_id = null
        this.form.color_id = null
        if (val) {
          cate_brand_list_all({ cate_id: this.form.cate_id, brand_id: val }).then(res => {
            this.model_options = res.datas
          })
        } else {
          this.model_options = []
          this.rom_options = []
          this.color_options = []
        }
      } else {
        this.form.rom_id = null
        this.form.color_id = null
        if (val) {
          cate_brand_list_all({ cate_id: this.form.cate_id, brand_id: this.form.brand_id, model_id: val }).then(res => {
            this.rom_options = res.datas['rom_list']
            this.color_options = res.datas['color_list']
          })
        } else {
          this.rom_options = []
          this.color_options = []
        }
      }
    },
    brandclick(e, v) {
      let target = e.target
      if (target.nodeName === 'SPAN') {
        target = e.target.parentNode
      }
      target.blur()
      this.brandform.brand_id = v.value
      this.getmodel()
    },
    modelclick(e, v) {
      let target = e.target
      if (target.nodeName === 'SPAN') {
        target = e.target.parentNode
      }
      target.blur()
      this.brandform.model_id = v.value
      this.getromcolor()
    },
    romclick(e, v) {
      let target = e.target
      if (target.nodeName === 'SPAN') {
        target = e.target.parentNode
      }
      target.blur()
      this.form.rom_id = v.value
    },
    colorclick(e, v) {
      let target = e.target
      if (target.nodeName === 'SPAN') {
        target = e.target.parentNode
      }
      target.blur()
      this.form.color_id = v.value
    },
    currentchange(data) {
      this.getbrand(data)
    },
    currentchange1(data) {
      this.getmodel(data)
    },
    currentchange2(data) {
      this.getromcolor(data)
    },
    handleClose() {
      this.$emit('update:upshow', false)
    }
  }
}
</script>

<style lang="less" scoped>
.topclass{
    display: flex;
    flex-direction: column;
    justify-content: space-between;
}
.bodyclass{
  margin-top:10px;
  display: flex;
  height: 820px;
}
.bodyclass ::v-deep .el-button{
    padding: 8px;
}
// .btnclass ::v-deep .el-button--primary:focus{
//     margin-right: 12px;
//     border-radius: 2px;
//     box-shadow: 0 2px 4px 0 #f4f4f4;
//     color: #2794f8;
//     background: white;
// }
.btnclass ::v-deep .el-button--primary:hover{
    border-radius: 2px;
    color: #2794f8;
    background: #d2e6fc;
    border-color:#b3d8ff ;
}
.body {
  display: flex;
  flex-direction: column;
}
.body-text {
  // display: flex;
  // flex-direction: column;
  margin-bottom: 15px;
}
.header {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 25px;
}
.text {
  font-size: 20px;
  font-weight: bold;
}
.text1 {
  font-size: 18px;
  // height: 20px;
}
.calculation {
  border: 1px solid #ebeef5;
  padding: 10px;
  font-size: 14px;
  span {
    padding: 0 10px;
    display: inline-block;
  }
}
table,td,th {
  border: 1px solid #d4d4d4;
}

table {
  border-collapse: collapse;
  height: 100px;
  width: 100%;
}

th {
  height: 65px;
}
.top{
    display: flex;
    background-color:#4ca48c;
    height:60px
}
.top-text{
    font-family: STHeiti;
    color: #fff;
    font-size: 26px;
    text-align: center;
}
.top-text1{
    font-family: STHeiti;
    color: #fff;
    font-size: 12px;
}
.fotter-top{
    display: flex;
    background-color:#4ca48c;
    height:60px;
    justify-content: space-between;
    align-items: center;
    padding: 0 30px;
}
.footer-Toptext{
    font-family: STHeiti;
    color: #fff;
    font-size: 24px;
}
.fotter-main{
    display: flex;
    // background-color:#fff;
    height:30px;
    justify-content: center;
}
.footer-maintext{
    font-family: STHeiti;
    color:#FFFFFF;
    font-size: 20px;
}
.el-col-6{
  width: 20%!important;
}
.level{
  display: inline-block;
    height: 20px;
    line-height: 20px;
    padding: 0 15px;
    background: #ff633c;
    border-radius: 12px;
    font-size: 12px;;
    font-weight: 500;
    color: #fff;
    cursor: pointer;
    margin:0px 10px;
}
.paiclass{
    height: 40px;
    background: #f2f2f2;
    display: flex;
    font-size: 16px;
    align-items: center;
}
</style>
