<template>
  <div class="create-shop-wrapper">
    <bread-crumb :curmbFirst="curmbFirst"></bread-crumb>
    <div class="create-shop-content screenSet" id="screenSet">
      <el-row class="step-content">
        <el-col :span="20">
          <p class="title">{{title}}</p>
        </el-col>
        <el-col :span="4" style="text-align: right">
          <el-button size="small" type="primary" @click="complete">完成</el-button>
        </el-col>
      </el-row>
        <el-form
            ref="formData"
            :model="formData"
            :rules="rules"
            label-width="150px"
            label-suffix="："
            class="basic-form"
        >

          <div class="basic-item-icon">
            <div class="part-tit">通用信息</div>
            <el-form-item label="商品名称" prop="goods_name">
              <el-col :span="13">
                <el-input
                    size="small"
                    v-model="formData.goods_name"
                    placeholder="请输入商品名称"
                    show-word-limit
                    maxlength="60"
                ></el-input>
                <!-- <span class="word-num">{{(formData.goods_name).replace(/\s+/g,"").length}}/60</span> -->
              </el-col>
            </el-form-item>

            <el-form-item label="商品介绍" prop="goods_description">
              <el-col :span="13">
                <el-input
                    type="textarea"
                    rows="4"

                    size="small"
                    v-model="formData.goods_description"
                    placeholder="请输入商品介绍"
                    maxlength="1000"
                    show-word-limit
                ></el-input>
                <!-- <span class="word-num">{{(formData.goods_description).replace(/\s+/g,"").length}}/1000</span> -->
              </el-col>
            </el-form-item>

            <el-form-item label="商品头图" class="required-label">
              <el-upload
                  v-model="formData.goods_image"
                  class="bg-uploader"
                  action="#"
                  :show-file-list="false"
                  :before-upload="beforeUploadPic1"
              >
                <img
                    v-if="formData.goods_image"
                    :src="formData.goods_image"
                    @mouseover.stop="imgMouseOver1=true"
                    class="bg-img"
                />
                <img v-if="!formData.goods_image" class="bg-img" src="../../assets/image/small.png" />
                <div
                    class="img-delete"
                    v-show="imgMouseOver1"
                    @click.stop="deleteImg(1)"
                    @mouseout.stop="imgMouseOver1=false"
                >
                  <i class="el-icon-delete"></i>
                </div>
                <div class="limit-text">
                  <p>限制大小: 200kb</p>
                  <p>最小尺寸：750*420</p>
                  <p>支持jpeg, png格式</p>
                </div>
              </el-upload>
              <p class="upload-message" v-if="!isGoods_image">请上传商品头图</p>
            </el-form-item>
            <el-form-item label="商品类型" prop="goods_type">
              <el-select
                  v-model="formData.goods_type"
                  placeholder="请选择商品类型"
                  size="small"
                  clearable>
                <el-option
                    v-for="(item,index) in typeList"
                    :key="index"
                    :label="item.label"
                    :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <!-- <p class="upload-message" v-if="uploadImgMessage1">请上传商品头图</p> -->
          </div>

          <div class="basic-item-icon">
            <div class="part-tit">药品信息</div>
            <el-form-item label="批准文号" prop="approval_number">
              <el-col :span="13">
                <el-input
                    size="small"
                    v-model="formData.approval_number"
                    placeholder="请输入批准文号"
                    style="width:70%;"
                ></el-input>
                <!-- <span class="word-num">{{(formData.name).replace(/\s+/g,"").length}}/60</span> -->
              </el-col>
            </el-form-item>

            <el-form-item label="药品通用名称" prop="medic_common_name">
              <el-col :span="13">
                <el-input
                    size="small"
                    v-model="formData.medic_common_name"
                    placeholder="请输入药品通用名称"
                    style="width:70%;"
                    maxlength="60"
                    show-word-limit
                ></el-input>

               <!-- <span class="word-num">{{(formData.medic_common_name).replace(/\s+/g,"").length}}/60</span> -->
              </el-col>
            </el-form-item>
            <el-form-item label="药品商品名称" prop="medic_goods_name">
              <el-col :span="13">
                <el-input
                    size="small"
                    v-model="formData.medic_goods_name"
                    placeholder="请输入药品商品名称"

                    maxlength="60"
                    show-word-limit
                ></el-input>

                <!-- <span class="word-num">{{(formData.medic_goods_name).replace(/\s+/g,"").length}}/60</span> -->
              </el-col>
            </el-form-item>
            <el-form-item label="药品规格" prop="size">
              <el-col :span="13">
                <el-input
                    size="small"
                    v-model="formData.size"
                    placeholder="请输入药品规格"
                    style="width:70%;"
                ></el-input>

                <span class="word-num">例如:0.125g*12袋/盒</span>
              </el-col>
            </el-form-item>
            <div class="inline">
              <el-form-item label="是否是处方药" prop="otc">
                <el-col :span="24">
                  <el-radio size="mini" v-model="formData.otc" label="1">处方药</el-radio>
                  <el-radio v-model="formData.otc" label="0">非处方药</el-radio>
                </el-col>
              </el-form-item>
              <el-form-item label="剂型" prop="dosage_id">
                <el-select
                    v-model="formData.dosage_id"
                    placeholder="请选择药品剂型"
                    size="small"
                    clearable>
                  <el-option
                      v-for="(item,index) in typeList"
                      :key="index"
                      :label="item.label"
                      :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </div>
            <el-form-item label="用法用量" prop="usage">
              <el-col :span="13">
                <el-input
                    size="small"
                    v-model="formData.usage"
                    placeholder="请输入用法用量"
                    style="width:70%;"
                ></el-input>

                <span class="word-num">例如:每次一袋 每天1-2次</span>
              </el-col>
            </el-form-item>

            <el-form-item label="药品类型" prop="category_id_level2">
              <el-select
                  v-model="formData.category_id_level2"
                  placeholder="请选择药品类型"
                  size="small"
                  clearable
              >
                <el-option
                    v-for="(item,index) in typeList"
                    :key="index"
                    :label="item.label"
                    :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
            <div class="inline">
              <el-form-item label="一级类目" prop="dynamicFlag">
                <el-select
                    v-model="formData.dynamicFlag"
                    placeholder="请选择一级类目"
                    size="small"
                    clearable
                >
                  <el-option
                      v-for="(item,index) in typeList"
                      :key="index"
                      :label="item.label"
                      :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="二级类目" prop="dynamicFlag">
                <el-select
                    v-model="formData.dynamicFlag"
                    placeholder="请选择二级类目"
                    size="small"
                    clearable
                >
                  <el-option
                      v-for="(item,index) in typeList"
                      :key="index"
                      :label="item.label"
                      :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="三级级类目" prop="dynamicFlag">
                <el-select
                    v-model="formData.dynamicFlag"
                    placeholder="请选择三级类目"
                    size="small"
                    clearable
                >
                  <el-option
                      v-for="(item,index) in typeList"
                      :key="index"
                      :label="item.label"
                      :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
            </div>
            <el-form-item label="生产厂家" prop="manufacturer">
              <el-col :span="13">
                <el-input
                    size="small"
                    v-model="formData.manufacturer"
                    placeholder="请输入生产厂家"
                    style="width:70%;"
                ></el-input>

                <span class="word-num"></span>
              </el-col>
            </el-form-item>
            <div class="inline">
              <el-form-item label="适用科室" prop="department">
                <el-select
                    v-model="formData.department"
                    placeholder="请选择适用科室"
                    size="small"
                    clearable
                >
                  <el-option
                      v-for="(item,index) in typeList"
                      :key="index"
                      :label="item.label"
                      :value="item.value"
                  ></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="治疗疾病" prop="treat_disease">
                <el-col :span="24">
                  <el-input
                      size="small"
                      v-model="formData.treat_disease"
                      placeholder="请输入治疗疾病"

                  ></el-input>

                  <span class="word-num"></span>
                </el-col>
              </el-form-item>
            </div>

            <el-form-item label="有效期" prop="expired_time">
              <el-col :span="13">
                <el-input
                    size="small"
                    v-model="formData.expired_time"
                    placeholder="请输入有效期"
                    style="width:70%;"
                ></el-input>

                <span class="word-num"></span>
              </el-col>
            </el-form-item>
            <el-form-item label="药品说明书" class="required-label">
              <el-upload
                  v-model="formData.specification_url"
                  class="bg-uploader"
                  action="#"
                  :show-file-list="false"
                  :before-upload="beforeUploadPic1"
              >
                <img
                    v-if="formData.specification_url"
                    :src="formData.specification_url"
                    @mouseover.stop="imgMouseOver1=true"
                    class="bg-img"
                />
                <img v-if="!formData.specification_url" class="bg-img" src="../../assets/image/small.png" />
                <div
                    class="img-delete"
                    v-show="imgMouseOver1"
                    @click.stop="deleteImg(1)"
                    @mouseout.stop="imgMouseOver1=false"
                >
                  <i class="el-icon-delete"></i>
                </div>
                <div class="limit-text">
                  <p>限制大小: 2M</p>
                  <!-- <p>最小尺寸：750*420</p> -->
                  <p>支持jpeg, png格式</p>
                </div>
              </el-upload>
              <p class="upload-message" v-if="!isSpecification_url">请上传药品说明书</p>
            </el-form-item>
          </div>

          <div class="basic-item-icon">
            <div class="part-tit">销售信息</div>
            <div class="inline">
              <el-form-item label="零售价" prop="name">
                <el-col :span="20">
                  <el-input
                      size="small"
                      v-model="formData.name"
                      placeholder="请输入零售价"

                  ></el-input>
                 <!-- <span class="word-num">{{(formData.name).replace(/\s+/g,"").length}}/60</span> -->
                </el-col>
              </el-form-item>
              <el-form-item label="折扣价" prop="name">
                <el-col :span="20">
                  <el-input
                      size="small"
                      v-model="formData.name"
                      placeholder="请输入折扣价"

                  ></el-input>
                 <!-- <span class="word-num">{{(formData.name).replace(/\s+/g,"").length}}/60</span> -->
                </el-col>
              </el-form-item>
            </div>
            <div class="inline">
              <el-form-item label="库存" prop="name">
                <el-col :span="20">
                  <div class="stock-com">
                    <span class="sp sp-l"><i class="el-icon-minus"></i></span>
                    <span class="sp sp-c">{{formData.stock}}</span>
                    <span class="sp sp-r"><i class="el-icon-plus"></i></span>
                  </div>
                 <!-- <el-input
                      size="small"
                      v-model="formData.name"
                      placeholder="请输入库存"

                  ></el-input> -->
                  <!-- <el-input-number   readonly size="small" v-model="formData.stock"></el-input-number> -->
                 <!-- <span class="word-num">{{(formData.name).replace(/\s+/g,"").length}}/60</span> -->
                </el-col>
              </el-form-item>
              <el-form-item label="条形码" prop="name">
                <el-col :span="20">
                  <el-input
                      size="small"
                      v-model="formData.name"
                      placeholder="请输入条形码"

                  ></el-input>
                 <!-- <span class="word-num">{{(formData.name).replace(/\s+/g,"").length}}/60</span> -->
                </el-col>
              </el-form-item>
            </div>
          </div>
        </el-form>
      <el-dialog
          class="dialog-title-border-old"
          title="图片裁剪"
          :visible.sync="showCropper"
          :width="currentOption.cropDialogWidth"
          center>
        <div slot="title" style="text-align: left;">
          <span style="font-weight: 700;">图片裁剪</span>
        </div>
        <div v-if="showCropper" style="margin-bottom: 20px;">
          <Cropper
              :cropOption="currentOption"
              @getCropImg="getCropImg(arguments)"
              :originImg="slide2.oriUrl"
          />
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
  import BreadCrumb from "@/components/breadcrumb.vue";
  let vm = null;
  import { openLoading, closeLoading } from "../../utils/utils";
  import { doUpload, getFilePath } from "../../utils/qiniu-util";
  import Cropper from '@/components/common/cropper.vue'
  export default {
    components: {
      BreadCrumb,
      Cropper
    },
    data(){
      let checkProjectStr = (rule, value, callback) => {
        if (value.indexOf("\\") != -1) {
          //存在
          callback(new Error("请勿输入字符“ \\ ”"));
        } else if (value.indexOf(".") != -1) {
          callback(new Error("请勿输入字符“ . ”"));
        } else {
          callback();
        }
      };
      return{
        curmbFirst: '云鹊店铺',
        isGoods_image:true,
        isSpecification_url:true,
        title:'',
        typeList: [{
          label: '药品',
          value: '1',
        }, {
          label: '药品器械',
          value: '2',
        }, {
          label: '其他',
          value: '3',
        }],
        formData: {
          id:'',
          goods_name:'',
          goods_description:'',
          goods_image:'',
          goods_type:'',
          medic_common_name:'',
          medic_goods_name:'',
          size:'',
          dosage_id:null,
          usage:'',
          category_id_level2:'',
          otc:'',
          approval_number:'',
          manufacturer:'',
          department:'',
          expired_time:'',
          specification_url:'',
          treat_disease:'',
          bar_code:'',
          discount_price:'',
          stock:1,
        },
        showCropper: false,
        currentOption: {
          offset_x: 120,
          offset_y: 185,
          width: 160,
          height: 120,
          cvWidth: 1000,
          cvHeight: 800,
          uploadType: 1,
          cropDialogWidth: '900px',
          cropBoxResizable: true,
          minCropBoxWidth: 100,
          minCropBoxHeight: 100,
          aspectRatio: 16/9
        },
        slide2: {
          oriUrl: '', // 原图
        },
        rules: {
          goods_name: [
            { required: true, message: "请输入商品名称", trigger: "blur" },
            {
              // min: 2,
              // max: 14,
              //message: "输入长度为2-14的内容，可包含中英文、数字及特殊符号",
              trigger: "blur"
            },
            { validator: checkProjectStr, trigger: "blur" }
          ],
          introduce: [
            { required: true, message: "请输入圈子介绍", trigger: "blur" },
            {
              min: 2,
              max: 40,
              message: "输入长度为2-40的内容，可包含中英文、数字及特殊符号",
              trigger: "blur"
            },
            { validator: checkProjectStr, trigger: "blur" }
          ],
          goods_image: [
            { required: true, message: "请上传商品头图", trigger: "blur" }
          ],
          goods_type:[
            { required: true, message: "请选择商品类型", trigger: "change" }
          ],
          medic_common_name:[
            { required: true, message: "请输入商品通用名称", trigger: "blur" }
          ],
          size:[
            { required: true, message: "请输入商品规格", trigger: "blur" }
          ],
          manufacturer:[
            { required: true, message: "请输入生产厂家", trigger: "blur" }
          ],
          approval_number:[
            { required: true, message: "请输入批准文号", trigger: "blur" }
          ],
          dosage_id:[
            { required: true, message: "请选择剂型", trigger: "change" }
          ],
          category_id_level2:[
            { required: true, message: "请选择药品类型", trigger: "blur" }
          ],
          otc:[
            { required: true, message: "请选择是否是处方", trigger: "blur" }
          ],
          specification_url:[
            { required: true, message: "请上传药品说明书", trigger: "blur" }
          ],

          dynamicFlag: [
            { required: true, message: "请选择内容发布权", trigger: "blur" },
          ]
        },
        imgMouseOver1: false,
        uploadImgMessage1: false,//未上传图片，校验提示语
      }
    },
    created() {
      vm = this;
      console.log(this.$route)
      const {id} = this.$route.query
      this.title = id == 'add' ? '新增商品' : '编辑商品'
    },
    methods: {
      complete() {
        let formName = "formData";

        this.isGoods_image = !this.formData.goods_image ? false :true;
        this.isSpecification_url = !this.formData.specification_url ? false : true
        this.$refs[formName].validate((valid) => {
          if (valid) {
            alert('submit!');
          } else {
            console.log('error submit!!');
            return false;
          }
        });

      },
      //表单校验
      submitForm() {
        let formName = "formData";
        let flag = null;
        if (!this.formData.headUrl){
          this.uploadImgMessage1 = true;
        }
        this.$refs[formName].validate(valid => {
          if (valid&&this.formData.headUrl!="") {
            flag = true;
          } else {
            console.log("error submit!!");
            flag = false;
          }
        });
        this.$emit('returnIsNext', flag);
      },
      //上传店铺logo
      beforeUploadPic1(file) {
        this.currentOption.aspectRatio = 1/1;
        this.currentOption.cropBoxResizable = true;
        this.currentOption.minCropBoxWidth = 160;
        this.currentOption.minCropBoxHeight = 160;
        let fileLimit = {
          width: 160,
          height: 160,
          size: 0.5,
          sizeText: "500K",
          key: "headUrl",
          more: "imgUrl1More",
          show: "uploadImgMessage1"
        };
        this.beforeUpload(file, fileLimit);
      },
      //上传图片
      beforeUpload(file, fileLimit) {
        let vm = this;
        const isJPG = file.type === "image/jpeg";
        const isPNG = file.type === "image/png";
        const isLt2M = file.size / 1024 / 1024 < fileLimit.size;
        if (!isJPG && !isPNG) {
          vm.$message.error("图片格式不符合规范，请根据规范上传图片");
          // return;
        }
        // if (!isLt2M) {
        //   vm.$message.error("图片大小不符合规范，请根据规范上传图片 ");
        //   return;
        // }
        let _img = new FileReader();
        _img.readAsDataURL(file);
        _img.onload = function (theFile) {
          let image = new Image()
          image.src = theFile.target.result
          vm.slide2.oriUrl = theFile.target.result;
          image.onload = function () {
            let _this = this;
            // if (_this.width != fileLimit.width || _this.height != fileLimit.height) {
            if (_this.width < fileLimit.width ||  _this.height < fileLimit.height) {
              vm.$message.error("图片尺寸不符合规范，请根据规范上传图片");
              return;
            } else if(_this.width > fileLimit.width ||  _this.height > fileLimit.height){
              vm.showCropper = true;
              vm.currentOption.cvWidth = _this.width;
              vm.currentOption.cvHeight = _this.height;
              return;
            } else {
              openLoading(vm);
              doUpload(vm, file, getFilePath(file, null), "preview4", "progress1", 1).then(function (path) {
                closeLoading(vm);
                console.log('上传成功后路径', path);
                if (fileLimit.show == "uploadImgMessage1") {
                  vm.uploadImgMessage1 = false;
                } else if (fileLimit.show == "uploadImgMessage2") {
                  vm.uploadImgMessage2 = false;
                }
                vm.formData[fileLimit.key] = path.fullPath;
                vm.formData[fileLimit.more] = {
                  attachmentName: path.name,
                  attachmentExt: path.ext,
                  attachmentSize: path.size
                };
                vm.$message.success("上传成功");
              });
            }
          };
        };
        return isJPG && isLt2M;
      },
      //删除图片
      deleteImg(type) {
        if (type == 1) {
          vm.formData.headUrl = "";
          vm.imgMouseOver1 = false;
        }
      },
      // 获取裁剪的图片数据
      getCropImg(argument) {
        this.showCropper = false;
        this.cropData = argument[1]
        vm.doUploadQiNiu(argument[2])
        argument[3] && argument[3].destroy();
        // vm.slide2.oriUrl = "";
      },
      // 上传七牛
      doUploadQiNiu(file){

        doUpload(this,file, getFilePath(file,null), 'preview4', 'uploadProgress1', '').then(function (path) {

          vm.formData.headUrl = path.fullPath;

          vm.$message.success('上传成功');
        });

      },
    },
  }
