<template>
  <div class="page">
    <topBar v-if="topList.length > 0" :topList="topList" :moren="moren"></topBar>
    <div class="main">
      <div class="form_all">
        <el-form ref="select" :model="select" label-width="auto">
          <el-row :gutter="10">
            <el-col :span="4">
              <el-form-item label="产品编号">
                <el-input clearable v-model="select.productCode"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-form-item label="产品名称">
                <el-input clearable v-model="select.productName"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="4">
              <el-button type="primary" @click="getList">
                {{ '搜索' }}</el-button>
              <el-button type="" @click="reset"> {{ '重置' }}</el-button>
            </el-col>
          </el-row>
        </el-form>
      </div>
      <div class="maintop">
        <div class="mainbtn">
          <el-button v-has-buttons="['WarnSetAdd']" size="small" @click="openDig" class="addBtn">{{
      '添加' }}</el-button>
        </div>
      </div>
      <div class="maintable">
        <el-table :data="tableData" v-loading="loading" style="width: 100%" :header-cell-style="{ background: '#EEEEEE' }"
          :row-class-name="tableRowClassName" @selection-change="handleSelectionChange">
          <el-table-column type="selection" width="55"> </el-table-column>
          <el-table-column align="center" prop="productCode" label="产品编号" />
          <el-table-column align="center" prop="productName" label="产品名称" />
          <el-table-column align="center" prop="specsName" label="规格名称" />
          <el-table-column align="center" prop="warnQuantity" label="预警数量" />
          <el-table-column align="center" prop="warnPeriod" label="预警天数" />
          <el-table-column align="center" :label="'操作'" fixed="right">
            <template slot-scope="scope">
              <el-button @click="updateRow(scope.row)" type="text" v-has-buttons="['WarnSetUp']" size="small">
                {{ '修改' }}
              </el-button>
              <el-button @click="handleDelete(scope.row.pkId)" type="text" v-has-buttons="['WarnSetDel']" 
                size="small" style="color: #48b2fd">
                {{ '删除' }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />
    <!-- 新增修改 -->
    <el-dialog :close-on-click-modal="false" :visible.sync="dialogVisible1" :title="dialogTitle" width="40%" center
      :before-close="handleClose1">
      <el-form ref="form" :rules="rules" :model="form" label-width="auto">
        <el-row :gutter="20">
          <el-col :span="8">
            <el-form-item label="产品编号" prop="productCode">
              <el-input clearable @focus="getProduct" v-model="form.productCode"
                :placeholder="'请选择'"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="产品名称">
              <el-input clearable disabled v-model="form.productName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="产品规格">
              <el-input clearable disabled v-model="form.specsName"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预警数量">
              <el-input clearable v-model="form.warnQuantity" :placeholder="'请输入'"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="预警天数">
              <el-input clearable v-model="form.warnPeriod" :placeholder="'请输入'"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose1">{{ '取消' }}</el-button>
        <el-button type="primary" @click="onSubmit('form')">{{
      '确认'
    }}</el-button>
      </span>
    </el-dialog>
    <!-- 产品弹框 -->
    <el-dialog :title="'添加'" :visible.sync="dialogVisible" :close-on-click-modal="false" width="50%" center
      :before-close="handleClose">
      <div class="cpHeight">
        <el-row>
          <el-col :span="22">
            <el-input v-model="select1.query" clearable :placeholder="'请输入'" @keyup.enter.native="getDataList"
              prefix-icon="el-icon-search"></el-input>
          </el-col>
          <el-col :span="2">
            <el-button type="primary" size="small" @click="getDataList">
              {{ '搜索' }}</el-button>
          </el-col>
        </el-row>
        <div style="height: 10px"></div>
        <el-table :data="dialogList" ref="multipleTable" style="width: 100%" height="500px" @select-all="onSelectAll"
          @select="selected" :header-cell-style="{ background: '#EEEEEE' }" :row-class-name="tableRowClassName">
          <el-table-column type="selection" :selectable="selectable" width="55"> </el-table-column>
          <el-table-column align="center" prop="productName" :label="'产品名称'">
          </el-table-column>
          <el-table-column align="center" prop="productCode" :label="'产品编号'">
          </el-table-column>
          <el-table-column align="center" prop="productName" :label="'规格'">
            <template slot-scope="scope">
              <div class="blueCor" v-show="scope.row.specsName == '' || scope.row.specsName == undefined
      " @click="selSku(scope.row.productCode, scope.$index)">
                {{ '选择规格' }}
              </div>
              <div class="blueCor" v-show="scope.row.specsName != '' && scope.row.specsName != undefined
      " @click="selSku(scope.row.productCode, scope.$index)">
                {{ scope.row.specsName }}
              </div>
            </template>
          </el-table-column>
        </el-table>
      </div>
      <pagination v-show="total1 > 0" :total="total1" :page.sync="queryParams1.pageNum"
        :limit.sync="queryParams1.pageSize" @pagination="getDataList" />
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">{{ '取消' }}</el-button>
        <el-button type="primary" @click="sureProduct">{{
      '确认'
    }}</el-button>
      </span>
    </el-dialog>
    <!-- 规格弹框 -->
    <el-dialog :title="'规格'" :visible.sync="ggVisible" :close-on-click-modal="false" width="40%" center
      :before-close="handleCloseGg">
      <el-form>
        <el-form-item :label="item.specsTypeName" v-for="(item, index) in skuList" :key="index">
          <el-radio-group v-model="item.pkProductSku">
            <el-radio v-for="(ctem, cndex) in item.specsList" @change="getCheckBox(item.pkId, item.specsName)"
              :key="ctem.pkSpecs" :label="ctem.pkSpecs">{{ ctem.name }}</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleCloseGg">{{ '取消' }}</el-button>
        <el-button type="primary" @click="sureSku">{{
      '确认'
          }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import topBar from "@/components/topBar";
import * as api from "@/api/supply.js";
export default {
  name: "Bzpz",
  components: {
    topBar,
  },
  data() {
    return {
      moren: "warnSet",
      topList: [
        {
          name: "预警配置",
          path: "warnSet",
        },
      ],
      select: {},
      select1: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 50,
      },
      queryParams1: {
        pageNum: 1,
        pageSize: 50,
      },
      total: 0,
      total1: 0,
      dialogVisible1: false,
      dialogVisible: false,
      ggVisible: false,
      tableData: [],
      loading:false,
      dialogList: [],
      form: {},
      dialogTitle: "",
      rules: {
        corpName: [
          { required: true, message: '请输入', trigger: "blur" },
        ],
        shortName: [
          { required: true, message: '请输入', trigger: "blur" },
        ],
        principal: [
          { required: true, message: '请输入', trigger: "blur" },
        ],
        phone: [
          { required: true, message: '请输入', trigger: "blur" },
        ],
        pkFatherOrg: [
          { required: true, message: '请输入', trigger: "blur" },
        ],
      },
      selSkuList: [],
      specsName: "",
      skuList: [],
      selIndex: "",
      selectData: {},
      addEdit:0
    };
  },
  mounted() {
    this.getOthers();
    // 获取列表
    this.getList();
  },
  methods: {
    getProduct() {
      this.getDataList();
      this.dialogVisible = true;
    },
    selectable(row) {
      if (row.specsName == '' || row.specsName == undefined) {
        return false //禁用
      } else {
        return true //可选
      }
    },
    getDataList() {
      api
        .productFind(Object.assign({}, this.queryParams1, this.select1))
        .then((res) => {
          res.rows.forEach((item) => {
            item.quantity = 1;
          });
          this.dialogList = res.rows;
          this.total1 = res.total;
        });
    },
    getCheckBox() {
      let arr1 = [],
        arr2 = []
      this.skuList.forEach((item) => {
        item.specsList.forEach((ctem) => {
          if (ctem.pkSpecs == item.pkProductSku) {
            arr1.push({
              pkSpecs: ctem.pkSpecs,
              specsName: ctem.name,
              pkSpecsType: item.pkSpecsType,
            })
            arr2.push(ctem.name)
          }
        })
      })
      this.selSkuList = arr1
      this.specsName = arr2.join(',')
    },
    selSku(productCode, index) {
      this.selIndex = index;
      api
        .productFindInfo({
          productCode: productCode,
        })
        .then((res) => {
          let arr1 = [],
            arr2 = [];
          res.data.productBasSpecsList.forEach((item) => {
            item.pkProductSku = item.specsList[0].pkSpecs;
            arr1.push({
              specsName: item.specsList[0].name,
              pkSpecs: item.specsList[0].pkSpecs,
              pkSpecsType: item.pkSpecsType,
            });
            arr2.push(item.specsList[0].name);
          });
          this.selSkuList = arr1;
          this.specsName = arr2.join(",");

          this.skuList = res.data.productBasSpecsList;
          this.ggVisible = true;
        });
    },
    onSelectAll() {
      this.$refs.multipleTable.clearSelection();
    },
    selected(selection, row) {
      this.$refs.multipleTable.clearSelection();
      if (selection.length == 0) return;
      this.$refs.multipleTable.toggleRowSelection(row, true);
      this.selectData = row;
    },
    sureProduct() {
      if (this.selectData.length == 0) {
        this.$message({
          message: '请选择产品',
          type: "warning",
        });
        return false;
      } else {
        this.form.productCode =
          this.selectData.productCode;
        this.form.pkProductBas =
          this.selectData.pkId;
        this.form.productName =
          this.selectData.productName;
        this.form.specsName =
          this.selectData.specsName;
        this.form.scmItemSpecsList =
          this.selectData.selSkuList
        this.handleClose();
        this.$forceUpdate();
      }
    },
    handleCloseGg() {
      this.ggVisible = false;
    },
    sureSku() {
      this.$set(this.dialogList[this.selIndex], "selSkuList", this.selSkuList);
      this.$set(this.dialogList[this.selIndex], "specsName", this.specsName);
      this.ggVisible = false;
    },
    getOthers() { },
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          if(this.addEdit == 1){
            api.warnAdd(this.form).then((res) => {
            if (res.code == 200) {
              this.$message({
                message: res.msg,
                type: "success",
              });
              this.dialogVisible1 = false;
              // 获取列表
              this.getList();
              this.$refs[formName].resetFields();
              this.$refs[formName].clearValidate();
            }
          });
          }else{
            api.warnUp(this.form).then((res) => {
            if (res.code == 200) {
              this.$message({
                message: res.msg,
                type: "success",
              });
              this.dialogVisible1 = false;
              // 获取列表
              this.getList();
              this.$refs[formName].resetFields();
              this.$refs[formName].clearValidate();
            }
          });
          }
          
        } else {
          return false;
        }
      });
    },
    reset() {
      this.select = {};
      this.getList();
    },
    // 删除
    handleDelete(id) {
      this.$confirm('确认删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: "warning",
      }).then(() => {
        api.warnDel(id).then((res) => {
          this.$message({
            message: res.msg,
            type: "success",
          });
          this.getList();
        });
      });
    },
    openDig() {
      this.form = {};
      this.dialogVisible1 = true;
      this.addEdit = 1
      this.dialogTitle = '添加';
    },
    updateRow(row) {
      this.dialogTitle = '修改';
      this.form = JSON.parse(JSON.stringify(row)) ;
      this.form.scmItemSpecsList = row.warnConfigSpecsList
      delete this.form.warnConfigSpecsList
      this.addEdit = 0
      this.dialogVisible1 = true;
    },
    getList() {
      this.loading = true
      api
        .warnList(Object.assign({}, this.queryParams, this.select))
        .then((res) => {
          this.tableData = res.rows;
          this.total = res.total;
          this.loading = false
        });
    },
    handleClose1() {
      this.dialogVisible1 = false;
    },
    handleClose() {
      this.dialogVisible = false;
    },
    handleSelectionChange(val) { },

    tableRowClassName({ row, rowIndex }) {
      if (rowIndex % 2 == 1) {
        return "warning-row";
      } else if (rowIndex % 2 == 0) {
        return "success-row";
      }
      return "";
    },
  },
};
</script>
<style scoped lang="scss">
::v-deep .el-table .warning-row {
  background: #f9f9f9;
}

