<template>
  <el-dialog
    :title="modalTitle"
    :visible.sync="modalVisible"
    :width="formLabelWidth"
    top="2vh"
    @close="handleCancel">
    <div class="mobile">
        <el-form label-width="90px" :model="form" :rules="rules" ref="from" class="from">
            <el-form-item label="价格:" prop="price">
                <el-input type="text" v-model="form.price" :disabled="limit.isRead"></el-input>
            </el-form-item>
            <el-form-item label="购买类别:" prop="checklist">
              <el-checkbox-group v-model="form.checklist" @change="selectType">
                  <el-checkbox
                    v-for="(item,index) in priceTypelist"
                    :label="item.guid + ':' +item.typeName"
                    :key="item.guid"
                    >{{item.typeName}}</el-checkbox>
              </el-checkbox-group>
            </el-form-item>
            <el-form-item label="备注说明:">
                <el-input type="textarea" v-model="form.checkRemark" :disabled="limit.isRead"></el-input>
            </el-form-item>
        </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleCancel">取 消</el-button>
      <el-button type="primary" @click="submit" v-if="!limit.isRead">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { addPrice , priceType } from "@/api/productManage/priceManage";
export default {
  name: 'addMobiel',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: 'title'
    },
    action: {
      type: String,
      default: 'add'
    },
    selectRow: {
      type: Object,
      default:null
    }
  },
  watch: {
    visible(newVal, oldVal) {
      this.modalVisible = newVal;
    },
    title(newVal, oldVal) {
      this.modalTitle = newVal;
    },
    action(newVal, oldVal) {
      this.modalAction = newVal;
    },
    selectRow(newVal, oldVal) {
      this.modalSelectRow = newVal;
    }
  },
  data() {
    return {
      modalTitle: this.title,
      modalVisible: this.visible,
      modalAction: this.action,
      modalSelectRow: this.selectRow,
      formLabelWidth: '1024px',
      //窗口权限
      limit: {
        isRead: false, //是否只读
        isAdd: false //是否可写
      },
      priceTypelist:[],
      //验证
      rules: {
        price: [{ required: true, message: '订价不能为空!', trigger: 'blur' }],
        checklist: [{ required: true, message: '购买类别不能为空!', trigger: 'blur' }],
      },
      //表单
      form: {
        checklist:[]
      }
    };
  },
  created() {
    this.getPriceType();
    switch (this.modalAction) {
      case 'add':
        this.limit.isAdd = true;
        break;
      case 'edit':
        this.limit.isAdd = true;
        // this.initDatas(this.modalSelectRow)
        break;
      case 'view':
        this.limit.isRead = true;
        // this.initDatas(this.modalSelectRow)
        break;
      default:
        break;
    }
  },
  methods: {
    /** 定价类别列表 */
    getPriceType(){
      priceType({}).then(res => {
          if(res.retHead.errCode == 0){
            this.priceTypelist = res.retBody
          }
        }
      );
    },
    /*选择类型*/
    selectType(val){
        this.checkValues = [];
        this.checkLabels= [];
        val.forEach(item=>{
          const value = item.split(':')[0];
          const label= item.split(':')[1];
          this.checkValues.push(value);
          this.checkLabels.push(label);
        });
    },
    //表单提交
    submit() {
      this.$refs.from.validate(valid => {
        if (valid) {
          let params = Object.assign({},this.form)
          params.fkProductType = this.checkValues.join(',')
          params.typeName = this.checkLabels.join(',')
          delete params.checklist
          console.log(params)
          addPrice(params).then(res => {
              if (res.retHead.errCode === 0) {
                this.msgSuccess("操作成功");
                this.modalVisible = false
                this.$emit('closeAddModal', true);
              }
          });
        } else {
          return false;
        }
      });
    },
    //窗口关闭
    handleCancel() {
      this.$emit('closeAddModal', false);
    }
  }
};
</script>

<style lang="scss" scoped>
  .hc-box{
    border:1px solid #e9e9e9;
    margin-bottom:20px;
    .hc-box-header{
        height:40px;
        line-height:40px;
        background:#fafafa;
        font-weight:bold;
        color:#333;
        padding:0 15px;
        .action{
          margin-left:10px;
          color:#304FFE
        }
    }
  }
</style>