</script>
<style lang="scss">
  .create-shop-wrapper{
    .create-shop-content{
      background: #fff;
      padding: 10px;
      .step-content {
        overflow: hidden;
        height: 60px;
        padding: 10px 10px 50px ;
        border-bottom: 1px solid #efefef;
        .title{
          font-size: 13px;
          color: #449284;
        }
      }
    }
    .el-button--text{
      color: #449284;
      font-size: 14px;
      &::after{
        content: '';
        position: relative;
        height: 14px;
        /*border-right: 1px solid #EBEEF5;*/
        border-right: 1px solid #aaaaaa;
        padding-right: 10px;
      }
      &:last-of-type{
        &::after{
          content: '';
          position: relative;
          width: 1px;
          height: 14px;
          border-right: none;
        }
      }
    }
  }
  .required-label .el-form-item__label::before {
    content: "*";
    color: #f56c6c;
    margin-right: 4px;
  }
  .basic-item-icon {
    position: relative;

    .require {
      position: absolute;
      left: 67px;
      top: 11px;
      color: #f56c6c;
    }

    .upload-message {
      position: absolute;
      left: 0;
      top: 105px;
      font-size: 12px;
      color: #f56c6c;
    }

    .img-delete {
      position: absolute;
      left: 0px;
      top: 0px;
      width: 84px;
      height: 100px;
      background: #000;
      opacity: 0.7;
      z-index: 999;

      i {
        color: #fff;
        margin-top: 39px;
        margin-left: 0px;
      }
    }
  }

  .word-num {
    font-size: 12px;
    color: #999;
    padding-top: 5px;
  }

  .line {
    margin-left: 10px;
    width: 20px;
  }

  .bg-uploader {
    .bg-img {
      float: left;
      width: 84px;
      height: 100px;
    }

    .limit-text {
      float: left;
      margin-left: 10px;
      margin-top: -10px;

      p {
        font-size: 12px;
        color: #999;
        text-align: left;
      }
    }
  }

  .el-upload__tip {
    position: absolute;
    top: -6px;
    left: 130px;
  }
  .span-mt-10{
    .edit-img {
      width: 20px;
      margin-top: 10px;
    }
  }
  .p-tips{
    font-size: 13px;
    color: #8C8C8C;
    line-height: 26px;
  }
  .part-tit{
    font-size: 14px;
    padding:20px 0 20px 20px;
  }
  .inline{
    .el-form-item{
      display: inline-block;
      vertical-align:top;
      .el-radio__label{
        font-size: 12px;
      }
    }
  }
  .stock-com{
    width: 158px;
    height: 32px;
    border:1px solid #dcdfe6;
    border-radius: 4px;
    overflow: hidden;
    .sp{
      display: inline-block;
      float: left;
    }
    .sp-c{
      line-height: 32px;
      width:92px;
      text-align: center;
    }
    .sp-l{
      border-right:1px solid #dcdfe6;

    }
    .sp-r{
      border-left:1px solid #dcdfe6;

    }
    .sp-l,.sp-r{
      width: 32px;
      text-align: center;
      line-height: 32px;
      font-size: 13px;
      background: #f5f7fa;
      cursor: pointer;

    }
  }
</style>
