<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="orderCarbonAmount">
                  <el-input type="text" v-model="form.orderCarbonAmount" :disabled="limit.isRead"></el-input>
              </el-form-item>
              <el-form-item label="购买区域:">
                <el-cascader
                 ref="areaSelect"
                 v-model="form.area"
                 style="width:100%;"
                 :props="areaProps"
                 clearable
                 ></el-cascader>
              </el-form-item>
              <el-form-item label="购买对象:">
                  <el-input type="text" v-model="form.applyCustomName" :disabled="true"></el-input>
              </el-form-item>
              <el-form-item label="购买类别:" prop="productTypeGuid">
                <el-checkbox-group v-model="form.productTypeGuid">
                    <el-checkbox
                      v-for="(item,index) in buyTypeList"
                      :label="item.guid"
                      :key="index"
                      >{{item.type_name}}</el-checkbox>
                </el-checkbox-group>
              </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 { listProductType , areaTree ,createOrder } from "@/api/order/subscription";
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: '624px',
      //窗口权限
      limit: {
        isRead: false, //是否只读
        isAdd: false //是否可写
      },
      //购买类型
      buyTypeList:[],
      //验证
      rules: {
        orderCarbonAmount: [{ required: true, message: '购买碳汇量不能为空!', trigger: 'blur' }],
        productTypeGuid: [{ required: true, message: ' 购买类别不能为空!', trigger: 'blur' }]
      },
      //行政区域
      areaProps:{
        lazy: true,
        checkStrictly:false,
        lazyLoad (node, resolve) {
          if (node.level === 0) {
             resolve( [{ label: '重庆市',value:'156050',leaf:false }] );
          }else{
             let params = {
               upCode:node.data.value
             }
             areaTree(params).then(res => {
                const areas = res.retBody.map((value,i) => ({
                   value:value.code,
                   label:value.name,
                   leaf:value.level >= 5
                }))
                resolve(areas)
             })
          }
        }
      },
      //表单
      form: {
        orderType:1,
        applyCostomerId:'',
        productTypeGuid:[],
        area:[]
      }
    };
  },
  created() {
    switch (this.modalAction) {
      case 'edit':
        this.limit.isAdd = true;
        this.form.orderCarbonAmount = this.modalSelectRow.amount
        this.form.applyCostomerId = this.modalSelectRow.applyCustomerId
        this.form.guid = this.modalSelectRow.guid
        this.form.applyCustomName = this.modalSelectRow.applyCustomName
        this.getType()
        break;
      default:
        break;
    }
  },
  methods: {
    getType(){
      listProductType().then(res => {
          if (res.retHead.errCode === 0) {
            this.buyTypeList = res.retBody
          }
      });
    },
    //表单提交
    submit() {
      this.$refs.from.validate(valid => {
        if (valid) {
          let params = Object.assign({},this.form)
          params.villageId = params.area.length == 0 ? "" : params.area[4]
          params.productTypeGuid = params.productTypeGuid.join(';')
          createOrder(params).then(res => {
              if (res.retHead.errCode === 0) {
                this.msgSuccess("操作成功");
                this.modalVisible = false
                this.$emit('closeModal', true);
              }
          });
        } else {
          return false;
        }
      });
    },
    //窗口关闭
    handleCancel() {
      this.$emit('closeModal', false);
    }
  }
};
</script>

<style lang="scss" scoped>

</style>
