<template>
    <!-- 弹出框表单 -->
    <el-drawer
            :title="title"
            :visible.sync="open"
            :direction="direction"
            :destroy-on-close="true"
            size="80%">
        <el-form ref="form" :model="form" :rules="rules" label-width="140px">
            <el-row>
              <Field label="公司名称" prop="companyName" v-model="form.companyName" placeholder="请输入公司名称"/>
                            <Field label="公司编码" prop="companyCode" v-model="form.companyCode" placeholder="请输入公司编码"/>
                            <Field label="注册号" prop="registrationNumber" v-model="form.registrationNumber" placeholder="请输入注册号"/>
                            <Field label="法定代表人" prop="legalRepresentative" v-model="form.legalRepresentative" placeholder="请输入法定代表人"/>
                            <Field label="注册资本" prop="registeredCapital" v-model="form.registeredCapital" placeholder="请输入注册资本"/>
                            <Field label="公司类型" prop="companyType" v-model="form.companyType" placeholder="请输入公司类型"/>
                            <Field :span="24" label="经营范围" prop="businessScope" v-model="form.businessScope" placeholder="请输入经营范围"/>
<!--                            <Field :span="24" label="经营地址" prop="businessAdress" v-model="form.businessAdress" placeholder="请输入经营地址">
                              <p>当前选中的行政区划编码为： {{regionCodeList}}</p>
                              <el-cascader  v-model="regionCodeList" :props="areaprops"></el-cascader>
                            </Field>-->

              <Field :span="24" label="经营地址"  prop="businessAdress" v-model="form.businessAdress" placeholder="请输入经营地址">

              <area-select v-model="form.areaCode"  @addressSelect="addressSelect"></area-select>

              </Field>

                            <Field label="所在地" prop="location" v-model="form.location" placeholder="请输入所在地"/>
                            <Field label="联系电话" prop="contactPhone" v-model="form.contactPhone" placeholder="请输入联系电话"/>
                            <Field label="电子邮件地址" prop="email" v-model="form.email" placeholder="请输入电子邮件地址"/>
                            <Field label="公司网站" prop="website" v-model="form.website" placeholder="请输入公司网站"/>
                            <Field label="税务登记号" prop="taxRegistrationNumber" v-model="form.taxRegistrationNumber" placeholder="请输入税务登记号"/>
                            <Field label="社会信用代码" prop="socialCreditCode" v-model="form.socialCreditCode" placeholder="请输入社会信用代码"/>
                            <Field label="商标信息" prop="trademarkInfo" v-model="form.trademarkInfo" placeholder="请输入商标信息"/>
                            <Field label="网站历史备案信息" prop="websiteRegistrationInfo" v-model="form.websiteRegistrationInfo" placeholder="请输入网站历史备案信息"/>
                            <Field label="员工总数" prop="totalEmployees" v-model="form.totalEmployees"   placeholder="员工总数"/>
                            <Field label="软件著作权数量" prop="softNum" v-model="form.softNum"  placeholder="软件著作权数量"/>
                            <Field label="专利数量" prop="patentNum" v-model="form.patentNum" placeholder="专利数量"/>
                            <Field label="历史发展" prop="hisDevelop" v-model="form.hisDevelop" type="textarea" placeholder="请输入历史发展"/>
                            <Field label="经度" prop="lon" v-model="form.lon" placeholder="请输入经度"/>
                            <Field label="纬度" prop="lati" v-model="form.lati" placeholder="请输入纬度"/>
                           <Field label="公司介绍" prop="companyIntroduction" v-model="form.companyIntroduction" type="textarea" placeholder="请输入公司介绍"/>
                            <Field label="公司文化" prop="companyCulture" v-model="form.companyCulture" type="textarea" placeholder="请输入公司文化"/>
                            <Field label="排序" prop="sort" v-model="form.sort" type="num" placeholder="请输入排序"/>
                            <Field label="成立日期" prop="establishDate" v-model="form.establishDate" type="date" />

              <Field :span="24" label="企业logo"><imageUpload v-model="form.logoPath" prePath="/file/preview"/></Field>
              <Field :span="24" label="企业视频"><fileUpload v-model="form.companyVideoPath" :fileType="[ 'mp4', 'avi']"  prePath="/file/fileupload"/></Field>
              <Field :span="24" label="企业宣传图片"><fileUpload v-model="form.companyPicPath" :fileType="['png', 'jpg', 'jpeg']" prePath="/file/preview"/></Field>

              <Field :span="24" label="备注" prop="remark" v-model="form.remark" type="textarea" placeholder="请输入备注"/>

            </el-row>
                <el-divider content-position="center">公司标签信息</el-divider>
                <br/>
                <el-row style="margin-left:10px" :gutter="10" class="mb8">
                    <el-col :span="1.5">
                        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddCompanyLabels">添加</el-button>
                    </el-col>
                    <el-col :span="1.5">
                        <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteCompanyLabels">删除</el-button>
                    </el-col>
                </el-row>

                <el-table :data="companyLabelsList" :row-class-name="rowCompanyLabelsIndex" @selection-change="handleCompanyLabelsSelectionChange" ref="companyLabels">
                    <el-table-column type="selection" width="50" align="center" />
                    <el-table-column label="序号" align="center" prop="index" width="50"/>
