<template>
  <div v-loading="loading">
    <div class="form-box">
      <el-form
        label-position="top"
        :model="form"
        :rules="formRules"
        ref="form"
        label-width="100px"
      >
        <el-form-item label="产品分类" prop="classifyId">
          <el-cascader
            :options="selectList"
            v-model="form.classifyId"
            :emitPath="false"
            :props="{ label: 'name', value: 'id' }"
            style="width: 100%"
          ></el-cascader>
        </el-form-item>
        <el-form-item label="产品类型" prop="type">
          <el-radio-group v-model="form.type">
            <el-radio-button
              v-for="item in productTypeList"
              :key="item.id"
              :label="item.dictKey"
            >
              {{ item.dictValue }}</el-radio-button
            >
          </el-radio-group>
        </el-form-item>
        <el-row :gutter="10">
          <el-col :span="6">
            <el-form-item label="产品编码" prop="code">
              <el-input v-model="form.code" placeholder="请输入"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="18">
            <el-form-item label=".">
              <span>不输入则自动生成，自动编码规则示例：P00001</span>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="产品名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="规格" prop="specs">
          <el-input v-model="form.specs" placeholder="请输入"></el-input>
        </el-form-item>
        <el-row>
          <el-col :span="6">
            <el-form-item label="产品单位" prop="unit">
              <el-select
                v-model="form.unit"
                placeholder="请选择"
                style="width: 100%"
              >
                <el-option
                  v-for="item in productUnitList"
                  :key="item.id"
                  :label="item.dictValue"
                  :value="item.dictKey"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="6">
            <el-form-item label="京东供价" prop="jdPurchasePrice">
              <el-input
                v-model="form.jdPurchasePrice"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="6">
            <el-form-item label="标准售价" prop="sellingPrice">
              <el-input
                v-model="form.sellingPrice"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="6">
            <el-form-item label="标准采购价" prop="purchasePrice">
              <el-input
                v-model="form.purchasePrice"
                placeholder="请输入"
              ></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="10">
          <el-col :span="6">
            <el-form-item label="库存清空期限" prop="clearancePeriod">
              <el-date-picker
                v-model="form.clearancePeriod"
                type="datetime"
                placeholder="请选择"
                value-format="yyyy-MM-dd HH:mm:ss"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="产品照片">
          <el-upload
            class="upload-demo"
            action="/api/service-file/uploadFile"
            :headers="uploadHeader"
            list-type="picture-card"
            :file-list="fileList"
            :on-preview="handlePreview"
            :on-success="handleSuccess"
            :on-remove="handleRemove"
          >
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="是否组合" prop="combination">
          <el-radio-group v-model="form.combination">
            <el-radio-button label="1"> 是</el-radio-button>
            <el-radio-button label="0"> 否</el-radio-button>
          </el-radio-group>
        </el-form-item>
        <div style="margin-top: 20px" v-if="form.combination === '1'">
          <labelTitle content="组合明细"></labelTitle>
        </div>
        <el-form-item label-width="0px" v-if="form.combination === '1'">
          <el-button size="small" @click="selectDialog = true"> 选择</el-button>
        </el-form-item>
        <el-form-item v-if="form.combination === '1'">
          <el-table :data="form.productCombinationList">
            <el-table-column label="产品编码" prop="code"> </el-table-column>
            <el-table-column label="产品名称" prop="name"> </el-table-column>
            <el-table-column label="本次入库" prop="linkQuantity">
              <template slot-scope="scope">
                <el-form-item
                  :prop="
                    'productCombinationList.' + scope.$index + '.linkQuantity'
                  "
                  :rules="formRules.linkQuantity"
                  :inline-message="true"
                  label-width="0"
                >
                  <el-input
                    v-model="scope.row.linkQuantity"
                    placeholder="请输入"
                    size="mini"
                  >
                  </el-input>
                </el-form-item>
              </template>
            </el-table-column>
            <el-table-column label="操作" width="100" align="center">
              <template slot-scope="scope">
                <el-button type="text" @click="deleteRow(scope.$index)"
                  >删除</el-button
                >
              </template>
            </el-table-column>
          </el-table>
        </el-form-item>
        <div style="margin-top: 20px">
          <labelTitle content="生命周期规则"></labelTitle>
        </div>
        <el-row :gutter="10">
          <el-form-item label="新品期" prop="newProductsExpectedSales">
            <el-col :span="2" style="text-align: center">
              0 &nbsp;&nbsp;~
            </el-col>
            <el-col :span="3">
              <el-input
                v-model="form.newProductsDay"
                placeholder="请输入"
              ></el-input>
            </el-col>
            <el-col :span="1" style="text-align: center"> ： </el-col>
            <el-col :span="6">
              <el-input
                v-model="form.newProductsExpectedSales"
                placeholder="预期销量"
              ></el-input>
            </el-col>
          </el-form-item>
        </el-row>
        <el-row :gutter="10">
          <el-form-item label="成长期" prop="growUpExpectedSales">
            <el-col :span="2" style="text-align: center">
              61 &nbsp;&nbsp;~
            </el-col>
            <el-col :span="3">
              <el-input
                v-model="form.growUpDay"
                placeholder="请输入"
              ></el-input>
            </el-col>
            <el-col :span="1" style="text-align: center"> ： </el-col>
            <el-col :span="6">
              <el-input
                v-model="form.growUpExpectedSales"
                placeholder="预期销量"
              ></el-input>
            </el-col>
          </el-form-item>
        </el-row>
        <el-row>
          <el-form-item label="成熟期" prop="matureExpectedSales">
            <el-col :span="3"> 大于120天 ： </el-col>
            <el-col :span="6">
              <el-input
                v-model="form.matureExpectedSales"
                placeholder="预期销量"
              ></el-input>
            </el-col>
          </el-form-item>
        </el-row>
      </el-form>
    </div>
    <div style="text-align: center; margin-top: 15px">
      <el-button size="small" @click="handleCancel">取消 </el-button>
      <el-button type="primary" size="small" @click="handleSubmit">
        确定</el-button
      >
    </div>

    <el-dialog
      title="产品选择"
      v-if="selectDialog"
      :visible.sync="selectDialog"
      width="80%"
      top="60px"
    >
      <selectProduct @select="handleSelect"></selectProduct>
    </el-dialog>
  </div>
