<template>
  <a-modal
    centered
    class="productInfoOffline-modal"
    :footer="null"
    :maskClosable="false"
    title="提示"
    v-model="isShow"
    @cancle="isShow=false"
    :width="800">
    <p v-if="loadData.length==1" style="font-weight: bold;margin-left: 20px;">确认要下线吗？</p>
    <p v-else style="font-weight: bold;margin-left: 20px;">已选有效数据{{ loadData.length }}条，确认要批量下线吗？</p>
    <a-spin :spinning="spinning" tip="Loading...">
      <a-form-model
        id="productInfoEdit-form"
        ref="ruleForm"
        :model="form"
        :rules="rules"
        :label-col="formItemLayout.labelCol"
        :wrapper-col="formItemLayout.wrapperCol">
        <a-form-model-item label="下线选项" prop="offlineReasonType">
          <v-select
            code="OFFLINE_REASON_TYPE"
            id="productInfoOffline-offlineReasonType"
            v-model="form.offlineReasonType"
            allowClear
            placeholder="请选择下线选项"
          ></v-select>
        </a-form-model-item>
        <a-form-model-item label="通用编码" :required="form.offlineReasonType=='通用'">
          <!-- 列表 -->
          <a-table
            class="sTable"
            ref="table"
            size="small"
            :rowKey="(record) => record.productSn"
            :columns="columns"
            :dataSource="loadData"
            :pagination="false"
            bordered>
            <!-- 通用编码 -->
            <template slot="commonProductList" slot-scope="text, record" >
              <productCodeList ref="productCodeList" mode="multiple" @change="e => productCodeChange(e, record)" />
            </template>
          </a-table>
        </a-form-model-item>
        <a-form-model-item label="下线备注" prop="offlineRemark">
          <a-textarea
            id="productInfoOffline-offlineRemark"
            :maxLength="60"
            v-model="form.offlineRemark"
            placeholder="请输入下线备注(最多60个字符)"
            allowClear />
        </a-form-model-item>
      </a-form-model>
      <div class="btn-cont">
        <a-button type="primary" id="productInfoOffline-submit" @click="handleSubmit">确定</a-button>
        <a-button id="productInfoOffline-cancel" @click="isShow = false" style="margin-left: 100px;">取消</a-button>
      </div>
    </a-spin>
  </a-modal>
</template>

<script>
import { VSelect } from '@/components'
import productCodeList from '@/views/common/productCodeList.vue'
import { productOffline } from '@/api/product'
export default {
  name: 'ProductInfoDetailModal',
  components: { VSelect, productCodeList },
  props: {
    openModal: { //  弹框显示状态
      type: Boolean,
      default: false
    },
    offlineProductList: {
      type: Array,
      default: () => {
        return []
      }
    }
  },
  data () {
    return {
      isShow: this.openModal, //  是否打开弹框
      spinning: false,
      formItemLayout: {
        labelCol: { span: 3 },
        wrapperCol: { span: 21 }
      },
      form: {
        offlineReasonType: undefined,
        offlineRemark: ''
      },
      rules: {
        offlineReasonType: [
          { required: true, message: '请选择下线选项', trigger: 'change' }
        ]
      },
      columns: [
        { title: '产品编码', dataIndex: 'code', width: 220, align: 'center', customRender: function (text) { return text || '--' } },
        { title: '通用编码', scopedSlots: { customRender: 'commonProductList' }, align: 'center' }
      ],
      loadData: []
    }
  },
  methods: {
    productCodeChange (val, row) {
      console.log(val)
      row.commonProductList = []
      if (val.length > 0) {
        val.map(item => {
          row.commonProductList.push({ code: item.row && item.row.code ? item.row.code : undefined, productSn: item.key || undefined })
        })
      }
    },
    //  确定 下线
    handleSubmit () {
      const _this = this
      if (_this.form.offlineReasonType == '通用') {
        const arr = []
        this.loadData.map((item, index) => {
          if (item.commonProductList.length < 1) {
            arr.push(index + 1)
          }
        })
        if (arr.length > 0) {
          this.$message.warning('请将通用编码填写完整后再提交！')
          return
        }
      }
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          const params = JSON.parse(JSON.stringify(_this.form))
          params.offlineProductList = _this.loadData
          console.log(params)
          return
          _this.spinning = true
          productOffline(params).then(res => {
            if (res.status == 200) {
              _this.$message.success(res.message)
              setTimeout(() => {
                _this.$emit('ok')
                _this.isShow = false
                _this.spinning = false
              }, 1000)
            } else {
              _this.spinning = false
            }
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  },
  watch: {
    //  父页面传过来的弹框状态
    openModal (newValue, oldValue) {
      this.isShow = newValue
    },
    //  重定义的弹框状态
    isShow (newValue, oldValue) {
      if (!newValue) {
        this.$emit('close')
        this.loadData = []
        this.form.offlineReasonType = undefined
        this.form.offlineRemark = ''
      }
    },
    offlineProductList: {
      handler (newValue, oldValue) {
        if (this.isShow && newValue) {
          this.loadData = JSON.parse(JSON.stringify(newValue))
        }
      },
      deep: true
    }
  }
}
</script>

<style lang="less">
  .productInfoOffline-modal{
    .ant-modal-body {
      padding: 40px 40px 24px;
    }
    .btn-cont {
      text-align: center;
      margin: 35px 0 10px;
    }
  }
</style>