<!--                            <el-table-column label="标签名称" prop="labelName">-->
<!--                                <template slot-scope="scope">-->
<!--                                    <el-input v-model="scope.row.labelName" placeholder="请选择标签名称" />-->
<!--                                </template>-->
<!--                            </el-table-column>-->

                  <el-table-column label="标签名称" prop="labelId">
                    <template slot-scope="scope">

                      <el-select  v-model="scope.row.labelId" placeholder="请选择标签名称"  >
                        <el-option
                            v-for="($label, $value) in dict.labels"
                            :key="$value"
                            :label="$label"
                            :value="$value"
                        ></el-option>
                      </el-select>

<!--                      <el-input v-model="scope.row.labelName" placeholder="请选择标签名称" />-->
                    </template>
                  </el-table-column>

<!--                            <el-table-column label="公司名称" prop="companyName">
                                <template slot-scope="scope">
                                    <el-input v-model="scope.row.companyName" placeholder="请输入公司名称" />
                                </template>
                            </el-table-column>-->
                            <el-table-column label="备注" prop="remark">
                                <template slot-scope="scope">
                                    <el-input v-model="scope.row.remark" placeholder="请输入备注" />
                                </template>
                            </el-table-column>


                </el-table>



          <el-divider content-position="center">公司专利信息</el-divider>
          <br/>
          <el-row style="margin-left:10px" :gutter="10" class="mb8">
            <el-col :span="1.5">
              <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddCompanyPatents">添加</el-button>
            </el-col>
            <el-col :span="1.5">
              <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteCompanyPatents">删除</el-button>
            </el-col>
          </el-row>

          <el-table :data="companyPatentsList" :row-class-name="rowCompanyPatentsIndex" @selection-change="handleCompanyPatentsSelectionChange" ref="companyPatents">
            <el-table-column type="selection" width="50" align="center" />
            <el-table-column label="序号" align="center" prop="index" width="50"/>
            <el-table-column label="知识产权类型" prop="intellectPropertyType">
                <template slot-scope="scope">
                    <el-input v-model="scope.row.intellectPropertyType" placeholder="请输入知识产权类型" />
                </template>
            </el-table-column>

            <el-table-column label="知识产权数量" prop="labelId">
              <template slot-scope="scope">

                <el-input-number    v-model="scope.row.intellectPropertyNum" size="small" placeholder='请输入知识产权数量' ></el-input-number>
              </template>
            </el-table-column>

            <el-table-column label="备注" prop="remark">
              <template slot-scope="scope">
                <el-input v-model="scope.row.remark" placeholder="请输入备注" />
              </template>
            </el-table-column>


          </el-table>

            <form-buttons @submit='submitForm' v-if="pageInfo.type!='view'" noCancelBtn />
        </el-form>

    </el-drawer>