</template>

<script>
import labelTitle from "@/components/label-title/index.vue";
import selectProduct from "@/components/select-product/index.vue";
import { getToken } from "@/util/auth";

import * as API from "@/api/product-material/product/index.js";
export default {
  name: "addProduct",
  components: {
    labelTitle,
    selectProduct,
  },
  props: {
    form: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    return {
      uploadHeader: {
        Authorization: "Basic c2FiZXI6c2FiZXJfc2VjcmV0",
        "Blade-Auth": "bearer " + getToken(),
      },
      loading: false,
      selectList: [],
      productTypeList: [],
      productUnitList: [],

      formRules: {
        classifyId: [
          {
            required: true,
            message: "请选择产品分类",
            trigger: "change",
          },
        ],
        type: [
          {
            required: true,
            message: "请选择产品类型",
            trigger: "change",
          },
        ],
        unit: [
          {
            required: true,
            message: "请选择产品单位",
            trigger: "change",
          },
        ],
        name: [
          {
            required: true,
            message: "请输入产品名称",
            trigger: "blur",
          },
        ],
        specs: [
          {
            required: true,
            message: "请输入规格",
            trigger: "blur",
          },
        ],
        combination: [
          {
            required: true,
            message: "请选择是否组合",
            trigger: "change",
          },
        ],
        linkQuantity: [
          {
            required: true,
            message: "请输入数量",
            trigger: "blur",
          },
        ],
      },
      selectDialog: false,
    };
  },
  created() {
    const businessDictData = JSON.parse(
      window.localStorage.getItem("businessDict")
    );
    this.productTypeList = businessDictData.find(
      (item) => item.code === "productType"
    ).children;
    this.productUnitList = businessDictData.find(
      (item) => item.code === "productUnit"
    ).children;

    this.loading = true;
    API.productTree({
      type: "1",
      name: "",
    }).then(
      (res) => {
        this.selectList = res.data.data;
        this.loading = false;
      },
      (err) => {
        console.log("productTree: " + err);
        this.loading = false;
      }
    );
  },
  methods: {
    handleSubmit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          this.loading = true;
          this.form.classifyId =
            this.form.classifyId[this.form.classifyId.length - 1];
          this.$emit("submit");
        }
      });
    },
    handleCancel() {
      this.$emit("cancel");
    },
    handleSelect(row) {
      this.form.productCombinationList.push({
        linkProductId: row.id,
        linkQuantity: "",
        code: row.code,
        name: row.name,
      });
      this.msgSuccess("添加成功");
    },
    deleteRow(index) {
      this.form.productCombinationList.splice(index, 1);
      this.msgSuccess("删除成功");
    },
  },
};
</script>

<style lang="scss" scoped>
.form-box {
  height: calc(100vh - 280px);
  overflow: auto;
  box-sizing: border-box;
  padding: 10px;
}
::v-deep {
  .el-form-item {
    margin-bottom: 3px;
  }
  .el-form--label-top .el-form-item__label {
    padding: 8px 0 0 0;
  }
}
</style>