<template>
  <el-dialog title="股权信息设置" :visible.sync="dialogVisible" class="stock-right">
    <el-form :model="form" :rules="rules" ref="form" label-width="80px">
      <el-form-item label="状态">
        <!--(-1:下架，0:筹备中，1:筹备完成，2:上架)-->
        <el-radio-group v-model="form.stockState">
          <el-radio-button style="margin-bottom: 10px;" label="-1">暂不募股</el-radio-button>
          <el-radio-button style="margin-bottom: 10px;" label="2">募股中</el-radio-button>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="总份额" prop="total">
        <el-input v-model="form.total" placeholder="请输入总份额"></el-input>
      </el-form-item>
      <el-form-item label="剩余份额" prop="balance">
        <el-input v-model="form.balance" placeholder="请输入剩余份额"></el-input>
      </el-form-item>
      <el-form-item label="股权单价" prop="price">
        <el-select class="filter-item" v-model="form.type" style="width: 100px;">
          <el-option :key="1" label="￥" :value="1"></el-option>
          <el-option :key="2" label="HK$" :value="2"></el-option>
        </el-select>
        <el-input style="width: 200px;margin: 0 20px;" v-model="form.price"  placeholder="请输入股权单价"></el-input>万
      </el-form-item>
      <el-form-item label="排序" prop="title">
        <el-input v-model="form.rank" placeholder="请输入1,2,3……"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer" >
      <el-button @click="cancel('form')">取 消</el-button>
      <el-button type="primary" @click="updateStock('form')">确 定</el-button>
      <!--<el-button type="primary" @click="update('dialogFrom')">上 架</el-button>-->
    </div>
  </el-dialog>
</template>

<style>
  .stock-right .el-form-item__content{
    display: flex;
    margin-left: 0;
  }
</style>

<script>
  import {mapGetters} from 'vuex';
  import ElRow from "element-ui/packages/row/src/row";
  import ElCol from "element-ui/packages/col/src/col";
  import ElFormItem from "../../../../node_modules/element-ui/packages/form/src/form-item.vue";
  import {
    updStockInfo,
    getCompanyDetail
  } from 'api/base_info/branch_company';
  export default {
    components: {
      ElFormItem,
      ElCol,
      ElRow},
    props: ["row"],
    name: 'stockRightModal',
    data() {
      return {
        form:{
          stockState: -1,
          type: 1//("股价类型:1-万/股；2-港万/股")
        },
        dialogVisible:false,
        rules: {
          total: [
            {
              validator: (rule, value, callback) => {
                if (!value) {
                  return callback(new Error('请输入总份额'));
                } else {
                  const reg = /^[1-9]\d*$/;
                  console.log(reg.test(value));
                  if (reg.test(value)) {
                    callback();
                  } else {
                    return callback(new Error('请输入大于零整数'));
                  }
                }
              },
              trigger: 'blur'
            }
          ],
          price: [
            {
              validator: (rule, value, callback) => {
                if (!value) {
                  return callback(new Error('请输入股权单价'));
                } else {
                  const reg = /^[1-9]\d*$/;
                  console.log(reg.test(value));
                  if (reg.test(value)) {
                    callback();
                  } else {
                    return callback(new Error('请输入大于零整数'));
                  }
                }
              },
              trigger: 'blur'
            }
          ],
          balance:[
            {
              validator: (rule, value, callback) => {
                if (!value) {
                  return callback(new Error('请输入剩余份额'));
                } else {
                  const reg = /^[1-9]\d*$/;
                  console.log(reg.test(value));
                  if (reg.test(value)) {
                    callback();
                  } else {
                    return callback(new Error('请输入大于零整数'));
                  }
                }
              },
              trigger: 'blur'
            }
          ],
        }
      }
    },
    created() {
      if(this.row.stockId){
        this.form.stockId = this.row.stockId;
        this.getInfo();
      } else {
        this.form.id = this.row.id;
      }
      this.bookRecord_btn_unbook = this.elements['bookRecord:btn_unbook'];
    },
    computed: {
      ...mapGetters([
        'elements',
      ]),
    },
    watch: {
      dialogVisible(newValue, oldValue){
        if(!newValue){
          this.$emit("stockRightDialogEvent", false);
        }
      },
    },
    mounted() {
      this.dialogVisible = true;
    },
    methods: {
      /**
       * 根据id获取分公司对应信息
       * */
      getInfo() {
        let t = this;
        this.listLoading = true;
        getCompanyDetail(this.row)
          .then(response => {
            if(response.status==200){
              let info = response.data;
              info.price = info.price?info.price/10000:undefined;
              t.form = info;
              t.form.total = info.total;
            } else {
              this.$notify({
                title: '失败',
                message: res.message,
                type: 'error',
                duration: 2000
              });
            }
          })
      },
      /**
       * 设置股权-保存
       * */
      updateStock(formName){
        const set = this.$refs;
        set[formName].validate(valid => {
          if (valid) {
            this.form.price = this.form.price*10000;
            updStockInfo(this.form)
              .then(res => {
                if(res.status==200){
                  this.$notify({
                    title: '成功',
                    message: '设置成功',
                    type: 'success',
                    duration: 2000
                  });
                  this.$emit("stockRightDialogEvent", true);
                }else{
                  this.$notify({
                    title: '失败',
                    message: res.message,
                    type: 'error',
                    duration: 2000
                  });
                }
              })
          } else {
            return;
          }
        });
      },
      /*
       * 弹框-取消
       * */
      cancel() {
        this.$emit("stockRightDialogEvent", false);
      }
    }
  }
</script>