</template>

<script>
    import form from "@/assets/mixins/formdialog";
    export default {
        name: "CompanyDetail",
        mixins: [form],
        components: {

        },
        created() {
            this.changePath("company")
        },
        data() {
          let that = this
            return {

                regionCodeList: [],
                areaprops: {
                  // 单选
                  checkStrictly: true,
                  // 懒加载
                  lazy: true,
                  lazyLoad(node, resolve) {
                    var param = {
                      pid:node.level===0?0:node.data.id,
                      size:-1
                    };

                    that.$post("/base/area/list", param)
                        .then(res => {
                          console.log(res)
                          let nodes = res.data.data.map(item => {
                            return {
                              value: item.areaCode,
                              label: item.name,
                              id:item.iid,
                              leaf: item.haveSonArea === "False"
                            }
                          })
                          resolve(nodes)
                        })
                  }
                },
                // 子表选中数据
                checkedCompanyLabels: [],
                // 公司标注表格数据
                companyLabelsList: [],
              // 子表选中数据
              checkedCompanyPatents: [],
              // 公司专利表格数据
              companyPatentsList: [],
                // 遮罩层
                loading: true,
                // 弹出层标题
                title: "公司",
                // 是否显示弹出层
                open: false,
                direction:"rtl",
                toString:[
                    "labelId"
                ],
                toDate:[
                    "establishDate",
                ],
                // 表单校验
                rules: {
                    companyName: [
                        {required: true,message: "请输入公司名称", trigger: "blur" },
                        {max: 128,message: "最多只能录入128个字符",trigger: "blur",},
                    ],
                    areaCode: [
                        {required: true,message: "请输入所属区域编码", trigger: "blur" },
                        {max: 128,message: "最多只能录入128个字符",trigger: "blur",},
                    ],
                    areaName: [
                        {required: true,message: "请输入所属区域名称", trigger: "blur" },
                        {max: 128,message: "最多只能录入128个字符",trigger: "blur",},
                    ],
                    contactPhone: [
                        {required: true,message: "请输入联系电话", trigger: "blur" },
                        {max: 32,message: "最多只能录入32个字符",trigger: "blur",},
                    ],
                    lon: [
                        {required: true,message: "请输入经度", trigger: "blur" },
                        {max: 32,message: "最多只能录入32个字符",trigger: "blur",},
                    ],
                    lati: [
                        {required: true,message: "请输入纬度", trigger: "blur" },
                        {max: 32,message: "最多只能录入32个字符",trigger: "blur",},
                    ],
                    logoPath: [
                        {required: true,message: "请输入企业logo", trigger: "blur" },
                        {max: 128,message: "最多只能录入128个字符",trigger: "blur",},
                    ],
                    establishDate: [
                        {required: true,message: "请选择成立日期" },
                    ],
                    createTime: [
                        {required: true,message: "请选择创建时间" },
                    ],
                }
            };
        },

        methods: {

          addressSelect(val){
            console.log("addressSelect",val)
          },

            /** 公司标注序号 */
            rowCompanyLabelsIndex({ row, rowIndex }) {
                row.index = rowIndex + 1;
            },
            /** 公司标注添加按钮操作 */
            handleAddCompanyLabels() {
                let obj = {};
                obj.labelId = "";
                obj.labelName = "";
                obj.companyName = "";
                obj.remark = "";
                obj.createUserId = "";
                obj.createTime = "";
                obj.updateUserId = "";
                obj.updateTime = "";
                this.companyLabelsList.push(obj);
            },
            /** 公司标注删除按钮操作 */
            handleDeleteCompanyLabels() {
                if (this.checkedCompanyLabels.length == 0) {
                    this.$alert("请先选择要删除的公司标注数据", "提示", { confirmButtonText: "确定", });
                } else {
                    this.companyLabelsList.splice(this.checkedCompanyLabels[0].index - 1, 1);
                }
            },
            /** 单选框选中数据 */
            handleCompanyLabelsSelectionChange(selection) {
                if (selection.length > 1) {
                    this.$refs.companyLabels.clearSelection();
                    this.$refs.companyLabels.toggleRowSelection(selection.pop());
                } else {
                    this.checkedCompanyLabels = selection;
                }
            },

          /** 公司专利序号 */
          rowCompanyPatentsIndex({ row, rowIndex }) {
            row.index = rowIndex + 1;
          },
          /** 公司专利添加按钮操作 */
          handleAddCompanyPatents() {
            let obj = {};
            obj.companyId = "";
            obj.intellectPropertyType = "";
            obj.intellectPropertyNum = 1;
            obj.remark = "";
            obj.createUserId = "";
            obj.createTime = "";
            obj.updateUserId = "";
            obj.updateTime = "";
            this.companyPatentsList.push(obj);
          },
          /** 公司专利删除按钮操作 */
          handleDeleteCompanyPatents() {
            if (this.checkedCompanyPatentss.length == 0) {
              this.$alert("请先选择要删除的公司专利数据", "提示", { confirmButtonText: "确定", });
            } else {
              this.companyPatentsList.splice(this.checkedCompanyPatents[0].index - 1, 1);
            }
          },
          /** 单选框选中数据 */
          handleCompanyPatentsSelectionChange(selection) {
            if (selection.length > 1) {
              this.$refs.companyPatents.clearSelection();
              this.$refs.companyPatents.toggleRowSelection(selection.pop());
            } else {
              this.checkedCompanyPatents = selection;
            }
          },

            // 渲染前置处理
            beforeRender(data) {
                if(data.entity.companyLabelsList) {
                  data.entity.companyLabelsList.forEach(item => {
                    //如果相等做操作
                    item['labelId'] = item[ 'labelId']+ '';
                  })

                    this.companyLabelsList = data.entity.companyLabelsList;
                }
              if(data.entity.companyPatentsList) {
                this.companyPatentsList = data.entity.companyPatentsList;
              }

                return data
            },
            /** 编辑 */
            edit(row) {
                this.reset()
                this.query = { id: row.id };
                this.urls.currUrl ="company/edit";
                this.getData();
                this.pageInfo.type="edit"
                this.title = "修改公司";
            },
            /** 新增 */
            add(row) {
                this.reset()
                this.urls.currUrl = "company/add";
                this.getData();
                this.pageInfo.type="add"
                this.title = "新增公司";
            },
            /** 查看*/
            view(row) {
                this.reset()
                this.query = { id: row.id };
                this.urls.currUrl ="company/view";
                this.getData();
                this.pageInfo.type="view"
                this.title = "公司详细";
            },
            /**取消按钮 */
            cancel() {
                this.open = false;
            },
            /**获取数据后弹框 */
            afterRender(data) {
                this.open = true;
            },

            afterSubmit(data) {
                this.open = false;
                this.$emit("ok");
            },

            // 表单重置
            reset() {
                this.form = {
                    companyName : "",
                    companyCode : "",
                    registrationNumber : "",
                    legalRepresentative : "",
                    registeredCapital : "",
                    companyType : "",
                    businessScope : "",
                    businessAdress : "",
                    location : "",
                    areaCode : "",
                    areaName : "",
                    contactPhone : "",
                    email : "",
                    website : "",
                    taxRegistrationNumber : "",
                    socialCreditCode : "",
                    trademarkInfo : "",
                    websiteRegistrationInfo : "",
                    totalEmployees : null,
                    softNum : null,
                    patentNum : null,
                    hisDevelop : "",
                    lon : "",
                    lati : "",
                    logoPath : "",
                    companyVideoPath : "",
                    companyPicPath : "",
                    companyIntroduction : "",
                    companyCulture : "",
                    sort : 0,
                    establishDate : null,
                    remark : "",
                  companyLabelsList:[],
                  companyPatentsList:[]
                };
                this.resetForm("form");
            },
            resetForm(refName) {
                if (this.$refs[refName]) {
                    this.$refs[refName].resetFields();
                }
            },
        },
    };
</script>