::v-deep .el-table .success-row {
  background: #ffffff;
}

::v-deep .el-table thead {
  color: #000000;
}

::v-deep .el-select {
  width: 100%;
}

.uploadIcon ::v-deep .el-upload--picture-card {
  display: none !important;
  /* 上传按钮隐藏 */
}

.page {
  padding: 20px;
  background: #f9f9f9;
  font-size: 14px;

  .main {
    background: #ffffff;
    border-radius: 8px;
    box-shadow: 0px 2px 20px 0px rgba(238, 238, 238, 0.5);

    .form_all {
      padding: 0px 20px 0 20px;
      // margin-bottom: 20px;
      background: #fff;
      border-radius: 8px;
    }

    .maintop {
      display: flex;
      padding: 0px 0px;
      justify-content: space-between;
      align-items: center;

      background: #f8f8f8;

      .mainbtn {
        .thebtn1 {
          background: #3181e5;
          color: #ffffff;
        }

        .thebtn2 {
          background: #ffad41;
          color: #ffffff;
        }
      }

      .maintitle {
        font-size: 10px;
        font-family: MicrosoftYaHei;
        color: #999999;
      }
    }
  }

  .tem {
    display: flex;
    align-items: center;
    justify-content: center;

    img {
      width: 28px;
      height: 20px;
      margin-right: 3px;
    }
  }

  .isRed {
    color: #ed1d25;
  }

  .isGreen {
    color: #1ab62b;
  }

  .bgImg {
    width: 48px;
    height: 48px;
  }
}

.openClose {
  text-align: right;
  margin-right: 10px;
  color: #3181e5;
}

.dizhi {
  ::v-deep .el-cascader {
    width: 100%;
  }
}

::v-deep .el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: auto;
}

::v-deep .el-form-item {
  margin-bottom: 20px !important;
}

::v-deep .el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 100% !important;
}

.blueCor {
  color: #589ff8;
  cursor: pointer;
}

::v-deep .el-input__icon {
  line-height: 33px;
}

::v-deep .el-button {
  margin-top: 0;
}
</style>
